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