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