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