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