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