Whamcloud - gitweb
b26cca98081d257e584baf0d6a4497ea84cf436a
[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
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  *cache_dir = NULL;
54         struct dentry *cache_dentry = NULL;
55         struct dentry *cache_parent = NULL;
56         void *handle = NULL;
57         struct hook_msg msg = {
58                 .dir = dir,
59                 .dentry = dentry,
60         };
61         int rc = 0;
62         
63         ENTRY;
64
65         cache_dir = I2CI(dir);
66         LASSERT(cache_dir);
67         LASSERT(cache_dir->i_op->create);
68
69         //lock_kernel();
70         cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
71         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
72         if (!cache_dentry || !cache_parent) {
73                 rc = -ENOMEM;
74                 goto exit;
75         }
76        
77         handle = smfs_trans_start(dir, FSFILT_OP_CREATE, NULL);
78         if (IS_ERR(handle)) {
79                 rc = -ENOSPC;
80                 goto exit;
81         }
82         
83         SMFS_PRE_HOOK(dir->i_sb, HOOK_CREATE, &msg);
84
85         pre_smfs_inode(dir, cache_dir);
86
87 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
88         rc = cache_dir->i_op->create(cache_dir, cache_dentry, mode);
89 #else
90         rc = cache_dir->i_op->create(cache_dir, cache_dentry, mode, nd);
91 #endif
92         if (!rc) {        
93                 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino, dir, 0);
94                 if (inode)
95                         d_instantiate(dentry, inode);
96                 else
97                         rc = -ENOENT;
98         }
99         
100         SMFS_POST_HOOK(dir->i_sb, HOOK_CREATE, &msg, rc); 
101
102         post_smfs_inode(dir, cache_dir);
103         smfs_trans_commit(dir, handle, 0);
104
105 exit:
106         //unlock_kernel();
107         post_smfs_dentry(cache_dentry);
108         post_smfs_dentry(cache_parent);
109         RETURN(rc);
110 }
111
112 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
113 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry)
114 #else
115 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry,
116                                   struct nameidata *nd)
117 #endif
118 {
119         struct inode *cache_dir;
120         struct inode *inode = NULL;
121         struct inode * cache_inode = NULL;
122         struct dentry *cache_dentry = NULL;
123         struct dentry *cache_parent = NULL;
124         struct dentry *rdentry = NULL;
125         int rc = 0;
126         struct hook_msg msg = {
127                 .dir = dir,
128                 .dentry = dentry,
129         };
130
131         ENTRY;
132         
133         cache_dir = I2CI(dir);
134         if (!cache_dir)
135                 RETURN(ERR_PTR(-ENOENT));
136
137         LASSERT(cache_dir->i_op->lookup);
138
139         /* preparing artificial backing fs dentries. */
140         cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
141         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
142         if (!cache_dentry || !cache_parent) 
143                 RETURN(ERR_PTR(-ENOMEM));
144         
145         SMFS_PRE_HOOK(dir->i_sb, HOOK_LOOKUP, &msg); 
146
147         /* perform lookup in backing fs. */
148 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
149         rdentry = cache_dir->i_op->lookup(cache_dir, cache_dentry);
150 #else
151         rdentry = cache_dir->i_op->lookup(cache_dir, cache_dentry, nd);
152 #endif
153         if (rdentry) {
154                 if (IS_ERR(rdentry))
155                         rc = PTR_ERR(rdentry);
156                 else {
157                         cache_inode = rdentry->d_inode;
158                         dput(rdentry);
159                 }
160         } else {
161                 cache_inode = cache_dentry->d_inode;
162         }
163         
164         if (cache_inode) { 
165                 inode = smfs_get_inode(dir->i_sb, cache_inode->i_ino, dir,0);
166                 if (!inode)
167                         rc = -ENOENT;
168         }
169
170         if (!rc)
171                 d_add(dentry, inode);
172         
173         SMFS_POST_HOOK(dir->i_sb, HOOK_LOOKUP, &msg, rc);
174 exit:        
175         post_smfs_dentry(cache_dentry);
176         post_smfs_dentry(cache_parent);
177         RETURN(ERR_PTR(rc));
178 }
179
180 static int smfs_link(struct dentry *old_dentry,
181                      struct inode *dir, struct dentry *dentry)
182 {
183         struct inode *cache_old_inode = NULL;
184         struct inode *cache_dir = NULL;
185         struct inode *old_inode = NULL;
186         struct dentry *cache_dentry = NULL;
187         struct dentry *cache_old_dentry = NULL;
188         struct dentry *cache_parent = NULL;
189         void *handle = NULL;
190         int rc = 0;
191         struct hook_msg msg = {
192                 .dir = dir,
193                 .dentry = old_dentry,
194         };
195
196         ENTRY;
197
198         cache_dir = I2CI(dir);
199         if (!cache_dir)
200                 RETURN(-ENOENT);
201         
202         old_inode = old_dentry->d_inode;        
203         cache_old_inode = I2CI(old_inode);
204         if (!cache_old_inode)
205                 RETURN(-ENOENT);
206         
207         cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
208         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
209         cache_old_dentry = pre_smfs_dentry(NULL, cache_old_inode, old_dentry);
210         if (!cache_old_dentry || !cache_dentry || !cache_parent) {
211                 rc = -ENOMEM;
212                 goto exit;
213         }        
214         
215         handle = smfs_trans_start(dir, FSFILT_OP_LINK, NULL);
216         if (IS_ERR(handle)) {
217                  rc = -ENOSPC;
218                  goto exit;
219         }
220
221         pre_smfs_inode(dir, cache_dir);
222         pre_smfs_inode(old_inode, cache_old_inode);
223
224         //lock_kernel();
225         SMFS_PRE_HOOK(dir->i_sb, HOOK_LINK, &msg); 
226
227         rc = cache_dir->i_op->link(cache_old_dentry, cache_dir, cache_dentry);
228         if (!rc) {
229                 atomic_inc(&old_inode->i_count);
230                 d_instantiate(dentry, old_inode);
231         }
232
233         SMFS_POST_HOOK(dir->i_sb, HOOK_LINK, &msg, rc); 
234         
235         post_smfs_inode(old_inode, cache_old_inode);
236         post_smfs_inode(dir, cache_dir);
237
238         smfs_trans_commit(dir, handle, 0);
239         
240 exit:
241         //unlock_kernel();
242         post_smfs_dentry(cache_dentry);
243         post_smfs_dentry(cache_parent);
244         post_smfs_dentry(cache_old_dentry);
245         
246         RETURN(rc);
247 }
248
249 static int smfs_unlink(struct inode * dir, struct dentry *dentry)
250 {
251         struct inode *cache_dir = I2CI(dir);
252         struct inode *cache_inode = I2CI(dentry->d_inode);
253         struct dentry *cache_dentry;
254         struct dentry *cache_parent;
255         void   *handle = NULL;
256         int    rc = 0;
257         //int    mode = 0;
258         struct hook_unlink_msg msg = {
259                 .dir = dir,
260                 .dentry = dentry,
261                 .mode = 0
262         };
263
264         ENTRY;
265         
266         if (!cache_dir || !cache_inode || !cache_dir->i_op->unlink)
267                 RETURN(-ENOENT);
268
269         cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
270         cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
271         if (!cache_dentry || !cache_parent) {
272                 rc = -ENOMEM;
273                 goto exit;
274         }
275                 
276         //lock_kernel();
277         handle = smfs_trans_start(dir, FSFILT_OP_UNLINK, NULL);
278         if (IS_ERR(handle)) {
279                 rc = -ENOSPC;
280                 goto exit;
281         }
282
283         pre_smfs_inode(dir, cache_dir);
284         pre_smfs_inode(dentry->d_inode, cache_inode);
285
286         SMFS_PRE_HOOK(dir->i_sb, HOOK_UNLINK, &msg); 
287         
288         rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
289                 
290         SMFS_POST_HOOK(dir->i_sb, HOOK_UNLINK, &msg, rc); 
291
292         post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
293         post_smfs_inode(dir, cache_dir);
294         //unlock_kernel();
295         
296         smfs_trans_commit(dir, handle, 0);
297 exit:
298         post_smfs_dentry(cache_dentry);
299         post_smfs_dentry(cache_parent);
300         RETURN(rc);
301 }
302
303 static int smfs_symlink(struct inode *dir, struct dentry *dentry,
304                         const char *symname)
305 {
306         struct inode *cache_dir = I2CI(dir);
307         struct inode *inode = NULL;
308         struct dentry *cache_dentry;
309         struct dentry *cache_parent;
310         void   *handle = NULL;
311         int    rc = 0;
312         struct hook_symlink_msg msg = {
313                 .dir = dir,
314                 .dentry = dentry,
315                 .tgt_len = strlen(symname) + 1,
316                 .symname = (char*)symname
317         };
318
319         ENTRY;
320         
321         if (!cache_dir || !cache_dir->i_op->symlink)
322                 RETURN(-ENOENT);
323
324         cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
325         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
326         if (!cache_parent || !cache_dentry) {
327                 rc = -ENOMEM;
328                 goto exit;
329         }
330        
331         handle = smfs_trans_start(dir, FSFILT_OP_SYMLINK, NULL);
332         if (IS_ERR(handle)) {
333                 rc = -ENOSPC;
334                 goto exit;
335         }
336         
337         //lock_kernel();
338         pre_smfs_inode(dir, cache_dir);
339
340         SMFS_PRE_HOOK(dir->i_sb, HOOK_SYMLINK, &msg); 
341         
342         rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
343         if (!rc) {        
344                 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
345                                        dir, 0);
346                 if (inode)
347                         d_instantiate(dentry, inode);
348                 else
349                         rc = -ENOENT;
350         }
351         
352         SMFS_POST_HOOK(dir->i_sb, HOOK_SYMLINK, &msg, rc);
353         
354         post_smfs_inode(dir, cache_dir);
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         RETURN(rc);
362 }
363
364 static int smfs_mkdir(struct inode *dir, struct dentry *dentry,
365                       int mode)
366 {
367         struct inode *cache_dir = I2CI(dir);
368         struct inode *inode = NULL;
369         struct dentry *cache_dentry;
370         struct dentry *cache_parent;
371         void   *handle = NULL;
372         int    rc = 0;
373         struct hook_msg msg = {
374                 .dir = dir,
375                 .dentry = dentry,
376         };
377
378         ENTRY;
379         
380         if (!cache_dir)
381                 RETURN(-ENOENT);
382
383         cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
384         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
385
386         if (!cache_parent || !cache_dentry) {
387                 rc = -ENOMEM;
388                 goto exit;
389         }
390
391         handle = smfs_trans_start(dir, FSFILT_OP_MKDIR, NULL);
392         if (IS_ERR(handle)) {
393                 rc = -ENOSPC;
394                 goto exit;
395         }
396         
397         pre_smfs_inode(dir, cache_dir);
398         SMFS_PRE_HOOK(dir->i_sb, HOOK_MKDIR, &msg); 
399         
400         rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
401         if (!rc) {
402                 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
403                                        dir, 0);
404                 if (inode)
405                         d_instantiate(dentry, inode);
406                 else
407                         rc = -ENOENT;
408         }
409
410         SMFS_POST_HOOK(dir->i_sb, HOOK_MKDIR, &msg, rc); 
411         post_smfs_inode(dir, cache_dir);
412         smfs_trans_commit(dir, handle, 0);
413
414 exit:
415         post_smfs_dentry(cache_dentry);
416         post_smfs_dentry(cache_parent);
417         RETURN(rc);
418 }
419
420 static int smfs_rmdir(struct inode *dir, struct dentry *dentry)
421 {
422         struct inode *cache_dir = I2CI(dir);
423         struct inode *cache_inode = I2CI(dentry->d_inode);
424         struct dentry *cache_dentry = NULL;
425         struct dentry *cache_parent = NULL;
426         void *handle = NULL;
427         int    rc = 0;
428         struct hook_unlink_msg msg = {
429                 .dir = dir,
430                 .dentry = dentry,
431                 .mode = S_IFDIR
432         };
433
434         ENTRY;
435         
436         if (!cache_dir || !cache_dir->i_op->rmdir)
437                 RETURN(-ENOENT);
438
439         cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
440         cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
441         if (!cache_parent || !cache_dentry) {
442                 rc = -ENOMEM;
443                 goto exit;
444         }
445
446         handle = smfs_trans_start(dir, FSFILT_OP_RMDIR, NULL);
447         if (IS_ERR(handle) ) {
448                 rc = -ENOSPC;
449                 goto exit;
450         }
451
452         pre_smfs_inode(dir, cache_dir);
453         pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
454         
455         SMFS_PRE_HOOK(dir->i_sb, HOOK_RMDIR, &msg); 
456
457         rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
458               
459         SMFS_POST_HOOK(dir->i_sb, HOOK_RMDIR, &msg, rc); 
460         
461         post_smfs_inode(dir, cache_dir);
462         post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
463
464         smfs_trans_commit(dir, handle, 0);
465
466 exit:
467         post_smfs_dentry(cache_dentry);
468         post_smfs_dentry(cache_parent);
469         RETURN(rc);
470 }
471
472 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
473 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
474                       int mode, int rdev)
475 #else
476 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
477                       int mode, dev_t rdev)
478 #endif
479 {
480         struct inode *cache_dir = I2CI(dir);
481         struct inode *inode = NULL;
482         struct dentry *cache_dentry = NULL;
483         struct dentry *cache_parent = NULL;
484         void *handle = NULL;
485         int rc = 0;
486         struct hook_msg msg = {
487                 .dir = dir,
488                 .dentry = dentry,
489         };
490  
491         ENTRY;
492         
493         if (!cache_dir || !cache_dir->i_op->mknod)
494                 RETURN(-ENOENT);
495
496         cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
497         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
498         if (!cache_parent || !cache_dentry) {
499                 rc = -ENOMEM;
500                 goto exit;
501         }
502
503         handle = smfs_trans_start(dir, FSFILT_OP_MKNOD, NULL);
504         if (IS_ERR(handle)) {
505                 rc = -ENOSPC;
506                 goto exit;
507         }
508         
509         pre_smfs_inode(dir, cache_dir);
510         pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
511
512         SMFS_PRE_HOOK(dir->i_sb, HOOK_MKNOD, &msg); 
513         
514         rc = cache_dir->i_op->mknod(cache_dir, cache_dentry, mode, rdev);
515         if (!rc) {
516                 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
517                                        dir, 0);
518                 if (inode)
519                         d_instantiate(dentry, inode);
520                 else
521                         rc = -ENOENT;
522         }
523
524         SMFS_POST_HOOK(dir->i_sb, HOOK_MKNOD, &msg, rc); 
525         
526         post_smfs_inode(dir, cache_dir);
527         post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
528         smfs_trans_commit(dir, handle, 0);
529
530 exit:
531         post_smfs_dentry(cache_dentry);
532         post_smfs_dentry(cache_parent);
533         RETURN(rc);
534 }
535
536 static int smfs_rename(struct inode *old_dir, struct dentry *old_dentry,
537                        struct inode *new_dir,struct dentry *new_dentry)
538 {
539         struct inode *cache_old_dir = I2CI(old_dir);
540         struct inode *cache_new_dir = I2CI(new_dir);
541         struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
542         struct inode *cache_new_inode = NULL;
543         struct dentry *cache_old_dentry = NULL;
544         struct dentry *cache_new_dentry = NULL;
545         struct dentry *cache_new_parent = NULL;
546         struct dentry *cache_old_parent = NULL;
547         void *handle = NULL;
548         int    rc = 0;
549         struct hook_rename_msg msg = {
550                 .dir = old_dir,
551                 .dentry = old_dentry,
552                 .new_dir = new_dir,
553                 .new_dentry = new_dentry
554         };
555
556         ENTRY;
557                 
558         if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
559                 RETURN(-ENOENT);
560
561         if (new_dentry->d_inode) {
562                 cache_new_inode = I2CI(new_dentry->d_inode);
563                 if (!cache_new_inode)
564                         RETURN(-ENOENT);
565         }
566         
567         cache_old_parent = pre_smfs_dentry(NULL, cache_old_dir, old_dentry);
568         cache_old_dentry = pre_smfs_dentry(cache_old_parent, cache_old_inode,
569                                            old_dentry);
570         if (!cache_old_parent || !cache_old_dentry) {
571                 rc = -ENOMEM;
572                 goto exit;
573         }
574         
575         cache_new_parent = pre_smfs_dentry(NULL, cache_new_dir, new_dentry);
576         cache_new_dentry = pre_smfs_dentry(cache_new_parent, cache_new_inode,
577                                            new_dentry);
578         if (!cache_new_parent || !cache_new_dentry) {
579                 rc = -ENOMEM;
580                 goto exit;
581         }
582
583         handle = smfs_trans_start(old_dir, FSFILT_OP_RENAME, NULL);
584         if (IS_ERR(handle)) {
585                 rc = -ENOSPC;
586                 goto exit;
587         }
588         
589         pre_smfs_inode(old_dir, cache_old_dir);
590         pre_smfs_inode(new_dir, cache_new_dir);
591         if (new_dentry->d_inode)
592                 pre_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
593
594         SMFS_PRE_HOOK(old_dir->i_sb, HOOK_RENAME, &msg); 
595         
596         rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
597                                          cache_new_dir, cache_new_dentry);
598         
599         SMFS_POST_HOOK(old_dir->i_sb, HOOK_RENAME, &msg, rc); 
600
601         post_smfs_inode(old_dir, cache_old_dir);
602         post_smfs_inode(new_dir, cache_new_dir);
603         if (new_dentry->d_inode)
604                 post_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
605         
606         smfs_trans_commit(old_dir, handle, 0);
607         
608 exit:
609         post_smfs_dentry(cache_old_dentry);
610         post_smfs_dentry(cache_old_parent);
611         post_smfs_dentry(cache_new_dentry);
612         post_smfs_dentry(cache_new_parent);
613         RETURN(rc);
614 }
615
616 struct inode_operations smfs_dir_iops = {
617         create:         smfs_create,
618         lookup:         smfs_lookup,
619         link:           smfs_link,              /* BKL held */
620         unlink:         smfs_unlink,            /* BKL held */
621         symlink:        smfs_symlink,           /* BKL held */
622         mkdir:          smfs_mkdir,             /* BKL held */
623         rmdir:          smfs_rmdir,             /* BKL held */
624         mknod:          smfs_mknod,             /* BKL held */
625         rename:         smfs_rename,            /* BKL held */
626         setxattr:       smfs_setxattr,          /* BKL held */
627         getxattr:       smfs_getxattr,          /* BKL held */
628         listxattr:      smfs_listxattr,         /* BKL held */
629         removexattr:    smfs_removexattr,       /* BKL held */
630 };
631
632 static ssize_t smfs_read_dir(struct file *filp, char *buf,
633                              size_t size, loff_t *ppos)
634 {
635         struct dentry *dentry = filp->f_dentry;
636         struct inode *cache_inode = NULL;
637         struct smfs_file_info *sfi = NULL;
638         loff_t tmp_ppos;
639         loff_t *cache_ppos = NULL;
640         int    rc = 0;
641
642         ENTRY;
643         
644         cache_inode = I2CI(dentry->d_inode);
645
646         if (!cache_inode || !cache_inode->i_fop->read)
647                 RETURN(-EINVAL);
648
649         sfi = F2SMFI(filp);
650         if (sfi->magic != SMFS_FILE_MAGIC)
651                 BUG();
652
653         if (ppos != &(filp->f_pos))
654                 cache_ppos = &tmp_ppos;
655         else
656                 cache_ppos = &sfi->c_file->f_pos;
657         
658         *cache_ppos = *ppos;
659
660         rc = cache_inode->i_fop->read(sfi->c_file, buf, size, cache_ppos);
661         if (rc)
662                 RETURN(rc);
663
664         *ppos = *cache_ppos;
665         
666         duplicate_file(filp, sfi->c_file);
667         
668         RETURN(rc);
669 }
670
671 static int smfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
672 {
673         struct dentry *dentry = filp->f_dentry;
674         struct inode *cache_inode = NULL;
675         struct smfs_file_info *sfi = NULL;
676         int    rc = 0;
677         struct hook_readdir_msg msg = {
678                 .dir = dentry->d_inode,
679                 .dentry = dentry,
680                 .filp = filp,
681                 .dirent = dirent,
682                 .filldir = filldir
683         };
684
685         ENTRY;
686         
687         cache_inode = I2CI(dentry->d_inode);
688         if (!cache_inode || !cache_inode->i_fop->readdir)
689                 RETURN(-EINVAL);
690
691         sfi = F2SMFI(filp);
692         if (sfi->magic != SMFS_FILE_MAGIC) BUG();
693
694         SMFS_PRE_HOOK(dentry->d_inode->i_sb, HOOK_READDIR, &msg); 
695         
696         rc = cache_inode->i_fop->readdir(sfi->c_file, dirent, filldir);
697         
698         SMFS_POST_HOOK(dentry->d_inode->i_sb, HOOK_READDIR, &msg, rc);
699         duplicate_file(filp, sfi->c_file);
700
701 exit:
702         if (rc > 0)
703                 rc = 0;
704
705         RETURN(rc);
706 }
707
708 struct file_operations smfs_dir_fops = {
709         .read           = smfs_read_dir,
710         .readdir        = smfs_readdir,       /* BKL held */
711         .ioctl          = smfs_ioctl,         /* BKL held */
712         .fsync          = smfs_fsync,         /* BKL held */
713         .open           = smfs_open,
714         .release        = smfs_release,
715 };