Whamcloud - gitweb
b=7049
[fs/lustre-release.git] / lustre / lvfs / fsfilt_ext3.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/lib/fsfilt_ext3.c
5  *  Lustre filesystem abstraction routines
6  *
7  *  Copyright (C) 2002, 2003 Cluster File Systems, Inc.
8  *   Author: Andreas Dilger <adilger@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/init.h>
29 #include <linux/module.h>
30 #include <linux/fs.h>
31 #include <linux/jbd.h>
32 #include <linux/slab.h>
33 #include <linux/pagemap.h>
34 #include <linux/quotaops.h>
35 #include <linux/ext3_fs.h>
36 #include <linux/ext3_jbd.h>
37 #include <linux/ext3_extents.h>
38 #include <linux/version.h>
39 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
40 #include <linux/ext3_xattr.h>
41 #else
42 #include <ext3/xattr.h>
43 #endif
44
45 #include <libcfs/kp30.h>
46 #include <linux/lustre_fsfilt.h>
47 #include <linux/obd.h>
48 #include <linux/obd_class.h>
49 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
50 #include <linux/module.h>
51 #include <linux/iobuf.h>
52 #endif
53
54
55 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,7))
56 # define lock_24kernel() lock_kernel()
57 # define unlock_24kernel() unlock_kernel()
58 #else
59 # define lock_24kernel() do {} while (0)
60 # define unlock_24kernel() do {} while (0)
61 #endif
62
63 static kmem_cache_t *fcb_cache;
64 static atomic_t fcb_cache_count = ATOMIC_INIT(0);
65
66 struct fsfilt_cb_data {
67         struct journal_callback cb_jcb; /* jbd private data - MUST BE FIRST */
68         fsfilt_cb_t cb_func;            /* MDS/OBD completion function */
69         struct obd_device *cb_obd;      /* MDS/OBD completion device */
70         __u64 cb_last_num;              /* MDS/OST last committed operation */
71         void *cb_data;                  /* MDS/OST completion function data */
72 };
73
74 #ifndef EXT3_XATTR_INDEX_TRUSTED        /* temporary until we hit l28 kernel */
75 #define EXT3_XATTR_INDEX_TRUSTED        4
76 #endif
77
78 #define XATTR_LUSTRE_MDS_LOV_EA         "lov"
79 #define XATTR_LUSTRE_MDS_MEA_EA         "mea"
80 #define XATTR_LUSTRE_MDS_MID_EA         "mid"
81 #define XATTR_LUSTRE_MDS_SID_EA         "sid"
82
83 /*
84  * We don't currently need any additional blocks for rmdir and
85  * unlink transactions because we are storing the OST oa_id inside
86  * the inode (which we will be changing anyways as part of this
87  * transaction).
88  */
89 static void *fsfilt_ext3_start(struct inode *inode, int op, void *desc_private,
90                                int logs)
91 {
92         /* For updates to the last recieved file */
93         int nblocks = EXT3_SINGLEDATA_TRANS_BLOCKS;
94         journal_t *journal;
95         void *handle;
96
97         if (current->journal_info) {
98                 CDEBUG(D_INODE, "increasing refcount on %p\n",
99                        current->journal_info);
100                 goto journal_start;
101         }
102
103         if (logs)
104                 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
105                             EXT3_SINGLEDATA_TRANS_BLOCKS) * logs;
106                 
107         switch(op) {
108         case FSFILT_OP_RMDIR:
109         case FSFILT_OP_UNLINK:
110                 /* delete one file + create/update logs for each stripe */
111                 nblocks += EXT3_DELETE_TRANS_BLOCKS;
112                 /*nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
113                             EXT3_SINGLEDATA_TRANS_BLOCKS) * logs;*/
114                 break;
115         case FSFILT_OP_RENAME:
116                 /* modify additional directory */
117                 nblocks += EXT3_SINGLEDATA_TRANS_BLOCKS;
118                 /* no break */
119         case FSFILT_OP_SYMLINK:
120                 /* additional block + block bitmap + GDT for long symlink */
121                 nblocks += 3;
122                 /* no break */
123         case FSFILT_OP_CREATE:
124                 /* create/update logs for each stripe */
125                 /*nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
126                             EXT3_SINGLEDATA_TRANS_BLOCKS) * logs;*/
127                 /* no break */
128         case FSFILT_OP_MKDIR:
129         case FSFILT_OP_MKNOD:
130                 /* modify one inode + block bitmap + GDT */
131                 nblocks += 3;
132                 /* no break */
133         case FSFILT_OP_LINK:
134                 /* modify parent directory */
135                 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS +
136                         EXT3_DATA_TRANS_BLOCKS;
137                 break;
138         case FSFILT_OP_SETATTR:
139                 /* Setattr on inode */
140                 nblocks += 1;
141                 break;
142         case FSFILT_OP_CANCEL_UNLINK:
143                 /* blocks for log header bitmap update OR
144                  * blocks for catalog header bitmap update + unlink of logs */
145                 nblocks = (LLOG_CHUNK_SIZE >> inode->i_blkbits) +
146                         EXT3_DELETE_TRANS_BLOCKS * logs;
147                 break;
148         case FSFILT_OP_NOOP:
149                 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
150                 break;
151         default: CERROR("unknown transaction start op %d\n", op);
152                 LBUG();
153         }
154
155         LASSERT(current->journal_info == desc_private);
156         journal = EXT3_SB(inode->i_sb)->s_journal;
157         if (nblocks > journal->j_max_transaction_buffers) {
158                 CERROR("too many credits %d for op %ux%u using %d instead\n",
159                        nblocks, op, logs, journal->j_max_transaction_buffers);
160                 nblocks = journal->j_max_transaction_buffers;
161         }
162
163  journal_start:
164         LASSERTF(nblocks > 0, "can't start %d credit transaction\n", nblocks);
165         lock_24kernel();
166         handle = journal_start(EXT3_JOURNAL(inode), nblocks);
167         unlock_24kernel();
168
169         if (!IS_ERR(handle))
170                 LASSERT(current->journal_info == handle);
171         else
172                 CERROR("error starting handle for op %u (%u credits): rc %ld\n",
173                        op, nblocks, PTR_ERR(handle));
174         return handle;
175 }
176
177 /*
178  * Calculate the number of buffer credits needed to write multiple pages in
179  * a single ext3 transaction.  No, this shouldn't be here, but as yet ext3
180  * doesn't have a nice API for calculating this sort of thing in advance.
181  *
182  * See comment above ext3_writepage_trans_blocks for details.  We assume
183  * no data journaling is being done, but it does allow for all of the pages
184  * being non-contiguous.  If we are guaranteed contiguous pages we could
185  * reduce the number of (d)indirect blocks a lot.
186  *
187  * With N blocks per page and P pages, for each inode we have at most:
188  * N*P indirect
189  * min(N*P, blocksize/4 + 1) dindirect blocks
190  * niocount tindirect
191  *
192  * For the entire filesystem, we have at most:
193  * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
194  * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
195  * objcount inode blocks
196  * 1 superblock
197  * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
198  *
199  * 1 EXT3_DATA_TRANS_BLOCKS for the last_rcvd update.
200  */
201 static int fsfilt_ext3_credits_needed(int objcount, struct fsfilt_objinfo *fso,
202                                       int niocount, struct niobuf_local *nb)
203 {
204         struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
205         __u64 next_indir;
206         const int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
207         int nbitmaps = 0, ngdblocks;
208         int needed = objcount + 1; /* inodes + superblock */
209         int i, j;
210
211         for (i = 0, j = 0; i < objcount; i++, fso++) {
212                 /* two or more dindirect blocks in case we cross boundary */
213                 int ndind = (long)((nb[j + fso->fso_bufcnt - 1].offset -
214                                     nb[j].offset) >>
215                                    sb->s_blocksize_bits) /
216                         (EXT3_ADDR_PER_BLOCK(sb) * EXT3_ADDR_PER_BLOCK(sb));
217                 nbitmaps += min(fso->fso_bufcnt, ndind > 0 ? ndind : 2);
218
219                 /* leaf, indirect, tindirect blocks for first block */
220                 nbitmaps += blockpp + 2;
221
222                 j += fso->fso_bufcnt;
223         }
224
225         next_indir = nb[0].offset +
226                 (EXT3_ADDR_PER_BLOCK(sb) << sb->s_blocksize_bits);
227         for (i = 1; i < niocount; i++) {
228                 if (nb[i].offset >= next_indir) {
229                         nbitmaps++;     /* additional indirect */
230                         next_indir = nb[i].offset +
231                                 (EXT3_ADDR_PER_BLOCK(sb)<<sb->s_blocksize_bits);
232                 } else if (nb[i].offset != nb[i - 1].offset + sb->s_blocksize) {
233                         nbitmaps++;     /* additional indirect */
234                 }
235                 nbitmaps += blockpp;    /* each leaf in different group? */
236         }
237
238         ngdblocks = nbitmaps;
239         if (nbitmaps > EXT3_SB(sb)->s_groups_count)
240                 nbitmaps = EXT3_SB(sb)->s_groups_count;
241         if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
242                 ngdblocks = EXT3_SB(sb)->s_gdb_count;
243
244         needed += nbitmaps + ngdblocks;
245
246         /* last_rcvd update */
247         needed += EXT3_DATA_TRANS_BLOCKS;
248
249 #if defined(CONFIG_QUOTA) && !defined(__x86_64__) /* XXX */
250         /* We assume that there will be 1 bit set in s_dquot.flags for each
251          * quota file that is active.  This is at least true for now.
252          */
253         needed += hweight32(sb_any_quota_enabled(sb)) *
254                 EXT3_SINGLEDATA_TRANS_BLOCKS;
255 #endif
256
257         return needed;
258 }
259
260 /* We have to start a huge journal transaction here to hold all of the
261  * metadata for the pages being written here.  This is necessitated by
262  * the fact that we do lots of prepare_write operations before we do
263  * any of the matching commit_write operations, so even if we split
264  * up to use "smaller" transactions none of them could complete until
265  * all of them were opened.  By having a single journal transaction,
266  * we eliminate duplicate reservations for common blocks like the
267  * superblock and group descriptors or bitmaps.
268  *
269  * We will start the transaction here, but each prepare_write will
270  * add a refcount to the transaction, and each commit_write will
271  * remove a refcount.  The transaction will be closed when all of
272  * the pages have been written.
273  */
274 static void *fsfilt_ext3_brw_start(int objcount, struct fsfilt_objinfo *fso,
275                                    int niocount, struct niobuf_local *nb,
276                                    void *desc_private, int logs)
277 {
278         journal_t *journal;
279         handle_t *handle;
280         int needed;
281         ENTRY;
282
283         LASSERT(current->journal_info == desc_private);
284         journal = EXT3_SB(fso->fso_dentry->d_inode->i_sb)->s_journal;
285         needed = fsfilt_ext3_credits_needed(objcount, fso, niocount, nb);
286
287         /* The number of blocks we could _possibly_ dirty can very large.
288          * We reduce our request if it is absurd (and we couldn't get that
289          * many credits for a single handle anyways).
290          *
291          * At some point we have to limit the size of I/Os sent at one time,
292          * increase the size of the journal, or we have to calculate the
293          * actual journal requirements more carefully by checking all of
294          * the blocks instead of being maximally pessimistic.  It remains to
295          * be seen if this is a real problem or not.
296          */
297         if (needed > journal->j_max_transaction_buffers) {
298                 CERROR("want too many journal credits (%d) using %d instead\n",
299                        needed, journal->j_max_transaction_buffers);
300                 needed = journal->j_max_transaction_buffers;
301         }
302
303         LASSERTF(needed > 0, "can't start %d credit transaction\n", needed);
304         lock_24kernel();
305         handle = journal_start(journal, needed);
306         unlock_24kernel();
307         if (IS_ERR(handle)) {
308                 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
309                        PTR_ERR(handle));
310         } else {
311                 LASSERT(handle->h_buffer_credits >= needed);
312                 LASSERT(current->journal_info == handle);
313         }
314
315         RETURN(handle);
316 }
317
318 static int fsfilt_ext3_commit(struct super_block *sb, struct inode *inode, 
319                               void *h, int force_sync)
320 {
321         int rc;
322         handle_t *handle = h;
323
324         LASSERT(current->journal_info == handle);
325         if (force_sync)
326                 handle->h_sync = 1; /* recovery likes this */
327
328         lock_24kernel();
329         rc = journal_stop(handle);
330         unlock_24kernel();
331
332         return rc;
333 }
334
335 static int fsfilt_ext3_commit_async(struct inode *inode, void *h,
336                                     void **wait_handle)
337 {
338         unsigned long tid;
339         transaction_t *transaction;
340 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
341         unsigned long rtid;
342 #endif
343         handle_t *handle = h;
344         journal_t *journal;
345         int rc;
346
347         LASSERT(current->journal_info == handle);
348
349         lock_24kernel();
350         transaction = handle->h_transaction;
351         journal = transaction->t_journal;
352         tid = transaction->t_tid;
353         /* we don't want to be blocked */
354         handle->h_sync = 0;
355         rc = journal_stop(handle);
356         if (rc) {
357                 CERROR("error while stopping transaction: %d\n", rc);
358                 unlock_kernel();
359                 return rc;
360         }
361 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
362         rtid = log_start_commit(journal, transaction);
363         if (rtid != tid)
364                 CERROR("strange race: %lu != %lu\n",
365                        (unsigned long) tid, (unsigned long) rtid);
366 #else
367         log_start_commit(journal, tid);
368 #endif
369         unlock_24kernel();
370
371         *wait_handle = (void *) tid;
372         CDEBUG(D_INODE, "commit async: %lu\n", (unsigned long) tid);
373         return 0;
374 }
375
376 static int fsfilt_ext3_commit_wait(struct inode *inode, void *h)
377 {
378         tid_t tid = (tid_t)(long)h;
379
380         CDEBUG(D_INODE, "commit wait: %lu\n", (unsigned long) tid);
381         if (is_journal_aborted(EXT3_JOURNAL(inode)))
382                 return -EIO;
383
384         log_wait_commit(EXT3_JOURNAL(inode), tid);
385
386         return 0;
387 }
388
389 static int fsfilt_ext3_setattr(struct dentry *dentry, void *handle,
390                                struct iattr *iattr, int do_trunc)
391 {
392         struct inode *inode = dentry->d_inode;
393         int rc;
394
395         lock_24kernel();
396
397         /* A _really_ horrible hack to avoid removing the data stored
398          * in the block pointers; this is really the "small" stripe MD data.
399          * We can avoid further hackery by virtue of the MDS file size being
400          * zero all the time (which doesn't invoke block truncate at unlink
401          * time), so we assert we never change the MDS file size from zero. */
402         if (iattr->ia_valid & ATTR_SIZE && !do_trunc) {
403                 /* ATTR_SIZE would invoke truncate: clear it */
404                 iattr->ia_valid &= ~ATTR_SIZE;
405                 EXT3_I(inode)->i_disksize = inode->i_size = iattr->ia_size;
406
407                 /* make sure _something_ gets set - so new inode
408                  * goes to disk (probably won't work over XFS */
409                 if (!(iattr->ia_valid & (ATTR_MODE | ATTR_MTIME | ATTR_CTIME))){
410                         iattr->ia_valid |= ATTR_MODE;
411                         iattr->ia_mode = inode->i_mode;
412                 }
413         }
414
415         /* Don't allow setattr to change file type */
416         iattr->ia_mode = (inode->i_mode & S_IFMT)|(iattr->ia_mode & ~S_IFMT);
417
418         /* We set these flags on the client, but have already checked perms
419          * so don't confuse inode_change_ok. */
420         iattr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET);
421
422         if (inode->i_op->setattr) {
423                 rc = inode->i_op->setattr(dentry, iattr);
424         } else {
425                 rc = inode_change_ok(inode, iattr);
426                 if (!rc)
427                         rc = inode_setattr(inode, iattr);
428         }
429
430         unlock_24kernel();
431
432         return rc;
433 }
434
435 static int fsfilt_ext3_iocontrol(struct inode * inode, struct file *file,
436                                  unsigned int cmd, unsigned long arg)
437 {
438         int rc = 0;
439         ENTRY;
440
441         if (inode->i_fop->ioctl)
442                 rc = inode->i_fop->ioctl(inode, file, cmd, arg);
443         else
444                 RETURN(-ENOTTY);
445
446         RETURN(rc);
447 }
448
449 static int fsfilt_ext3_set_xattr(struct inode * inode, void *handle, char *name,
450                                  void *buffer, int buffer_size)
451 {
452         int rc = 0;
453
454         lock_24kernel();
455
456         rc = ext3_xattr_set_handle(handle, inode, EXT3_XATTR_INDEX_TRUSTED,
457                                    name, buffer, buffer_size, 0);
458         unlock_24kernel();
459         if (rc)
460                 CERROR("set xattr %s from inode %lu: rc %d\n",
461                        name,  inode->i_ino, rc);
462         return rc;
463 }
464
465 static int fsfilt_ext3_get_xattr(struct inode *inode, char *name,
466                                  void *buffer, int buffer_size)
467 {
468         int rc = 0;
469        
470         lock_24kernel();
471
472         rc = ext3_xattr_get(inode, EXT3_XATTR_INDEX_TRUSTED,
473                             name, buffer, buffer_size);
474         unlock_24kernel();
475
476         if (buffer == NULL)
477                 return (rc == -ENODATA) ? 0 : rc;
478         if (rc < 0) {
479                 CDEBUG(D_INFO, "error getting EA %s from inode %lu: rc %d\n",
480                        name,  inode->i_ino, rc);
481                 memset(buffer, 0, buffer_size);
482                 return (rc == -ENODATA) ? 0 : rc;
483         }
484
485         return rc;
486 }
487
488 static int fsfilt_ext3_set_md(struct inode *inode, void *handle,
489                               void *lmm, int lmm_size,
490                               enum ea_type type)
491 {
492         int rc;
493         
494         switch(type) {
495         case EA_LOV:
496                 rc = fsfilt_ext3_set_xattr(inode, handle,
497                                            XATTR_LUSTRE_MDS_LOV_EA,
498                                            lmm, lmm_size);
499                 break;
500         case EA_MEA:
501                 rc = fsfilt_ext3_set_xattr(inode, handle,
502                                            XATTR_LUSTRE_MDS_MEA_EA,
503                                            lmm, lmm_size);
504                 break;
505         case EA_SID:
506                 rc = fsfilt_ext3_set_xattr(inode, handle,
507                                            XATTR_LUSTRE_MDS_SID_EA,
508                                            lmm, lmm_size);
509                 break;
510         case EA_MID:
511                 rc = fsfilt_ext3_set_xattr(inode, handle,
512                                            XATTR_LUSTRE_MDS_MID_EA,
513                                            lmm, lmm_size);
514                 break;
515         default:
516                 return -EINVAL;
517         }
518
519         return rc;
520 }
521
522 static int fsfilt_ext3_get_md(struct inode *inode, void *lmm,
523                               int lmm_size, enum ea_type type)
524 {
525         int rc;
526         
527         switch (type) {
528         case EA_LOV:
529                 rc = fsfilt_ext3_get_xattr(inode,
530                                            XATTR_LUSTRE_MDS_LOV_EA,
531                                            lmm, lmm_size);
532                 break;
533         case EA_MEA:
534                 rc = fsfilt_ext3_get_xattr(inode,
535                                            XATTR_LUSTRE_MDS_MEA_EA,
536                                            lmm, lmm_size);
537                 break;
538         case EA_SID:
539                 rc = fsfilt_ext3_get_xattr(inode,
540                                            XATTR_LUSTRE_MDS_SID_EA,
541                                            lmm, lmm_size);
542                 break;
543         case EA_MID:
544                 rc = fsfilt_ext3_get_xattr(inode,
545                                            XATTR_LUSTRE_MDS_MID_EA,
546                                            lmm, lmm_size);
547                 break;
548         default:
549                 return -EINVAL;
550         }
551         
552         return rc;
553 }
554
555 static int fsfilt_ext3_send_bio(int rw, struct inode *inode, void *bio)
556 {
557         int rc = 0;
558 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
559         submit_bio(rw, (struct bio *)bio);
560 #else
561         struct bio *b = (struct kiobuf *)bio;
562         int blocks_per_page;
563         
564         rc = brw_kiovec(rw, 1, &b, inode->i_dev,
565                         b->blocks, 1 << inode->i_blkbits);
566
567         blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
568
569         if (rc != (1 << inode->i_blkbits) * b->nr_pages * blocks_per_page) {
570                 CERROR("short write?  expected %d, wrote %d\n",
571                        (1 << inode->i_blkbits) * b->nr_pages *
572                        blocks_per_page, rc);
573         }
574 #endif
575         return rc;
576 }
577
578 static struct page *fsfilt_ext3_getpage(struct inode *inode, long int index)
579 {
580         int rc;
581         struct page *page;
582
583         page = grab_cache_page(inode->i_mapping, index);
584         if (page == NULL)
585                 return ERR_PTR(-ENOMEM);
586
587         if (PageUptodate(page)) {
588                 unlock_page(page);
589                 return page;
590         }
591
592         rc = inode->i_mapping->a_ops->readpage(NULL, page);
593         if (rc < 0) {
594                 page_cache_release(page);
595                 return ERR_PTR(rc);
596         }
597
598         return page;
599 }
600
601 static ssize_t fsfilt_ext3_readpage(struct file *file, char *buf, size_t count,
602                                     loff_t *off)
603 {
604         struct inode *inode = file->f_dentry->d_inode;
605         int rc = 0;
606
607         if (S_ISREG(inode->i_mode))
608                 rc = file->f_op->read(file, buf, count, off);
609         else {
610                 const int blkbits = inode->i_sb->s_blocksize_bits;
611                 const int blksize = inode->i_sb->s_blocksize;
612
613                 CDEBUG(D_EXT2, "reading "LPSZ" at dir %lu+%llu\n",
614                        count, inode->i_ino, *off);
615                 while (count > 0) {
616                         struct buffer_head *bh;
617
618                         bh = NULL;
619                         if (*off < inode->i_size) {
620                                 int err = 0;
621
622                                 bh = ext3_bread(NULL, inode, *off >> blkbits,
623                                                 0, &err);
624
625                                 CDEBUG(D_EXT2, "read %u@%llu\n", blksize, *off);
626
627                                 if (bh) {
628                                         memcpy(buf, bh->b_data, blksize);
629                                         brelse(bh);
630                                 } else if (err) {
631                                         /* XXX in theory we should just fake
632                                          * this buffer and continue like ext3,
633                                          * especially if this is a partial read
634                                          */
635                                         CERROR("error read dir %lu+%llu: %d\n",
636                                                inode->i_ino, *off, err);
637                                         RETURN(err);
638                                 }
639                         }
640                         if (!bh) {
641                                 struct ext3_dir_entry_2 *fake = (void *)buf;
642
643                                 CDEBUG(D_EXT2, "fake %u@%llu\n", blksize, *off);
644                                 memset(fake, 0, sizeof(*fake));
645                                 fake->rec_len = cpu_to_le32(blksize);
646                         }
647                         count -= blksize;
648                         buf += blksize;
649                         *off += blksize;
650                         rc += blksize;
651                 }
652         }
653
654         return rc;
655 }
656
657 static void fsfilt_ext3_cb_func(struct journal_callback *jcb, int error)
658 {
659         struct fsfilt_cb_data *fcb = (struct fsfilt_cb_data *)jcb;
660
661         fcb->cb_func(fcb->cb_obd, fcb->cb_last_num, fcb->cb_data, error);
662
663         OBD_SLAB_FREE(fcb, fcb_cache, sizeof *fcb);
664         atomic_dec(&fcb_cache_count);
665 }
666
667 static int fsfilt_ext3_add_journal_cb(struct obd_device *obd,
668                                       struct super_block *sb,
669                                       __u64 last_num, void *handle,
670                                       fsfilt_cb_t cb_func,
671                                       void *cb_data)
672 {
673         struct fsfilt_cb_data *fcb;
674
675         OBD_SLAB_ALLOC(fcb, fcb_cache, GFP_NOFS, sizeof *fcb);
676         if (fcb == NULL)
677                 RETURN(-ENOMEM);
678
679         atomic_inc(&fcb_cache_count);
680         fcb->cb_func = cb_func;
681         fcb->cb_obd = obd;
682         fcb->cb_last_num = last_num;
683         fcb->cb_data = cb_data;
684
685         CDEBUG(D_EXT2, "set callback for last_num: "LPD64"\n", last_num);
686         lock_24kernel();
687         journal_callback_set(handle, fsfilt_ext3_cb_func,
688                              (struct journal_callback *)fcb);
689         unlock_24kernel();
690         return 0;
691 }
692
693 /*
694  * We need to hack the return value for the free inode counts because
695  * the current EA code requires one filesystem block per inode with EAs,
696  * so it is possible to run out of blocks before we run out of inodes.
697  *
698  * This can be removed when the ext3 EA code is fixed.
699  */
700 static int fsfilt_ext3_statfs(struct super_block *sb, struct obd_statfs *osfs)
701 {
702         struct kstatfs sfs;
703         int rc;
704
705         memset(&sfs, 0, sizeof(sfs));
706
707         rc = sb->s_op->statfs(sb, &sfs);
708
709         if (!rc && sfs.f_bfree < sfs.f_ffree) {
710                 sfs.f_files = (sfs.f_files - sfs.f_ffree) + sfs.f_bfree;
711                 sfs.f_ffree = sfs.f_bfree;
712         }
713
714         statfs_pack(osfs, &sfs);
715         return rc;
716 }
717
718 static int fsfilt_ext3_sync(struct super_block *sb)
719 {
720         return ext3_force_commit(sb);
721 }
722
723 #ifdef EXT3_MULTIBLOCK_ALLOCATOR
724 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
725 #define ext3_up_truncate_sem(inode)  up_write(&EXT3_I(inode)->truncate_sem);
726 #define ext3_down_truncate_sem(inode)  down_write(&EXT3_I(inode)->truncate_sem);
727 #else
728 #define ext3_up_truncate_sem(inode)  up(&EXT3_I(inode)->truncate_sem);
729 #define ext3_down_truncate_sem(inode)  down(&EXT3_I(inode)->truncate_sem);
730 #endif
731
732 #include <linux/lustre_version.h>
733 #if EXT3_EXT_MAGIC == 0xf301
734 #define ee_start e_start
735 #define ee_block e_block
736 #define ee_len   e_num
737 #endif
738 #ifndef EXT3_BB_MAX_BLOCKS
739 #define ext3_mb_new_blocks(handle, inode, goal, count, aflags, err) \
740         ext3_new_blocks(handle, inode, count, goal, err)
741 #endif
742
743 struct bpointers {
744         unsigned long *blocks;
745         int *created;
746         unsigned long start;
747         int num;
748         int init_num;
749         int create;
750 };
751 static int ext3_ext_find_goal(struct inode *inode, struct ext3_ext_path *path,
752                                 unsigned long block, int *aflags)
753 {
754         struct ext3_inode_info *ei = EXT3_I(inode);
755         unsigned long bg_start;
756         unsigned long colour;
757         int depth;
758                                                                                                                                                                                                      
759         if (path) {
760                 struct ext3_extent *ex;
761                 depth = path->p_depth;
762                                                                                                                                                                                                      
763                 /* try to predict block placement */
764                 if ((ex = path[depth].p_ext)) {
765 #if 0
766                         /* This prefers to eat into a contiguous extent
767                          * rather than find an extent that the whole
768                          * request will fit into.  This can fragment data
769                          * block allocation and prevents our lovely 1M I/Os
770                          * from reaching the disk intact. */
771
772                         if (ex->ee_block + ex->ee_len == block)
773                                 *aflags |= 1;
774 #endif
775                         return ex->ee_start + (block - ex->ee_block);
776                 }
777                                                                                                                                                                                                      
778                 /* it looks index is empty
779                  * try to find starting from index itself */
780                 if (path[depth].p_bh)
781                         return path[depth].p_bh->b_blocknr;
782         }
783                                                                                                                                                                                                      
784         /* OK. use inode's group */
785         bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
786                 le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
787         colour = (current->pid % 16) *
788                         (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
789         return bg_start + colour + block;
790 }
791
792 static int ext3_ext_new_extent_cb(struct ext3_extents_tree *tree,
793                                   struct ext3_ext_path *path,
794                                   struct ext3_extent *newex, int exist)
795 {
796         struct inode *inode = tree->inode;
797         struct bpointers *bp = tree->private;
798         int count, err, goal;
799         unsigned long pblock;
800         unsigned long tgen;
801         loff_t new_i_size;
802         handle_t *handle;
803         int i, aflags = 0;
804         
805         i = EXT_DEPTH(tree);
806         EXT_ASSERT(i == path->p_depth);
807         EXT_ASSERT(path[i].p_hdr);
808         
809         if (exist) {
810                 err = EXT_CONTINUE;
811                 goto map;
812         }
813         
814         if (bp->create == 0) {
815                 i = 0;
816                 if (newex->ee_block < bp->start)
817                         i = bp->start - newex->ee_block;
818                 if (i >= newex->ee_len)
819                         CERROR("nothing to do?! i = %d, e_num = %u\n",
820                                         i, newex->ee_len);
821                 for (; i < newex->ee_len && bp->num; i++) {
822                         *(bp->created) = 0;
823                         bp->created++;
824                         *(bp->blocks) = 0;
825                         bp->blocks++;
826                         bp->num--;
827                         bp->start++;
828                 }
829                                                                                                                                                                                                      
830                 return EXT_CONTINUE;
831         }
832         tgen = EXT_GENERATION(tree);
833         count = ext3_ext_calc_credits_for_insert(tree, path);
834         ext3_up_truncate_sem(inode);
835         lock_24kernel();
836         handle = journal_start(EXT3_JOURNAL(inode), count + EXT3_ALLOC_NEEDED + 1);
837         unlock_24kernel();
838         if (IS_ERR(handle)) {
839                 ext3_down_truncate_sem(inode);
840                 return PTR_ERR(handle);
841         }
842         
843         if (tgen != EXT_GENERATION(tree)) {
844                 /* the tree has changed. so path can be invalid at moment */
845                 lock_24kernel();
846                 journal_stop(handle);
847                 unlock_24kernel();
848                 ext3_down_truncate_sem(inode);
849                 return EXT_REPEAT;
850         }
851         ext3_down_truncate_sem(inode);
852         count = newex->ee_len;
853         goal = ext3_ext_find_goal(inode, path, newex->ee_block, &aflags);
854         aflags |= 2; /* block have been already reserved */
855         pblock = ext3_mb_new_blocks(handle, inode, goal, &count, aflags, &err);
856         if (!pblock)
857                 goto out;
858         EXT_ASSERT(count <= newex->ee_len);
859                                                                                                                                                                                                      
860         /* insert new extent */
861         newex->ee_start = pblock;
862         newex->ee_len = count;
863         err = ext3_ext_insert_extent(handle, tree, path, newex);
864         if (err)
865                 goto out;
866                                                                                                                                                                                                      
867         /* correct on-disk inode size */
868         if (newex->ee_len > 0) {
869                 new_i_size = (loff_t) newex->ee_block + newex->ee_len;
870                 new_i_size = new_i_size << inode->i_blkbits;
871                 if (new_i_size > EXT3_I(inode)->i_disksize) {
872                         EXT3_I(inode)->i_disksize = new_i_size;
873                         err = ext3_mark_inode_dirty(handle, inode);
874                 }
875         }
876 out:
877         lock_24kernel();
878         journal_stop(handle);
879         unlock_24kernel();
880 map:
881         if (err >= 0) {
882                 /* map blocks */
883                 if (bp->num == 0) {
884                         CERROR("hmm. why do we find this extent?\n");
885                         CERROR("initial space: %lu:%u\n",
886                                 bp->start, bp->init_num);
887                         CERROR("current extent: %u/%u/%u %d\n",
888                                 newex->ee_block, newex->ee_len,
889                                 newex->ee_start, exist);
890                 }
891                 i = 0;
892                 if (newex->ee_block < bp->start)
893                         i = bp->start - newex->ee_block;
894                 if (i >= newex->ee_len)
895                         CERROR("nothing to do?! i = %d, e_num = %u\n",
896                                         i, newex->ee_len);
897                 for (; i < newex->ee_len && bp->num; i++) {
898                         *(bp->created) = (exist == 0 ? 1 : 0);
899                         bp->created++;
900                         *(bp->blocks) = newex->ee_start + i;
901                         bp->blocks++;
902                         bp->num--;
903                         bp->start++;
904                 }
905         }
906         return err;
907 }
908                                                                                                                                                                                                      
909 int fsfilt_map_nblocks(struct inode *inode, unsigned long block,
910                        unsigned long num, unsigned long *blocks,
911                        int *created, int create)
912 {
913         struct ext3_extents_tree tree;
914         struct bpointers bp;
915         int err, i;
916                                                                                                                                                                                                      
917         CDEBUG(D_OTHER, "blocks %lu-%lu requested for inode %u\n",
918                 block, block + num, (unsigned) inode->i_ino);
919                                                                                                                                                                                                      
920         ext3_init_tree_desc(&tree, inode);
921         tree.private = &bp;
922         bp.blocks = blocks;
923         bp.created = created;
924         bp.start = block;
925         bp.init_num = bp.num = num;
926         bp.create = create;
927         
928         ext3_down_truncate_sem(inode);
929         err = ext3_ext_walk_space(&tree, block, num, ext3_ext_new_extent_cb);
930         ext3_ext_invalidate_cache(&tree);
931         ext3_up_truncate_sem(inode);
932
933         /* unmap underlying pages/buffers from blockdevice mapping */
934         if (create) {
935                 struct block_device *bdev = inode->i_sb->s_bdev;
936                 for (i = 0; i < num; i++) {
937                         if (created[i] == 0)
938                                 continue;
939                         unmap_underlying_metadata(bdev, blocks[i]);
940                 }
941         }
942         return err;
943 }
944
945 int fsfilt_ext3_map_ext_inode_pages(struct inode *inode, struct page **page,
946                                     int pages, unsigned long *blocks,
947                                     int *created, int create)
948 {
949         int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
950         int rc = 0, i = 0;
951         struct page *fp = NULL;
952         int clen = 0;
953
954         CDEBUG(D_OTHER, "inode %lu: map %d pages from %lu\n",
955                 inode->i_ino, pages, (*page)->index);
956
957         /* pages are sorted already. so, we just have to find
958          * contig. space and process them properly */
959         while (i < pages) {
960                 if (fp == NULL) {
961                         /* start new extent */
962                         fp = *page++;
963                         clen = 1;
964                         i++;
965                         continue;
966                 } else if (fp->index + clen == (*page)->index) {
967                         /* continue the extent */
968                         page++;
969                         clen++;
970                         i++;
971                         continue;
972                 }
973
974                 /* process found extent */
975                 rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page,
976                                         clen * blocks_per_page, blocks,
977                                         created, create);
978                 if (rc)
979                         GOTO(cleanup, rc);
980
981                 /* look for next extent */
982                 fp = NULL;
983                 blocks += blocks_per_page * clen;
984                 created += blocks_per_page * clen;
985         }
986
987         if (fp)
988                 rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page,
989                                         clen * blocks_per_page, blocks,
990                                         created, create);
991 cleanup:
992         return rc;
993 }
994 #endif
995
996 extern int ext3_map_inode_page(struct inode *inode, struct page *page,
997                                unsigned long *blocks, int *created, int create);
998 int fsfilt_ext3_map_bm_inode_pages(struct inode *inode, struct page **page,
999                                    int pages, unsigned long *blocks,
1000                                    int *created, int create)
1001 {
1002         int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
1003         unsigned long *b;
1004         int rc = 0, i, *cr;
1005
1006         for (i = 0, cr = created, b = blocks; i < pages; i++, page++) {
1007                 rc = ext3_map_inode_page(inode, *page, b, cr, create);
1008                 if (rc) {
1009                         CERROR("ino %lu, blk %lu cr %u create %d: rc %d\n",
1010                                inode->i_ino, *b, *cr, create, rc);
1011                         break;
1012                 }
1013
1014                 b += blocks_per_page;
1015                 cr += blocks_per_page;
1016         }
1017         return rc;
1018 }
1019
1020 int fsfilt_ext3_map_inode_pages(struct inode *inode, struct page **page,
1021                                 int pages, unsigned long *blocks,
1022                                 int *created, int create,
1023                                 struct semaphore *optional_sem)
1024 {
1025         int rc;
1026 #ifdef EXT3_MULTIBLOCK_ALLOCATOR
1027         if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL) {
1028                 rc = fsfilt_ext3_map_ext_inode_pages(inode, page, pages,
1029                                                      blocks, created, create);
1030                 return rc;
1031         }
1032 #endif
1033         if (optional_sem != NULL)
1034                 down(optional_sem);
1035         rc = fsfilt_ext3_map_bm_inode_pages(inode, page, pages, blocks,
1036                                             created, create);
1037         if (optional_sem != NULL)
1038                 up(optional_sem);
1039
1040         return rc;
1041 }
1042
1043 extern int ext3_prep_san_write(struct inode *inode, long *blocks,
1044                                int nblocks, loff_t newsize);
1045 static int fsfilt_ext3_prep_san_write(struct inode *inode, long *blocks,
1046                                       int nblocks, loff_t newsize)
1047 {
1048         return ext3_prep_san_write(inode, blocks, nblocks, newsize);
1049 }
1050
1051 static int fsfilt_ext3_read_record(struct file * file, void *buf,
1052                                    int size, loff_t *offs)
1053 {
1054         struct inode *inode = file->f_dentry->d_inode;
1055         unsigned long block;
1056         struct buffer_head *bh;
1057         int err, blocksize, csize, boffs;
1058
1059         /* prevent reading after eof */
1060         lock_kernel();
1061         if (inode->i_size < *offs + size) {
1062                 size = inode->i_size - *offs;
1063                 unlock_kernel();
1064                 if (size < 0) {
1065                         CERROR("size %llu is too short for read %u@%llu\n",
1066                                inode->i_size, size, *offs);
1067                         return -EIO;
1068                 } else if (size == 0) {
1069                         return 0;
1070                 }
1071         } else {
1072                 unlock_kernel();
1073         }
1074
1075         blocksize = 1 << inode->i_blkbits;
1076
1077         while (size > 0) {
1078                 block = *offs >> inode->i_blkbits;
1079                 boffs = *offs & (blocksize - 1);
1080                 csize = min(blocksize - boffs, size);
1081                 bh = ext3_bread(NULL, inode, block, 0, &err);
1082                 if (!bh) {
1083                         CERROR("can't read block: %d\n", err);
1084                         return err;
1085                 }
1086
1087                 memcpy(buf, bh->b_data + boffs, csize);
1088                 brelse(bh);
1089
1090                 *offs += csize;
1091                 buf += csize;
1092                 size -= csize;
1093         }
1094         return 0;
1095 }
1096
1097 static int fsfilt_ext3_write_record(struct file *file, void *buf, int bufsize,
1098                                     loff_t *offs, int force_sync)
1099 {
1100         struct buffer_head *bh = NULL;
1101         unsigned long block;
1102         struct inode *inode = file->f_dentry->d_inode;
1103         loff_t old_size = inode->i_size, offset = *offs;
1104         loff_t new_size = inode->i_size;
1105         journal_t *journal;
1106         handle_t *handle;
1107         int err = 0, block_count = 0, blocksize, size, boffs;
1108
1109         /* Determine how many transaction credits are needed */
1110         blocksize = 1 << inode->i_blkbits;
1111         block_count = (*offs & (blocksize - 1)) + bufsize;
1112         block_count = (block_count + blocksize - 1) >> inode->i_blkbits;
1113
1114         journal = EXT3_SB(inode->i_sb)->s_journal;
1115         lock_24kernel();
1116         handle = journal_start(journal,
1117                                block_count * EXT3_DATA_TRANS_BLOCKS + 2);
1118         unlock_24kernel();
1119         if (IS_ERR(handle)) {
1120                 CERROR("can't start transaction\n");
1121                 return PTR_ERR(handle);
1122         }
1123
1124         while (bufsize > 0) {
1125                 if (bh != NULL)
1126                         brelse(bh);
1127
1128                 block = offset >> inode->i_blkbits;
1129                 boffs = offset & (blocksize - 1);
1130                 size = min(blocksize - boffs, bufsize);
1131                 bh = ext3_bread(handle, inode, block, 1, &err);
1132                 if (!bh) {
1133                         CERROR("can't read/create block: %d\n", err);
1134                         goto out;
1135                 }
1136
1137                 err = ext3_journal_get_write_access(handle, bh);
1138                 if (err) {
1139                         CERROR("journal_get_write_access() returned error %d\n",
1140                                err);
1141                         goto out;
1142                 }
1143                 LASSERT(bh->b_data + boffs + size <= bh->b_data + bh->b_size);
1144                 memcpy(bh->b_data + boffs, buf, size);
1145                 err = ext3_journal_dirty_metadata(handle, bh);
1146                 if (err) {
1147                         CERROR("journal_dirty_metadata() returned error %d\n",
1148                                err);
1149                         goto out;
1150                 }
1151                 if (offset + size > new_size)
1152                         new_size = offset + size;
1153                 offset += size;
1154                 bufsize -= size;
1155                 buf += size;
1156         }
1157
1158         if (force_sync)
1159                 handle->h_sync = 1; /* recovery likes this */
1160 out:
1161         if (bh)
1162                 brelse(bh);
1163
1164         /* correct in-core and on-disk sizes */
1165         if (new_size > inode->i_size) {
1166                 lock_kernel();
1167                 if (new_size > inode->i_size)
1168                         inode->i_size = new_size;
1169                 if (inode->i_size > EXT3_I(inode)->i_disksize)
1170                         EXT3_I(inode)->i_disksize = inode->i_size;
1171                 if (inode->i_size > old_size)
1172                         mark_inode_dirty(inode);
1173                 unlock_kernel();
1174         }
1175
1176         lock_24kernel();
1177         journal_stop(handle);
1178         unlock_24kernel();
1179
1180         if (err == 0)
1181                 *offs = offset;
1182         return err;
1183 }
1184
1185 static int fsfilt_ext3_setup(struct obd_device *obd, struct super_block *sb)
1186 {
1187 #ifdef EXT3_FEATURE_INCOMPAT_MDSNUM
1188         struct mds_obd *mds = &obd->u.mds;
1189 #endif
1190 #if 0
1191         EXT3_SB(sb)->dx_lock = fsfilt_ext3_dx_lock;
1192         EXT3_SB(sb)->dx_unlock = fsfilt_ext3_dx_unlock;
1193 #endif
1194 #ifdef S_PDIROPS
1195         CWARN("Enabling PDIROPS\n");
1196         set_opt(EXT3_SB(sb)->s_mount_opt, PDIROPS);
1197         sb->s_flags |= S_PDIROPS;
1198 #endif
1199         /* setup mdsnum in underlying fs */
1200 #ifdef EXT3_FEATURE_INCOMPAT_MDSNUM
1201         if (mds->mds_md_obd) {
1202                 struct ext3_sb_info *sbi = EXT3_SB(sb);
1203                 struct ext3_super_block *es = sbi->s_es;
1204                 handle_t *handle;
1205                 int err;
1206                 
1207                 if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_MDSNUM)) {
1208                         CWARN("%s: set mdsnum %d in ext3\n",
1209                               obd->obd_name, mds->mds_num);
1210                         lock_24kernel();
1211                         handle = journal_start(sbi->s_journal, 1);
1212                         unlock_24kernel();
1213                         LASSERT(!IS_ERR(handle));
1214                         err = ext3_journal_get_write_access(handle, sbi->s_sbh);
1215                         LASSERT(err == 0);
1216                         EXT3_SET_INCOMPAT_FEATURE(sb,
1217                                                 EXT3_FEATURE_INCOMPAT_MDSNUM);
1218                         es->s_mdsnum = mds->mds_num;
1219                         err = ext3_journal_dirty_metadata(handle, sbi->s_sbh);
1220                         LASSERT(err == 0);
1221                         lock_24kernel();
1222                         journal_stop(handle);
1223                         unlock_24kernel();
1224                 } else {
1225                         CWARN("%s: mdsnum initialized to %u in ext3fs\n",
1226                                 obd->obd_name, es->s_mdsnum);
1227                 }
1228                 sbi->s_mdsnum = es->s_mdsnum;
1229         }
1230 #endif
1231         return 0;
1232 }
1233
1234 extern int ext3_add_dir_entry(struct dentry *dentry);
1235 extern int ext3_del_dir_entry(struct dentry *dentry);
1236
1237 static int fsfilt_ext3_add_dir_entry(struct obd_device *obd,
1238                                      struct dentry *parent,
1239                                      char *name, int namelen,
1240                                      unsigned long ino,
1241                                      unsigned long generation,
1242                                      unsigned long mds, 
1243                                      unsigned long fid)
1244 {
1245 #ifdef EXT3_FEATURE_INCOMPAT_MDSNUM
1246         struct dentry *dentry;
1247         int err;
1248         LASSERT(ino != 0);
1249         LASSERT(namelen != 0);
1250         dentry = ll_lookup_one_len(name, parent, namelen);
1251         if (IS_ERR(dentry)) {
1252                 CERROR("can't lookup %*s in %lu/%lu: %d\n", dentry->d_name.len,
1253                        dentry->d_name.name, dentry->d_inode->i_ino,
1254                        (unsigned long) dentry->d_inode->i_generation,
1255                        (int) PTR_ERR(dentry));
1256                 RETURN(PTR_ERR(dentry));
1257         }
1258         if (dentry->d_inode != NULL || dentry->d_flags & DCACHE_CROSS_REF) {
1259                 CERROR("dentry %*s(0x%p) found\n", dentry->d_name.len,
1260                        dentry->d_name.name, dentry);
1261                 l_dput(dentry);
1262                 RETURN(-EEXIST);
1263         }
1264
1265         /* mds_reint_rename() may use this method to add dir entry 
1266          * that points onto local inode. and we don't want to find
1267          * it cross-ref by subsequent lookups */
1268         d_drop(dentry);
1269
1270         dentry->d_flags |= DCACHE_CROSS_REF;
1271         dentry->d_inum = ino;
1272         dentry->d_mdsnum = mds;
1273         dentry->d_generation = generation;
1274         dentry->d_fid = fid;
1275         lock_24kernel();
1276         err = ext3_add_dir_entry(dentry);
1277         unlock_24kernel();
1278         
1279         l_dput(dentry);
1280
1281         RETURN(err);
1282 #else
1283 #error "rebuild kernel and lustre with ext3-mds-num patch!"
1284         LASSERT(0);
1285 #endif
1286 }
1287
1288 static int fsfilt_ext3_del_dir_entry(struct obd_device *obd,
1289                                  struct dentry *dentry)
1290 {
1291 #ifdef EXT3_FEATURE_INCOMPAT_MDSNUM
1292         int err;
1293         lock_24kernel();
1294         err = ext3_del_dir_entry(dentry);
1295         unlock_24kernel();
1296         if (err == 0)
1297                 d_drop(dentry);
1298         return err;
1299 #else
1300 #error "rebuild kernel and lustre with ext3-mds-num patch!"
1301         LASSERT(0);
1302 #endif
1303 }
1304
1305 /* If fso is NULL, op is FSFILT operation, otherwise op is number of fso
1306    objects. Logs is number of logfiles to update */
1307 static int fsfilt_ext3_get_op_len(int op, struct fsfilt_objinfo *fso, int logs)
1308 {
1309         if ( !fso ) {
1310                 switch(op) {
1311                 case FSFILT_OP_CREATE:
1312                                  /* directory leaf, index & indirect & EA*/
1313                         return 4 + 3 * logs;
1314                 case FSFILT_OP_UNLINK:
1315                         return 3 * logs;
1316                 }
1317         } else {
1318                 int i;
1319                 int needed = 0;
1320                 struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
1321                 int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
1322                 int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
1323                 for (i = 0; i < op; i++, fso++) {
1324                         int nblocks = fso->fso_bufcnt * blockpp;
1325                         int ndindirect = min(nblocks, addrpp + 1);
1326                         int nindir = nblocks + ndindirect + 1;
1327
1328                         needed += nindir;
1329                 }
1330                 return needed + 3 * logs;
1331         }
1332
1333         return 0;
1334 }
1335
1336
1337 #define EXTENTS_EA "write_extents"
1338 #define EXTENTS_EA_SIZE 64
1339
1340 int ext3_ext_in_ea_alloc_space(struct inode *, int, const char *, unsigned long, unsigned long);
1341 int ext3_ext_in_ea_remove_space(struct inode *, int, const char *, unsigned long, unsigned long);
1342 int ext3_ext_in_ea_get_extents(struct inode *, int, const char *, char **, int *);
1343 int ext3_ext_in_ea_get_extents_num(struct inode *, int, const char *, int *);
1344
1345 static int fsfilt_ext3_insert_extents_ea(struct inode *inode, 
1346                                       unsigned long from, 
1347                                       unsigned long num) 
1348 {
1349         int rc = 0;
1350
1351         rc = ext3_ext_in_ea_alloc_space(inode, EXT3_XATTR_INDEX_TRUSTED,
1352                                         EXTENTS_EA, from, num);  
1353         return rc;
1354 }
1355
1356 static int fsfilt_ext3_remove_extents_ea(struct inode *inode, 
1357                                          unsigned long from, 
1358                                          unsigned long num) 
1359 {
1360         int rc = 0;
1361
1362         rc = ext3_ext_in_ea_remove_space(inode, EXT3_XATTR_INDEX_TRUSTED,
1363                                          EXTENTS_EA, from, num);  
1364         return rc;
1365 }
1366
1367 extern int ext3_init_tree_in_ea(struct inode *inode, int name_index,
1368                                 const char *eaname, int size);
1369                                 
1370 static int fsfilt_ext3_init_extents_ea(struct inode *inode)
1371 {
1372         int rc = 0;
1373
1374         rc = ext3_init_tree_in_ea(inode, EXT3_XATTR_INDEX_TRUSTED,
1375                                   EXTENTS_EA, 64);  
1376         return rc;
1377 }
1378
1379 static int fsfilt_ext3_get_inode_write_extents(struct inode *inode, 
1380                                          char **pbuf, int *size)
1381 {
1382         int rc = 0;
1383         
1384         rc = ext3_ext_in_ea_get_extents(inode, EXT3_XATTR_INDEX_TRUSTED,
1385                                         EXTENTS_EA,  pbuf, size);
1386         return rc; 
1387
1388
1389 static int fsfilt_ext3_get_write_extents_num(struct inode *inode, int *size)
1390 {
1391         int rc = 0;
1392         
1393         rc = ext3_ext_in_ea_get_extents_num(inode, EXT3_XATTR_INDEX_TRUSTED, 
1394                                             EXTENTS_EA, size);
1395         return rc; 
1396
1397
1398 static struct fsfilt_operations fsfilt_ext3_ops = {
1399         .fs_type                    = "ext3",
1400         .fs_owner                   = THIS_MODULE,
1401         .fs_start                   = fsfilt_ext3_start,
1402         .fs_brw_start               = fsfilt_ext3_brw_start,
1403         .fs_commit                  = fsfilt_ext3_commit,
1404         .fs_commit_async            = fsfilt_ext3_commit_async,
1405         .fs_commit_wait             = fsfilt_ext3_commit_wait,
1406         .fs_setattr                 = fsfilt_ext3_setattr,
1407         .fs_iocontrol               = fsfilt_ext3_iocontrol,
1408         .fs_set_md                  = fsfilt_ext3_set_md,
1409         .fs_get_md                  = fsfilt_ext3_get_md,
1410         .fs_readpage                = fsfilt_ext3_readpage,
1411         .fs_add_journal_cb          = fsfilt_ext3_add_journal_cb,
1412         .fs_statfs                  = fsfilt_ext3_statfs,
1413         .fs_sync                    = fsfilt_ext3_sync,
1414         .fs_map_inode_pages         = fsfilt_ext3_map_inode_pages,
1415         .fs_prep_san_write          = fsfilt_ext3_prep_san_write,
1416         .fs_write_record            = fsfilt_ext3_write_record,
1417         .fs_read_record             = fsfilt_ext3_read_record,
1418         .fs_setup                   = fsfilt_ext3_setup,
1419         .fs_getpage                 = fsfilt_ext3_getpage,
1420         .fs_send_bio                = fsfilt_ext3_send_bio,
1421         .fs_set_xattr               = fsfilt_ext3_set_xattr,
1422         .fs_get_xattr               = fsfilt_ext3_get_xattr,
1423         .fs_get_op_len              = fsfilt_ext3_get_op_len,
1424         .fs_add_dir_entry           = fsfilt_ext3_add_dir_entry,
1425         .fs_del_dir_entry           = fsfilt_ext3_del_dir_entry,
1426         .fs_init_extents_ea         = fsfilt_ext3_init_extents_ea,
1427         .fs_insert_extents_ea       = fsfilt_ext3_insert_extents_ea,
1428         .fs_remove_extents_ea       = fsfilt_ext3_remove_extents_ea,
1429         .fs_get_inode_write_extents = fsfilt_ext3_get_inode_write_extents,
1430         .fs_get_write_extents_num   = fsfilt_ext3_get_write_extents_num,
1431 };
1432
1433 static int __init fsfilt_ext3_init(void)
1434 {
1435         int rc;
1436
1437         fcb_cache = kmem_cache_create("fsfilt_ext3_fcb",
1438                                       sizeof(struct fsfilt_cb_data), 0,
1439                                       0, NULL, NULL);
1440         if (!fcb_cache) {
1441                 CERROR("error allocating fsfilt journal callback cache\n");
1442                 GOTO(out, rc = -ENOMEM);
1443         }
1444
1445         rc = fsfilt_register_ops(&fsfilt_ext3_ops);
1446
1447         if (rc)
1448                 kmem_cache_destroy(fcb_cache);
1449 out:
1450         return rc;
1451 }
1452
1453 static void __exit fsfilt_ext3_exit(void)
1454 {
1455         fsfilt_unregister_ops(&fsfilt_ext3_ops);
1456         LASSERTF(kmem_cache_destroy(fcb_cache) == 0,
1457                  "can't free fsfilt callback cache: count %d\n",
1458                  atomic_read(&fcb_cache_count));
1459 }
1460
1461 module_init(fsfilt_ext3_init);
1462 module_exit(fsfilt_ext3_exit);
1463
1464 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1465 MODULE_DESCRIPTION("Lustre ext3 Filesystem Helper v0.1");
1466 MODULE_LICENSE("GPL");