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);
69 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_CREATE, handle, dir, rc);
71 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
72 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
75 if (!cache_dentry || !cache_parent)
76 GOTO(exit, rc = -ENOMEM);
79 SMFS_PRE_COW(dir, dentry, NULL, NULL, REINT_CREATE, "create", rc, exit);
81 pre_smfs_inode(dir, cache_dir);
82 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
83 if (cache_dir && cache_dir->i_op->create)
84 rc = cache_dir->i_op->create(cache_dir, cache_dentry,
87 if (cache_dir && cache_dir->i_op->create)
88 rc = cache_dir->i_op->create(cache_dir, cache_dentry,
94 inode = iget4(dir->i_sb, cache_dentry->d_inode->i_ino, NULL,
95 &I2SMI(dir)->smi_flags);
97 GOTO(exit, rc = -ENOMEM);
99 d_instantiate(dentry, inode);
100 sm_set_inode_ops(cache_dentry->d_inode, inode);
101 post_smfs_inode(dir, cache_dir);
105 SMFS_KML_POST(dir, dentry, NULL, NULL, REINT_CREATE,
107 SMFS_CACHE_HOOK_POST(CACHE_HOOK_CREATE, handle, dir, dentry,
108 NULL, NULL, rc, exit);
111 post_smfs_dentry(cache_dentry);
112 post_smfs_dentry(cache_parent);
113 smfs_trans_commit(dir, handle, 0);
117 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
118 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry)
120 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry,
121 struct nameidata *nd)
124 struct inode *cache_dir;
125 struct inode *cache_inode;
126 struct inode *inode = NULL;
127 struct dentry *cache_dentry = NULL;
128 struct dentry *cache_parent = NULL;
129 struct dentry *rc = NULL;
135 if (!(cache_dir = I2CI(dir)))
136 RETURN(ERR_PTR(-ENOENT));
138 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_LOOKUP, handle, dir, rc2);
141 RETURN(ERR_PTR(rc2));
143 /* preparing artificial backing fs dentries. */
144 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
145 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
147 if (!cache_dentry || !cache_parent)
148 GOTO(exit, rc = ERR_PTR(-ENOMEM));
150 if (!cache_dir && cache_dir->i_op->lookup)
151 GOTO(exit, rc = ERR_PTR(-ENOENT));
153 SMFS_PRE_COW(dir, dentry, NULL, NULL, SNAP_LOOKUP, "lookup", rc2, exit);
155 /* perform lookup in backing fs. */
156 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
157 rc = cache_dir->i_op->lookup(cache_dir, cache_dentry);
159 rc = cache_dir->i_op->lookup(cache_dir, cache_dentry, nd);
162 if (rc && IS_ERR(rc))
165 if ((cache_inode = rc ? rc->d_inode : cache_dentry->d_inode)) {
166 if (IS_ERR(cache_inode)) {
167 dentry->d_inode = cache_inode;
168 GOTO(exit, rc = NULL);
170 inode = iget4(dir->i_sb, cache_inode->i_ino, NULL,
171 &I2SMI(dir)->smi_flags);
177 d_add(dentry, inode);
180 SMFS_CACHE_HOOK_POST(CACHE_HOOK_LOOKUP, handle, dir, dentry,
181 NULL, NULL, rc2, exit);
185 post_smfs_dentry(cache_dentry);
186 post_smfs_dentry(cache_parent);
187 smfs_trans_commit(dir, handle, 0);
191 static int smfs_link(struct dentry * old_dentry,
192 struct inode * dir, struct dentry *dentry)
194 struct inode *cache_old_inode = NULL;
195 struct inode *cache_dir = I2CI(dir);
196 struct inode *inode = NULL;
197 struct dentry *cache_dentry = NULL;
198 struct dentry *cache_old_dentry = NULL;
199 struct dentry *cache_parent = NULL;
203 inode = old_dentry->d_inode;
205 cache_old_inode = I2CI(inode);
207 handle = smfs_trans_start(dir, FSFILT_OP_LINK, NULL);
211 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_LINK, handle, dir, rc);
215 SMFS_PRE_COW(dir, old_dentry, NULL, NULL, REINT_LINK, "link", rc, exit);
217 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
218 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
220 if (!cache_parent || !cache_dentry)
221 GOTO(exit, rc = -ENOMEM);
223 cache_old_dentry = pre_smfs_dentry(NULL, cache_old_inode,
225 if (!cache_old_dentry)
226 GOTO(exit, rc = -ENOMEM);
228 pre_smfs_inode(dir, cache_dir);
229 pre_smfs_inode(inode, cache_old_dentry->d_inode);
231 if (cache_dir->i_op->link)
232 rc = cache_dir->i_op->link(cache_old_dentry, cache_dir,
237 atomic_inc(&inode->i_count);
238 post_smfs_inode(inode, cache_old_dentry->d_inode);
239 d_instantiate(dentry, inode);
240 post_smfs_inode(dir, cache_dir);
242 SMFS_KML_POST(dir, old_dentry, dentry, NULL,
243 REINT_LINK, "link", rc, exit);
245 SMFS_CACHE_HOOK_POST(CACHE_HOOK_LINK, handle,
246 dir, old_dentry, NULL, NULL, rc, exit);
249 post_smfs_dentry(cache_dentry);
250 post_smfs_dentry(cache_parent);
251 post_smfs_dentry(cache_old_dentry);
252 smfs_trans_commit(dir, handle, 0);
256 static int smfs_unlink(struct inode * dir,
257 struct dentry *dentry)
259 struct inode *cache_dir = I2CI(dir);
260 struct inode *cache_inode = I2CI(dentry->d_inode);
261 struct dentry *cache_dentry;
262 struct dentry *cache_parent;
267 if (!cache_dir || !cache_inode)
270 handle = smfs_trans_start(dir, FSFILT_OP_UNLINK, NULL);
274 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_UNLINK, handle, dir, rc);
276 SMFS_PRE_COW(dir, dentry, NULL, NULL, REINT_UNLINK, "unlink", rc, exit);
278 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
279 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
281 if (!cache_parent || !cache_dentry)
282 GOTO(exit, rc = -ENOMEM);
285 pre_smfs_inode(dir, cache_dir);
286 pre_smfs_inode(dentry->d_inode, cache_inode);
287 if (cache_dir->i_op->unlink)
288 rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
289 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
290 post_smfs_inode(dir, cache_dir);
292 SMFS_KML_POST(dir, dentry, &mode, NULL, REINT_UNLINK,
295 SMFS_CACHE_HOOK_POST(CACHE_HOOK_UNLINK, handle, dir, dentry,
296 NULL, NULL, rc, exit);
298 post_smfs_dentry(cache_dentry);
299 post_smfs_dentry(cache_parent);
300 smfs_trans_commit(dir, handle, 0);
304 static int smfs_symlink(struct inode *dir, struct dentry *dentry,
307 struct inode *cache_dir = I2CI(dir);
308 struct inode *inode = NULL;
309 struct dentry *cache_dentry;
310 struct dentry *cache_parent;
317 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
318 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
320 if (!cache_parent || !cache_dentry)
321 GOTO(exit, rc = -ENOMEM);
323 handle = smfs_trans_start(dir, FSFILT_OP_SYMLINK, NULL);
327 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_SYMLINK, handle, dir, rc);
329 SMFS_PRE_COW(dir, dentry, NULL, NULL, REINT_CREATE, "symlink", rc,
332 pre_smfs_inode(dir, cache_dir);
334 if (cache_dir->i_op->symlink)
335 rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
337 inode = iget4(dir->i_sb, cache_dentry->d_inode->i_ino, NULL,
338 &I2SMI(dir)->smi_flags);
339 post_smfs_inode(dir, cache_dir);
341 d_instantiate(dentry, inode);
345 tgt_len = strlen(symname) + 1;
346 SMFS_KML_POST(dir, dentry, (char*)symname, &tgt_len, REINT_CREATE,
347 "symlink", rc, exit);
349 SMFS_CACHE_HOOK_POST(CACHE_HOOK_SYMLINK, handle, dir, dentry,
350 NULL, NULL, rc, exit);
353 post_smfs_dentry(cache_dentry);
354 post_smfs_dentry(cache_parent);
355 smfs_trans_commit(dir, handle, 0);
359 static int smfs_mkdir(struct inode *dir, struct dentry *dentry,
362 struct inode *cache_dir = I2CI(dir);
363 struct inode *inode = NULL;
364 struct dentry *cache_dentry;
365 struct dentry *cache_parent;
372 handle = smfs_trans_start(dir, FSFILT_OP_MKDIR, NULL);
376 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_MKDIR, handle, dir, rc);
378 SMFS_PRE_COW(dir, dentry, NULL, NULL, REINT_CREATE, "mkdir", rc,
380 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
381 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
384 if (!cache_parent || !cache_dentry)
385 GOTO(exit, rc = -ENOMEM);
387 pre_smfs_inode(dir, cache_dir);
389 if (cache_dir->i_op->mkdir)
390 rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
395 inode = iget4(dir->i_sb, cache_dentry->d_inode->i_ino, NULL,
396 &I2SMI(dir)->smi_flags);
398 GOTO(exit, rc = -ENOENT);
400 d_instantiate(dentry, inode);
401 post_smfs_inode(dir, cache_dir);
403 SMFS_KML_POST(dir, dentry, NULL, NULL,
404 REINT_CREATE, "mkdir", rc, exit);
406 SMFS_CACHE_HOOK_POST(CACHE_HOOK_MKDIR, handle,
407 dir, dentry, NULL, NULL, rc, exit);
410 post_smfs_dentry(cache_dentry);
411 post_smfs_dentry(cache_parent);
412 smfs_trans_commit(dir, handle, 0);
416 static int smfs_rmdir(struct inode *dir, struct dentry *dentry)
418 struct inode *cache_dir = I2CI(dir);
419 struct inode *cache_inode = I2CI(dentry->d_inode);
420 struct dentry *cache_dentry = NULL;
421 struct dentry *cache_parent = NULL;
423 int rc = 0, mode = S_IFDIR;
428 handle = smfs_trans_start(dir, FSFILT_OP_RMDIR, NULL);
429 if (IS_ERR(handle) ) {
430 CERROR("smfs_do_mkdir: no space for transaction\n");
434 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_RMDIR, handle, dir, rc);
436 SMFS_PRE_COW(dir, dentry, NULL, NULL, REINT_UNLINK, "rmdir", rc, exit);
438 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
439 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
442 if (!cache_parent || !cache_dentry)
443 GOTO(exit, rc = -ENOMEM);
445 pre_smfs_inode(dir, cache_dir);
446 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
447 if (cache_dir->i_op->rmdir)
448 rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
450 post_smfs_inode(dir, cache_dir);
451 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
454 SMFS_KML_POST(dir, dentry, &mode, NULL,
455 REINT_UNLINK, "rmdir", rc, exit);
457 SMFS_CACHE_HOOK_POST(CACHE_HOOK_RMDIR, handle, dir, dentry,
458 NULL, NULL, rc, exit);
460 post_smfs_dentry(cache_dentry);
461 post_smfs_dentry(cache_parent);
462 smfs_trans_commit(dir, handle, 0);
466 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
467 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
470 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
471 int mode, dev_t rdev)
474 struct inode *cache_dir = I2CI(dir);
475 struct inode *inode = NULL;
476 struct dentry *cache_dentry = NULL;
477 struct dentry *cache_parent = NULL;
484 handle = smfs_trans_start(dir, FSFILT_OP_MKNOD, NULL);
485 if (IS_ERR(handle)) {
486 CERROR("smfs_do_mkdir: no space for transaction\n");
489 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_MKNOD, handle, dir, rc);
491 SMFS_PRE_COW(dir, dentry, NULL, NULL, REINT_CREATE, "mknod", rc, exit);
492 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
493 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
495 if (!cache_parent || !cache_dentry)
496 GOTO(exit, rc = -ENOMEM);
498 pre_smfs_inode(dir, cache_dir);
499 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
501 if (!cache_dir->i_op->mknod)
504 if ((rc = cache_dir->i_op->mknod(cache_dir, cache_dentry,
508 inode = iget4(dir->i_sb, cache_dentry->d_inode->i_ino, NULL,
509 &I2SMI(dir)->smi_flags);
510 d_instantiate(dentry, inode);
512 pre_smfs_inode(dir, cache_dir);
513 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
515 SMFS_KML_POST(dir, dentry, NULL, NULL,
516 REINT_CREATE, "mknod", rc, exit);
518 SMFS_CACHE_HOOK_POST(CACHE_HOOK_MKNOD, handle, dir,
519 dentry, NULL, NULL, rc, exit);
522 post_smfs_dentry(cache_dentry);
523 post_smfs_dentry(cache_parent);
524 smfs_trans_commit(dir, handle, 0);
528 static int smfs_rename(struct inode * old_dir, struct dentry *old_dentry,
529 struct inode * new_dir,struct dentry *new_dentry)
531 struct inode *cache_old_dir = I2CI(old_dir);
532 struct inode *cache_new_dir = I2CI(new_dir);
533 struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
535 struct inode *cache_new_inode = new_dentry->d_inode ?
536 I2CI(new_dentry->d_inode) : NULL;
538 struct dentry *cache_old_dentry = NULL;
539 struct dentry *cache_new_dentry = NULL;
540 struct dentry *cache_new_parent = NULL;
541 struct dentry *cache_old_parent = NULL;
545 if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
548 handle = smfs_trans_start(old_dir, FSFILT_OP_RENAME, NULL);
549 if (IS_ERR(handle)) {
550 CERROR("smfs_do_mkdir: no space for transaction\n");
555 SMFS_PRE_COW(old_dir, old_dentry, new_dir, new_dentry, REINT_RENAME,
557 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_RENAME, handle, old_dir, rc);
559 cache_old_parent = pre_smfs_dentry(NULL, cache_old_dir, old_dentry);
560 cache_old_dentry = pre_smfs_dentry(cache_old_parent, cache_old_inode,
562 if (!cache_old_parent || !cache_old_dentry)
563 GOTO(exit, rc = -ENOMEM);
565 cache_new_parent = pre_smfs_dentry(NULL, cache_new_dir, new_dentry);
566 cache_new_dentry = pre_smfs_dentry(cache_new_parent, cache_new_inode,
568 if (!cache_new_parent || !cache_new_dentry)
569 GOTO(exit, rc = -ENOMEM);
571 pre_smfs_inode(old_dir, cache_old_dir);
572 pre_smfs_inode(new_dir, cache_new_dir);
574 if (cache_old_dir->i_op->rename)
575 rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
576 cache_new_dir, cache_new_dentry);
578 post_smfs_inode(old_dir, cache_old_dir);
579 post_smfs_inode(new_dir, cache_new_dir);
581 SMFS_KML_POST(old_dir, old_dentry, new_dir,
582 new_dentry, REINT_RENAME, "rename", rc, exit);
583 if (new_dentry->d_inode)
584 post_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
586 SMFS_CACHE_HOOK_POST(CACHE_HOOK_RENAME, handle, old_dir, old_dentry,
587 new_dir, new_dentry, rc, exit);
590 post_smfs_dentry(cache_old_dentry);
591 post_smfs_dentry(cache_old_parent);
592 post_smfs_dentry(cache_new_dentry);
593 post_smfs_dentry(cache_new_parent);
594 smfs_trans_commit(old_dir, handle, 0);
598 struct inode_operations smfs_dir_iops = {
601 link: smfs_link, /* BKL held */
602 unlink: smfs_unlink, /* BKL held */
603 symlink: smfs_symlink, /* BKL held */
604 mkdir: smfs_mkdir, /* BKL held */
605 rmdir: smfs_rmdir, /* BKL held */
606 mknod: smfs_mknod, /* BKL held */
607 rename: smfs_rename, /* BKL held */
608 setxattr: smfs_setxattr, /* BKL held */
609 getxattr: smfs_getxattr, /* BKL held */
610 listxattr: smfs_listxattr, /* BKL held */
611 removexattr: smfs_removexattr, /* BKL held */
614 static ssize_t smfs_read_dir(struct file *filp, char *buf,
615 size_t size, loff_t *ppos)
617 struct dentry *dentry = filp->f_dentry;
618 struct inode *cache_inode = NULL;
619 struct smfs_file_info *sfi = NULL;
624 cache_inode = I2CI(dentry->d_inode);
630 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
632 if (ppos != &(filp->f_pos))
633 cache_ppos = &tmp_ppos;
635 cache_ppos = &sfi->c_file->f_pos;
638 if (cache_inode->i_fop->read)
639 rc = cache_inode->i_fop->read(sfi->c_file, buf, size,
643 duplicate_file(filp, sfi->c_file);
647 static int smfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
649 struct dentry *dentry = filp->f_dentry;
650 struct inode *cache_inode = NULL;
651 struct smfs_file_info *sfi = NULL;
654 cache_inode = I2CI(dentry->d_inode);
659 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
661 if (cache_inode->i_fop->readdir)
662 rc = cache_inode->i_fop->readdir(sfi->c_file, dirent, filldir);
664 duplicate_file(filp, sfi->c_file);
668 struct file_operations smfs_dir_fops = {
670 readdir: smfs_readdir, /* BKL held */
671 ioctl: smfs_ioctl, /* BKL held */
672 fsync: smfs_fsync, /* BKL held */
674 release: smfs_release,