Whamcloud - gitweb
af36a3b865d4ec5c4aa21d335256b460c84da015
[fs/lustre-release.git] / lustre / lvfs / fsfilt_smfs.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/lib/fsfilt_smfs.c
5  *  Lustre filesystem abstraction routines
6  *
7  *  Copyright (C) 2004 Cluster File Systems, Inc.
8  *   Author: Wang Di <wangdi@clusterfs.com>
9  *
10  *   This file is part of Lustre, http://www.lustre.org.
11  *
12  *   Lustre is free software; you can redistribute it and/or
13  *   modify it under the terms of version 2 of the GNU General Public
14  *   License as published by the Free Software Foundation.
15  *
16  *   Lustre is distributed in the hope that it will be useful,
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *   GNU General Public License for more details.
20  *
21  *   You should have received a copy of the GNU General Public License
22  *   along with Lustre; if not, write to the Free Software
23  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 #define DEBUG_SUBSYSTEM S_FILTER
27
28 #include <linux/fs.h>
29 #include <linux/jbd.h>
30 #include <linux/slab.h>
31 #include <linux/pagemap.h>
32 #include <linux/quotaops.h>
33 #include <linux/version.h>
34 #include <linux/kp30.h>
35 #include <linux/lustre_fsfilt.h>
36 #include <linux/lustre_smfs.h>
37 #include <linux/obd.h>
38 #include <linux/obd_class.h>
39 #include <linux/module.h>
40 #include <linux/init.h>
41
42 #include <linux/lustre_snap.h>
43 #include <linux/lustre_smfs.h>
44 static void *fsfilt_smfs_start(struct inode *inode, int op,
45                                void *desc_private, int logs)
46 {
47         void *handle;
48         struct inode *cache_inode = I2CI(inode);
49         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
50
51         if (cache_fsfilt == NULL)
52                 return NULL;
53
54         SMFS_TRANS_OP(inode, op);
55
56         if (!cache_fsfilt->fs_start)
57                 return ERR_PTR(-ENOSYS);
58
59         handle = cache_fsfilt->fs_start(cache_inode, op, desc_private, logs);
60         return handle;
61 }
62
63 static void *fsfilt_smfs_brw_start(int objcount, struct fsfilt_objinfo *fso,
64                                    int niocount, struct niobuf_local *nb,
65                                    void *desc_private, int logs)
66 {
67         struct fsfilt_operations *cache_fsfilt;
68         struct dentry *cache_dentry = NULL;
69         struct inode *cache_inode = NULL;
70         struct fsfilt_objinfo cache_fso;
71         void   *rc = NULL;
72
73         ENTRY;
74         cache_fsfilt = I2FOPS(fso->fso_dentry->d_inode);
75         if (cache_fsfilt == NULL)
76                 return NULL;
77
78         cache_inode = I2CI(fso->fso_dentry->d_inode);
79          cache_dentry = pre_smfs_dentry(NULL, cache_inode, fso->fso_dentry);
80
81         if (!cache_dentry)
82                 GOTO(exit, rc = ERR_PTR(-ENOMEM));
83
84         cache_fso.fso_dentry = cache_dentry;
85         cache_fso.fso_bufcnt = fso->fso_bufcnt;
86
87         if (!cache_fsfilt->fs_brw_start)
88                 return ERR_PTR(-ENOSYS);
89
90         rc = cache_fsfilt->fs_brw_start(objcount, &cache_fso, niocount, nb,
91                                         desc_private, logs);
92 exit:
93         post_smfs_dentry(cache_dentry);
94         return rc;
95 }
96
97 /* FIXME-WANGDI: here we can easily have inode == NULL due to
98    mds_open() behavior. It passes NULL inode to mds_finish_transno()
99    sometimes. Probably we should have spare way to get cache fsfilt
100    operations. */
101 static int fsfilt_smfs_commit(struct super_block *sb, struct inode *inode, 
102                               void *h, int force_sync)
103 {
104         struct fsfilt_operations *cache_fsfilt = S2SMI(sb)->sm_cache_fsfilt;
105         struct super_block *csb = S2CSB(sb); 
106         struct inode *cache_inode = NULL;
107         int    rc = -EIO;
108
109         if (inode)
110                 cache_inode = I2CI(inode);
111
112         if (cache_fsfilt == NULL)
113                 RETURN(rc);
114
115         if (!cache_fsfilt->fs_commit)
116                 RETURN(-ENOSYS);
117
118         rc = cache_fsfilt->fs_commit(csb, cache_inode, h, force_sync);
119
120         RETURN(rc);
121 }
122
123 static int fsfilt_smfs_commit_async(struct inode *inode, void *h,
124                                     void **wait_handle)
125 {
126         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
127         struct inode *cache_inode = NULL;
128         int    rc = -EIO;
129
130         cache_inode = I2CI(inode);
131         if (cache_fsfilt == NULL)
132                 RETURN(-EINVAL);
133
134         if (!cache_fsfilt->fs_commit_async)
135                 RETURN(-ENOSYS);
136
137         rc = cache_fsfilt->fs_commit_async(cache_inode, h, wait_handle);
138
139         RETURN(rc);
140 }
141
142 static int fsfilt_smfs_commit_wait(struct inode *inode, void *h)
143 {
144         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
145         struct inode *cache_inode = NULL;
146         int    rc = -EIO;
147
148         cache_inode = I2CI(inode);
149         if (cache_fsfilt == NULL)
150                 RETURN(-EINVAL);
151
152         if (!cache_fsfilt->fs_commit_wait)
153                 RETURN(-ENOSYS);
154
155         rc = cache_fsfilt->fs_commit_wait(cache_inode, h);
156
157         RETURN(rc);
158 }
159
160 static int fsfilt_smfs_setattr(struct dentry *dentry, void *handle,
161                                struct iattr *iattr, int do_trunc)
162 {
163         struct fsfilt_operations *cache_fsfilt = I2FOPS(dentry->d_inode);
164         struct dentry *cache_dentry = NULL;
165         struct inode *cache_inode = NULL;
166         int    rc = -EIO;
167
168         if (!cache_fsfilt)
169                 RETURN(rc);
170
171         cache_inode = I2CI(dentry->d_inode);
172
173         cache_dentry = pre_smfs_dentry(NULL, cache_inode, dentry);
174         if (!cache_dentry)
175                 GOTO(exit, rc = -ENOMEM);
176
177         pre_smfs_inode(dentry->d_inode, cache_inode);
178
179         if (!cache_fsfilt->fs_setattr)
180                 RETURN(-ENOSYS);
181
182         rc = cache_fsfilt->fs_setattr(cache_dentry, handle, iattr, do_trunc);
183
184         post_smfs_inode(dentry->d_inode, cache_inode);
185
186         if (rc == 0) {
187                 struct super_block *sb = dentry->d_inode->i_sb;
188
189                 if (SMFS_DO_REC(S2SMI(sb)) && 
190                     SMFS_DO_INODE_REC(dentry->d_inode)) {
191                         rc = smfs_rec_setattr(dentry->d_inode, dentry, iattr);
192                 }
193         }
194 exit:
195         post_smfs_dentry(cache_dentry);
196         RETURN(rc);
197 }
198
199 static int fsfilt_smfs_iocontrol(struct inode *inode, struct file *file,
200                                  unsigned int cmd, unsigned long arg)
201 {
202         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
203         struct inode *cache_inode = NULL;
204         struct smfs_file_info *sfi = NULL;
205         int    rc = -EIO;
206         ENTRY;
207
208         if (!cache_fsfilt)
209                 RETURN(rc);
210
211         cache_inode = I2CI(inode);
212
213         if (!cache_inode)
214                 RETURN(rc);
215
216         if (file != NULL) {
217                 sfi = F2SMFI(file);
218
219                 if (sfi->magic != SMFS_FILE_MAGIC)
220                         BUG();
221         } else {
222                 sfi = NULL;
223         }
224
225         if (!cache_fsfilt->fs_iocontrol)
226                 RETURN(-ENOSYS);
227
228         if (sfi) {
229                 rc = cache_fsfilt->fs_iocontrol(cache_inode, sfi->c_file, cmd,
230                                                 arg);
231         } else {
232                 rc = cache_fsfilt->fs_iocontrol(cache_inode, NULL, cmd, arg);
233         }
234
235         /* FIXME-UMKA: Should this be in duplicate_inode()? */
236         if (rc == 0 && cmd == EXT3_IOC_SETFLAGS)
237                 inode->i_flags = cache_inode->i_flags;
238
239         post_smfs_inode(inode, cache_inode);
240
241         RETURN(rc);
242 }
243
244 static int fsfilt_smfs_set_md(struct inode *inode, void *handle,
245                               void *lmm, int lmm_size)
246 {
247         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
248         struct inode *cache_inode = NULL;
249         int    rc = -EIO;
250
251         if (!cache_fsfilt)
252                 RETURN(-EINVAL);
253
254         cache_inode = I2CI(inode);
255
256         if (!cache_inode)
257                 RETURN(-ENOENT);
258
259         pre_smfs_inode(inode, cache_inode);
260
261         if (!cache_fsfilt->fs_set_md)
262                 RETURN(-ENOSYS);
263
264         down(&cache_inode->i_sem);
265         rc = cache_fsfilt->fs_set_md(cache_inode, handle, lmm, lmm_size);
266         up(&cache_inode->i_sem);
267
268         post_smfs_inode(inode, cache_inode);
269
270         smfs_rec_md(inode, lmm, lmm_size);
271
272         RETURN(rc);
273 }
274
275 /* Must be called with i_sem held */
276 static int fsfilt_smfs_get_md(struct inode *inode, void *lmm, int lmm_size)
277 {
278         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
279         struct inode *cache_inode = NULL;
280         int    rc = -EIO;
281
282         if (!cache_fsfilt)
283                 RETURN(-EINVAL);
284
285         cache_inode = I2CI(inode);
286
287         if (!cache_inode)
288                 RETURN(-ENOENT);
289
290         pre_smfs_inode(inode, cache_inode);
291
292         if (!cache_fsfilt->fs_get_md)
293                 RETURN(-ENOSYS);
294
295         down(&cache_inode->i_sem);
296         rc = cache_fsfilt->fs_get_md(cache_inode, lmm, lmm_size);
297         up(&cache_inode->i_sem);
298
299         post_smfs_inode(inode, cache_inode);
300
301         RETURN(rc);
302 }
303
304 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
305 static int fsfilt_smfs_send_bio(struct inode *inode, struct bio *bio)
306 #else
307 static int fsfilt_smfs_send_bio(struct inode *inode, struct kiobuf *bio)
308 #endif
309 {
310         struct inode *cache_inode;
311         struct fsfilt_operations *cache_fsfilt;
312
313         cache_fsfilt = I2FOPS(inode);
314         if (!cache_fsfilt)
315                 RETURN(-EINVAL);
316
317         cache_inode = I2CI(inode);
318         if (!cache_inode)
319                 RETURN(-EINVAL);
320
321         if (!cache_fsfilt->fs_send_bio)
322                 RETURN(-ENOSYS);
323
324         return cache_fsfilt->fs_send_bio(cache_inode, bio);
325 }
326
327 static struct page *
328 fsfilt_smfs_getpage(struct inode *inode, long int index)
329 {
330         struct  fsfilt_operations *cache_fsfilt;
331         struct  inode *cache_inode;
332
333         cache_fsfilt = I2FOPS(inode);
334         if (!cache_fsfilt)
335                 RETURN(ERR_PTR(-EINVAL));
336
337         cache_inode = I2CI(inode);
338         if (!cache_inode)
339                 RETURN(ERR_PTR(-EINVAL));
340
341         if (!cache_fsfilt->fs_getpage)
342                 RETURN(ERR_PTR(-ENOSYS));
343 #if CONFIG_SNAPFS
344         if (SMFS_DO_COW(S2SMI(inode->i_sb))) {
345                 struct address_space_operations *aops = 
346                                 cache_inode->i_mapping->a_ops;
347                 if (aops->bmap(cache_inode->i_mapping, index)) {
348                         struct inode *ind_inode = NULL;
349                         struct inode *cache_ind = NULL;
350                         struct page  *page = NULL;
351                         
352                         ind_inode = smfs_cow_get_ind(inode, index);
353                         if (!ind_inode) {
354                                 RETURN(ERR_PTR(-EIO));
355                         }
356                         cache_ind = I2CI(ind_inode);
357                         /*FIXME cow inode should be bottom fs inode */         
358                         page = cache_fsfilt->fs_getpage(cache_ind, index);
359                         iput(ind_inode); 
360                         RETURN(page);
361                 } 
362         }
363 #endif
364         return cache_fsfilt->fs_getpage(cache_inode, index);
365 }
366
367 static ssize_t fsfilt_smfs_readpage(struct file *file, char *buf,
368                                     size_t count, loff_t *off)
369 {
370         struct fsfilt_operations *cache_fsfilt;
371         struct smfs_file_info *sfi;
372         struct inode *cache_inode;
373         loff_t tmp_ppos;
374         loff_t *cache_ppos;
375         ssize_t rc = -EIO;
376
377         ENTRY;
378
379         cache_fsfilt = I2FOPS(file->f_dentry->d_inode);
380         if (!cache_fsfilt)
381                 RETURN(rc);
382
383         cache_inode = I2CI(file->f_dentry->d_inode);
384         if (!cache_inode)
385                 RETURN(rc);
386
387         sfi = F2SMFI(file);
388         if (sfi->magic != SMFS_FILE_MAGIC)
389                 BUG();
390
391         if (off != &(file->f_pos))
392                 cache_ppos = &tmp_ppos;
393         else
394                 cache_ppos = &sfi->c_file->f_pos;
395         *cache_ppos = *off;
396
397         pre_smfs_inode(file->f_dentry->d_inode, cache_inode);
398
399         if (cache_fsfilt->fs_readpage)
400                 rc = cache_fsfilt->fs_readpage(sfi->c_file, buf, count,
401                                                cache_ppos);
402
403         *off = *cache_ppos;
404         post_smfs_inode(file->f_dentry->d_inode, cache_inode);
405         duplicate_file(file, sfi->c_file);
406
407         RETURN(rc);
408 }
409
410
411 static int fsfilt_smfs_add_journal_cb(struct obd_device *obd,
412                                       struct super_block *sb, __u64 last_rcvd,
413                                       void *handle, fsfilt_cb_t cb_func,
414                                       void *cb_data)
415 {
416         struct fsfilt_operations *cache_fsfilt = S2SMI(sb)->sm_cache_fsfilt;
417         struct super_block *csb = S2CSB(sb);
418         int rc = -EIO;
419
420         if (!cache_fsfilt)
421                  RETURN(rc);
422         if (cache_fsfilt->fs_add_journal_cb)
423                 rc = cache_fsfilt->fs_add_journal_cb(obd, csb, last_rcvd,
424                                                      handle, cb_func, cb_data);
425         RETURN(rc);
426 }
427
428 static int fsfilt_smfs_statfs(struct super_block *sb, struct obd_statfs *osfs)
429 {
430         struct fsfilt_operations *cache_fsfilt = S2SMI(sb)->sm_cache_fsfilt;
431         struct super_block *csb = S2CSB(sb);
432         int rc = -EIO;
433
434         if (!cache_fsfilt)
435                 RETURN(rc);
436
437         if (!cache_fsfilt->fs_statfs)
438                 RETURN(-ENOSYS);
439
440         rc = cache_fsfilt->fs_statfs(csb, osfs);
441         duplicate_sb(csb, sb);
442
443         RETURN(rc);
444 }
445
446 static int fsfilt_smfs_sync(struct super_block *sb)
447 {
448         struct fsfilt_operations *cache_fsfilt = S2SMI(sb)->sm_cache_fsfilt;
449         struct super_block *csb = S2CSB(sb);
450         int    rc = -EIO;
451
452         if (!cache_fsfilt)
453                 RETURN(-EINVAL);
454
455         if (!cache_fsfilt->fs_sync)
456                 RETURN(-ENOSYS);
457
458         rc = cache_fsfilt->fs_sync(csb);
459
460         RETURN(rc);
461 }
462
463 int fsfilt_smfs_map_inode_pages(struct inode *inode, struct page **page,
464                                 int pages, unsigned long *blocks, int *created,
465                                 int create, struct semaphore *sem)
466 {
467         struct  fsfilt_operations *cache_fsfilt = I2FOPS(inode);
468         struct  inode *cache_inode = NULL;
469         int     rc = -EIO;
470
471         if (!cache_fsfilt)
472                 RETURN(-EINVAL);
473
474         cache_inode = I2CI(inode);
475
476         if (!cache_inode)
477                 RETURN(rc);
478
479         if (!cache_fsfilt->fs_map_inode_pages)
480                 RETURN(-ENOSYS);
481
482         down(&cache_inode->i_sem);
483         rc = cache_fsfilt->fs_map_inode_pages(cache_inode, page, pages, blocks,
484                                               created, create, sem);
485         up(&cache_inode->i_sem);
486
487         RETURN(rc);
488 }
489
490 static int fsfilt_smfs_prep_san_write(struct inode *inode, long *blocks,
491                                       int nblocks, loff_t newsize)
492 {
493         struct  fsfilt_operations *cache_fsfilt = I2FOPS(inode);
494         struct  inode *cache_inode = NULL;
495         int     rc = -EIO;
496
497         if (!cache_fsfilt)
498                 RETURN(-EINVAL);
499
500         cache_inode = I2CI(inode);
501
502         if (!cache_inode)
503                 RETURN(-EINVAL);
504
505         if (!cache_fsfilt->fs_prep_san_write)
506                 RETURN(-ENOSYS);
507
508         down(&cache_inode->i_sem);
509         rc = cache_fsfilt->fs_prep_san_write(cache_inode, blocks, nblocks,
510                                              newsize);
511         up(&cache_inode->i_sem);
512
513         RETURN(rc);
514 }
515
516 static int fsfilt_smfs_read_record(struct file * file, void *buf,
517                                    int size, loff_t *offs)
518 {
519         struct  fsfilt_operations *cache_fsfilt;
520         struct  inode *cache_inode;
521         struct  smfs_file_info *sfi;
522         loff_t  tmp_ppos;
523         loff_t  *cache_ppos;
524         ssize_t rc;
525
526         ENTRY;
527         cache_fsfilt = I2FOPS(file->f_dentry->d_inode);
528         if (!cache_fsfilt)
529                 RETURN(-EINVAL);
530
531         cache_inode = I2CI(file->f_dentry->d_inode);
532
533         if (!cache_inode)
534                 RETURN(-EINVAL);
535
536         sfi = F2SMFI(file);
537         if (sfi->magic != SMFS_FILE_MAGIC) BUG();
538
539         if (offs != &(file->f_pos))
540                 cache_ppos = &tmp_ppos;
541         else
542                 cache_ppos = &sfi->c_file->f_pos;
543         *cache_ppos = *offs;
544
545         pre_smfs_inode(file->f_dentry->d_inode, cache_inode);
546
547         if (!cache_fsfilt->fs_read_record)
548                 RETURN(-ENOSYS);
549
550         rc = cache_fsfilt->fs_read_record(sfi->c_file, buf, size, cache_ppos);
551
552         *offs = *cache_ppos;
553         post_smfs_inode(file->f_dentry->d_inode, cache_inode);
554         duplicate_file(file, sfi->c_file);
555
556         RETURN(rc);
557 }
558
559 static int fsfilt_smfs_write_record(struct file *file, void *buf, int bufsize,
560                                     loff_t *offs, int force_sync)
561 {
562         struct  fsfilt_operations *cache_fsfilt;
563         struct  inode *cache_inode;
564         struct  smfs_file_info *sfi;
565         loff_t  tmp_ppos;
566         loff_t  *cache_ppos;
567         ssize_t rc = -EIO;
568
569         ENTRY;
570
571         cache_fsfilt = I2FOPS(file->f_dentry->d_inode);
572         if (!cache_fsfilt)
573                 RETURN(-EINVAL);
574
575         cache_inode = I2CI(file->f_dentry->d_inode);
576
577         if (!cache_inode)
578                 RETURN(-EINVAL);
579
580         sfi = F2SMFI(file);
581         if (sfi->magic != SMFS_FILE_MAGIC)
582                 BUG();
583
584         if (offs != &(file->f_pos))
585                 cache_ppos = &tmp_ppos;
586         else
587                 cache_ppos = &sfi->c_file->f_pos;
588         *cache_ppos = *offs;
589
590         pre_smfs_inode(file->f_dentry->d_inode, cache_inode);
591
592         if (!cache_fsfilt->fs_write_record)
593                 RETURN(-ENOSYS);
594
595         rc = cache_fsfilt->fs_write_record(sfi->c_file, buf,
596                                            bufsize, cache_ppos, force_sync);
597         *offs = *cache_ppos;
598         post_smfs_inode(file->f_dentry->d_inode, cache_inode);
599         duplicate_file(file, sfi->c_file);
600
601         RETURN(rc);
602 }
603
604 static int fsfilt_smfs_post_setup(struct obd_device *obd, struct vfsmount *mnt)
605 {
606         struct super_block *sb = NULL;
607         int rc = 0;
608
609         if (mnt) {
610                 sb = mnt->mnt_sb;
611                 S2SMI(sb)->smsi_exp = obd->obd_self_export;
612                 smfs_post_setup(sb, mnt);
613                 if (SMFS_DO_REC(S2SMI(sb)))
614                         rc = smfs_start_rec(sb, mnt);
615 #if CONFIG_SNAPFS
616                 if (SMFS_DO_COW(S2SMI(sb)))
617                         rc = smfs_start_cow(sb);
618 #endif
619                 if (rc)
620                         GOTO(exit, rc);
621                 if (obd)
622                         obd->obd_llog_ctxt[LLOG_REINT_ORIG_CTXT] =
623                                         S2SMI(sb)->smsi_rec_log;
624         }
625 exit:
626         if (rc)
627                 CERROR("can not do post setup in obd %p rc=%d", obd, rc);
628
629         RETURN(rc);
630 }
631
632 static int fsfilt_smfs_post_cleanup(struct obd_device *obd,
633                                     struct vfsmount *mnt)
634 {
635         struct super_block *sb = NULL;
636         int rc = 0;
637         ENTRY;
638         
639         if (mnt) {
640                 sb = mnt->mnt_sb;
641                 if (SMFS_DO_REC(S2SMI(sb)))
642                         rc = smfs_stop_rec(sb);
643 #if CONFIG_SNAPFS
644                 if (SMFS_DO_COW(S2SMI(sb)))
645                         rc = smfs_stop_cow(sb);
646 #endif
647                 smfs_post_cleanup(sb);
648         }
649         RETURN(rc);
650 }
651
652 static int fsfilt_smfs_set_fs_flags(struct inode *inode, int flags)
653 {
654         int rc = 0;
655         ENTRY;
656
657         if (SMFS_DO_REC(S2SMI(inode->i_sb)) && (flags & SM_DO_REC))
658                 SMFS_SET_INODE_REC(inode);
659         if (SMFS_DO_COW(S2SMI(inode->i_sb)) && (flags & SM_DO_COW))
660                 SMFS_SET_INODE_COW(inode);
661         RETURN(rc);
662 }
663
664 static int fsfilt_smfs_clear_fs_flags(struct inode *inode, int flags)
665 {
666         int rc = 0;
667         ENTRY;
668         
669         if (SMFS_DO_REC(S2SMI(inode->i_sb)) && (flags & SM_DO_REC))
670                 SMFS_CLEAN_INODE_REC(inode);
671         if (SMFS_DO_COW(S2SMI(inode->i_sb)) && (flags & SM_DO_COW))
672                 SMFS_CLEAN_INODE_COW(inode);
673         RETURN(rc);
674 }
675
676 static int fsfilt_smfs_get_fs_flags(struct dentry *de)
677 {
678         struct inode *inode = de->d_inode;
679         int flags = 0;
680         ENTRY;
681
682         LASSERT(inode);
683
684         if (SMFS_DO_REC(S2SMI(inode->i_sb)) && SMFS_DO_INODE_REC(inode))
685                 flags |= SM_DO_REC;
686         if (SMFS_DO_COW(S2SMI(inode->i_sb)) && SMFS_DO_INODE_COW(inode))
687                 flags |= SM_DO_COW;
688        
689         RETURN(flags); 
690 }
691 static int fsfilt_smfs_set_ost_flags(struct super_block *sb)
692 {
693         int rc = 0;
694         SET_REC_PACK_TYPE_INDEX(S2SMI(sb)->smsi_flags, PACK_OST);
695         RETURN(rc);
696 }
697
698 static int fsfilt_smfs_set_mds_flags(struct super_block *sb)
699 {
700         int rc = 0;
701         SET_REC_PACK_TYPE_INDEX(S2SMI(sb)->smsi_flags, PACK_MDS);
702         RETURN(rc);
703 }
704
705 static int fsfilt_smfs_get_reint_log_ctxt(struct super_block *sb,
706                                           struct llog_ctxt **ctxt)
707 {
708         struct smfs_super_info *smfs_info = S2SMI(sb);
709         int rc = 0;
710
711         *ctxt = smfs_info->smsi_rec_log;
712         RETURN(rc);
713 }
714
715 static int fsfilt_smfs_setup(struct obd_device *obd, struct super_block *sb)
716 {
717         struct smfs_super_info *smfs_info = S2SMI(sb);
718         struct fsfilt_operations *cache_fsfilt;
719         struct super_block *csb;
720         int rc = 0;
721
722         /* It should be initialized olready by smfs_read_super(). */
723         if (!(cache_fsfilt = smfs_info->sm_cache_fsfilt))
724                     cache_fsfilt = fsfilt_get_ops(smfs_info->smsi_cache_ftype);
725
726         if (!cache_fsfilt)
727                 RETURN(-ENOENT);
728
729         csb = S2CSB(sb);
730         if (cache_fsfilt->fs_setup) 
731                 rc = cache_fsfilt->fs_setup(obd, csb);
732         
733         duplicate_sb(sb, csb);
734         
735         RETURN(rc);
736 }
737
738 static int fsfilt_smfs_set_xattr(struct inode *inode, void *handle, char *name,
739                                  void *buffer, int buffer_size)
740 {
741         struct  fsfilt_operations *cache_fsfilt = I2FOPS(inode);
742         struct  inode *cache_inode = NULL;
743         int     rc = -EIO;
744
745         if (!cache_fsfilt)
746                 RETURN(rc);
747
748         cache_inode = I2CI(inode);
749         if (!cache_inode)
750                 RETURN(rc);
751
752         pre_smfs_inode(inode, cache_inode);
753
754         if (cache_fsfilt->fs_set_xattr)
755                 rc = cache_fsfilt->fs_set_xattr(cache_inode, handle, name,
756                                                 buffer, buffer_size);
757         post_smfs_inode(inode, cache_inode);
758
759         RETURN(rc);
760 }
761
762 static int fsfilt_smfs_get_xattr(struct inode *inode, char *name,
763                                  void *buffer, int buffer_size)
764 {
765         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
766         struct inode *cache_inode = NULL;
767         int    rc = -EIO;
768
769         if (!cache_fsfilt)
770                 RETURN(rc);
771
772         cache_inode = I2CI(inode);
773         if (!cache_inode)
774                 RETURN(rc);
775
776         pre_smfs_inode(inode, cache_inode);
777
778         if (cache_fsfilt->fs_get_xattr)
779                 rc = cache_fsfilt->fs_get_xattr(cache_inode, name,
780                                                 buffer, buffer_size);
781         post_smfs_inode(inode, cache_inode);
782
783         RETURN(rc);
784 }
785
786 static int fsfilt_smfs_insert_extents_ea(struct inode *inode,
787                                          unsigned long from, unsigned long num)
788 {
789         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
790         struct inode *cache_inode = NULL;
791         int    rc = -EIO;
792
793         if (!cache_fsfilt)
794                 RETURN(rc);
795
796         cache_inode = I2CI(inode);
797         if (!cache_inode)
798                 RETURN(rc);
799
800         pre_smfs_inode(inode, cache_inode);
801
802         if (cache_fsfilt->fs_insert_extents_ea)
803                 rc = cache_fsfilt->fs_insert_extents_ea(cache_inode, from, num);
804
805         post_smfs_inode(inode, cache_inode);
806         return rc;
807 }
808
809 static int fsfilt_smfs_remove_extents_ea(struct inode *inode,
810                                          unsigned long from, unsigned long num)
811 {
812         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
813         struct inode *cache_inode = NULL;
814         int    rc = -EIO;
815
816         if (!cache_fsfilt)
817                 RETURN(rc);
818
819         cache_inode = I2CI(inode);
820         if (!cache_inode)
821                 RETURN(rc);
822
823         pre_smfs_inode(inode, cache_inode);
824
825         if (cache_fsfilt->fs_remove_extents_ea)
826                 rc = cache_fsfilt->fs_remove_extents_ea(cache_inode, from, num);
827
828         post_smfs_inode(inode, cache_inode);
829         return rc;
830 }
831
832 static int fsfilt_smfs_init_extents_ea(struct inode *inode)
833 {
834         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
835         struct inode *cache_inode = NULL;
836         int    rc = -EIO;
837
838         if (!cache_fsfilt)
839                 RETURN(rc);
840
841         cache_inode = I2CI(inode);
842         if (!cache_inode)
843                 RETURN(rc);
844
845         pre_smfs_inode(inode, cache_inode);
846
847         if (cache_fsfilt->fs_init_extents_ea)
848                 rc = cache_fsfilt->fs_init_extents_ea(cache_inode);
849
850         post_smfs_inode(inode, cache_inode);
851         return rc;
852 }
853
854 static int fsfilt_smfs_free_extents(struct super_block *sb, ino_t ino,
855                                     char *pbuf, int size)
856 {
857         OBD_FREE(pbuf, size * (sizeof(struct ldlm_extent)));
858         return 0;
859 }
860
861 static int fsfilt_smfs_write_extents(struct dentry *dentry,
862                                      unsigned long from, unsigned long num)
863 {
864         int rc = 0;
865
866         if (SMFS_DO_REC(S2SMI(dentry->d_inode->i_sb)))
867                 rc = smfs_write_extents(dentry->d_inode, dentry, from, num);
868
869         return rc;
870 }
871
872 static int fsfilt_smfs_precreate_rec(struct dentry *dentry, int *count, 
873                                      struct obdo *oa)
874 {
875         int rc = 0;
876
877         if (SMFS_DO_REC(S2SMI(dentry->d_inode->i_sb)))
878                 rc = smfs_rec_precreate(dentry, count, oa);
879
880         return rc;
881 }
882
883 static int fsfilt_smfs_get_ino_write_extents(struct super_block *sb, ino_t ino,
884                                              char **pbuf, int *size)
885 {
886         struct fs_extent *fs_extents;
887         struct ldlm_extent *extents = NULL;
888         struct inode *inode;
889         struct inode *cache_inode;
890         struct fsfilt_operations *cache_fsfilt = NULL;
891         struct lvfs_run_ctxt saved;
892         int    rc = 0, fs_ex_size, ex_num, flags;
893         char   *buf = NULL, *ex_buf = NULL;
894
895         push_ctxt(&saved, S2SMI(sb)->smsi_ctxt, NULL);
896
897         inode = iget(sb, ino);
898
899         if (!inode || is_bad_inode(inode)) {
900                 CWARN("Can not get inode %lu ino\n", ino);
901                 GOTO(out, rc = 0);
902         }
903         cache_inode = I2CI(inode);
904         cache_fsfilt = I2FOPS(inode);
905
906         rc = cache_fsfilt->fs_get_xattr(cache_inode, REINT_EXTENTS_FLAGS,
907                                         &flags, sizeof(int));
908         if (!(flags & SMFS_OVER_WRITE) && !(flags & SMFS_DIRTY_WRITE)) {
909                 GOTO(out, rc = 0);
910         } else if (flags & SMFS_OVER_WRITE) {
911                 *size = 1;
912                 OBD_ALLOC(ex_buf, sizeof(struct ldlm_extent));
913                 if (!ex_buf)
914                         GOTO(out, rc=-ENOMEM);
915                 extents = (struct ldlm_extent*)(ex_buf);
916                 extents->start = 0;
917                 extents->end = 0xffffffff;
918         }
919         if (rc < 0)
920                 GOTO(out, rc);
921         rc = cache_fsfilt->fs_get_write_extents_num(cache_inode, &fs_ex_size);
922         if (rc)
923                 GOTO(out, rc);
924         OBD_ALLOC(buf, fs_ex_size);
925         if (!buf)
926                 GOTO(out, rc=-ENOMEM);
927
928         rc = cache_fsfilt->fs_get_inode_write_extents(cache_inode, &buf,
929                                                       &fs_ex_size);
930         if (rc < 0)
931                 GOTO(out, rc);
932         rc = 0;
933         ex_num = fs_ex_size / sizeof(struct fs_extent);
934         *size =  ex_num;
935         OBD_ALLOC(ex_buf, ex_num* sizeof(struct ldlm_extent));
936         if (!ex_buf)
937                 GOTO(out, rc=-ENOMEM);
938
939         fs_extents = (struct fs_extent*)(buf);
940         extents = (struct ldlm_extent*)(ex_buf);
941         while (ex_num > 0) {
942                 int blk_size = I2CI(inode)->i_blksize;
943
944                 extents->start = fs_extents->e_block * blk_size;
945                 extents->end = extents->start + fs_extents->e_num * blk_size;
946                 fs_extents++;
947                 extents++;
948                 ex_num--;
949         }
950         *pbuf = ex_buf;
951 out:
952         iput(inode);
953         if (buf)
954                 OBD_FREE(buf, fs_ex_size);
955         if (rc && extents)
956                 OBD_FREE(ex_buf, (*size) * (sizeof(struct ldlm_extent)));
957         pop_ctxt(&saved, S2SMI(sb)->smsi_ctxt, NULL);
958         return rc;
959 }
960
961 static int fsfilt_smfs_set_snap_item(struct super_block *sb, char *name)
962 {
963         int rc = 0;
964
965         ENTRY;
966 #if CONFIG_SNAPFS
967         rc = smfs_add_snap_item(sb, name);
968 #endif
969         RETURN(rc);        
970 }
971 static int fsfilt_smfs_do_write_cow(struct dentry *de, void *extents,
972                                     int num_extents)
973 {
974         int rc = 0;
975 #if CONFIG_SNAPFS
976         struct write_extents *w_ext = (struct write_extents *)extents;
977         int i = 0;
978         ENTRY;
979         for (i = 0; i < num_extents; i++) {
980                size_t count = w_ext->w_count;
981                loff_t off = w_ext->w_pos;
982                rc = smfs_cow_write(de->d_inode, de, &count, &off);
983                if (rc)
984                         RETURN(rc);  
985                w_ext ++;
986         }
987 #endif
988         RETURN(rc);
989 }
990 static struct fsfilt_operations fsfilt_smfs_ops = {
991         .fs_type                = "smfs",
992         .fs_owner               = THIS_MODULE,
993         .fs_start               = fsfilt_smfs_start,
994         .fs_brw_start           = fsfilt_smfs_brw_start,
995         .fs_commit              = fsfilt_smfs_commit,
996         .fs_commit_async        = fsfilt_smfs_commit_async,
997         .fs_commit_wait         = fsfilt_smfs_commit_wait,
998         .fs_setattr             = fsfilt_smfs_setattr,
999         .fs_iocontrol           = fsfilt_smfs_iocontrol,
1000         .fs_set_md              = fsfilt_smfs_set_md,
1001         .fs_get_md              = fsfilt_smfs_get_md,
1002         .fs_readpage            = fsfilt_smfs_readpage,
1003         .fs_getpage             = fsfilt_smfs_getpage,
1004         .fs_add_journal_cb      = fsfilt_smfs_add_journal_cb,
1005         .fs_statfs              = fsfilt_smfs_statfs,
1006         .fs_sync                = fsfilt_smfs_sync,
1007         .fs_map_inode_pages     = fsfilt_smfs_map_inode_pages,
1008         .fs_prep_san_write      = fsfilt_smfs_prep_san_write,
1009         .fs_write_record        = fsfilt_smfs_write_record,
1010         .fs_read_record         = fsfilt_smfs_read_record,
1011         .fs_setup               = fsfilt_smfs_setup,
1012         .fs_post_setup          = fsfilt_smfs_post_setup,
1013         .fs_post_cleanup        = fsfilt_smfs_post_cleanup,
1014         .fs_set_fs_flags       = fsfilt_smfs_set_fs_flags,
1015         .fs_clear_fs_flags     = fsfilt_smfs_clear_fs_flags,
1016         .fs_get_fs_flags       = fsfilt_smfs_get_fs_flags,
1017         .fs_set_ost_flags       = fsfilt_smfs_set_ost_flags,
1018         .fs_set_mds_flags       = fsfilt_smfs_set_mds_flags,
1019         .fs_precreate_rec       = fsfilt_smfs_precreate_rec,
1020         .fs_get_reint_log_ctxt  = fsfilt_smfs_get_reint_log_ctxt,
1021         .fs_send_bio            = fsfilt_smfs_send_bio,
1022         .fs_set_xattr           = fsfilt_smfs_set_xattr,
1023         .fs_get_xattr           = fsfilt_smfs_get_xattr,
1024         .fs_init_extents_ea     = fsfilt_smfs_init_extents_ea,
1025         .fs_insert_extents_ea   = fsfilt_smfs_insert_extents_ea,
1026         .fs_remove_extents_ea   = fsfilt_smfs_remove_extents_ea,
1027         .fs_get_ino_write_extents = fsfilt_smfs_get_ino_write_extents,
1028         .fs_free_write_extents  = fsfilt_smfs_free_extents,
1029         .fs_write_extents       = fsfilt_smfs_write_extents,
1030         .fs_set_snap_item       = fsfilt_smfs_set_snap_item,
1031         .fs_do_write_cow        = fsfilt_smfs_do_write_cow,
1032         /* FIXME-UMKA: probably fsfilt_smfs_get_op_len() should be
1033          * put here too. */
1034 };
1035
1036 static int __init fsfilt_smfs_init(void)
1037 {
1038         int rc;
1039
1040         rc = fsfilt_register_ops(&fsfilt_smfs_ops);
1041         return rc;
1042 }
1043
1044 static void __exit fsfilt_smfs_exit(void)
1045 {
1046         fsfilt_unregister_ops(&fsfilt_smfs_ops);
1047 }
1048
1049 module_init(fsfilt_smfs_init);
1050 module_exit(fsfilt_smfs_exit);
1051
1052 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1053 MODULE_DESCRIPTION("Lustre SMFS Filesystem Helper v0.1");
1054 MODULE_LICENSE("GPL");