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