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