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;
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,
73 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
74 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
76 if (!cache_dentry || !cache_parent)
77 GOTO(exit, rc = -ENOMEM);
79 pre_smfs_inode(dir, cache_dir);
81 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
82 if (cache_dir && cache_dir->i_op->create)
83 rc = cache_dir->i_op->create(cache_dir, cache_dentry,
86 if (cache_dir && cache_dir->i_op->create)
87 rc = cache_dir->i_op->create(cache_dir, cache_dentry,
93 SMFS_GET_INODE(dir->i_sb, cache_dentry->d_inode, dir, inode,
96 d_instantiate(dentry, inode);
97 post_smfs_inode(dir, cache_dir);
99 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_CREATE, handle,
100 POST_HOOK, rc, exit);
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 /* preparing artificial backing fs dentries. */
131 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
132 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
134 if (!cache_dentry || !cache_parent)
135 GOTO(exit, rc = ERR_PTR(-ENOMEM));
137 if (!cache_dir && cache_dir->i_op->lookup)
138 GOTO(exit, rc = ERR_PTR(-ENOENT));
140 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_LOOKUP, handle,
141 PRE_HOOK, rc2, exit);
143 /* perform lookup in backing fs. */
144 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
145 rc = cache_dir->i_op->lookup(cache_dir, cache_dentry);
147 rc = cache_dir->i_op->lookup(cache_dir, cache_dentry, nd);
149 if (rc && IS_ERR(rc))
152 if ((cache_inode = rc ? rc->d_inode : cache_dentry->d_inode)) {
153 if (IS_ERR(cache_inode)) {
154 dentry->d_inode = cache_inode;
155 GOTO(exit, rc = NULL);
157 SMFS_GET_INODE(dir->i_sb, cache_inode, dir, inode, rc2, exit);
160 d_add(dentry, inode);
163 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_LOOKUP, handle, POST_HOOK, rc2,
168 post_smfs_dentry(cache_dentry);
169 post_smfs_dentry(cache_parent);
170 smfs_trans_commit(dir, handle, 0);
174 static int smfs_link(struct dentry *old_dentry,
175 struct inode *dir, struct dentry *dentry)
177 struct inode *cache_old_inode = NULL;
178 struct inode *cache_dir = I2CI(dir);
179 struct inode *inode = NULL;
180 struct dentry *cache_dentry = NULL;
181 struct dentry *cache_old_dentry = NULL;
182 struct dentry *cache_parent = NULL;
186 inode = old_dentry->d_inode;
187 cache_old_inode = I2CI(inode);
189 handle = smfs_trans_start(dir, FSFILT_OP_LINK, NULL);
194 SMFS_HOOK(dir, old_dentry, NULL, NULL, HOOK_LINK, handle,
197 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
198 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
200 if (!cache_parent || !cache_dentry)
201 GOTO(exit, rc = -ENOMEM);
203 cache_old_dentry = pre_smfs_dentry(NULL, cache_old_inode,
205 if (!cache_old_dentry)
206 GOTO(exit, rc = -ENOMEM);
208 pre_smfs_inode(dir, cache_dir);
209 pre_smfs_inode(inode, cache_old_dentry->d_inode);
211 if (cache_dir->i_op->link)
212 rc = cache_dir->i_op->link(cache_old_dentry, cache_dir,
217 atomic_inc(&inode->i_count);
218 post_smfs_inode(inode, cache_old_dentry->d_inode);
219 d_instantiate(dentry, inode);
220 post_smfs_inode(dir, cache_dir);
222 SMFS_HOOK(dir, old_dentry, dentry, NULL, HOOK_LINK, handle,
223 POST_HOOK, rc, exit);
226 post_smfs_dentry(cache_dentry);
227 post_smfs_dentry(cache_parent);
228 post_smfs_dentry(cache_old_dentry);
229 smfs_trans_commit(dir, handle, 0);
233 static int smfs_unlink(struct inode * dir,
234 struct dentry *dentry)
236 struct inode *cache_dir = I2CI(dir);
237 struct inode *cache_inode = I2CI(dentry->d_inode);
238 struct dentry *cache_dentry;
239 struct dentry *cache_parent;
244 if (!cache_dir || !cache_inode)
247 handle = smfs_trans_start(dir, FSFILT_OP_UNLINK, NULL);
251 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_UNLINK, handle, PRE_HOOK, rc,
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);
263 if (cache_dir->i_op->unlink)
264 rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
265 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
266 post_smfs_inode(dir, cache_dir);
269 SMFS_HOOK(dir, dentry, &mode, NULL, HOOK_UNLINK, handle, POST_HOOK,
272 post_smfs_dentry(cache_dentry);
273 post_smfs_dentry(cache_parent);
274 smfs_trans_commit(dir, handle, 0);
278 static int smfs_symlink(struct inode *dir, struct dentry *dentry,
281 struct inode *cache_dir = I2CI(dir);
282 struct inode *inode = NULL;
283 struct dentry *cache_dentry;
284 struct dentry *cache_parent;
291 handle = smfs_trans_start(dir, FSFILT_OP_SYMLINK, NULL);
296 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_SYMLINK, handle, PRE_HOOK, rc,
299 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
300 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
302 if (!cache_parent || !cache_dentry)
303 GOTO(exit, rc = -ENOMEM);
305 pre_smfs_inode(dir, cache_dir);
306 if (cache_dir->i_op->symlink)
307 rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
309 post_smfs_inode(dir, cache_dir);
311 SMFS_GET_INODE(dir->i_sb, cache_dentry->d_inode, dir, inode, rc, exit);
313 d_instantiate(dentry, inode);
317 tgt_len = strlen(symname) + 1;
319 SMFS_HOOK(dir, dentry, (char *)symname, &tgt_len, HOOK_SYMLINK, handle,
320 POST_HOOK, rc, exit);
323 post_smfs_dentry(cache_dentry);
324 post_smfs_dentry(cache_parent);
325 smfs_trans_commit(dir, handle, 0);
329 static int smfs_mkdir(struct inode *dir, struct dentry *dentry,
332 struct inode *cache_dir = I2CI(dir);
333 struct inode *inode = NULL;
334 struct dentry *cache_dentry;
335 struct dentry *cache_parent;
342 handle = smfs_trans_start(dir, FSFILT_OP_MKDIR, NULL);
348 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_MKDIR, handle, PRE_HOOK, rc,
351 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
352 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
354 if (!cache_parent || !cache_dentry)
355 GOTO(exit, rc = -ENOMEM);
357 pre_smfs_inode(dir, cache_dir);
359 if (cache_dir->i_op->mkdir)
360 rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
365 SMFS_GET_INODE(dir->i_sb, cache_dentry->d_inode, dir, inode, rc, exit);
366 d_instantiate(dentry, inode);
367 post_smfs_inode(dir, cache_dir);
369 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_MKDIR, handle, POST_HOOK, rc,
373 post_smfs_dentry(cache_dentry);
374 post_smfs_dentry(cache_parent);
375 smfs_trans_commit(dir, handle, 0);
379 static int smfs_rmdir(struct inode *dir, struct dentry *dentry)
381 struct inode *cache_dir = I2CI(dir);
382 struct inode *cache_inode = I2CI(dentry->d_inode);
383 struct dentry *cache_dentry = NULL;
384 struct dentry *cache_parent = NULL;
386 int rc = 0, mode = S_IFDIR;
391 handle = smfs_trans_start(dir, FSFILT_OP_RMDIR, NULL);
392 if (IS_ERR(handle) ) {
393 CERROR("smfs_do_mkdir: no space for transaction\n");
399 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_RMDIR, handle, PRE_HOOK, rc,
402 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
403 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
405 if (!cache_parent || !cache_dentry)
406 GOTO(exit, rc = -ENOMEM);
408 pre_smfs_inode(dir, cache_dir);
409 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
410 if (cache_dir->i_op->rmdir)
411 rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
413 post_smfs_inode(dir, cache_dir);
414 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
417 SMFS_HOOK(dir, dentry, &mode, NULL, HOOK_RMDIR, handle, POST_HOOK,
420 post_smfs_dentry(cache_dentry);
421 post_smfs_dentry(cache_parent);
422 smfs_trans_commit(dir, handle, 0);
426 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
427 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
430 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
431 int mode, dev_t rdev)
434 struct inode *cache_dir = I2CI(dir);
435 struct inode *inode = NULL;
436 struct dentry *cache_dentry = NULL;
437 struct dentry *cache_parent = NULL;
444 handle = smfs_trans_start(dir, FSFILT_OP_MKNOD, NULL);
445 if (IS_ERR(handle)) {
446 CERROR("smfs_do_mkdir: no space for transaction\n");
451 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_MKNOD, handle, PRE_HOOK, rc,
454 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
455 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
456 if (!cache_parent || !cache_dentry)
457 GOTO(exit, rc = -ENOMEM);
459 pre_smfs_inode(dir, cache_dir);
460 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
462 if (!cache_dir->i_op->mknod)
465 if ((rc = cache_dir->i_op->mknod(cache_dir, cache_dentry,
469 SMFS_GET_INODE(dir->i_sb, cache_dentry->d_inode, dir, inode, rc, exit);
471 d_instantiate(dentry, inode);
473 pre_smfs_inode(dir, cache_dir);
474 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
476 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_MKNOD, handle, POST_HOOK, rc,
481 post_smfs_dentry(cache_dentry);
482 post_smfs_dentry(cache_parent);
483 smfs_trans_commit(dir, handle, 0);
487 static int smfs_rename(struct inode *old_dir, struct dentry *old_dentry,
488 struct inode *new_dir,struct dentry *new_dentry)
490 struct inode *cache_old_dir = I2CI(old_dir);
491 struct inode *cache_new_dir = I2CI(new_dir);
492 struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
494 struct inode *cache_new_inode = new_dentry->d_inode ?
495 I2CI(new_dentry->d_inode) : NULL;
497 struct dentry *cache_old_dentry = NULL;
498 struct dentry *cache_new_dentry = NULL;
499 struct dentry *cache_new_parent = NULL;
500 struct dentry *cache_old_parent = NULL;
504 if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
507 handle = smfs_trans_start(old_dir, FSFILT_OP_RENAME, NULL);
508 if (IS_ERR(handle)) {
509 CERROR("smfs_do_mkdir: no space for transaction\n");
515 SMFS_HOOK(old_dir, old_dentry, new_dir, new_dentry, HOOK_RENAME,
516 handle, PRE_HOOK, rc, exit);
518 cache_old_parent = pre_smfs_dentry(NULL, cache_old_dir, old_dentry);
520 cache_old_dentry = pre_smfs_dentry(cache_old_parent, cache_old_inode,
523 if (!cache_old_parent || !cache_old_dentry)
524 GOTO(exit, rc = -ENOMEM);
526 cache_new_parent = pre_smfs_dentry(NULL, cache_new_dir, new_dentry);
528 cache_new_dentry = pre_smfs_dentry(cache_new_parent, cache_new_inode,
531 if (!cache_new_parent || !cache_new_dentry)
532 GOTO(exit, rc = -ENOMEM);
534 pre_smfs_inode(old_dir, cache_old_dir);
535 pre_smfs_inode(new_dir, cache_new_dir);
537 if (cache_old_dir->i_op->rename)
538 rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
539 cache_new_dir, cache_new_dentry);
541 post_smfs_inode(old_dir, cache_old_dir);
542 post_smfs_inode(new_dir, cache_new_dir);
544 SMFS_HOOK(old_dir, old_dentry, new_dir, new_dentry, HOOK_RENAME, handle,
545 POST_HOOK, rc, exit);
547 if (new_dentry->d_inode)
548 post_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
551 post_smfs_dentry(cache_old_dentry);
552 post_smfs_dentry(cache_old_parent);
553 post_smfs_dentry(cache_new_dentry);
554 post_smfs_dentry(cache_new_parent);
555 smfs_trans_commit(old_dir, handle, 0);
559 struct inode_operations smfs_dir_iops = {
562 link: smfs_link, /* BKL held */
563 unlink: smfs_unlink, /* BKL held */
564 symlink: smfs_symlink, /* BKL held */
565 mkdir: smfs_mkdir, /* BKL held */
566 rmdir: smfs_rmdir, /* BKL held */
567 mknod: smfs_mknod, /* BKL held */
568 rename: smfs_rename, /* BKL held */
569 setxattr: smfs_setxattr, /* BKL held */
570 getxattr: smfs_getxattr, /* BKL held */
571 listxattr: smfs_listxattr, /* BKL held */
572 removexattr: smfs_removexattr, /* BKL held */
575 static ssize_t smfs_read_dir(struct file *filp, char *buf,
576 size_t size, loff_t *ppos)
578 struct dentry *dentry = filp->f_dentry;
579 struct inode *cache_inode = NULL;
580 struct smfs_file_info *sfi = NULL;
585 cache_inode = I2CI(dentry->d_inode);
591 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
593 if (ppos != &(filp->f_pos))
594 cache_ppos = &tmp_ppos;
596 cache_ppos = &sfi->c_file->f_pos;
599 if (cache_inode->i_fop->read)
600 rc = cache_inode->i_fop->read(sfi->c_file, buf, size,
603 duplicate_file(filp, sfi->c_file);
607 static int smfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
609 struct dentry *dentry = filp->f_dentry;
610 struct inode *cache_inode = NULL;
611 struct smfs_file_info *sfi = NULL;
614 cache_inode = I2CI(dentry->d_inode);
619 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
621 SMFS_HOOK(dentry->d_inode, filp, dirent, filldir, HOOK_READDIR, NULL,
624 if (cache_inode->i_fop->readdir)
625 rc = cache_inode->i_fop->readdir(sfi->c_file, dirent, filldir);
627 SMFS_HOOK(dentry->d_inode, filp, dirent, filldir, HOOK_READDIR, NULL,
628 POST_HOOK, rc, exit);
630 duplicate_file(filp, sfi->c_file);
637 struct file_operations smfs_dir_fops = {
638 .read = smfs_read_dir,
639 .readdir = smfs_readdir, /* BKL held */
640 .ioctl = smfs_ioctl, /* BKL held */
641 .fsync = smfs_fsync, /* BKL held */
643 .release = smfs_release,