Whamcloud - gitweb
b=6274
[fs/lustre-release.git] / lustre / smfs / dir.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2004 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
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.
11  *
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.
16  *
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.
20  *
21  */
22
23 #define DEBUG_SUBSYSTEM S_SM
24
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>
39 #include <linux/security.h>
40 #include "smfs_internal.h"
41
42 //#define NAME_ALLOC_LEN(len)     ((len+16) & ~15)
43
44 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
45 static int smfs_create(struct inode *dir, struct dentry *dentry,
46                        int mode)
47 #else
48 static int smfs_create(struct inode *dir, struct dentry *dentry,
49                        int mode, struct nameidata *nd)
50 #endif
51 {
52         struct inode *inode = NULL;
53         struct inode *parent = I2CI(dentry->d_parent->d_inode);
54         struct inode *cache_dir = I2CI(dir);
55         struct dentry *cache_dentry = NULL;
56         struct dentry *cache_parent = NULL;
57         void *handle = NULL;
58         struct hook_msg msg = {
59                 .dentry = dentry,
60         };
61         int rc = 0;
62         
63         ENTRY;
64
65         LASSERT(cache_dir);
66         LASSERT(cache_dir->i_op->create);
67
68         //lock_kernel();
69         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
70         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
71         if (!cache_dentry || !cache_parent) {
72                 rc = -ENOMEM;
73                 goto exit;
74         }
75        
76         handle = smfs_trans_start(dir, FSFILT_OP_CREATE, NULL);
77         if (IS_ERR(handle)) {
78                 rc = -ENOSPC;
79                 goto exit;
80         }
81         
82         SMFS_PRE_HOOK(dir, HOOK_CREATE, &msg);
83
84         pre_smfs_inode(dir, cache_dir);
85
86 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
87         rc = cache_dir->i_op->create(cache_dir, cache_dentry, mode);
88 #else
89         rc = cache_dir->i_op->create(cache_dir, cache_dentry, mode, nd);
90 #endif
91         if (!rc) {
92                 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
93                                        dir, 0);
94                 if (inode) {
95                         d_instantiate(dentry, inode);
96                 }
97                 else
98                         rc = -ENOENT;
99         }
100         
101         SMFS_POST_HOOK(dir, HOOK_CREATE, &msg, rc); 
102
103         post_smfs_inode(dir, cache_dir);
104         smfs_trans_commit(dir, handle, 0);
105
106 exit:
107         //unlock_kernel();
108         post_smfs_dentry(cache_dentry);
109         post_smfs_dentry(cache_parent);
110         RETURN(rc);
111 }
112
113 static struct dentry * iopen_connect_dentry(struct dentry * dentry,
114                                      struct inode *inode, int rehash)
115 {
116         struct dentry *tmp, *goal = NULL;
117         struct list_head *lp;
118
119         /* verify this dentry is really new */
120         LASSERT(dentry->d_inode == NULL);
121         LASSERT(list_empty(&dentry->d_alias));          /* d_instantiate */
122         if (rehash)
123                 LASSERT(d_unhashed(dentry));    /* d_rehash */
124         LASSERT(list_empty(&dentry->d_subdirs));
125
126         spin_lock(&dcache_lock);
127         if (!inode)
128                 goto do_rehash;
129
130         /* preferrably return a connected dentry */
131         list_for_each(lp, &inode->i_dentry) {
132                 tmp = list_entry(lp, struct dentry, d_alias);
133                 if (tmp->d_flags & DCACHE_DISCONNECTED) {
134                         LASSERT(tmp->d_alias.next == &inode->i_dentry);
135                         LASSERT(tmp->d_alias.prev == &inode->i_dentry);
136                         goal = tmp;
137                         dget_locked(goal);
138                         break;
139                 }
140         }
141
142         if (!goal)
143                 goto do_instantiate;
144
145         /* Move the goal to the de hash queue */
146         goal->d_flags &= ~ DCACHE_DISCONNECTED;
147         security_d_instantiate(goal, inode);
148         __d_rehash(dentry);
149         __d_move(goal, dentry);
150         spin_unlock(&dcache_lock);
151         iput(inode);
152
153         RETURN(goal);
154
155         /* d_add(), but don't drop dcache_lock before adding dentry to inode */
156 do_instantiate:
157         list_add(&dentry->d_alias, &inode->i_dentry);   /* d_instantiate */
158         dentry->d_inode = inode;
159 do_rehash:
160         if (rehash)
161                 __d_rehash(dentry);
162         spin_unlock(&dcache_lock);
163
164         RETURN(NULL);
165
166 }
167
168 static int smfs_do_lookup (struct inode * dir, 
169                            struct dentry * dentry,
170                            struct nameidata *nd,
171                            struct inode **inode)
172 {
173         struct inode *cache_dir = I2CI(dir);
174         struct inode *parent = I2CI(dentry->d_parent->d_inode);
175         struct dentry *cache_dentry = NULL;
176         struct dentry *cache_parent = NULL;
177         struct dentry *rdentry = NULL, *tmp = NULL;
178         int rc = 0;
179         struct hook_msg msg = {
180                 .dentry = dentry,
181         };
182
183         ENTRY;
184         
185         cache_dir = I2CI(dir);
186         if (!cache_dir)
187                 RETURN(-ENOENT);
188
189         LASSERT(cache_dir->i_op->lookup);
190
191         /* preparing artificial backing fs dentries. */
192         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
193         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
194         if (!cache_dentry || !cache_parent) {
195                 rc = -ENOMEM;
196                 goto exit;
197         }
198         
199         SMFS_PRE_HOOK(dir, HOOK_LOOKUP, &msg);
200         
201         /* perform lookup in backing fs. */
202         rdentry = cache_dir->i_op->lookup(cache_dir, cache_dentry, nd);
203         if (rdentry) {
204                 if (IS_ERR(rdentry)) {
205                         rc = PTR_ERR(rdentry);
206                         rdentry = NULL;
207                 }
208                 else {
209                         tmp = rdentry;
210                         //copy fields if DCACHE_CROSS_REF
211                         smfs_update_dentry(dentry, tmp);
212                 }
213         } else 
214                 tmp = cache_dentry;
215         
216         SMFS_POST_HOOK(dir, HOOK_LOOKUP, &msg, rc);
217      
218         if (tmp && tmp->d_inode) {
219                 *inode = smfs_get_inode(dir->i_sb, tmp->d_inode->i_ino, 
220                                         dir, 0); 
221                 if (!(*inode))
222                         rc = -ENOENT;
223         }
224         
225         if (rdentry) {
226                 LASSERT(atomic_read(&rdentry->d_count) > 0);
227                 dput(rdentry);
228         }
229         
230 exit:
231         post_smfs_dentry(cache_dentry);
232         post_smfs_dentry(cache_parent);
233         
234         RETURN(rc);
235 }
236
237 static struct dentry * smfs_iopen_lookup(struct inode * dir, 
238                                          struct dentry *dentry,
239                                          struct nameidata *nd)
240 {
241         struct dentry * alternate = NULL;
242         struct inode *inode = NULL;
243         int rc = 0;
244         ENTRY;
245         
246         rc = smfs_do_lookup(dir, dentry, nd, &inode);
247         if (rc)
248                 RETURN(ERR_PTR(rc));
249         
250         LASSERT(inode);
251         /* preferrably return a connected dentry */
252         spin_lock(&dcache_lock);
253         list_for_each_entry(alternate, &inode->i_dentry, d_alias) {
254                 LASSERT(!(alternate->d_flags & DCACHE_DISCONNECTED));
255         }
256
257         list_for_each_entry(alternate, &inode->i_dentry, d_alias) {
258                 dget_locked(alternate);
259                 spin_lock(&alternate->d_lock);
260                 alternate->d_flags |= DCACHE_REFERENCED;
261                 spin_unlock(&alternate->d_lock);
262                 iput(inode);
263                 spin_unlock(&dcache_lock);
264                 RETURN(alternate);
265         }
266         
267         dentry->d_flags |= DCACHE_DISCONNECTED;
268
269         /* d_add(), but don't drop dcache_lock before adding dentry to inode */
270         list_add(&dentry->d_alias, &inode->i_dentry);   /* d_instantiate */
271         dentry->d_inode = inode;
272
273         __d_rehash(dentry);                             /* d_rehash */
274         spin_unlock(&dcache_lock);
275
276         return NULL;
277 }
278
279 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
280 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry)
281 #else
282 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry,
283                                   struct nameidata *nd)
284 #endif
285 {
286         struct dentry * rdentry = NULL;
287         struct inode * inode = NULL;
288         int rc;
289         
290         rc = smfs_do_lookup(dir, dentry, nd, &inode);
291         if (rc)
292                 RETURN(ERR_PTR(rc));
293         
294         //lmv stuff. Special dentry that has no inode.
295         if (dentry->d_flags & DCACHE_CROSS_REF) {
296                 d_add(dentry, NULL);
297                 RETURN(NULL);
298         }
299         //TODO: should flags be checked and copied before?        
300         rdentry = iopen_connect_dentry(dentry, inode, 1);
301         
302         RETURN(rdentry);
303 }
304
305 static int smfs_link(struct dentry *old_dentry,
306                      struct inode *dir, struct dentry *dentry)
307 {
308         struct inode *parent = I2CI(dentry->d_parent->d_inode);
309         struct inode *cache_dir = I2CI(dir);
310         struct inode *old_inode = old_dentry->d_inode;
311         struct inode *cache_old_inode = I2CI(old_inode);
312         struct dentry *cache_old_dentry = NULL;
313         struct dentry *cache_dentry = NULL;
314         struct dentry *cache_parent = NULL;
315         void *handle = NULL;
316         int rc = 0;
317         struct hook_link_msg msg = {
318                 .dentry = old_dentry,
319                 .new_dentry = dentry
320         };
321
322         ENTRY;
323
324         if (!cache_dir)
325                 RETURN(-ENOENT);
326         
327         if (!cache_old_inode)
328                 RETURN(-ENOENT);
329         
330         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
331         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
332         cache_old_dentry = pre_smfs_dentry(NULL, cache_old_inode, old_dentry);
333         if (!cache_old_dentry || !cache_dentry || !cache_parent) {
334                 rc = -ENOMEM;
335                 goto exit;
336         }        
337         
338         handle = smfs_trans_start(dir, FSFILT_OP_LINK, NULL);
339         if (IS_ERR(handle)) {
340                  rc = -ENOSPC;
341                  goto exit;
342         }
343
344         pre_smfs_inode(dir, cache_dir);
345         pre_smfs_inode(old_inode, cache_old_inode);
346
347         //lock_kernel();
348         SMFS_PRE_HOOK(dir, HOOK_LINK, &msg); 
349
350         rc = cache_dir->i_op->link(cache_old_dentry, cache_dir, cache_dentry);
351         if (!rc) {
352                 atomic_inc(&old_inode->i_count);
353                 dput(iopen_connect_dentry(dentry, old_inode, 0));
354         }
355
356         SMFS_POST_HOOK(dir, HOOK_LINK, &msg, rc); 
357         
358         post_smfs_inode(old_inode, cache_old_inode);
359         post_smfs_inode(dir, cache_dir);
360
361         smfs_trans_commit(dir, handle, 0);
362         
363 exit:
364         //unlock_kernel();
365         post_smfs_dentry(cache_dentry);
366         post_smfs_dentry(cache_parent);
367         post_smfs_dentry(cache_old_dentry);
368         
369         RETURN(rc);
370 }
371
372 static int smfs_unlink(struct inode * dir, struct dentry *dentry)
373 {
374         struct inode *cache_dir = I2CI(dir);
375         struct inode *cache_inode = I2CI(dentry->d_inode);
376         struct inode *parent = I2CI(dentry->d_parent->d_inode);
377         struct dentry *cache_dentry = NULL;
378         struct dentry *cache_parent = NULL;
379         void   *handle = NULL;
380         int    rc = 0;
381         //int    mode = 0;
382         struct hook_unlink_msg msg = {
383                 .dentry = dentry,
384                 .mode = 0
385         };
386
387         ENTRY;
388         
389         LASSERT(cache_dir);
390         LASSERT(cache_inode);
391         LASSERT(cache_dir->i_op->unlink);
392         LASSERT(parent);
393         
394         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
395         cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
396         if (!cache_dentry || !cache_parent) {
397                 rc = -ENOMEM;
398                 goto exit;
399         }
400                 
401         //lock_kernel();
402         handle = smfs_trans_start(dir, FSFILT_OP_UNLINK, NULL);
403         if (IS_ERR(handle)) {
404                 rc = -ENOSPC;
405                 goto exit;
406         }
407
408         pre_smfs_inode(dir, cache_dir);
409         pre_smfs_inode(dentry->d_inode, cache_inode);
410
411         SMFS_PRE_HOOK(dir, HOOK_UNLINK, &msg); 
412         
413         rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
414                 
415         SMFS_POST_HOOK(dir, HOOK_UNLINK, &msg, rc); 
416
417         post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
418         post_smfs_inode(dir, cache_dir);
419         //unlock_kernel();
420         
421         smfs_trans_commit(dir, handle, 0);
422 exit:
423         post_smfs_dentry(cache_dentry);
424         post_smfs_dentry(cache_parent);
425         RETURN(rc);
426 }
427
428 static int smfs_symlink(struct inode *dir, struct dentry *dentry,
429                         const char *symname)
430 {
431         struct inode *cache_dir = I2CI(dir);
432         struct inode *inode = NULL;
433         struct inode *parent = I2CI(dentry->d_parent->d_inode);
434         struct dentry *cache_dentry = NULL;
435         struct dentry *cache_parent = NULL;
436         void   *handle = NULL;
437         int    rc = 0;
438         struct hook_symlink_msg msg = {
439                 .dentry = dentry,
440                 .tgt_len = strlen(symname) + 1,
441                 .symname = (char*)symname
442         };
443
444         ENTRY;
445         
446         LASSERT(cache_dir);
447         LASSERT(cache_dir->i_op->symlink);
448         LASSERT(parent);
449
450         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
451         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
452         if (!cache_parent || !cache_dentry) {
453                 rc = -ENOMEM;
454                 goto exit;
455         }
456        
457         handle = smfs_trans_start(dir, FSFILT_OP_SYMLINK, NULL);
458         if (IS_ERR(handle)) {
459                 rc = -ENOSPC;
460                 goto exit;
461         }
462         
463         //lock_kernel();
464         pre_smfs_inode(dir, cache_dir);
465
466         SMFS_PRE_HOOK(dir, HOOK_SYMLINK, &msg); 
467         
468         rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
469         if (!rc) {        
470                 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
471                                        dir, 0);
472                 if (inode) {
473                         //smfs_update_dentry(dentry, cache_dentry);
474                         d_instantiate(dentry, inode);
475                 }
476                 else
477                         rc = -ENOENT;
478         }
479         
480         SMFS_POST_HOOK(dir, HOOK_SYMLINK, &msg, rc);
481         
482         post_smfs_inode(dir, cache_dir);
483         smfs_trans_commit(dir, handle, 0);
484
485 exit:
486         //unlock_kernel();
487         post_smfs_dentry(cache_dentry);
488         post_smfs_dentry(cache_parent);
489         RETURN(rc);
490 }
491
492 static int smfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
493 {
494         struct inode *cache_dir = I2CI(dir);
495         struct inode *parent = I2CI(dentry->d_parent->d_inode);
496         struct inode *inode = NULL;
497         struct dentry *cache_dentry = NULL;
498         struct dentry *cache_parent = NULL;
499         void   *handle = NULL;
500         int    rc = 0;
501         struct hook_msg msg = {
502                 .dentry = dentry,
503         };
504
505         ENTRY;
506         
507         LASSERT(cache_dir);
508         LASSERT(parent);
509         
510         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
511         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
512         if (!cache_parent || !cache_dentry) {
513                 rc = -ENOMEM;
514                 goto exit;
515         }
516
517         handle = smfs_trans_start(dir, FSFILT_OP_MKDIR, NULL);
518         if (IS_ERR(handle)) {
519                 rc = -ENOSPC;
520                 goto exit;
521         }
522         
523         pre_smfs_inode(dir, cache_dir);
524         SMFS_PRE_HOOK(dir, HOOK_MKDIR, &msg); 
525         
526         rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
527         if (!rc) {
528                 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
529                                        dir, 0);
530                 if (inode) {
531                         //smsf_update_dentry(dentry, cache_dentry);
532                         d_instantiate(dentry, inode);
533                 }
534                 else
535                         rc = -ENOENT;
536         }
537
538         SMFS_POST_HOOK(dir, HOOK_MKDIR, &msg, rc); 
539         post_smfs_inode(dir, cache_dir);
540         smfs_trans_commit(dir, handle, 0);
541
542 exit:
543         post_smfs_dentry(cache_dentry);
544         post_smfs_dentry(cache_parent);
545         RETURN(rc);
546 }
547
548 static int smfs_rmdir(struct inode *dir, struct dentry *dentry)
549 {
550         struct inode *cache_dir = I2CI(dir);
551         struct inode *cache_inode = I2CI(dentry->d_inode);
552         struct inode *parent = I2CI(dentry->d_parent->d_inode);
553         struct dentry *cache_dentry = NULL;
554         struct dentry *cache_parent = NULL;
555         void *handle = NULL;
556         int    rc = 0;
557         struct hook_unlink_msg msg = {
558                 .dentry = dentry,
559                 .mode = S_IFDIR
560         };
561
562         ENTRY;
563         
564         LASSERT(cache_dir);
565         LASSERT(cache_dir->i_op->rmdir);
566         LASSERT(parent);
567
568         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
569         cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
570         if (!cache_parent || !cache_dentry) {
571                 rc = -ENOMEM;
572                 goto exit;
573         }
574
575         handle = smfs_trans_start(dir, FSFILT_OP_RMDIR, NULL);
576         if (IS_ERR(handle) ) {
577                 rc = -ENOSPC;
578                 goto exit;
579         }
580
581         pre_smfs_inode(dir, cache_dir);
582         pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
583         
584         SMFS_PRE_HOOK(dir, HOOK_RMDIR, &msg); 
585         
586         rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
587               
588         SMFS_POST_HOOK(dir, HOOK_RMDIR, &msg, rc); 
589         
590         post_smfs_inode(dir, cache_dir);
591         post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
592         //like vfs_rmdir is doing with inode
593         if (!rc)
594                 cache_dentry->d_inode->i_flags |= S_DEAD;
595         
596         smfs_trans_commit(dir, handle, 0);
597
598 exit:
599         post_smfs_dentry(cache_dentry);
600         post_smfs_dentry(cache_parent);
601         RETURN(rc);
602 }
603
604 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
605 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
606                       int mode, int rdev)
607 #else
608 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
609                       int mode, dev_t rdev)
610 #endif
611 {
612         struct inode *cache_dir = I2CI(dir);
613         struct inode *inode = NULL;
614         struct inode *parent = I2CI(dentry->d_parent->d_inode);
615         struct dentry *cache_dentry = NULL;
616         struct dentry *cache_parent = NULL;
617         void *handle = NULL;
618         int rc = 0;
619         struct hook_msg msg = {
620                 .dentry = dentry,
621         };
622  
623         ENTRY;
624         
625         LASSERT(parent);
626         LASSERT(cache_dir);
627         LASSERT(cache_dir->i_op->mknod);
628
629         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
630         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
631         if (!cache_parent || !cache_dentry) {
632                 rc = -ENOMEM;
633                 goto exit;
634         }
635
636         handle = smfs_trans_start(dir, FSFILT_OP_MKNOD, NULL);
637         if (IS_ERR(handle)) {
638                 rc = -ENOSPC;
639                 goto exit;
640         }
641         
642         pre_smfs_inode(dir, cache_dir);
643         
644         SMFS_PRE_HOOK(dir, HOOK_MKNOD, &msg); 
645         
646         rc = cache_dir->i_op->mknod(cache_dir, cache_dentry, mode, rdev);
647         if (!rc) {
648                 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
649                                        dir, 0);
650                 if (inode) {
651                         //smsf_update_dentry(dentry, cache_dentry);
652                         d_instantiate(dentry, inode);
653                 }
654                 else
655                         rc = -ENOENT;
656         }
657
658         SMFS_POST_HOOK(dir, HOOK_MKNOD, &msg, rc); 
659         
660         post_smfs_inode(dir, cache_dir);
661         
662         smfs_trans_commit(dir, handle, 0);
663
664 exit:
665         post_smfs_dentry(cache_dentry);
666         post_smfs_dentry(cache_parent);
667         RETURN(rc);
668 }
669
670 static int smfs_rename(struct inode *old_dir, struct dentry *old_dentry,
671                        struct inode *new_dir,struct dentry *new_dentry)
672 {
673         struct inode *cache_old_dir = I2CI(old_dir);
674         struct inode *cache_new_dir = I2CI(new_dir);
675         struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
676         struct inode *old_parent = I2CI(old_dentry->d_parent->d_inode);
677         struct inode *new_parent = I2CI(new_dentry->d_parent->d_inode);
678         struct inode *cache_new_inode = NULL;
679         struct dentry *cache_old_dentry = NULL;
680         struct dentry *cache_new_dentry = NULL;
681         struct dentry *cache_new_parent = NULL;
682         struct dentry *cache_old_parent = NULL;
683         void *handle = NULL;
684         int    rc = 0;
685         struct hook_rename_msg msg = {
686                 .dentry = old_dentry,
687                 .new_dir = new_dir,
688                 .new_dentry = new_dentry
689         };
690
691         ENTRY;
692                 
693         if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
694                 RETURN(-ENOENT);
695
696         if (new_dentry->d_inode) {
697                 cache_new_inode = I2CI(new_dentry->d_inode);
698                 if (!cache_new_inode)
699                         RETURN(-ENOENT);
700         }
701         
702         cache_old_parent = pre_smfs_dentry(NULL, old_parent, old_dentry->d_parent);
703         cache_old_dentry = pre_smfs_dentry(cache_old_parent, cache_old_inode,
704                                            old_dentry);
705         if (!cache_old_parent || !cache_old_dentry) {
706                 rc = -ENOMEM;
707                 goto exit;
708         }
709         
710         cache_new_parent = pre_smfs_dentry(NULL, new_parent, new_dentry->d_parent);
711         cache_new_dentry = pre_smfs_dentry(cache_new_parent, cache_new_inode,
712                                            new_dentry);
713         if (!cache_new_parent || !cache_new_dentry) {
714                 rc = -ENOMEM;
715                 goto exit;
716         }
717
718         handle = smfs_trans_start(old_dir, FSFILT_OP_RENAME, NULL);
719         if (IS_ERR(handle)) {
720                 rc = -ENOSPC;
721                 goto exit;
722         }
723         
724         pre_smfs_inode(old_dir, cache_old_dir);
725         pre_smfs_inode(new_dir, cache_new_dir);
726         if (new_dentry->d_inode)
727                 pre_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
728
729         SMFS_PRE_HOOK(old_dir, HOOK_RENAME, &msg); 
730         
731         rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
732                                          cache_new_dir, cache_new_dentry);
733         
734         SMFS_POST_HOOK(old_dir, HOOK_RENAME, &msg, rc); 
735
736         post_smfs_inode(old_dir, cache_old_dir);
737         post_smfs_inode(new_dir, cache_new_dir);
738         if (new_dentry->d_inode)
739                 post_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
740         
741         smfs_trans_commit(old_dir, handle, 0);
742         
743 exit:
744         post_smfs_dentry(cache_old_dentry);
745         post_smfs_dentry(cache_old_parent);
746         post_smfs_dentry(cache_new_dentry);
747         post_smfs_dentry(cache_new_parent);
748         RETURN(rc);
749 }
750
751 struct inode_operations smfs_dir_iops = {
752         create:         smfs_create,
753         lookup:         smfs_lookup,
754         link:           smfs_link,              /* BKL held */
755         unlink:         smfs_unlink,            /* BKL held */
756         symlink:        smfs_symlink,           /* BKL held */
757         mkdir:          smfs_mkdir,             /* BKL held */
758         rmdir:          smfs_rmdir,             /* BKL held */
759         mknod:          smfs_mknod,             /* BKL held */
760         rename:         smfs_rename,            /* BKL held */
761         setxattr:       smfs_setxattr,          /* BKL held */
762         getxattr:       smfs_getxattr,          /* BKL held */
763         listxattr:      smfs_listxattr,         /* BKL held */
764         removexattr:    smfs_removexattr,       /* BKL held */
765 };
766
767 struct inode_operations smfs_iopen_iops = {
768         lookup:         smfs_iopen_lookup,
769 };
770
771 static ssize_t smfs_read_dir(struct file *filp, char *buf,
772                              size_t size, loff_t *ppos)
773 {
774         struct dentry *dentry = filp->f_dentry;
775         struct inode *cache_inode = NULL;
776         struct smfs_file_info *sfi = NULL;
777         loff_t tmp_ppos;
778         loff_t *cache_ppos = NULL;
779         int    rc = 0;
780
781         ENTRY;
782         
783         cache_inode = I2CI(dentry->d_inode);
784
785         if (!cache_inode || !cache_inode->i_fop->read)
786                 RETURN(-EINVAL);
787
788         sfi = F2SMFI(filp);
789         if (sfi->magic != SMFS_FILE_MAGIC)
790                 BUG();
791
792         if (ppos != &(filp->f_pos))
793                 cache_ppos = &tmp_ppos;
794         else
795                 cache_ppos = &sfi->c_file->f_pos;
796         
797         *cache_ppos = *ppos;
798
799         rc = cache_inode->i_fop->read(sfi->c_file, buf, size, cache_ppos);
800         if (rc)
801                 RETURN(rc);
802
803         *ppos = *cache_ppos;
804         
805         duplicate_file(filp, sfi->c_file);
806         
807         RETURN(rc);
808 }
809
810 static int smfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
811 {
812         struct dentry *dentry = filp->f_dentry;
813         struct inode *cache_inode = NULL;
814         struct smfs_file_info *sfi = NULL;
815         int    rc = 0;
816         struct hook_readdir_msg msg = {
817                 .dentry = dentry,
818                 .filp = filp,
819                 .dirent = dirent,
820                 .filldir = filldir
821         };
822
823         ENTRY;
824         
825         cache_inode = I2CI(dentry->d_inode);
826         if (!cache_inode || !cache_inode->i_fop->readdir)
827                 RETURN(-EINVAL);
828
829         sfi = F2SMFI(filp);
830         if (sfi->magic != SMFS_FILE_MAGIC) BUG();
831
832         SMFS_PRE_HOOK(dentry->d_inode, HOOK_READDIR, &msg); 
833         
834         rc = cache_inode->i_fop->readdir(sfi->c_file, dirent, filldir);
835         
836         SMFS_POST_HOOK(dentry->d_inode, HOOK_READDIR, &msg, rc);
837         duplicate_file(filp, sfi->c_file);
838
839         RETURN(rc);
840 }
841
842 struct file_operations smfs_dir_fops = {
843         .read           = smfs_read_dir,
844         .readdir        = smfs_readdir,       /* BKL held */
845         .ioctl          = smfs_ioctl,         /* BKL held */
846         .fsync          = smfs_fsync,         /* BKL held */
847         .open           = smfs_open,
848         .release        = smfs_release,
849 };
850
851 struct file_operations smfs_iopen_fops = {
852         .read           = smfs_read_dir,
853 };
854