Whamcloud - gitweb
This changes the fsfilt transaction calculations to take into account the
[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/fs.h>
29 #include <linux/jbd.h>
30 #include <linux/slab.h>
31 #include <linux/pagemap.h>
32 #include <linux/quotaops.h>
33 #include <linux/ext3_fs.h>
34 #include <linux/ext3_jbd.h>
35 #include <linux/version.h>
36 /* XXX ugh */
37 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
38  #include <linux/ext3_xattr.h>
39 #else
40  #include <linux/../../fs/ext3/xattr.h>
41 #endif
42 #include <linux/kp30.h>
43 #include <linux/lustre_fsfilt.h>
44 #include <linux/obd.h>
45 #include <linux/obd_class.h>
46 #include <linux/module.h>
47
48 static kmem_cache_t *fcb_cache;
49 static atomic_t fcb_cache_count = ATOMIC_INIT(0);
50
51 struct fsfilt_cb_data {
52         struct journal_callback cb_jcb; /* jbd private data - MUST BE FIRST */
53         fsfilt_cb_t cb_func;            /* MDS/OBD completion function */
54         struct obd_device *cb_obd;      /* MDS/OBD completion device */
55         __u64 cb_last_rcvd;             /* MDS/OST last committed operation */
56         void *cb_data;                  /* MDS/OST completion function data */
57 };
58
59 #ifndef EXT3_XATTR_INDEX_TRUSTED        /* temporary until we hit l28 kernel */
60 #define EXT3_XATTR_INDEX_TRUSTED        4
61 #endif
62 #define XATTR_LUSTRE_MDS_LOV_EA         "lov"
63
64 #define EXT3_XATTR_INDEX_LUSTRE         5                         /* old */
65 #define XATTR_LUSTRE_MDS_OBJID          "system.lustre_mds_objid" /* old */
66
67 /*
68  * We don't currently need any additional blocks for rmdir and
69  * unlink transactions because we are storing the OST oa_id inside
70  * the inode (which we will be changing anyways as part of this
71  * transaction).
72  */
73 static void *fsfilt_ext3_start(struct inode *inode, int op, void *desc_private,
74                                int logs)
75 {
76         /* For updates to the last recieved file */
77         int nblocks = EXT3_DATA_TRANS_BLOCKS;
78         void *handle;
79
80         if (current->journal_info) {
81                 CDEBUG(D_INODE, "increasing refcount on %p\n", current->journal_info);
82                 goto journal_start;
83         }
84
85         switch(op) {
86         case FSFILT_OP_RMDIR:
87         case FSFILT_OP_UNLINK:
88                 nblocks += EXT3_DELETE_TRANS_BLOCKS;
89                 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
90                             EXT3_DATA_TRANS_BLOCKS) * logs;
91                 break;
92         case FSFILT_OP_RENAME:
93                 /* modify additional directory */
94                 nblocks += EXT3_DATA_TRANS_BLOCKS;
95                 /* no break */
96         case FSFILT_OP_SYMLINK:
97                 /* additional block + block bitmap + GDT for long symlink */
98                 nblocks += 3;
99                 /* no break */
100         case FSFILT_OP_CREATE:
101                 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
102                             EXT3_DATA_TRANS_BLOCKS) * logs;
103         case FSFILT_OP_MKDIR:
104         case FSFILT_OP_MKNOD:
105                 /* modify one inode + block bitmap + GDT */
106                 nblocks += 3;
107                 /* no break */
108         case FSFILT_OP_LINK:
109                 /* modify parent directory */
110                 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
111                 break;
112         case FSFILT_OP_SETATTR:
113                 /* Setattr on inode */
114                 nblocks += 1;
115                 break;
116         case FSFILT_OP_CANCEL_UNLINK:
117                 nblocks = (LLOG_CHUNK_SIZE >> inode->i_blkbits) +
118                         EXT3_DELETE_TRANS_BLOCKS * logs;
119                 break;
120         default: CERROR("unknown transaction start op %d\n", op);
121                  LBUG();
122         }
123
124         LASSERT(current->journal_info == desc_private);
125
126  journal_start:
127         lock_kernel();
128         handle = journal_start(EXT3_JOURNAL(inode), nblocks);
129         unlock_kernel();
130
131         if (!IS_ERR(handle))
132                 LASSERT(current->journal_info == handle);
133         return handle;
134 }
135
136 /*
137  * Calculate the number of buffer credits needed to write multiple pages in
138  * a single ext3 transaction.  No, this shouldn't be here, but as yet ext3
139  * doesn't have a nice API for calculating this sort of thing in advance.
140  *
141  * See comment above ext3_writepage_trans_blocks for details.  We assume
142  * no data journaling is being done, but it does allow for all of the pages
143  * being non-contiguous.  If we are guaranteed contiguous pages we could
144  * reduce the number of (d)indirect blocks a lot.
145  *
146  * With N blocks per page and P pages, for each inode we have at most:
147  * N*P indirect
148  * min(N*P, blocksize/4 + 1) dindirect blocks
149  * niocount tindirect
150  *
151  * For the entire filesystem, we have at most:
152  * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
153  * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
154  * objcount inode blocks
155  * 1 superblock
156  * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
157  *
158  * 1 EXT3_DATA_TRANS_BLOCKS for the last_rcvd update.
159  */
160 static int fsfilt_ext3_credits_needed(int objcount, struct fsfilt_objinfo *fso,
161                                       int niocount, struct niobuf_local *nb)
162 {
163         struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
164         __u64 next_indir;
165         const int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
166         int nbitmaps = 0, ngdblocks;
167         int needed = objcount + 1; /* inodes + superblock */
168         int i, j;
169
170         for (i = 0, j = 0; i < objcount; i++, fso++) {
171                 /* two or more dindirect blocks in case we cross boundary */
172                 int ndind = (long)((nb[j + fso->fso_bufcnt - 1].offset -
173                                     nb[j].offset) >>
174                                    sb->s_blocksize_bits) /
175                         (EXT3_ADDR_PER_BLOCK(sb) * EXT3_ADDR_PER_BLOCK(sb));
176                 nbitmaps += min(fso->fso_bufcnt, ndind > 0 ? ndind : 2);
177
178                 /* leaf, indirect, tindirect blocks for first block */
179                 nbitmaps += blockpp + 2;
180
181                 j += fso->fso_bufcnt;
182         }
183
184         next_indir = nb[0].offset +
185                 (EXT3_ADDR_PER_BLOCK(sb) << sb->s_blocksize_bits);
186         for (i = 1; i < niocount; i++) {
187                 if (nb[i].offset >= next_indir) {
188                         nbitmaps++;     /* additional indirect */
189                         next_indir = nb[i].offset +
190                                 (EXT3_ADDR_PER_BLOCK(sb)<<sb->s_blocksize_bits);
191                 } else if (nb[i].offset != nb[i - 1].offset + sb->s_blocksize) {
192                         nbitmaps++;     /* additional indirect */
193                 }
194                 nbitmaps += blockpp;    /* each leaf in different group? */
195         }
196
197         ngdblocks = nbitmaps;
198         if (nbitmaps > EXT3_SB(sb)->s_groups_count)
199                 nbitmaps = EXT3_SB(sb)->s_groups_count;
200         if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
201                 ngdblocks = EXT3_SB(sb)->s_gdb_count;
202
203         needed += nbitmaps + ngdblocks;
204
205         /* last_rcvd update */
206         needed += EXT3_DATA_TRANS_BLOCKS;
207
208 #if defined(CONFIG_QUOTA) && !defined(__x86_64__) /* XXX */
209         /* We assume that there will be 1 bit set in s_dquot.flags for each
210          * quota file that is active.  This is at least true for now.
211          */
212         needed += hweight32(sb_any_quota_enabled(sb)) *
213                 EXT3_SINGLEDATA_TRANS_BLOCKS;
214 #endif
215
216         return needed;
217 }
218
219 /* We have to start a huge journal transaction here to hold all of the
220  * metadata for the pages being written here.  This is necessitated by
221  * the fact that we do lots of prepare_write operations before we do
222  * any of the matching commit_write operations, so even if we split
223  * up to use "smaller" transactions none of them could complete until
224  * all of them were opened.  By having a single journal transaction,
225  * we eliminate duplicate reservations for common blocks like the
226  * superblock and group descriptors or bitmaps.
227  *
228  * We will start the transaction here, but each prepare_write will
229  * add a refcount to the transaction, and each commit_write will
230  * remove a refcount.  The transaction will be closed when all of
231  * the pages have been written.
232  */
233 static void *fsfilt_ext3_brw_start(int objcount, struct fsfilt_objinfo *fso,
234                                    int niocount, struct niobuf_local *nb,
235                                    void *desc_private, int logs)
236 {
237         journal_t *journal;
238         handle_t *handle;
239         int needed;
240         ENTRY;
241
242         LASSERT(current->journal_info == desc_private);
243         journal = EXT3_SB(fso->fso_dentry->d_inode->i_sb)->s_journal;
244         needed = fsfilt_ext3_credits_needed(objcount, fso, niocount, nb);
245
246         /* The number of blocks we could _possibly_ dirty can very large.
247          * We reduce our request if it is absurd (and we couldn't get that
248          * many credits for a single handle anyways).
249          *
250          * At some point we have to limit the size of I/Os sent at one time,
251          * increase the size of the journal, or we have to calculate the
252          * actual journal requirements more carefully by checking all of
253          * the blocks instead of being maximally pessimistic.  It remains to
254          * be seen if this is a real problem or not.
255          */
256         if (needed > journal->j_max_transaction_buffers) {
257                 CERROR("want too many journal credits (%d) using %d instead\n",
258                        needed, journal->j_max_transaction_buffers);
259                 needed = journal->j_max_transaction_buffers;
260         }
261
262         lock_kernel();
263         handle = journal_start(journal, needed);
264         unlock_kernel();
265         if (IS_ERR(handle)) {
266                 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
267                        PTR_ERR(handle));
268         } else {
269                 LASSERT(handle->h_buffer_credits >= needed);
270                 LASSERT(current->journal_info == handle);
271         }
272
273         RETURN(handle);
274 }
275
276 static int fsfilt_ext3_commit(struct inode *inode, void *h, int force_sync)
277 {
278         int rc;
279         handle_t *handle = h;
280
281         LASSERT(current->journal_info == handle);
282         if (force_sync)
283                 handle->h_sync = 1; /* recovery likes this */
284
285         lock_kernel();
286         rc = journal_stop(handle);
287         unlock_kernel();
288
289         // LASSERT(current->journal_info == NULL);
290         return rc;
291 }
292
293 static int fsfilt_ext3_commit_async(struct inode *inode, void *h,
294                                         void **wait_handle)
295 {
296         transaction_t *transaction;
297         unsigned long tid, rtid;
298         handle_t *handle = h;
299         journal_t *journal;
300         int rc;
301
302         LASSERT(current->journal_info == handle);
303
304         lock_kernel();
305         transaction = handle->h_transaction;
306         journal = transaction->t_journal;
307         tid = transaction->t_tid;
308         /* we don't want to be blocked */
309         handle->h_sync = 0;
310         rc = journal_stop(handle);
311         if (rc) {
312                 CERROR("error while stopping transaction: %d\n", rc);
313                 unlock_kernel();
314                 return rc;
315         }
316 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
317         rtid = log_start_commit(journal, transaction);
318         if (rtid != tid)
319                 CERROR("strange race: %lu != %lu\n",
320                        (unsigned long) tid, (unsigned long) rtid);
321 #else
322         log_start_commit(journal, transaction->t_tid);
323 #endif
324         unlock_kernel();
325
326         *wait_handle = (void *) tid;
327         CDEBUG(D_INODE, "commit async: %lu\n", (unsigned long) tid);
328         return 0;
329 }
330
331 static int fsfilt_ext3_commit_wait(struct inode *inode, void *h)
332 {
333         tid_t tid = (tid_t)(long)h;
334
335         CDEBUG(D_INODE, "commit wait: %lu\n", (unsigned long) tid);
336         if (is_journal_aborted(EXT3_JOURNAL(inode)))
337                 return -EIO;
338
339         log_wait_commit(EXT3_JOURNAL(inode), tid);
340
341         return 0;
342 }
343
344 static int fsfilt_ext3_setattr(struct dentry *dentry, void *handle,
345                                struct iattr *iattr, int do_trunc)
346 {
347         struct inode *inode = dentry->d_inode;
348         int rc;
349
350         lock_kernel();
351
352         /* A _really_ horrible hack to avoid removing the data stored
353          * in the block pointers; this is really the "small" stripe MD data.
354          * We can avoid further hackery by virtue of the MDS file size being
355          * zero all the time (which doesn't invoke block truncate at unlink
356          * time), so we assert we never change the MDS file size from zero. */
357         if (iattr->ia_valid & ATTR_SIZE && !do_trunc) {
358                 /* ATTR_SIZE would invoke truncate: clear it */
359                 iattr->ia_valid &= ~ATTR_SIZE;
360                 EXT3_I(inode)->i_disksize = inode->i_size = iattr->ia_size;
361
362                 /* make sure _something_ gets set - so new inode
363                  * goes to disk (probably won't work over XFS */
364                 if (!(iattr->ia_valid & (ATTR_MODE | ATTR_MTIME | ATTR_CTIME))){
365                         iattr->ia_valid |= ATTR_MODE;
366                         iattr->ia_mode = inode->i_mode;
367                 }
368         }
369
370         /* Don't allow setattr to change file type */
371         iattr->ia_mode = (inode->i_mode & S_IFMT)|(iattr->ia_mode & ~S_IFMT);
372
373         /* We set these flags on the client, but have already checked perms
374          * so don't confuse inode_change_ok. */
375         iattr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET);
376
377         if (inode->i_op->setattr) {
378                 rc = inode->i_op->setattr(dentry, iattr);
379         } else {
380                 rc = inode_change_ok(inode, iattr);
381                 if (!rc)
382                         rc = inode_setattr(inode, iattr);
383         }
384
385         unlock_kernel();
386
387         return rc;
388 }
389
390 static int fsfilt_ext3_iocontrol(struct inode * inode, struct file *file,
391                                  unsigned int cmd, unsigned long arg)
392 {
393         int rc = 0;
394         ENTRY;
395
396         if (inode->i_fop->ioctl)
397                 rc = inode->i_fop->ioctl(inode, file, cmd, arg);
398         else
399                 RETURN(-ENOTTY);
400
401         RETURN(rc);
402 }
403
404 #undef INLINE_EA
405 #undef OLD_EA
406 static int fsfilt_ext3_set_md(struct inode *inode, void *handle,
407                               void *lmm, int lmm_size)
408 {
409         int rc, old_ea = 0;
410
411 #ifdef INLINE_EA  /* can go away before 1.0 - just for testing bug 2097 now */
412         /* Nasty hack city - store stripe MD data in the block pointers if
413          * it will fit, because putting it in an EA currently kills the MDS
414          * performance.  We'll fix this with "fast EAs" in the future.
415          */
416         if (inode->i_blocks == 0 && lmm_size <= sizeof(EXT3_I(inode)->i_data) -
417                                             sizeof(EXT3_I(inode)->i_data[0])) {
418                 unsigned old_size = EXT3_I(inode)->i_data[0];
419                 if (old_size != 0) {
420                         LASSERT(old_size < sizeof(EXT3_I(inode)->i_data));
421                         CERROR("setting EA on %lu/%u again... interesting\n",
422                                inode->i_ino, inode->i_generation);
423                 }
424
425                 EXT3_I(inode)->i_data[0] = cpu_to_le32(lmm_size);
426                 memcpy(&EXT3_I(inode)->i_data[1], lmm, lmm_size);
427                 mark_inode_dirty(inode);
428                 return 0;
429         }
430 #endif
431 #ifdef OLD_EA
432         /* keep this when we get rid of OLD_EA (too noisy during conversion) */
433         if (EXT3_I(inode)->i_file_acl /* || large inode EA flag */) {
434                 CWARN("setting EA on %lu/%u again... interesting\n",
435                        inode->i_ino, inode->i_generation);
436                 old_ea = 1;
437         }
438
439         lock_kernel();
440         /* this can go away before 1.0.  For bug 2097 testing only. */
441         rc = ext3_xattr_set_handle(handle, inode, EXT3_XATTR_INDEX_LUSTRE,
442                                    XATTR_LUSTRE_MDS_OBJID, lmm, lmm_size, 0);
443 #else
444         lock_kernel();
445         rc = ext3_xattr_set_handle(handle, inode, EXT3_XATTR_INDEX_TRUSTED,
446                                    XATTR_LUSTRE_MDS_LOV_EA, lmm, lmm_size, 0);
447
448         /* This tries to delete the old-format LOV EA, but only as long as we
449          * have successfully saved the new-format LOV EA (we can always try
450          * the conversion again the next time the file is accessed).  It is
451          * possible (although unlikely) that the new-format LOV EA couldn't be
452          * saved because it ran out of space but we would need a file striped
453          * over least 123 OSTs before the two EAs filled a 4kB block.
454          *
455          * This can be removed when all filesystems have converted to the
456          * new EA format, but otherwise adds little if any overhead.  If we
457          * wanted backward compatibility for existing files, we could keep
458          * the old EA around for a while but we'd have to clean it up later. */
459         if (rc >= 0 && old_ea) {
460                 int err = ext3_xattr_set_handle(handle, inode,
461                                                 EXT3_XATTR_INDEX_LUSTRE,
462                                                 XATTR_LUSTRE_MDS_OBJID,
463                                                 NULL, 0, 0);
464                 if (err)
465                         CERROR("error deleting old LOV EA on %lu/%u: rc %d\n",
466                                inode->i_ino, inode->i_generation, err);
467         }
468 #endif
469         unlock_kernel();
470
471         if (rc)
472                 CERROR("error adding MD data to inode %lu: rc = %d\n",
473                        inode->i_ino, rc);
474         return rc;
475 }
476
477 /* Must be called with i_sem held */
478 static int fsfilt_ext3_get_md(struct inode *inode, void *lmm, int lmm_size)
479 {
480         int rc;
481
482         LASSERT(down_trylock(&inode->i_sem) != 0);
483         lock_kernel();
484         /* Keep support for reading "inline EAs" until we convert
485          * users over to new format entirely.  See bug 841/2097. */
486         if (inode->i_blocks == 0 && EXT3_I(inode)->i_data[0]) {
487                 unsigned size = le32_to_cpu(EXT3_I(inode)->i_data[0]);
488                 void *handle;
489
490                 LASSERT(size < sizeof(EXT3_I(inode)->i_data));
491                 if (lmm) {
492                         if (size > lmm_size) {
493                                 CERROR("inline EA on %lu/%u bad size %u > %u\n",
494                                        inode->i_ino, inode->i_generation,
495                                        size, lmm_size);
496                                 return -ERANGE;
497                         }
498                         memcpy(lmm, &EXT3_I(inode)->i_data[1], size);
499                 }
500
501 #ifndef INLINE_EA
502                 /* migrate LOV EA data to external block - keep same format */
503                 CWARN("DEBUG: migrate inline EA for inode %lu/%u to block\n",
504                       inode->i_ino, inode->i_generation);
505
506                 handle = journal_start(EXT3_JOURNAL(inode),
507                                        EXT3_XATTR_TRANS_BLOCKS);
508                 if (!IS_ERR(handle)) {
509                         int err;
510                         rc = fsfilt_ext3_set_md(inode, handle,
511                                                 &EXT3_I(inode)->i_data[1],size);
512                         if (rc == 0) {
513                                 memset(EXT3_I(inode)->i_data, 0,
514                                        sizeof(EXT3_I(inode)->i_data));
515                                 mark_inode_dirty(inode);
516                         }
517                         err = journal_stop(handle);
518                         if (err && rc == 0)
519                                 rc = err;
520                 } else {
521                         rc = PTR_ERR(handle);
522                 }
523 #endif
524                 unlock_kernel();
525                 return size;
526         }
527
528         rc = ext3_xattr_get(inode, EXT3_XATTR_INDEX_TRUSTED,
529                             XATTR_LUSTRE_MDS_LOV_EA, lmm, lmm_size);
530         /* try old EA type if new one failed - MDS will convert it for us */
531         if (rc == -ENODATA) {
532                 CDEBUG(D_INFO,"failed new LOV EA %d/%s from inode %lu: rc %d\n",
533                        EXT3_XATTR_INDEX_TRUSTED, XATTR_LUSTRE_MDS_LOV_EA,
534                        inode->i_ino, rc);
535
536                 rc = ext3_xattr_get(inode, EXT3_XATTR_INDEX_LUSTRE,
537                                     XATTR_LUSTRE_MDS_OBJID, lmm, lmm_size);
538         }
539         unlock_kernel();
540
541         /* This gives us the MD size */
542         if (lmm == NULL)
543                 return (rc == -ENODATA) ? 0 : rc;
544
545         if (rc < 0) {
546                 CDEBUG(D_INFO, "error getting EA %d/%s from inode %lu: rc %d\n",
547                        EXT3_XATTR_INDEX_LUSTRE, XATTR_LUSTRE_MDS_OBJID,
548                        inode->i_ino, rc);
549                 memset(lmm, 0, lmm_size);
550                 return (rc == -ENODATA) ? 0 : rc;
551         }
552
553         return rc;
554 }
555
556 static ssize_t fsfilt_ext3_readpage(struct file *file, char *buf, size_t count,
557                                     loff_t *off)
558 {
559         struct inode *inode = file->f_dentry->d_inode;
560         int rc = 0;
561
562         if (S_ISREG(inode->i_mode))
563                 rc = file->f_op->read(file, buf, count, off);
564         else {
565                 const int blkbits = inode->i_sb->s_blocksize_bits;
566                 const int blksize = inode->i_sb->s_blocksize;
567
568                 CDEBUG(D_EXT2, "reading "LPSZ" at dir %lu+%llu\n",
569                        count, inode->i_ino, *off);
570                 while (count > 0) {
571                         struct buffer_head *bh;
572
573                         bh = NULL;
574                         if (*off < inode->i_size) {
575                                 int err = 0;
576
577                                 bh = ext3_bread(NULL, inode, *off >> blkbits,
578                                                 0, &err);
579
580                                 CDEBUG(D_EXT2, "read %u@%llu\n", blksize, *off);
581
582                                 if (bh) {
583                                         memcpy(buf, bh->b_data, blksize);
584                                         brelse(bh);
585                                 } else if (err) {
586                                         /* XXX in theory we should just fake
587                                          * this buffer and continue like ext3,
588                                          * especially if this is a partial read
589                                          */
590                                         CERROR("error read dir %lu+%llu: %d\n",
591                                                inode->i_ino, *off, err);
592                                         RETURN(err);
593                                 }
594                         }
595                         if (!bh) {
596                                 struct ext3_dir_entry_2 *fake = (void *)buf;
597
598                                 CDEBUG(D_EXT2, "fake %u@%llu\n", blksize, *off);
599                                 memset(fake, 0, sizeof(*fake));
600                                 fake->rec_len = cpu_to_le32(blksize);
601                         }
602                         count -= blksize;
603                         buf += blksize;
604                         *off += blksize;
605                         rc += blksize;
606                 }
607         }
608
609         return rc;
610 }
611
612 static void fsfilt_ext3_cb_func(struct journal_callback *jcb, int error)
613 {
614         struct fsfilt_cb_data *fcb = (struct fsfilt_cb_data *)jcb;
615
616         fcb->cb_func(fcb->cb_obd, fcb->cb_last_rcvd, fcb->cb_data, error);
617
618         OBD_SLAB_FREE(fcb, fcb_cache, sizeof *fcb);
619         atomic_dec(&fcb_cache_count);
620 }
621
622 static int fsfilt_ext3_add_journal_cb(struct obd_device *obd, __u64 last_rcvd,
623                                       void *handle, fsfilt_cb_t cb_func,
624                                       void *cb_data)
625 {
626         struct fsfilt_cb_data *fcb;
627
628         OBD_SLAB_ALLOC(fcb, fcb_cache, GFP_NOFS, sizeof *fcb);
629         if (fcb == NULL)
630                 RETURN(-ENOMEM);
631
632         atomic_inc(&fcb_cache_count);
633         fcb->cb_func = cb_func;
634         fcb->cb_obd = obd;
635         fcb->cb_last_rcvd = last_rcvd;
636         fcb->cb_data = cb_data;
637
638         CDEBUG(D_EXT2, "set callback for last_rcvd: "LPD64"\n", last_rcvd);
639         lock_kernel();
640         journal_callback_set(handle, fsfilt_ext3_cb_func,
641                              (struct journal_callback *)fcb);
642         unlock_kernel();
643
644         return 0;
645 }
646
647 /*
648  * We need to hack the return value for the free inode counts because
649  * the current EA code requires one filesystem block per inode with EAs,
650  * so it is possible to run out of blocks before we run out of inodes.
651  *
652  * This can be removed when the ext3 EA code is fixed.
653  */
654 static int fsfilt_ext3_statfs(struct super_block *sb, struct obd_statfs *osfs)
655 {
656         struct kstatfs sfs;
657         int rc;
658
659         memset(&sfs, 0, sizeof(sfs));
660
661         rc = sb->s_op->statfs(sb, &sfs);
662
663         if (!rc && sfs.f_bfree < sfs.f_ffree) {
664                 sfs.f_files = (sfs.f_files - sfs.f_ffree) + sfs.f_bfree;
665                 sfs.f_ffree = sfs.f_bfree;
666         }
667
668         statfs_pack(osfs, &sfs);
669         return rc;
670 }
671
672 static int fsfilt_ext3_sync(struct super_block *sb)
673 {
674         return ext3_force_commit(sb);
675 }
676
677 extern int ext3_map_inode_page(struct inode *inode, struct page *page,
678                                unsigned long *blocks, int *created, int create);
679 int fsfilt_ext3_map_inode_page(struct inode *inode, struct page *page,
680                                unsigned long *blocks, int *created, int create)
681 {
682         return ext3_map_inode_page(inode, page, blocks, created, create);
683 }
684
685 extern int ext3_prep_san_write(struct inode *inode, long *blocks,
686                                int nblocks, loff_t newsize);
687 static int fsfilt_ext3_prep_san_write(struct inode *inode, long *blocks,
688                                       int nblocks, loff_t newsize)
689 {
690         return ext3_prep_san_write(inode, blocks, nblocks, newsize);
691 }
692
693 static int fsfilt_ext3_read_record(struct file * file, void *buf,
694                                    int size, loff_t *offs)
695 {
696         struct inode *inode = file->f_dentry->d_inode;
697         unsigned long block;
698         struct buffer_head *bh;
699         int err, blocksize, csize, boffs;
700
701         /* prevent reading after eof */
702         lock_kernel();
703         if (inode->i_size < *offs + size) {
704                 size = inode->i_size - *offs;
705                 unlock_kernel();
706                 if (size < 0) {
707                         CERROR("size %llu is too short for read %u@%llu\n",
708                                inode->i_size, size, *offs);
709                         return -EIO;
710                 } else if (size == 0) {
711                         return 0;
712                 }
713         } else {
714                 unlock_kernel();
715         }
716
717         blocksize = 1 << inode->i_blkbits;
718
719         while (size > 0) {
720                 block = *offs >> inode->i_blkbits;
721                 boffs = *offs & (blocksize - 1);
722                 csize = min(blocksize - boffs, size);
723                 bh = ext3_bread(NULL, inode, block, 0, &err);
724                 if (!bh) {
725                         CERROR("can't read block: %d\n", err);
726                         return err;
727                 }
728
729                 memcpy(buf, bh->b_data + boffs, csize);
730                 brelse(bh);
731
732                 *offs += csize;
733                 buf += csize;
734                 size -= csize;
735         }
736         return 0;
737 }
738
739 static int fsfilt_ext3_write_record(struct file *file, void *buf, int bufsize,
740                                     loff_t *offs, int force_sync)
741 {
742         struct buffer_head *bh = NULL;
743         unsigned long block;
744         struct inode *inode = file->f_dentry->d_inode;
745         loff_t old_size = inode->i_size, offset = *offs;
746         loff_t new_size = inode->i_size;
747         journal_t *journal;
748         handle_t *handle;
749         int err, block_count = 0, blocksize, size, boffs;
750
751         /* Determine how many transaction credits are needed */
752         blocksize = 1 << inode->i_blkbits;
753         block_count = (*offs & (blocksize - 1)) + bufsize;
754         block_count = (block_count + blocksize - 1) >> inode->i_blkbits;
755
756         journal = EXT3_SB(inode->i_sb)->s_journal;
757         lock_kernel();
758         handle = journal_start(journal,
759                                block_count * EXT3_DATA_TRANS_BLOCKS + 2);
760         unlock_kernel();
761         if (IS_ERR(handle)) {
762                 CERROR("can't start transaction\n");
763                 return PTR_ERR(handle);
764         }
765
766         while (bufsize > 0) {
767                 if (bh != NULL)
768                         brelse(bh);
769
770                 block = offset >> inode->i_blkbits;
771                 boffs = offset & (blocksize - 1);
772                 size = min(blocksize - boffs, bufsize);
773                 bh = ext3_bread(handle, inode, block, 1, &err);
774                 if (!bh) {
775                         CERROR("can't read/create block: %d\n", err);
776                         goto out;
777                 }
778
779                 err = ext3_journal_get_write_access(handle, bh);
780                 if (err) {
781                         CERROR("journal_get_write_access() returned error %d\n",
782                                err);
783                         goto out;
784                 }
785                 LASSERT(bh->b_data + boffs + size <= bh->b_data + bh->b_size);
786                 memcpy(bh->b_data + boffs, buf, size);
787                 err = ext3_journal_dirty_metadata(handle, bh);
788                 if (err) {
789                         CERROR("journal_dirty_metadata() returned error %d\n",
790                                err);
791                         goto out;
792                 }
793                 if (offset + size > new_size)
794                         new_size = offset + size;
795                 offset += size;
796                 bufsize -= size;
797                 buf += size;
798         }
799
800         if (force_sync)
801                 handle->h_sync = 1; /* recovery likes this */
802 out:
803         if (bh)
804                 brelse(bh);
805
806         /* correct in-core and on-disk sizes */
807         if (new_size > inode->i_size) {
808                 lock_kernel();
809                 if (new_size > inode->i_size)
810                         inode->i_size = new_size;
811                 if (inode->i_size > EXT3_I(inode)->i_disksize)
812                         EXT3_I(inode)->i_disksize = inode->i_size;
813                 if (inode->i_size > old_size)
814                         mark_inode_dirty(inode);
815                 unlock_kernel();
816         }
817
818         lock_kernel();
819         journal_stop(handle);
820         unlock_kernel();
821
822         if (err == 0)
823                 *offs = offset;
824         return err;
825 }
826
827 static int fsfilt_ext3_setup(struct super_block *sb)
828 {
829 #if 0
830         EXT3_SB(sb)->dx_lock = fsfilt_ext3_dx_lock;
831         EXT3_SB(sb)->dx_unlock = fsfilt_ext3_dx_unlock;
832 #endif
833 #ifdef S_PDIROPS
834         CWARN("Enabling PDIROPS\n");
835         set_opt(EXT3_SB(sb)->s_mount_opt, PDIROPS);
836         sb->s_flags |= S_PDIROPS;
837 #endif
838         return 0;
839 }
840
841 /* If fso is NULL, op is FSFILT operation, otherwise op is number of fso
842    objects. Logs is number of logfiles to update */
843 static int fsfilt_ext3_get_op_len(int op, struct fsfilt_objinfo *fso, int logs)
844 {
845         if ( !fso ) {
846                 switch(op) {
847                 case FSFILT_OP_CREATE:
848                                  /* directory leaf, index & indirect & EA*/
849                         return 4 + 3 * logs;
850                 case FSFILT_OP_UNLINK:
851                         return 3 * logs;
852                 }
853
854         } else {
855                 int i;
856                 int needed = 0;
857                 struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
858                 int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
859                 int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
860                 for (i = 0; i < op; i++, fso++) {
861                         int nblocks = fso->fso_bufcnt * blockpp;
862                         int ndindirect = min(nblocks, addrpp + 1);
863                         int nindir = nblocks + ndindirect + 1;
864
865                         needed += nindir;
866                 }
867                 return needed + 3 * logs;
868         }
869
870         return 0;
871 }
872
873 static struct fsfilt_operations fsfilt_ext3_ops = {
874         fs_type:                "ext3",
875         fs_owner:               THIS_MODULE,
876         fs_start:               fsfilt_ext3_start,
877         fs_brw_start:           fsfilt_ext3_brw_start,
878         fs_commit:              fsfilt_ext3_commit,
879         fs_commit_async:        fsfilt_ext3_commit_async,
880         fs_commit_wait:         fsfilt_ext3_commit_wait,
881         fs_setattr:             fsfilt_ext3_setattr,
882         fs_iocontrol:           fsfilt_ext3_iocontrol,
883         fs_set_md:              fsfilt_ext3_set_md,
884         fs_get_md:              fsfilt_ext3_get_md,
885         fs_readpage:            fsfilt_ext3_readpage,
886         fs_add_journal_cb:      fsfilt_ext3_add_journal_cb,
887         fs_statfs:              fsfilt_ext3_statfs,
888         fs_sync:                fsfilt_ext3_sync,
889         fs_map_inode_page:      fsfilt_ext3_map_inode_page,
890         fs_prep_san_write:      fsfilt_ext3_prep_san_write,
891         fs_write_record:        fsfilt_ext3_write_record,
892         fs_read_record:         fsfilt_ext3_read_record,
893         fs_setup:               fsfilt_ext3_setup,
894         fs_get_op_len:          fsfilt_ext3_get_op_len,
895 };
896
897 static int __init fsfilt_ext3_init(void)
898 {
899         int rc;
900
901         //rc = ext3_xattr_register();
902         fcb_cache = kmem_cache_create("fsfilt_ext3_fcb",
903                                       sizeof(struct fsfilt_cb_data), 0,
904                                       0, NULL, NULL);
905         if (!fcb_cache) {
906                 CERROR("error allocating fsfilt journal callback cache\n");
907                 GOTO(out, rc = -ENOMEM);
908         }
909
910         rc = fsfilt_register_ops(&fsfilt_ext3_ops);
911
912         if (rc)
913                 kmem_cache_destroy(fcb_cache);
914 out:
915         return rc;
916 }
917
918 static void __exit fsfilt_ext3_exit(void)
919 {
920         int rc;
921
922         fsfilt_unregister_ops(&fsfilt_ext3_ops);
923         rc = kmem_cache_destroy(fcb_cache);
924
925         if (rc || atomic_read(&fcb_cache_count)) {
926                 CERROR("can't free fsfilt callback cache: count %d, rc = %d\n",
927                        atomic_read(&fcb_cache_count), rc);
928         }
929
930         //rc = ext3_xattr_unregister();
931 }
932
933 module_init(fsfilt_ext3_init);
934 module_exit(fsfilt_ext3_exit);
935
936 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
937 MODULE_DESCRIPTION("Lustre ext3 Filesystem Helper v0.1");
938 MODULE_LICENSE("GPL");