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