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