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