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, PRE_HOOK, rc,
72 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry, NULL);
73 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry, NULL);
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 = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino, dir, 0);
93 GOTO(exit, rc = -ENOMEM);
95 d_instantiate(dentry, inode);
96 sm_set_inode_ops(cache_dentry->d_inode, inode);
97 post_smfs_inode(dir, cache_dir);
98 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_CREATE, handle, POST_HOOK, rc,
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 *rc = NULL;
122 int rc2 = 0, index = 0;
126 if (!(cache_dir = I2CI(dir)))
127 RETURN(ERR_PTR(-ENOENT));
129 /* preparing artificial backing fs dentries. */
130 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent, NULL);
131 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry, &index);
133 if (!cache_dentry || !cache_parent)
134 GOTO(exit, rc = ERR_PTR(-ENOMEM));
136 if (!cache_dir && cache_dir->i_op->lookup)
137 GOTO(exit, rc = ERR_PTR(-ENOENT));
139 SMFS_HOOK(dir, dentry, &index, NULL, HOOK_LOOKUP, handle,
140 PRE_HOOK, rc2, exit);
142 /* perform lookup in backing fs. */
143 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
144 rc = cache_dir->i_op->lookup(cache_dir, cache_dentry);
146 rc = cache_dir->i_op->lookup(cache_dir, cache_dentry, nd);
148 if (rc && IS_ERR(rc))
151 if ((cache_inode = rc ? rc->d_inode : cache_dentry->d_inode)) {
152 if (IS_ERR(cache_inode)) {
153 dentry->d_inode = cache_inode;
154 GOTO(exit, rc = NULL);
156 inode = smfs_get_inode(dir->i_sb, cache_inode->i_ino, dir, index);
162 d_add(dentry, inode);
165 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_LOOKUP, handle, POST_HOOK, rc2,
170 post_smfs_dentry(cache_dentry);
171 post_smfs_dentry(cache_parent);
172 smfs_trans_commit(dir, handle, 0);
176 static int smfs_link(struct dentry * old_dentry,
177 struct inode * dir, struct dentry *dentry)
179 struct inode *cache_old_inode = NULL;
180 struct inode *cache_dir = I2CI(dir);
181 struct inode *inode = NULL;
182 struct dentry *cache_dentry = NULL;
183 struct dentry *cache_old_dentry = NULL;
184 struct dentry *cache_parent = NULL;
188 inode = old_dentry->d_inode;
190 cache_old_inode = I2CI(inode);
192 handle = smfs_trans_start(dir, FSFILT_OP_LINK, NULL);
197 SMFS_HOOK(dir, old_dentry, NULL, NULL, HOOK_LINK, handle, PRE_HOOK, rc,
200 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry, NULL);
201 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry, NULL);
203 if (!cache_parent || !cache_dentry)
204 GOTO(exit, rc = -ENOMEM);
206 cache_old_dentry = pre_smfs_dentry(NULL, cache_old_inode,
208 if (!cache_old_dentry)
209 GOTO(exit, rc = -ENOMEM);
211 pre_smfs_inode(dir, cache_dir);
212 pre_smfs_inode(inode, cache_old_dentry->d_inode);
214 if (cache_dir->i_op->link)
215 rc = cache_dir->i_op->link(cache_old_dentry, cache_dir,
220 atomic_inc(&inode->i_count);
221 post_smfs_inode(inode, cache_old_dentry->d_inode);
222 d_instantiate(dentry, inode);
223 post_smfs_inode(dir, cache_dir);
225 SMFS_HOOK(dir, old_dentry, dentry, NULL, HOOK_LINK, handle, POST_HOOK,
230 post_smfs_dentry(cache_dentry);
231 post_smfs_dentry(cache_parent);
232 post_smfs_dentry(cache_old_dentry);
233 smfs_trans_commit(dir, handle, 0);
237 static int smfs_unlink(struct inode * dir,
238 struct dentry *dentry)
240 struct inode *cache_dir = I2CI(dir);
241 struct inode *cache_inode = I2CI(dentry->d_inode);
242 struct dentry *cache_dentry;
243 struct dentry *cache_parent;
248 if (!cache_dir || !cache_inode)
251 handle = smfs_trans_start(dir, FSFILT_OP_UNLINK, NULL);
255 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_UNLINK, handle, PRE_HOOK, rc,
258 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry, NULL);
259 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry, NULL);
261 if (!cache_parent || !cache_dentry)
262 GOTO(exit, rc = -ENOMEM);
265 pre_smfs_inode(dir, cache_dir);
266 pre_smfs_inode(dentry->d_inode, cache_inode);
267 if (cache_dir->i_op->unlink)
268 rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
269 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
270 post_smfs_inode(dir, cache_dir);
273 SMFS_HOOK(dir, dentry, &mode, NULL, HOOK_UNLINK, handle, POST_HOOK,
276 post_smfs_dentry(cache_dentry);
277 post_smfs_dentry(cache_parent);
278 smfs_trans_commit(dir, handle, 0);
282 static int smfs_symlink(struct inode *dir, struct dentry *dentry,
285 struct inode *cache_dir = I2CI(dir);
286 struct inode *inode = NULL;
287 struct dentry *cache_dentry;
288 struct dentry *cache_parent;
295 handle = smfs_trans_start(dir, FSFILT_OP_SYMLINK, NULL);
300 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_SYMLINK, handle, PRE_HOOK, rc,
303 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry, NULL);
304 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry, NULL);
306 if (!cache_parent || !cache_dentry)
307 GOTO(exit, rc = -ENOMEM);
309 pre_smfs_inode(dir, cache_dir);
310 if (cache_dir->i_op->symlink)
311 rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
314 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino, dir, 0);
315 post_smfs_inode(dir, cache_dir);
317 d_instantiate(dentry, inode);
321 tgt_len = strlen(symname) + 1;
323 SMFS_HOOK(dir, dentry, (char *)symname, &tgt_len, HOOK_SYMLINK, handle,
324 POST_HOOK, rc, exit);
327 post_smfs_dentry(cache_dentry);
328 post_smfs_dentry(cache_parent);
329 smfs_trans_commit(dir, handle, 0);
333 static int smfs_mkdir(struct inode *dir, struct dentry *dentry,
336 struct inode *cache_dir = I2CI(dir);
337 struct inode *inode = NULL;
338 struct dentry *cache_dentry;
339 struct dentry *cache_parent;
346 handle = smfs_trans_start(dir, FSFILT_OP_MKDIR, NULL);
352 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_MKDIR, handle, PRE_HOOK, rc,
355 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry, NULL);
356 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry, NULL);
358 if (!cache_parent || !cache_dentry)
359 GOTO(exit, rc = -ENOMEM);
361 pre_smfs_inode(dir, cache_dir);
363 if (cache_dir->i_op->mkdir)
364 rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
369 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino, dir, 0);
372 GOTO(exit, rc = -ENOENT);
374 d_instantiate(dentry, inode);
375 post_smfs_inode(dir, cache_dir);
377 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_MKDIR, handle, POST_HOOK, rc,
381 post_smfs_dentry(cache_dentry);
382 post_smfs_dentry(cache_parent);
383 smfs_trans_commit(dir, handle, 0);
387 static int smfs_rmdir(struct inode *dir, struct dentry *dentry)
389 struct inode *cache_dir = I2CI(dir);
390 struct inode *cache_inode = I2CI(dentry->d_inode);
391 struct dentry *cache_dentry = NULL;
392 struct dentry *cache_parent = NULL;
394 int rc = 0, mode = S_IFDIR;
399 handle = smfs_trans_start(dir, FSFILT_OP_RMDIR, NULL);
400 if (IS_ERR(handle) ) {
401 CERROR("smfs_do_mkdir: no space for transaction\n");
407 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_RMDIR, handle, PRE_HOOK, rc,
410 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry, NULL);
411 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry, NULL);
413 if (!cache_parent || !cache_dentry)
414 GOTO(exit, rc = -ENOMEM);
416 pre_smfs_inode(dir, cache_dir);
417 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
418 if (cache_dir->i_op->rmdir)
419 rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
421 post_smfs_inode(dir, cache_dir);
422 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
425 SMFS_HOOK(dir, dentry, &mode, NULL, HOOK_RMDIR, handle, POST_HOOK,
428 post_smfs_dentry(cache_dentry);
429 post_smfs_dentry(cache_parent);
430 smfs_trans_commit(dir, handle, 0);
434 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
435 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
438 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
439 int mode, dev_t rdev)
442 struct inode *cache_dir = I2CI(dir);
443 struct inode *inode = NULL;
444 struct dentry *cache_dentry = NULL;
445 struct dentry *cache_parent = NULL;
452 handle = smfs_trans_start(dir, FSFILT_OP_MKNOD, NULL);
453 if (IS_ERR(handle)) {
454 CERROR("smfs_do_mkdir: no space for transaction\n");
459 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_MKNOD, handle, PRE_HOOK, rc,
462 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent, NULL);
463 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry, NULL);
464 if (!cache_parent || !cache_dentry)
465 GOTO(exit, rc = -ENOMEM);
467 pre_smfs_inode(dir, cache_dir);
468 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
470 if (!cache_dir->i_op->mknod)
473 if ((rc = cache_dir->i_op->mknod(cache_dir, cache_dentry,
477 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino, dir, 0);
479 d_instantiate(dentry, inode);
481 pre_smfs_inode(dir, cache_dir);
482 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
484 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_MKNOD, handle, POST_HOOK, rc,
489 post_smfs_dentry(cache_dentry);
490 post_smfs_dentry(cache_parent);
491 smfs_trans_commit(dir, handle, 0);
495 static int smfs_rename(struct inode * old_dir, struct dentry *old_dentry,
496 struct inode * new_dir,struct dentry *new_dentry)
498 struct inode *cache_old_dir = I2CI(old_dir);
499 struct inode *cache_new_dir = I2CI(new_dir);
500 struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
502 struct inode *cache_new_inode = new_dentry->d_inode ?
503 I2CI(new_dentry->d_inode) : NULL;
505 struct dentry *cache_old_dentry = NULL;
506 struct dentry *cache_new_dentry = NULL;
507 struct dentry *cache_new_parent = NULL;
508 struct dentry *cache_old_parent = NULL;
512 if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
515 handle = smfs_trans_start(old_dir, FSFILT_OP_RENAME, NULL);
516 if (IS_ERR(handle)) {
517 CERROR("smfs_do_mkdir: no space for transaction\n");
523 SMFS_HOOK(old_dir, old_dentry, new_dir, new_dentry, HOOK_RENAME, handle,
526 cache_old_parent = pre_smfs_dentry(NULL, cache_old_dir, old_dentry, NULL);
527 cache_old_dentry = pre_smfs_dentry(cache_old_parent, cache_old_inode,
529 if (!cache_old_parent || !cache_old_dentry)
530 GOTO(exit, rc = -ENOMEM);
532 cache_new_parent = pre_smfs_dentry(NULL, cache_new_dir, new_dentry, NULL);
533 cache_new_dentry = pre_smfs_dentry(cache_new_parent, cache_new_inode,
535 if (!cache_new_parent || !cache_new_dentry)
536 GOTO(exit, rc = -ENOMEM);
538 pre_smfs_inode(old_dir, cache_old_dir);
539 pre_smfs_inode(new_dir, cache_new_dir);
541 if (cache_old_dir->i_op->rename)
542 rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
543 cache_new_dir, cache_new_dentry);
545 post_smfs_inode(old_dir, cache_old_dir);
546 post_smfs_inode(new_dir, cache_new_dir);
548 SMFS_HOOK(old_dir, old_dentry, new_dir, new_dentry, HOOK_RENAME, handle,
549 POST_HOOK, rc, exit);
551 if (new_dentry->d_inode)
552 post_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
555 post_smfs_dentry(cache_old_dentry);
556 post_smfs_dentry(cache_old_parent);
557 post_smfs_dentry(cache_new_dentry);
558 post_smfs_dentry(cache_new_parent);
559 smfs_trans_commit(old_dir, handle, 0);
563 struct inode_operations smfs_dir_iops = {
566 link: smfs_link, /* BKL held */
567 unlink: smfs_unlink, /* BKL held */
568 symlink: smfs_symlink, /* BKL held */
569 mkdir: smfs_mkdir, /* BKL held */
570 rmdir: smfs_rmdir, /* BKL held */
571 mknod: smfs_mknod, /* BKL held */
572 rename: smfs_rename, /* BKL held */
573 setxattr: smfs_setxattr, /* BKL held */
574 getxattr: smfs_getxattr, /* BKL held */
575 listxattr: smfs_listxattr, /* BKL held */
576 removexattr: smfs_removexattr, /* BKL held */
579 static ssize_t smfs_read_dir(struct file *filp, char *buf,
580 size_t size, loff_t *ppos)
582 struct dentry *dentry = filp->f_dentry;
583 struct inode *cache_inode = NULL;
584 struct smfs_file_info *sfi = NULL;
589 cache_inode = I2CI(dentry->d_inode);
595 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
597 if (ppos != &(filp->f_pos))
598 cache_ppos = &tmp_ppos;
600 cache_ppos = &sfi->c_file->f_pos;
603 if (cache_inode->i_fop->read)
604 rc = cache_inode->i_fop->read(sfi->c_file, buf, size,
608 duplicate_file(filp, sfi->c_file);
612 static int smfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
614 struct dentry *dentry = filp->f_dentry;
615 struct inode *cache_inode = NULL;
616 struct smfs_file_info *sfi = NULL;
619 cache_inode = I2CI(dentry->d_inode);
624 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
626 if (cache_inode->i_fop->readdir)
627 rc = cache_inode->i_fop->readdir(sfi->c_file, dirent, filldir);
629 duplicate_file(filp, sfi->c_file);
633 struct file_operations smfs_dir_fops = {
635 readdir: smfs_readdir, /* BKL held */
636 ioctl: smfs_ioctl, /* BKL held */
637 fsync: smfs_fsync, /* BKL held */
639 release: smfs_release,