Whamcloud - gitweb
b=5881
[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, int * flags)
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;
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                 RETURN(-ENOMEM);
196         
197         SMFS_PRE_HOOK(dir, HOOK_LOOKUP, &msg);
198         
199         /* perform lookup in backing fs. */
200         rdentry = cache_dir->i_op->lookup(cache_dir, cache_dentry, nd);
201         if (rdentry) {
202                 if (IS_ERR(rdentry))
203                         rc = PTR_ERR(rdentry);
204         } else {
205                 rdentry = dget(cache_dentry);
206         }
207         
208         if (!rc && rdentry->d_inode) {
209                 *inode = smfs_get_inode(dir->i_sb, rdentry->d_inode->i_ino, 
210                                        dir, 0);
211                 if (!(*inode)) {
212                         rc = -ENOENT;
213                 }
214         }
215
216         SMFS_POST_HOOK(dir, HOOK_LOOKUP, &msg, rc);
217         
218         if (!rc) {
219                 smfs_update_dentry(dentry, rdentry);
220                 *flags = rdentry->d_flags;
221                 dput(rdentry);
222         }
223         post_smfs_dentry(cache_dentry);
224         post_smfs_dentry(cache_parent);
225         
226         RETURN(rc);
227 }
228
229 static struct dentry * smfs_iopen_lookup(struct inode * dir, 
230                                          struct dentry *dentry,
231                                          struct nameidata *nd)
232 {
233         struct dentry * alternate = NULL;
234         struct inode *inode = NULL;
235         int flags = 0;
236         int rc = 0;
237         ENTRY;
238         
239         rc = smfs_do_lookup(dir, dentry, nd, &inode, &flags);
240         if (rc)
241                 RETURN(ERR_PTR(rc));
242         
243         LASSERT(inode);
244         /* preferrably return a connected dentry */
245         spin_lock(&dcache_lock);
246         list_for_each_entry(alternate, &inode->i_dentry, d_alias) {
247                 LASSERT(!(alternate->d_flags & DCACHE_DISCONNECTED));
248         }
249
250         list_for_each_entry(alternate, &inode->i_dentry, d_alias) {
251                 dget_locked(alternate);
252                 spin_lock(&alternate->d_lock);
253                 alternate->d_flags |= DCACHE_REFERENCED;
254                 spin_unlock(&alternate->d_lock);
255                 iput(inode);
256                 spin_unlock(&dcache_lock);
257                 RETURN(alternate);
258         }
259         
260         dentry->d_flags |= DCACHE_DISCONNECTED;
261
262         /* d_add(), but don't drop dcache_lock before adding dentry to inode */
263         list_add(&dentry->d_alias, &inode->i_dentry);   /* d_instantiate */
264         dentry->d_inode = inode;
265
266         __d_rehash(dentry);                             /* d_rehash */
267         spin_unlock(&dcache_lock);
268
269         return NULL;
270 }
271
272 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
273 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry)
274 #else
275 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry,
276                                   struct nameidata *nd)
277 #endif
278 {
279         struct dentry * rdentry = NULL;
280         struct inode * inode = NULL;
281         int flags = 0;
282         int rc;
283         
284         rc = smfs_do_lookup(dir, dentry, nd, &inode, &flags);
285         if (rc)
286                 RETURN(ERR_PTR(rc));
287         
288         //lmv stuff. Special dentry that has no inode.
289         if (flags & DCACHE_CROSS_REF) {
290                 d_add(dentry, NULL);
291                 RETURN(NULL);
292         }
293         //TODO: should flags be checked and copied before?        
294         rdentry = iopen_connect_dentry(dentry, inode, 1);
295         
296         RETURN(rdentry);
297 }
298
299 static int smfs_link(struct dentry *old_dentry,
300                      struct inode *dir, struct dentry *dentry)
301 {
302         struct inode *parent = I2CI(dentry->d_parent->d_inode);
303         struct inode *cache_dir = I2CI(dir);
304         struct inode *old_inode = old_dentry->d_inode;
305         struct inode *cache_old_inode = I2CI(old_inode);
306         struct dentry *cache_old_dentry = NULL;
307         struct dentry *cache_dentry = NULL;
308         struct dentry *cache_parent = NULL;
309         void *handle = NULL;
310         int rc = 0;
311         struct hook_link_msg msg = {
312                 .dentry = old_dentry,
313                 .new_dentry = dentry
314         };
315
316         ENTRY;
317
318         if (!cache_dir)
319                 RETURN(-ENOENT);
320         
321         if (!cache_old_inode)
322                 RETURN(-ENOENT);
323         
324         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
325         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
326         cache_old_dentry = pre_smfs_dentry(NULL, cache_old_inode, old_dentry);
327         if (!cache_old_dentry || !cache_dentry || !cache_parent) {
328                 rc = -ENOMEM;
329                 goto exit;
330         }        
331         
332         handle = smfs_trans_start(dir, FSFILT_OP_LINK, NULL);
333         if (IS_ERR(handle)) {
334                  rc = -ENOSPC;
335                  goto exit;
336         }
337
338         pre_smfs_inode(dir, cache_dir);
339         pre_smfs_inode(old_inode, cache_old_inode);
340
341         //lock_kernel();
342         SMFS_PRE_HOOK(dir, HOOK_LINK, &msg); 
343
344         rc = cache_dir->i_op->link(cache_old_dentry, cache_dir, cache_dentry);
345         if (!rc) {
346                 atomic_inc(&old_inode->i_count);
347                 dput(iopen_connect_dentry(dentry, old_inode, 0));
348         }
349
350         SMFS_POST_HOOK(dir, HOOK_LINK, &msg, rc); 
351         
352         post_smfs_inode(old_inode, cache_old_inode);
353         post_smfs_inode(dir, cache_dir);
354
355         smfs_trans_commit(dir, handle, 0);
356         
357 exit:
358         //unlock_kernel();
359         post_smfs_dentry(cache_dentry);
360         post_smfs_dentry(cache_parent);
361         post_smfs_dentry(cache_old_dentry);
362         
363         RETURN(rc);
364 }
365
366 static int smfs_unlink(struct inode * dir, struct dentry *dentry)
367 {
368         struct inode *cache_dir = I2CI(dir);
369         struct inode *cache_inode = I2CI(dentry->d_inode);
370         struct inode *parent = I2CI(dentry->d_parent->d_inode);
371         struct dentry *cache_dentry = NULL;
372         struct dentry *cache_parent = NULL;
373         void   *handle = NULL;
374         int    rc = 0;
375         //int    mode = 0;
376         struct hook_unlink_msg msg = {
377                 .dentry = dentry,
378                 .mode = 0
379         };
380
381         ENTRY;
382         
383         LASSERT(cache_dir);
384         LASSERT(cache_inode);
385         LASSERT(cache_dir->i_op->unlink);
386         LASSERT(parent);
387         
388         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
389         cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
390         if (!cache_dentry || !cache_parent) {
391                 rc = -ENOMEM;
392                 goto exit;
393         }
394                 
395         //lock_kernel();
396         handle = smfs_trans_start(dir, FSFILT_OP_UNLINK, NULL);
397         if (IS_ERR(handle)) {
398                 rc = -ENOSPC;
399                 goto exit;
400         }
401
402         pre_smfs_inode(dir, cache_dir);
403         pre_smfs_inode(dentry->d_inode, cache_inode);
404
405         SMFS_PRE_HOOK(dir, HOOK_UNLINK, &msg); 
406         
407         rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
408                 
409         SMFS_POST_HOOK(dir, HOOK_UNLINK, &msg, rc); 
410
411         post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
412         post_smfs_inode(dir, cache_dir);
413         //unlock_kernel();
414         
415         smfs_trans_commit(dir, handle, 0);
416 exit:
417         post_smfs_dentry(cache_dentry);
418         post_smfs_dentry(cache_parent);
419         RETURN(rc);
420 }
421
422 static int smfs_symlink(struct inode *dir, struct dentry *dentry,
423                         const char *symname)
424 {
425         struct inode *cache_dir = I2CI(dir);
426         struct inode *inode = NULL;
427         struct inode *parent = I2CI(dentry->d_parent->d_inode);
428         struct dentry *cache_dentry = NULL;
429         struct dentry *cache_parent = NULL;
430         void   *handle = NULL;
431         int    rc = 0;
432         struct hook_symlink_msg msg = {
433                 .dentry = dentry,
434                 .tgt_len = strlen(symname) + 1,
435                 .symname = (char*)symname
436         };
437
438         ENTRY;
439         
440         LASSERT(cache_dir);
441         LASSERT(cache_dir->i_op->symlink);
442         LASSERT(parent);
443
444         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
445         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
446         if (!cache_parent || !cache_dentry) {
447                 rc = -ENOMEM;
448                 goto exit;
449         }
450        
451         handle = smfs_trans_start(dir, FSFILT_OP_SYMLINK, NULL);
452         if (IS_ERR(handle)) {
453                 rc = -ENOSPC;
454                 goto exit;
455         }
456         
457         //lock_kernel();
458         pre_smfs_inode(dir, cache_dir);
459
460         SMFS_PRE_HOOK(dir, HOOK_SYMLINK, &msg); 
461         
462         rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
463         if (!rc) {        
464                 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
465                                        dir, 0);
466                 if (inode) {
467                         //smfs_update_dentry(dentry, cache_dentry);
468                         d_instantiate(dentry, inode);
469                 }
470                 else
471                         rc = -ENOENT;
472         }
473         
474         SMFS_POST_HOOK(dir, HOOK_SYMLINK, &msg, rc);
475         
476         post_smfs_inode(dir, cache_dir);
477         smfs_trans_commit(dir, handle, 0);
478
479 exit:
480         //unlock_kernel();
481         post_smfs_dentry(cache_dentry);
482         post_smfs_dentry(cache_parent);
483         RETURN(rc);
484 }
485
486 static int smfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
487 {
488         struct inode *cache_dir = I2CI(dir);
489         struct inode *parent = I2CI(dentry->d_parent->d_inode);
490         struct inode *inode = NULL;
491         struct dentry *cache_dentry = NULL;
492         struct dentry *cache_parent = NULL;
493         void   *handle = NULL;
494         int    rc = 0;
495         struct hook_msg msg = {
496                 .dentry = dentry,
497         };
498
499         ENTRY;
500         
501         LASSERT(cache_dir);
502         LASSERT(parent);
503         
504         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
505         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
506         if (!cache_parent || !cache_dentry) {
507                 rc = -ENOMEM;
508                 goto exit;
509         }
510
511         handle = smfs_trans_start(dir, FSFILT_OP_MKDIR, NULL);
512         if (IS_ERR(handle)) {
513                 rc = -ENOSPC;
514                 goto exit;
515         }
516         
517         pre_smfs_inode(dir, cache_dir);
518         SMFS_PRE_HOOK(dir, HOOK_MKDIR, &msg); 
519         
520         rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
521         if (!rc) {
522                 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
523                                        dir, 0);
524                 if (inode) {
525                         //smsf_update_dentry(dentry, cache_dentry);
526                         d_instantiate(dentry, inode);
527                 }
528                 else
529                         rc = -ENOENT;
530         }
531
532         SMFS_POST_HOOK(dir, HOOK_MKDIR, &msg, rc); 
533         post_smfs_inode(dir, cache_dir);
534         smfs_trans_commit(dir, handle, 0);
535
536 exit:
537         post_smfs_dentry(cache_dentry);
538         post_smfs_dentry(cache_parent);
539         RETURN(rc);
540 }
541
542 static int smfs_rmdir(struct inode *dir, struct dentry *dentry)
543 {
544         struct inode *cache_dir = I2CI(dir);
545         struct inode *cache_inode = I2CI(dentry->d_inode);
546         struct inode *parent = I2CI(dentry->d_parent->d_inode);
547         struct dentry *cache_dentry = NULL;
548         struct dentry *cache_parent = NULL;
549         void *handle = NULL;
550         int    rc = 0;
551         struct hook_unlink_msg msg = {
552                 .dentry = dentry,
553                 .mode = S_IFDIR
554         };
555
556         ENTRY;
557         
558         LASSERT(cache_dir);
559         LASSERT(cache_dir->i_op->rmdir);
560         LASSERT(parent);
561
562         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
563         cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
564         if (!cache_parent || !cache_dentry) {
565                 rc = -ENOMEM;
566                 goto exit;
567         }
568
569         handle = smfs_trans_start(dir, FSFILT_OP_RMDIR, NULL);
570         if (IS_ERR(handle) ) {
571                 rc = -ENOSPC;
572                 goto exit;
573         }
574
575         pre_smfs_inode(dir, cache_dir);
576         pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
577         
578         SMFS_PRE_HOOK(dir, HOOK_RMDIR, &msg); 
579         
580         rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
581               
582         SMFS_POST_HOOK(dir, HOOK_RMDIR, &msg, rc); 
583         
584         post_smfs_inode(dir, cache_dir);
585         post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
586         //like vfs_rmdir is doing with inode
587         if (!rc)
588                 cache_dentry->d_inode->i_flags |= S_DEAD;
589         
590         smfs_trans_commit(dir, handle, 0);
591
592 exit:
593         post_smfs_dentry(cache_dentry);
594         post_smfs_dentry(cache_parent);
595         RETURN(rc);
596 }
597
598 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
599 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
600                       int mode, int rdev)
601 #else
602 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
603                       int mode, dev_t rdev)
604 #endif
605 {
606         struct inode *cache_dir = I2CI(dir);
607         struct inode *inode = NULL;
608         struct inode *parent = I2CI(dentry->d_parent->d_inode);
609         struct dentry *cache_dentry = NULL;
610         struct dentry *cache_parent = NULL;
611         void *handle = NULL;
612         int rc = 0;
613         struct hook_msg msg = {
614                 .dentry = dentry,
615         };
616  
617         ENTRY;
618         
619         LASSERT(parent);
620         LASSERT(cache_dir);
621         LASSERT(cache_dir->i_op->mknod);
622
623         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
624         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
625         if (!cache_parent || !cache_dentry) {
626                 rc = -ENOMEM;
627                 goto exit;
628         }
629
630         handle = smfs_trans_start(dir, FSFILT_OP_MKNOD, NULL);
631         if (IS_ERR(handle)) {
632                 rc = -ENOSPC;
633                 goto exit;
634         }
635         
636         pre_smfs_inode(dir, cache_dir);
637         
638         SMFS_PRE_HOOK(dir, HOOK_MKNOD, &msg); 
639         
640         rc = cache_dir->i_op->mknod(cache_dir, cache_dentry, mode, rdev);
641         if (!rc) {
642                 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
643                                        dir, 0);
644                 if (inode) {
645                         //smsf_update_dentry(dentry, cache_dentry);
646                         d_instantiate(dentry, inode);
647                 }
648                 else
649                         rc = -ENOENT;
650         }
651
652         SMFS_POST_HOOK(dir, HOOK_MKNOD, &msg, rc); 
653         
654         post_smfs_inode(dir, cache_dir);
655         
656         smfs_trans_commit(dir, handle, 0);
657
658 exit:
659         post_smfs_dentry(cache_dentry);
660         post_smfs_dentry(cache_parent);
661         RETURN(rc);
662 }
663
664 static int smfs_rename(struct inode *old_dir, struct dentry *old_dentry,
665                        struct inode *new_dir,struct dentry *new_dentry)
666 {
667         struct inode *cache_old_dir = I2CI(old_dir);
668         struct inode *cache_new_dir = I2CI(new_dir);
669         struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
670         struct inode *old_parent = I2CI(old_dentry->d_parent->d_inode);
671         struct inode *new_parent = I2CI(new_dentry->d_parent->d_inode);
672         struct inode *cache_new_inode = NULL;
673         struct dentry *cache_old_dentry = NULL;
674         struct dentry *cache_new_dentry = NULL;
675         struct dentry *cache_new_parent = NULL;
676         struct dentry *cache_old_parent = NULL;
677         void *handle = NULL;
678         int    rc = 0;
679         struct hook_rename_msg msg = {
680                 .dentry = old_dentry,
681                 .new_dir = new_dir,
682                 .new_dentry = new_dentry
683         };
684
685         ENTRY;
686                 
687         if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
688                 RETURN(-ENOENT);
689
690         if (new_dentry->d_inode) {
691                 cache_new_inode = I2CI(new_dentry->d_inode);
692                 if (!cache_new_inode)
693                         RETURN(-ENOENT);
694         }
695         
696         cache_old_parent = pre_smfs_dentry(NULL, old_parent, old_dentry->d_parent);
697         cache_old_dentry = pre_smfs_dentry(cache_old_parent, cache_old_inode,
698                                            old_dentry);
699         if (!cache_old_parent || !cache_old_dentry) {
700                 rc = -ENOMEM;
701                 goto exit;
702         }
703         
704         cache_new_parent = pre_smfs_dentry(NULL, new_parent, new_dentry->d_parent);
705         cache_new_dentry = pre_smfs_dentry(cache_new_parent, cache_new_inode,
706                                            new_dentry);
707         if (!cache_new_parent || !cache_new_dentry) {
708                 rc = -ENOMEM;
709                 goto exit;
710         }
711
712         handle = smfs_trans_start(old_dir, FSFILT_OP_RENAME, NULL);
713         if (IS_ERR(handle)) {
714                 rc = -ENOSPC;
715                 goto exit;
716         }
717         
718         pre_smfs_inode(old_dir, cache_old_dir);
719         pre_smfs_inode(new_dir, cache_new_dir);
720         if (new_dentry->d_inode)
721                 pre_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
722
723         SMFS_PRE_HOOK(old_dir, HOOK_RENAME, &msg); 
724         
725         rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
726                                          cache_new_dir, cache_new_dentry);
727         
728         SMFS_POST_HOOK(old_dir, HOOK_RENAME, &msg, rc); 
729
730         post_smfs_inode(old_dir, cache_old_dir);
731         post_smfs_inode(new_dir, cache_new_dir);
732         if (new_dentry->d_inode)
733                 post_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
734         
735         smfs_trans_commit(old_dir, handle, 0);
736         
737 exit:
738         post_smfs_dentry(cache_old_dentry);
739         post_smfs_dentry(cache_old_parent);
740         post_smfs_dentry(cache_new_dentry);
741         post_smfs_dentry(cache_new_parent);
742         RETURN(rc);
743 }
744
745 struct inode_operations smfs_dir_iops = {
746         create:         smfs_create,
747         lookup:         smfs_lookup,
748         link:           smfs_link,              /* BKL held */
749         unlink:         smfs_unlink,            /* BKL held */
750         symlink:        smfs_symlink,           /* BKL held */
751         mkdir:          smfs_mkdir,             /* BKL held */
752         rmdir:          smfs_rmdir,             /* BKL held */
753         mknod:          smfs_mknod,             /* BKL held */
754         rename:         smfs_rename,            /* BKL held */
755         setxattr:       smfs_setxattr,          /* BKL held */
756         getxattr:       smfs_getxattr,          /* BKL held */
757         listxattr:      smfs_listxattr,         /* BKL held */
758         removexattr:    smfs_removexattr,       /* BKL held */
759 };
760
761 struct inode_operations smfs_iopen_iops = {
762         lookup:         smfs_iopen_lookup,
763 };
764
765 static ssize_t smfs_read_dir(struct file *filp, char *buf,
766                              size_t size, loff_t *ppos)
767 {
768         struct dentry *dentry = filp->f_dentry;
769         struct inode *cache_inode = NULL;
770         struct smfs_file_info *sfi = NULL;
771         loff_t tmp_ppos;
772         loff_t *cache_ppos = NULL;
773         int    rc = 0;
774
775         ENTRY;
776         
777         cache_inode = I2CI(dentry->d_inode);
778
779         if (!cache_inode || !cache_inode->i_fop->read)
780                 RETURN(-EINVAL);
781
782         sfi = F2SMFI(filp);
783         if (sfi->magic != SMFS_FILE_MAGIC)
784                 BUG();
785
786         if (ppos != &(filp->f_pos))
787                 cache_ppos = &tmp_ppos;
788         else
789                 cache_ppos = &sfi->c_file->f_pos;
790         
791         *cache_ppos = *ppos;
792
793         rc = cache_inode->i_fop->read(sfi->c_file, buf, size, cache_ppos);
794         if (rc)
795                 RETURN(rc);
796
797         *ppos = *cache_ppos;
798         
799         duplicate_file(filp, sfi->c_file);
800         
801         RETURN(rc);
802 }
803
804 static int smfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
805 {
806         struct dentry *dentry = filp->f_dentry;
807         struct inode *cache_inode = NULL;
808         struct smfs_file_info *sfi = NULL;
809         int    rc = 0;
810         struct hook_readdir_msg msg = {
811                 .dentry = dentry,
812                 .filp = filp,
813                 .dirent = dirent,
814                 .filldir = filldir
815         };
816
817         ENTRY;
818         
819         cache_inode = I2CI(dentry->d_inode);
820         if (!cache_inode || !cache_inode->i_fop->readdir)
821                 RETURN(-EINVAL);
822
823         sfi = F2SMFI(filp);
824         if (sfi->magic != SMFS_FILE_MAGIC) BUG();
825
826         SMFS_PRE_HOOK(dentry->d_inode, HOOK_READDIR, &msg); 
827         
828         rc = cache_inode->i_fop->readdir(sfi->c_file, dirent, filldir);
829         
830         SMFS_POST_HOOK(dentry->d_inode, HOOK_READDIR, &msg, rc);
831         duplicate_file(filp, sfi->c_file);
832
833         RETURN(rc);
834 }
835
836 struct file_operations smfs_dir_fops = {
837         .read           = smfs_read_dir,
838         .readdir        = smfs_readdir,       /* BKL held */
839         .ioctl          = smfs_ioctl,         /* BKL held */
840         .fsync          = smfs_fsync,         /* BKL held */
841         .open           = smfs_open,
842         .release        = smfs_release,
843 };
844
845 struct file_operations smfs_iopen_fops = {
846         .read           = smfs_read_dir,
847 };
848