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