Whamcloud - gitweb
LU-84 Allow building against kernel devel headers with quotas
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/lvfs/fsfilt_ext3.c
37  *
38  * Author: Andreas Dilger <adilger@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_FILTER
42
43 #include <linux/init.h>
44 #include <linux/module.h>
45 #include <linux/fs.h>
46 #include <linux/slab.h>
47 #include <linux/pagemap.h>
48 #include <linux/quotaops.h>
49 #ifdef HAVE_LINUX_EXPORTFS_H
50 #include <linux/exportfs.h>
51 #endif
52 #ifdef HAVE_EXT4_LDISKFS
53 #include <ext4/ext4.h>
54 #include <ext4/ext4_jbd2.h>
55 #else
56 #include <linux/jbd.h>
57 #include <linux/ext3_fs.h>
58 #include <linux/ext3_jbd.h>
59 #endif
60 #include <linux/version.h>
61 #include <linux/bitops.h>
62 #include <linux/quota.h>
63 #ifdef HAVE_QUOTAIO_V1_H
64 # include <linux/quotaio_v1.h>
65 # include <linux/quotaio_v2.h>
66 #elif defined(HAVE_FS_QUOTA_QUOTAIO_V1_H)
67 # include <quota/quotaio_v1.h>
68 # include <quota/quotaio_v2.h>
69 # include <quota/quota_tree.h>
70 # define V2_DQTREEOFF    QT_TREEOFF
71 #elif defined(HAVE_FS_QUOTAIO_V1_H)
72 # include <quotaio_v1.h>
73 # include <quotaio_v2.h>
74 # include <quota_tree.h>
75 # define V2_DQTREEOFF    QT_TREEOFF
76 #endif
77
78 #ifdef QFMT_VFS_V1
79 #define QFMT_LUSTRE QFMT_VFS_V1
80 #else
81 #define QFMT_LUSTRE QFMT_VFS_V0
82 #endif
83
84 #if defined(HAVE_EXT3_XATTR_H)
85 #include <ext3/xattr.h>
86 #else
87 /* ext3 xattr.h not available in rh style kernel-devel rpm */
88 extern int ext3_xattr_get(struct inode *, int, const char *, void *, size_t);
89 extern int ext3_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
90 #endif
91
92 #include <libcfs/libcfs.h>
93 #include <lustre_fsfilt.h>
94 #include <obd.h>
95 #include <lustre_quota.h>
96 #include <linux/lustre_compat25.h>
97 #include <linux/lprocfs_status.h>
98
99 #ifdef HAVE_EXT4_LDISKFS
100 #include <ext4/ext4_extents.h>
101 #else
102 #include <linux/ext3_extents.h>
103 #endif
104
105 #include "lustre_quota_fmt.h"
106
107 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
108 #define FSFILT_DATA_TRANS_BLOCKS(sb)      EXT3_DATA_TRANS_BLOCKS
109 #define FSFILT_DELETE_TRANS_BLOCKS(sb)    EXT3_DELETE_TRANS_BLOCKS
110 #else
111 #define FSFILT_DATA_TRANS_BLOCKS(sb)      EXT3_DATA_TRANS_BLOCKS(sb)
112 #define FSFILT_DELETE_TRANS_BLOCKS(sb)    EXT3_DELETE_TRANS_BLOCKS(sb)
113 #endif
114
115 #ifdef EXT3_SINGLEDATA_TRANS_BLOCKS_HAS_SB
116 /* for kernels 2.6.18 and later */
117 #define FSFILT_SINGLEDATA_TRANS_BLOCKS(sb) EXT3_SINGLEDATA_TRANS_BLOCKS(sb)
118 #else
119 #define FSFILT_SINGLEDATA_TRANS_BLOCKS(sb) EXT3_SINGLEDATA_TRANS_BLOCKS
120 #endif
121
122 #ifdef EXT_INSERT_EXTENT_WITH_5ARGS
123 #define fsfilt_ext3_ext_insert_extent(handle, inode, path, newext, flag) \
124                ext3_ext_insert_extent(handle, inode, path, newext, flag)
125 #else
126 #define fsfilt_ext3_ext_insert_extent(handle, inode, path, newext, flag) \
127                ext3_ext_insert_extent(handle, inode, path, newext)
128 #endif
129
130 #ifdef EXT3_DISCARD_PREALLOCATIONS
131 #define ext3_mb_discard_inode_preallocations(inode) \
132                  ext3_discard_preallocations(inode)
133 #endif
134
135
136 static cfs_mem_cache_t *fcb_cache;
137
138 struct fsfilt_cb_data {
139         struct journal_callback cb_jcb; /* jbd private data - MUST BE FIRST */
140         fsfilt_cb_t cb_func;            /* MDS/OBD completion function */
141         struct obd_device *cb_obd;      /* MDS/OBD completion device */
142         __u64 cb_last_rcvd;             /* MDS/OST last committed operation */
143         void *cb_data;                  /* MDS/OST completion function data */
144 };
145
146 #ifndef EXT3_XATTR_INDEX_TRUSTED        /* temporary until we hit l28 kernel */
147 #define EXT3_XATTR_INDEX_TRUSTED        4
148 #endif
149
150 #ifdef HAVE_EXT4_LDISKFS
151 #define fsfilt_log_start_commit(journal, tid) jbd2_log_start_commit(journal, tid)
152 #define fsfilt_log_wait_commit(journal, tid) jbd2_log_wait_commit(journal, tid)
153 #define fsfilt_journal_callback_set(handle, func, jcb) jbd2_journal_callback_set(handle, func, jcb)
154 #else
155 #define fsfilt_log_start_commit(journal, tid) log_start_commit(journal, tid)
156 #define fsfilt_log_wait_commit(journal, tid) log_wait_commit(journal, tid)
157 #define fsfilt_journal_callback_set(handle, func, jcb) journal_callback_set(handle, func, jcb)
158 #define ext_pblock(ex) le32_to_cpu((ex)->ee_start)
159 #define ext3_ext_store_pblock(ex, pblock)  ((ex)->ee_start = cpu_to_le32(pblock))
160 #define ext3_inode_bitmap(sb,desc) le32_to_cpu((desc)->bg_inode_bitmap)
161 #endif
162
163 #ifndef ext3_find_next_bit
164 #define ext3_find_next_bit           ext2_find_next_bit
165 #endif
166
167 #ifndef ext2_find_next_bit
168 #ifdef __LITTLE_ENDIAN
169 #define ext2_find_next_bit(addr, size, off) find_next_bit((unsigned long *)(addr), (size), (off))
170 #else
171 error "Need implementation of find_next_bit on big-endian systems"
172 #endif  /* __LITTLE_ENDIAN */
173 #endif  /* !ext2_find_next_le_bit */
174
175 static char *fsfilt_ext3_get_label(struct super_block *sb)
176 {
177         return EXT3_SB(sb)->s_es->s_volume_name;
178 }
179
180 static int fsfilt_ext3_set_label(struct super_block *sb, char *label)
181 {
182         /* see e.g. fsfilt_ext3_write_record() */
183         journal_t *journal;
184         handle_t *handle;
185         int err;
186
187         journal = EXT3_SB(sb)->s_journal;
188         handle = ext3_journal_start_sb(sb, 1);
189         if (IS_ERR(handle)) {
190                 CERROR("can't start transaction\n");
191                 return(PTR_ERR(handle));
192         }
193
194         err = ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
195         if (err)
196                 goto out;
197
198         memcpy(EXT3_SB(sb)->s_es->s_volume_name, label,
199                sizeof(EXT3_SB(sb)->s_es->s_volume_name));
200
201         err = ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
202
203 out:
204         ext3_journal_stop(handle);
205
206         return(err);
207 }
208
209 static char *fsfilt_ext3_uuid(struct super_block *sb)
210 {
211         return EXT3_SB(sb)->s_es->s_uuid;
212 }
213
214 #ifdef HAVE_DISK_INODE_VERSION
215
216 static __u64 get_i_version(struct inode *inode)
217 {
218 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)) && defined(HAVE_EXT4_LDISKFS)
219         return inode->i_version;
220 #else
221         return EXT3_I(inode)->i_fs_version;
222 #endif
223 }
224
225 static void set_i_version(struct inode *inode, __u64 new_version)
226 {
227 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)) && defined(HAVE_EXT4_LDISKFS)
228         inode->i_version = new_version;
229 #else
230         (EXT3_I(inode))->i_fs_version = new_version;
231 #endif
232 }
233
234 /*
235  * Get the 64-bit version for an inode.
236  */
237 static __u64 fsfilt_ext3_get_version(struct inode *inode)
238 {
239         CDEBUG(D_INFO, "Get version "LPX64" for inode %lu\n",
240                get_i_version(inode), inode->i_ino);
241         return get_i_version(inode);
242 }
243
244 /*
245  * Set the 64-bit version and return the old version.
246  */
247 static __u64 fsfilt_ext3_set_version(struct inode *inode, __u64 new_version)
248 {
249         __u64 old_version = get_i_version(inode);
250
251         CDEBUG(D_INFO, "Set version "LPX64" (old "LPX64") for inode %lu\n",
252                new_version, old_version, inode->i_ino);
253         set_i_version(inode, new_version);
254         /* version is set after all inode operations are finished, so we should
255          * mark it dirty here */
256         inode->i_sb->s_op->dirty_inode(inode);
257         return old_version;
258 }
259
260 #endif
261
262 /*
263  * We don't currently need any additional blocks for rmdir and
264  * unlink transactions because we are storing the OST oa_id inside
265  * the inode (which we will be changing anyways as part of this
266  * transaction).
267  */
268 static void *fsfilt_ext3_start(struct inode *inode, int op, void *desc_private,
269                                int logs)
270 {
271         /* For updates to the last received file */
272         int nblocks = FSFILT_SINGLEDATA_TRANS_BLOCKS(inode->i_sb);
273         journal_t *journal;
274         void *handle;
275
276         if (current->journal_info) {
277                 CDEBUG(D_INODE, "increasing refcount on %p\n",
278                        current->journal_info);
279                 goto journal_start;
280         }
281
282         switch(op) {
283         case FSFILT_OP_RMDIR:
284         case FSFILT_OP_UNLINK:
285                 /* delete one file + create/update logs for each stripe */
286                 nblocks += FSFILT_DELETE_TRANS_BLOCKS(inode->i_sb);
287                 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
288                             FSFILT_SINGLEDATA_TRANS_BLOCKS(inode->i_sb)) * logs;
289                 break;
290         case FSFILT_OP_RENAME:
291                 /* modify additional directory */
292                 nblocks += FSFILT_SINGLEDATA_TRANS_BLOCKS(inode->i_sb);
293                 /* no break */
294         case FSFILT_OP_SYMLINK:
295                 /* additional block + block bitmap + GDT for long symlink */
296                 nblocks += 3;
297                 /* no break */
298         case FSFILT_OP_CREATE: {
299 #if defined(EXT3_EXTENTS_FL) && defined(EXT3_INDEX_FL) && !defined(HAVE_EXT4_LDISKFS)
300                 static int warned;
301                 if (!warned) {
302                         if (!test_opt(inode->i_sb, EXTENTS)) {
303                                 warned = 1;
304                         } else if (((EXT3_I(inode)->i_flags &
305                               cpu_to_le32(EXT3_EXTENTS_FL | EXT3_INDEX_FL)) ==
306                               cpu_to_le32(EXT3_EXTENTS_FL | EXT3_INDEX_FL))) {
307                                 CWARN("extent-mapped directory found with "
308                                       "ext3-based ldiskfs - contact "
309                                       "http://bugzilla.lustre.org/\n");
310                                 warned = 1;
311                         }
312                 }
313 #endif
314                 /* no break */
315         }
316         case FSFILT_OP_MKDIR:
317         case FSFILT_OP_MKNOD:
318                 /* modify one inode + block bitmap + GDT */
319                 nblocks += 3;
320                 /* no break */
321         case FSFILT_OP_LINK:
322                 /* modify parent directory */
323                 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS +
324                          FSFILT_DATA_TRANS_BLOCKS(inode->i_sb);
325                 /* create/update logs for each stripe */
326                 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
327                             FSFILT_SINGLEDATA_TRANS_BLOCKS(inode->i_sb)) * logs;
328                 break;
329         case FSFILT_OP_SETATTR:
330                 /* Setattr on inode */
331                 nblocks += 1;
332                 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS +
333                          FSFILT_DATA_TRANS_BLOCKS(inode->i_sb);
334                 /* quota chown log for each stripe */
335                 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
336                             FSFILT_SINGLEDATA_TRANS_BLOCKS(inode->i_sb)) * logs;
337                 break;
338         case FSFILT_OP_CANCEL_UNLINK:
339                 /* blocks for log header bitmap update OR
340                  * blocks for catalog header bitmap update + unlink of logs */
341                 nblocks = (LLOG_CHUNK_SIZE >> inode->i_blkbits) +
342                         FSFILT_DELETE_TRANS_BLOCKS(inode->i_sb) * logs;
343                 break;
344         default: CERROR("unknown transaction start op %d\n", op);
345                 LBUG();
346         }
347
348         LASSERT(current->journal_info == desc_private);
349         journal = EXT3_SB(inode->i_sb)->s_journal;
350         if (nblocks > journal->j_max_transaction_buffers) {
351                 CWARN("too many credits %d for op %ux%u using %d instead\n",
352                        nblocks, op, logs, journal->j_max_transaction_buffers);
353                 nblocks = journal->j_max_transaction_buffers;
354         }
355
356  journal_start:
357         LASSERTF(nblocks > 0, "can't start %d credit transaction\n", nblocks);
358         handle = ext3_journal_start(inode, nblocks);
359
360         if (!IS_ERR(handle))
361                 LASSERT(current->journal_info == handle);
362         else
363                 CERROR("error starting handle for op %u (%u credits): rc %ld\n",
364                        op, nblocks, PTR_ERR(handle));
365         return handle;
366 }
367
368 /*
369  * Calculate the number of buffer credits needed to write multiple pages in
370  * a single ext3 transaction.  No, this shouldn't be here, but as yet ext3
371  * doesn't have a nice API for calculating this sort of thing in advance.
372  *
373  * See comment above ext3_writepage_trans_blocks for details.  We assume
374  * no data journaling is being done, but it does allow for all of the pages
375  * being non-contiguous.  If we are guaranteed contiguous pages we could
376  * reduce the number of (d)indirect blocks a lot.
377  *
378  * With N blocks per page and P pages, for each inode we have at most:
379  * N*P indirect
380  * min(N*P, blocksize/4 + 1) dindirect blocks
381  * niocount tindirect
382  *
383  * For the entire filesystem, we have at most:
384  * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
385  * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
386  * objcount inode blocks
387  * 1 superblock
388  * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
389  *
390  * 1 EXT3_DATA_TRANS_BLOCKS for the last_rcvd update.
391  */
392 static int fsfilt_ext3_credits_needed(int objcount, struct fsfilt_objinfo *fso,
393                                       int niocount, struct niobuf_local *nb)
394 {
395         struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
396         __u64 next_indir;
397         const int blockpp = 1 << (CFS_PAGE_SHIFT - sb->s_blocksize_bits);
398         int nbitmaps = 0, ngdblocks;
399         int needed = objcount + 1; /* inodes + superblock */
400         int i, j;
401
402         for (i = 0, j = 0; i < objcount; i++, fso++) {
403                 /* two or more dindirect blocks in case we cross boundary */
404                 int ndind = (long)((nb[j + fso->fso_bufcnt - 1].offset -
405                                     nb[j].offset) >>
406                                    sb->s_blocksize_bits) /
407                         (EXT3_ADDR_PER_BLOCK(sb) * EXT3_ADDR_PER_BLOCK(sb));
408                 nbitmaps += min(fso->fso_bufcnt, ndind > 0 ? ndind : 2);
409
410                 /* leaf, indirect, tindirect blocks for first block */
411                 nbitmaps += blockpp + 2;
412
413                 j += fso->fso_bufcnt;
414         }
415
416         next_indir = nb[0].offset +
417                 (EXT3_ADDR_PER_BLOCK(sb) << sb->s_blocksize_bits);
418         for (i = 1; i < niocount; i++) {
419                 if (nb[i].offset >= next_indir) {
420                         nbitmaps++;     /* additional indirect */
421                         next_indir = nb[i].offset +
422                                 (EXT3_ADDR_PER_BLOCK(sb)<<sb->s_blocksize_bits);
423                 } else if (nb[i].offset != nb[i - 1].offset + sb->s_blocksize) {
424                         nbitmaps++;     /* additional indirect */
425                 }
426                 nbitmaps += blockpp;    /* each leaf in different group? */
427         }
428
429         ngdblocks = nbitmaps;
430         if (nbitmaps > EXT3_SB(sb)->s_groups_count)
431                 nbitmaps = EXT3_SB(sb)->s_groups_count;
432         if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
433                 ngdblocks = EXT3_SB(sb)->s_gdb_count;
434
435         needed += nbitmaps + ngdblocks;
436
437         /* last_rcvd update */
438         needed += FSFILT_DATA_TRANS_BLOCKS(sb);
439
440 #if defined(CONFIG_QUOTA)
441         /* We assume that there will be 1 bit set in s_dquot.flags for each
442          * quota file that is active.  This is at least true for now.
443          */
444         needed += hweight32(ll_sb_any_quota_active(sb)) *
445                 FSFILT_SINGLEDATA_TRANS_BLOCKS(sb);
446 #endif
447
448         return needed;
449 }
450
451 /* We have to start a huge journal transaction here to hold all of the
452  * metadata for the pages being written here.  This is necessitated by
453  * the fact that we do lots of prepare_write operations before we do
454  * any of the matching commit_write operations, so even if we split
455  * up to use "smaller" transactions none of them could complete until
456  * all of them were opened.  By having a single journal transaction,
457  * we eliminate duplicate reservations for common blocks like the
458  * superblock and group descriptors or bitmaps.
459  *
460  * We will start the transaction here, but each prepare_write will
461  * add a refcount to the transaction, and each commit_write will
462  * remove a refcount.  The transaction will be closed when all of
463  * the pages have been written.
464  */
465 static void *fsfilt_ext3_brw_start(int objcount, struct fsfilt_objinfo *fso,
466                                    int niocount, struct niobuf_local *nb,
467                                    void *desc_private, int logs)
468 {
469         journal_t *journal;
470         handle_t *handle;
471         int needed;
472         ENTRY;
473
474         LASSERT(current->journal_info == desc_private);
475         journal = EXT3_SB(fso->fso_dentry->d_inode->i_sb)->s_journal;
476         needed = fsfilt_ext3_credits_needed(objcount, fso, niocount, nb);
477
478         /* The number of blocks we could _possibly_ dirty can very large.
479          * We reduce our request if it is absurd (and we couldn't get that
480          * many credits for a single handle anyways).
481          *
482          * At some point we have to limit the size of I/Os sent at one time,
483          * increase the size of the journal, or we have to calculate the
484          * actual journal requirements more carefully by checking all of
485          * the blocks instead of being maximally pessimistic.  It remains to
486          * be seen if this is a real problem or not.
487          */
488         if (needed > journal->j_max_transaction_buffers) {
489                 CERROR("want too many journal credits (%d) using %d instead\n",
490                        needed, journal->j_max_transaction_buffers);
491                 needed = journal->j_max_transaction_buffers;
492         }
493
494         LASSERTF(needed > 0, "can't start %d credit transaction\n", needed);
495         handle = ext3_journal_start(fso->fso_dentry->d_inode, needed);
496         if (IS_ERR(handle)) {
497                 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
498                        PTR_ERR(handle));
499         } else {
500                 LASSERT(handle->h_buffer_credits >= needed);
501                 LASSERT(current->journal_info == handle);
502         }
503
504         RETURN(handle);
505 }
506
507 static int fsfilt_ext3_extend(struct inode *inode, unsigned int nblocks,void *h)
508 {
509        handle_t *handle = h;
510
511        /* fsfilt_extend called with nblocks = 0 for testing in special cases */
512        if (nblocks == 0) {
513                handle->h_buffer_credits = 0;
514                CWARN("setting credits of handle %p to zero by request\n", h);
515        }
516
517        if (handle->h_buffer_credits > nblocks)
518                 return 0;
519        if (ext3_journal_extend(handle, nblocks) == 0)
520                 return 0;
521
522        ext3_mark_inode_dirty(handle, inode);
523        return ext3_journal_restart(handle, nblocks);
524 }
525
526 static int fsfilt_ext3_commit(struct inode *inode, void *h, int force_sync)
527 {
528         int rc;
529         handle_t *handle = h;
530
531         LASSERT(current->journal_info == handle);
532         if (force_sync)
533                 handle->h_sync = 1; /* recovery likes this */
534
535         rc = ext3_journal_stop(handle);
536
537         return rc;
538 }
539
540 static int fsfilt_ext3_commit_async(struct inode *inode, void *h,
541                                     void **wait_handle)
542 {
543         unsigned long tid;
544         transaction_t *transaction;
545         handle_t *handle = h;
546         journal_t *journal;
547         int rc;
548
549         LASSERT(current->journal_info == handle);
550
551         transaction = handle->h_transaction;
552         journal = transaction->t_journal;
553         tid = transaction->t_tid;
554         /* we don't want to be blocked */
555         handle->h_sync = 0;
556         rc = ext3_journal_stop(handle);
557         if (rc) {
558                 CERROR("error while stopping transaction: %d\n", rc);
559                 return rc;
560         }
561         fsfilt_log_start_commit(journal, tid);
562
563         *wait_handle = (void *) tid;
564         CDEBUG(D_INODE, "commit async: %lu\n", (unsigned long) tid);
565         return 0;
566 }
567
568 static int fsfilt_ext3_commit_wait(struct inode *inode, void *h)
569 {
570         journal_t *journal = EXT3_JOURNAL(inode);
571         tid_t tid = (tid_t)(long)h;
572
573         CDEBUG(D_INODE, "commit wait: %lu\n", (unsigned long) tid);
574         if (unlikely(is_journal_aborted(journal)))
575                 return -EIO;
576
577         fsfilt_log_wait_commit(EXT3_JOURNAL(inode), tid);
578
579         if (unlikely(is_journal_aborted(journal)))
580                 return -EIO;
581         return 0;
582 }
583
584 static int fsfilt_ext3_setattr(struct dentry *dentry, void *handle,
585                                struct iattr *iattr, int do_trunc)
586 {
587         struct inode *inode = dentry->d_inode;
588         int rc = 0;
589
590         /* Avoid marking the inode dirty on the superblock list unnecessarily.
591          * We are already writing the inode to disk as part of this
592          * transaction and want to avoid a lot of extra inode writeout
593          * later on. b=9828 */
594         if (iattr->ia_valid & ATTR_SIZE && !do_trunc) {
595                 /* ATTR_SIZE would invoke truncate: clear it */
596                 iattr->ia_valid &= ~ATTR_SIZE;
597                 EXT3_I(inode)->i_disksize = iattr->ia_size;
598                 i_size_write(inode, iattr->ia_size);
599
600                 if (iattr->ia_valid & ATTR_UID)
601                         inode->i_uid = iattr->ia_uid;
602                 if (iattr->ia_valid & ATTR_GID)
603                         inode->i_gid = iattr->ia_gid;
604                 if (iattr->ia_valid & ATTR_ATIME)
605                         inode->i_atime = iattr->ia_atime;
606                 if (iattr->ia_valid & ATTR_MTIME)
607                         inode->i_mtime = iattr->ia_mtime;
608                 if (iattr->ia_valid & ATTR_CTIME)
609                         inode->i_ctime = iattr->ia_ctime;
610                 if (iattr->ia_valid & ATTR_MODE) {
611                         inode->i_mode = iattr->ia_mode;
612
613                         if (!cfs_curproc_is_in_groups(inode->i_gid) &&
614                             !cfs_capable(CFS_CAP_FSETID))
615                                 inode->i_mode &= ~S_ISGID;
616                 }
617
618                 inode->i_sb->s_op->dirty_inode(inode);
619
620                 goto out;
621         }
622
623         /* Don't allow setattr to change file type */
624         if (iattr->ia_valid & ATTR_MODE)
625                 iattr->ia_mode = (inode->i_mode & S_IFMT) |
626                                  (iattr->ia_mode & ~S_IFMT);
627
628         /* We set these flags on the client, but have already checked perms
629          * so don't confuse inode_change_ok. */
630         iattr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET);
631
632         if (inode->i_op->setattr) {
633                 rc = inode->i_op->setattr(dentry, iattr);
634         } else {
635                 rc = inode_change_ok(inode, iattr);
636                 if (!rc)
637                         rc = inode_setattr(inode, iattr);
638         }
639
640  out:
641         RETURN(rc);
642 }
643
644 static int fsfilt_ext3_iocontrol(struct inode *inode, struct file *file,
645                                  unsigned int cmd, unsigned long arg)
646 {
647         int rc = 0;
648         ENTRY;
649
650         /* FIXME: Can't do this because of nested transaction deadlock */
651         if (cmd == EXT3_IOC_SETFLAGS) {
652                 /* We can't enable data journaling on OST objects, because
653                 * this forces the transaction to be closed in order to
654                 * flush the journal, but the caller will already have a
655                 * compound transaction open to update the last_rcvd file,
656                 * and this thread would deadlock trying to set the flag. */
657                 if ((*(int *)arg) & EXT3_JOURNAL_DATA_FL) {
658                         CERROR("can't set data journal flag on file\n");
659                         RETURN(-EPERM);
660                 }
661                 /* Because the MDS does not see the EXTENTS_FL set on the
662                  * OST objects, mask this flag into all set flags.  It is
663                  * not legal to clear this flag in any case, so we are not
664                  * changing the functionality by doing this.  b=22911 */
665                 *(int *)arg |= EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL;
666         }
667
668 #ifdef HAVE_EXT4_LDISKFS
669         /* ext4_ioctl does not have a inode argument */
670         if (inode->i_fop->unlocked_ioctl)
671                 rc = inode->i_fop->unlocked_ioctl(file, cmd, arg);
672 #else
673         if (inode->i_fop->ioctl)
674                 rc = inode->i_fop->ioctl(inode, file, cmd, arg);
675 #endif
676         else
677                 RETURN(-ENOTTY);
678
679         RETURN(rc);
680 }
681
682 static int fsfilt_ext3_set_md(struct inode *inode, void *handle,
683                               void *lmm, int lmm_size, const char *name)
684 {
685         int rc;
686
687         LASSERT(TRYLOCK_INODE_MUTEX(inode) == 0);
688
689         rc = ext3_xattr_set_handle(handle, inode, EXT3_XATTR_INDEX_TRUSTED,
690                                    name, lmm, lmm_size, XATTR_NO_CTIME);
691
692
693         if (rc && rc != -EROFS)
694                 CERROR("error adding MD data to inode %lu: rc = %d\n",
695                        inode->i_ino, rc);
696         return rc;
697 }
698
699 /* Must be called with i_mutex held */
700 static int fsfilt_ext3_get_md(struct inode *inode, void *lmm, int lmm_size,
701                               const char *name)
702 {
703         int rc;
704
705         LASSERT(TRYLOCK_INODE_MUTEX(inode) == 0);
706
707         rc = ext3_xattr_get(inode, EXT3_XATTR_INDEX_TRUSTED,
708                             name, lmm, lmm_size);
709
710         /* This gives us the MD size */
711         if (lmm == NULL)
712                 return (rc == -ENODATA) ? 0 : rc;
713
714         if (rc < 0) {
715                 CDEBUG(D_INFO, "error getting EA %d/%s from inode %lu: rc %d\n",
716                        EXT3_XATTR_INDEX_TRUSTED, name,
717                        inode->i_ino, rc);
718                 memset(lmm, 0, lmm_size);
719                 return (rc == -ENODATA) ? 0 : rc;
720         }
721
722         return rc;
723 }
724
725 static int fsfilt_ext3_send_bio(int rw, struct inode *inode, struct bio *bio)
726 {
727         submit_bio(rw, bio);
728         return 0;
729 }
730
731 static ssize_t fsfilt_ext3_readpage(struct file *file, char *buf, size_t count,
732                                     loff_t *off)
733 {
734         struct inode *inode = file->f_dentry->d_inode;
735         int rc = 0;
736
737         if (S_ISREG(inode->i_mode))
738                 rc = file->f_op->read(file, buf, count, off);
739         else {
740                 const int blkbits = inode->i_sb->s_blocksize_bits;
741                 const int blksize = inode->i_sb->s_blocksize;
742
743                 CDEBUG(D_EXT2, "reading %lu at dir %lu+%llu\n",
744                        (unsigned long)count, inode->i_ino, *off);
745                 while (count > 0) {
746                         struct buffer_head *bh;
747
748                         bh = NULL;
749                         if (*off < i_size_read(inode)) {
750                                 int err = 0;
751
752                                 bh = ext3_bread(NULL, inode, *off >> blkbits,
753                                                 0, &err);
754
755                                 CDEBUG(D_EXT2, "read %u@%llu\n", blksize, *off);
756
757                                 if (bh) {
758                                         memcpy(buf, bh->b_data, blksize);
759                                         brelse(bh);
760                                 } else if (err) {
761                                         /* XXX in theory we should just fake
762                                          * this buffer and continue like ext3,
763                                          * especially if this is a partial read
764                                          */
765                                         CERROR("error read dir %lu+%llu: %d\n",
766                                                inode->i_ino, *off, err);
767                                         RETURN(err);
768                                 }
769                         }
770                         if (!bh) {
771                                 struct ext3_dir_entry_2 *fake = (void *)buf;
772
773                                 CDEBUG(D_EXT2, "fake %u@%llu\n", blksize, *off);
774                                 memset(fake, 0, sizeof(*fake));
775                                 fake->rec_len = cpu_to_le16(blksize);
776                         }
777                         count -= blksize;
778                         buf += blksize;
779                         *off += blksize;
780                         rc += blksize;
781                 }
782         }
783
784         return rc;
785 }
786
787 static void fsfilt_ext3_cb_func(struct journal_callback *jcb, int error)
788 {
789         struct fsfilt_cb_data *fcb = (struct fsfilt_cb_data *)jcb;
790
791         fcb->cb_func(fcb->cb_obd, fcb->cb_last_rcvd, fcb->cb_data, error);
792
793         OBD_SLAB_FREE(fcb, fcb_cache, sizeof *fcb);
794 }
795
796 static int fsfilt_ext3_add_journal_cb(struct obd_device *obd, __u64 last_rcvd,
797                                       void *handle, fsfilt_cb_t cb_func,
798                                       void *cb_data)
799 {
800         struct fsfilt_cb_data *fcb;
801
802         OBD_SLAB_ALLOC_PTR_GFP(fcb, fcb_cache, CFS_ALLOC_IO);
803         if (fcb == NULL)
804                 RETURN(-ENOMEM);
805
806         fcb->cb_func = cb_func;
807         fcb->cb_obd = obd;
808         fcb->cb_last_rcvd = last_rcvd;
809         fcb->cb_data = cb_data;
810
811         CDEBUG(D_EXT2, "set callback for last_rcvd: "LPD64"\n", last_rcvd);
812         fsfilt_journal_callback_set(handle, fsfilt_ext3_cb_func,
813                                     (struct journal_callback *)fcb);
814
815         return 0;
816 }
817
818 /*
819  * We need to hack the return value for the free inode counts because
820  * the current EA code requires one filesystem block per inode with EAs,
821  * so it is possible to run out of blocks before we run out of inodes.
822  *
823  * This can be removed when the ext3 EA code is fixed.
824  */
825 static int fsfilt_ext3_statfs(struct super_block *sb, struct obd_statfs *osfs)
826 {
827         struct kstatfs sfs;
828         int rc;
829
830         memset(&sfs, 0, sizeof(sfs));
831         rc = ll_do_statfs(sb, &sfs);
832         if (!rc && sfs.f_bfree < sfs.f_ffree) {
833                 sfs.f_files = (sfs.f_files - sfs.f_ffree) + sfs.f_bfree;
834                 sfs.f_ffree = sfs.f_bfree;
835         }
836
837         statfs_pack(osfs, &sfs);
838         return rc;
839 }
840
841 static int fsfilt_ext3_sync(struct super_block *sb)
842 {
843         return ext3_force_commit(sb);
844 }
845
846 #ifndef EXT3_EXTENTS_FL
847 #define EXT3_EXTENTS_FL                 0x00080000 /* Inode uses extents */
848 #endif
849
850 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
851 # define fsfilt_up_truncate_sem(inode)  up(&LDISKFS_I(inode)->truncate_sem);
852 # define fsfilt_down_truncate_sem(inode)  down(&LDISKFS_I(inode)->truncate_sem);
853 #else
854 # ifdef HAVE_EXT4_LDISKFS
855 #  ifdef WALK_SPACE_HAS_DATA_SEM /* We only use it in fsfilt_map_nblocks() for now */
856 #   define fsfilt_up_truncate_sem(inode) do{ }while(0)
857 #   define fsfilt_down_truncate_sem(inode) do{ }while(0)
858 #  else
859 #   define fsfilt_up_truncate_sem(inode) up_write((&EXT4_I(inode)->i_data_sem))
860 #   define fsfilt_down_truncate_sem(inode) down_write((&EXT4_I(inode)->i_data_sem))
861 #  endif
862 # else
863 #  define fsfilt_up_truncate_sem(inode)  mutex_unlock(&EXT3_I(inode)->truncate_mutex)
864 #  define fsfilt_down_truncate_sem(inode)  mutex_lock(&EXT3_I(inode)->truncate_mutex)
865 # endif
866 #endif
867
868 #ifndef EXT_ASSERT
869 #define EXT_ASSERT(cond)  BUG_ON(!(cond))
870 #endif
871
872 #ifdef EXT3_EXT_HAS_NO_TREE
873 /* for kernels 2.6.18 and later */
874 #ifdef HAVE_EXT4_LDISKFS
875 #define EXT_GENERATION(inode)           (EXT4_I(inode)->i_ext_generation)
876 #else
877 #define EXT_GENERATION(inode)           ext_generation(inode)
878 #endif
879 #define ext3_ext_base                   inode
880 #define ext3_ext_base2inode(inode)      (inode)
881 #define EXT_DEPTH(inode)                ext_depth(inode)
882 #define fsfilt_ext3_ext_walk_space(inode, block, num, cb, cbdata) \
883                         ext3_ext_walk_space(inode, block, num, cb, cbdata);
884 #else
885 #define ext3_ext_base                   ext3_extents_tree
886 #define ext3_ext_base2inode(tree)       (tree->inode)
887 #define fsfilt_ext3_ext_walk_space(tree, block, num, cb, cbdata) \
888                         ext3_ext_walk_space(tree, block, num, cb);
889 #endif
890
891 #ifdef EXT_INSERT_EXTENT_WITH_5ARGS
892 #define fsfilt_ext3_ext_insert_extent(handle, inode, path, newext, flag) \
893                ext3_ext_insert_extent(handle, inode, path, newext, flag)
894 #else
895 #define fsfilt_ext3_ext_insert_extent(handle, inode, path, newext, flag) \
896                ext3_ext_insert_extent(handle, inode, path, newext)
897 #endif
898
899 #include <linux/lustre_version.h>
900
901 struct bpointers {
902         unsigned long *blocks;
903         int *created;
904         unsigned long start;
905         int num;
906         int init_num;
907         int create;
908 };
909
910 static long ext3_ext_find_goal(struct inode *inode, struct ext3_ext_path *path,
911                                unsigned long block, int *aflags)
912 {
913         struct ext3_inode_info *ei = EXT3_I(inode);
914         unsigned long bg_start;
915         unsigned long colour;
916         int depth;
917
918         if (path) {
919                 struct ext3_extent *ex;
920                 depth = path->p_depth;
921
922                 /* try to predict block placement */
923                 if ((ex = path[depth].p_ext))
924                         return ext_pblock(ex) + (block - le32_to_cpu(ex->ee_block));
925
926                 /* it looks index is empty
927                  * try to find starting from index itself */
928                 if (path[depth].p_bh)
929                         return path[depth].p_bh->b_blocknr;
930         }
931
932         /* OK. use inode's group */
933         bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
934                 le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
935         colour = (current->pid % 16) *
936                 (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
937         return bg_start + colour + block;
938 }
939
940 #define ll_unmap_underlying_metadata(sb, blocknr) \
941         unmap_underlying_metadata((sb)->s_bdev, blocknr)
942
943 #ifndef EXT3_MB_HINT_GROUP_ALLOC
944 static unsigned long new_blocks(handle_t *handle, struct ext3_ext_base *base,
945                                 struct ext3_ext_path *path, unsigned long block,
946                                 unsigned long *count, int *err)
947 {
948         unsigned long pblock, goal;
949         int aflags = 0;
950         struct inode *inode = ext3_ext_base2inode(base);
951
952         goal = ext3_ext_find_goal(inode, path, block, &aflags);
953         aflags |= 2; /* block have been already reserved */
954         pblock = ext3_mb_new_blocks(handle, inode, goal, count, aflags, err);
955         return pblock;
956
957 }
958 #else
959 static unsigned long new_blocks(handle_t *handle, struct ext3_ext_base *base,
960                                 struct ext3_ext_path *path, unsigned long block,
961                                 unsigned long *count, int *err)
962 {
963         struct inode *inode = ext3_ext_base2inode(base);
964         struct ext3_allocation_request ar;
965         unsigned long pblock;
966         int aflags;
967
968         /* find neighbour allocated blocks */
969         ar.lleft = block;
970         *err = ext3_ext_search_left(base, path, &ar.lleft, &ar.pleft);
971         if (*err)
972                 return 0;
973         ar.lright = block;
974         *err = ext3_ext_search_right(base, path, &ar.lright, &ar.pright);
975         if (*err)
976                 return 0;
977
978         /* allocate new block */
979         ar.goal = ext3_ext_find_goal(inode, path, block, &aflags);
980         ar.inode = inode;
981         ar.logical = block;
982         ar.len = *count;
983         ar.flags = EXT3_MB_HINT_DATA;
984         pblock = ext3_mb_new_blocks(handle, &ar, err);
985         *count = ar.len;
986         return pblock;
987 }
988 #endif
989
990 #ifdef EXT3_EXT_HAS_NO_TREE
991 static int ext3_ext_new_extent_cb(struct ext3_ext_base *base,
992                                   struct ext3_ext_path *path,
993                                   struct ext3_ext_cache *cex,
994 #ifdef HAVE_EXT_PREPARE_CB_EXTENT
995                                    struct ext3_extent *ex,
996 #endif
997                                   void *cbdata)
998 {
999         struct bpointers *bp = cbdata;
1000 #else
1001 static int ext3_ext_new_extent_cb(struct ext3_ext_base *base,
1002                                   struct ext3_ext_path *path,
1003                                   struct ext3_ext_cache *cex
1004 #ifdef HAVE_EXT_PREPARE_CB_EXTENT
1005                                   , struct ext3_extent *ex
1006 #endif
1007                                  )
1008 {
1009         struct bpointers *bp = base->private;
1010 #endif
1011         struct inode *inode = ext3_ext_base2inode(base);
1012         struct ext3_extent nex;
1013         unsigned long pblock;
1014         unsigned long tgen;
1015         int err, i;
1016         unsigned long count;
1017         handle_t *handle;
1018
1019         i = EXT_DEPTH(base);
1020         EXT_ASSERT(i == path->p_depth);
1021         EXT_ASSERT(path[i].p_hdr);
1022
1023         if (cex->ec_type == EXT3_EXT_CACHE_EXTENT) {
1024                 err = EXT_CONTINUE;
1025                 goto map;
1026         }
1027
1028         if (bp->create == 0) {
1029                 i = 0;
1030                 if (cex->ec_block < bp->start)
1031                         i = bp->start - cex->ec_block;
1032                 if (i >= cex->ec_len)
1033                         CERROR("nothing to do?! i = %d, e_num = %u\n",
1034                                         i, cex->ec_len);
1035                 for (; i < cex->ec_len && bp->num; i++) {
1036                         *(bp->created) = 0;
1037                         bp->created++;
1038                         *(bp->blocks) = 0;
1039                         bp->blocks++;
1040                         bp->num--;
1041                         bp->start++;
1042                 }
1043
1044                 return EXT_CONTINUE;
1045         }
1046
1047         tgen = EXT_GENERATION(base);
1048         count = ext3_ext_calc_credits_for_insert(base, path);
1049         fsfilt_up_truncate_sem(inode);
1050
1051         handle = ext3_journal_start(inode, count+EXT3_ALLOC_NEEDED+1);
1052         if (IS_ERR(handle)) {
1053                 fsfilt_down_truncate_sem(inode);
1054                 return PTR_ERR(handle);
1055         }
1056
1057         fsfilt_down_truncate_sem(inode);
1058         if (tgen != EXT_GENERATION(base)) {
1059                 /* the tree has changed. so path can be invalid at moment */
1060                 ext3_journal_stop(handle);
1061                 return EXT_REPEAT;
1062         }
1063
1064         count = cex->ec_len;
1065         pblock = new_blocks(handle, base, path, cex->ec_block, &count, &err);
1066         if (!pblock)
1067                 goto out;
1068         EXT_ASSERT(count <= cex->ec_len);
1069
1070         /* insert new extent */
1071         nex.ee_block = cpu_to_le32(cex->ec_block);
1072         ext3_ext_store_pblock(&nex, pblock);
1073         nex.ee_len = cpu_to_le16(count);
1074         err = fsfilt_ext3_ext_insert_extent(handle, base, path, &nex, 0);
1075         if (err) {
1076                 /* free data blocks we just allocated */
1077                 /* not a good idea to call discard here directly,
1078                  * but otherwise we'd need to call it every free() */
1079 #ifdef EXT3_MB_HINT_GROUP_ALLOC
1080                 ext3_mb_discard_inode_preallocations(inode);
1081 #endif
1082                 ext3_free_blocks(handle, inode, ext_pblock(&nex),
1083                                  cpu_to_le16(nex.ee_len), 0);
1084                 goto out;
1085         }
1086
1087         /*
1088          * Putting len of the actual extent we just inserted,
1089          * we are asking ext3_ext_walk_space() to continue
1090          * scaning after that block
1091          */
1092         cex->ec_len = le16_to_cpu(nex.ee_len);
1093         cex->ec_start = ext_pblock(&nex);
1094         BUG_ON(le16_to_cpu(nex.ee_len) == 0);
1095         BUG_ON(le32_to_cpu(nex.ee_block) != cex->ec_block);
1096
1097 out:
1098         ext3_journal_stop(handle);
1099 map:
1100         if (err >= 0) {
1101                 /* map blocks */
1102                 if (bp->num == 0) {
1103                         CERROR("hmm. why do we find this extent?\n");
1104                         CERROR("initial space: %lu:%u\n",
1105                                 bp->start, bp->init_num);
1106                         CERROR("current extent: %u/%u/%llu %d\n",
1107                                 cex->ec_block, cex->ec_len,
1108                                 (unsigned long long)cex->ec_start,
1109                                 cex->ec_type);
1110                 }
1111                 i = 0;
1112                 if (cex->ec_block < bp->start)
1113                         i = bp->start - cex->ec_block;
1114                 if (i >= cex->ec_len)
1115                         CERROR("nothing to do?! i = %d, e_num = %u\n",
1116                                         i, cex->ec_len);
1117                 for (; i < cex->ec_len && bp->num; i++) {
1118                         *(bp->blocks) = cex->ec_start + i;
1119                         if (cex->ec_type == EXT3_EXT_CACHE_EXTENT) {
1120                                 *(bp->created) = 0;
1121                         } else {
1122                                 *(bp->created) = 1;
1123                                 /* unmap any possible underlying metadata from
1124                                  * the block device mapping.  bug 6998. */
1125                                 ll_unmap_underlying_metadata(inode->i_sb,
1126                                                              *(bp->blocks));
1127                         }
1128                         bp->created++;
1129                         bp->blocks++;
1130                         bp->num--;
1131                         bp->start++;
1132                 }
1133         }
1134         return err;
1135 }
1136
1137 int fsfilt_map_nblocks(struct inode *inode, unsigned long block,
1138                        unsigned long num, unsigned long *blocks,
1139                        int *created, int create)
1140 {
1141 #ifdef EXT3_EXT_HAS_NO_TREE
1142         struct ext3_ext_base *base = inode;
1143 #else
1144         struct ext3_extents_tree tree;
1145         struct ext3_ext_base *base = &tree;
1146 #endif
1147         struct bpointers bp;
1148         int err;
1149
1150         CDEBUG(D_OTHER, "blocks %lu-%lu requested for inode %u\n",
1151                block, block + num - 1, (unsigned) inode->i_ino);
1152
1153 #ifndef EXT3_EXT_HAS_NO_TREE
1154         ext3_init_tree_desc(base, inode);
1155         tree.private = &bp;
1156 #endif
1157         bp.blocks = blocks;
1158         bp.created = created;
1159         bp.start = block;
1160         bp.init_num = bp.num = num;
1161         bp.create = create;
1162
1163         fsfilt_down_truncate_sem(inode);
1164         err = fsfilt_ext3_ext_walk_space(base, block, num,
1165                                          ext3_ext_new_extent_cb, &bp);
1166         ext3_ext_invalidate_cache(base);
1167         fsfilt_up_truncate_sem(inode);
1168
1169         return err;
1170 }
1171
1172 int fsfilt_ext3_map_ext_inode_pages(struct inode *inode, struct page **page,
1173                                     int pages, unsigned long *blocks,
1174                                     int *created, int create)
1175 {
1176         int blocks_per_page = CFS_PAGE_SIZE >> inode->i_blkbits;
1177         int rc = 0, i = 0;
1178         struct page *fp = NULL;
1179         int clen = 0;
1180
1181         CDEBUG(D_OTHER, "inode %lu: map %d pages from %lu\n",
1182                 inode->i_ino, pages, (*page)->index);
1183
1184         /* pages are sorted already. so, we just have to find
1185          * contig. space and process them properly */
1186         while (i < pages) {
1187                 if (fp == NULL) {
1188                         /* start new extent */
1189                         fp = *page++;
1190                         clen = 1;
1191                         i++;
1192                         continue;
1193                 } else if (fp->index + clen == (*page)->index) {
1194                         /* continue the extent */
1195                         page++;
1196                         clen++;
1197                         i++;
1198                         continue;
1199                 }
1200
1201                 /* process found extent */
1202                 rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page,
1203                                         clen * blocks_per_page, blocks,
1204                                         created, create);
1205                 if (rc)
1206                         GOTO(cleanup, rc);
1207
1208                 /* look for next extent */
1209                 fp = NULL;
1210                 blocks += blocks_per_page * clen;
1211                 created += blocks_per_page * clen;
1212         }
1213
1214         if (fp)
1215                 rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page,
1216                                         clen * blocks_per_page, blocks,
1217                                         created, create);
1218 cleanup:
1219         return rc;
1220 }
1221
1222 extern int ext3_map_inode_page(struct inode *inode, struct page *page,
1223                                unsigned long *blocks, int *created, int create);
1224 int fsfilt_ext3_map_bm_inode_pages(struct inode *inode, struct page **page,
1225                                    int pages, unsigned long *blocks,
1226                                    int *created, int create)
1227 {
1228         int blocks_per_page = CFS_PAGE_SIZE >> inode->i_blkbits;
1229         unsigned long *b;
1230         int rc = 0, i, *cr;
1231
1232         for (i = 0, cr = created, b = blocks; i < pages; i++, page++) {
1233                 rc = ext3_map_inode_page(inode, *page, b, cr, create);
1234                 if (rc) {
1235                         CERROR("ino %lu, blk %lu cr %u create %d: rc %d\n",
1236                                inode->i_ino, *b, *cr, create, rc);
1237                         break;
1238                 }
1239
1240                 b += blocks_per_page;
1241                 cr += blocks_per_page;
1242         }
1243         return rc;
1244 }
1245
1246 int fsfilt_ext3_map_inode_pages(struct inode *inode, struct page **page,
1247                                 int pages, unsigned long *blocks,
1248                                 int *created, int create,
1249                                 cfs_semaphore_t *optional_sem)
1250 {
1251         int rc;
1252
1253         if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL) {
1254                 rc = fsfilt_ext3_map_ext_inode_pages(inode, page, pages,
1255                                                      blocks, created, create);
1256                 return rc;
1257         }
1258         if (optional_sem != NULL)
1259                 cfs_down(optional_sem);
1260         rc = fsfilt_ext3_map_bm_inode_pages(inode, page, pages, blocks,
1261                                             created, create);
1262         if (optional_sem != NULL)
1263                 cfs_up(optional_sem);
1264
1265         return rc;
1266 }
1267
1268 int fsfilt_ext3_read(struct inode *inode, void *buf, int size, loff_t *offs)
1269 {
1270         unsigned long block;
1271         struct buffer_head *bh;
1272         int err, blocksize, csize, boffs, osize = size;
1273
1274         /* prevent reading after eof */
1275         cfs_lock_kernel();
1276         if (i_size_read(inode) < *offs + size) {
1277                 size = i_size_read(inode) - *offs;
1278                 cfs_unlock_kernel();
1279                 if (size < 0) {
1280                         CDEBUG(D_EXT2, "size %llu is too short for read @%llu\n",
1281                                i_size_read(inode), *offs);
1282                         return -EBADR;
1283                 } else if (size == 0) {
1284                         return 0;
1285                 }
1286         } else {
1287                 cfs_unlock_kernel();
1288         }
1289
1290         blocksize = 1 << inode->i_blkbits;
1291
1292         while (size > 0) {
1293                 block = *offs >> inode->i_blkbits;
1294                 boffs = *offs & (blocksize - 1);
1295                 csize = min(blocksize - boffs, size);
1296                 bh = ext3_bread(NULL, inode, block, 0, &err);
1297                 if (!bh) {
1298                         CERROR("can't read block: %d\n", err);
1299                         return err;
1300                 }
1301
1302                 memcpy(buf, bh->b_data + boffs, csize);
1303                 brelse(bh);
1304
1305                 *offs += csize;
1306                 buf += csize;
1307                 size -= csize;
1308         }
1309         return osize;
1310 }
1311 EXPORT_SYMBOL(fsfilt_ext3_read);
1312
1313 static int fsfilt_ext3_read_record(struct file * file, void *buf,
1314                                    int size, loff_t *offs)
1315 {
1316         int rc;
1317         rc = fsfilt_ext3_read(file->f_dentry->d_inode, buf, size, offs);
1318         if (rc > 0)
1319                 rc = 0;
1320         return rc;
1321 }
1322
1323 int fsfilt_ext3_write_handle(struct inode *inode, void *buf, int bufsize,
1324                                 loff_t *offs, handle_t *handle)
1325 {
1326         struct buffer_head *bh = NULL;
1327         loff_t old_size = i_size_read(inode), offset = *offs;
1328         loff_t new_size = i_size_read(inode);
1329         unsigned long block;
1330         int err = 0, blocksize = 1 << inode->i_blkbits, size, boffs;
1331
1332         while (bufsize > 0) {
1333                 if (bh != NULL)
1334                         brelse(bh);
1335
1336                 block = offset >> inode->i_blkbits;
1337                 boffs = offset & (blocksize - 1);
1338                 size = min(blocksize - boffs, bufsize);
1339                 bh = ext3_bread(handle, inode, block, 1, &err);
1340                 if (!bh) {
1341                         CERROR("can't read/create block: %d\n", err);
1342                         break;
1343                 }
1344
1345                 err = ext3_journal_get_write_access(handle, bh);
1346                 if (err) {
1347                         CERROR("journal_get_write_access() returned error %d\n",
1348                                err);
1349                         break;
1350                 }
1351                 LASSERT(bh->b_data + boffs + size <= bh->b_data + bh->b_size);
1352                 memcpy(bh->b_data + boffs, buf, size);
1353                 err = ext3_journal_dirty_metadata(handle, bh);
1354                 if (err) {
1355                         CERROR("journal_dirty_metadata() returned error %d\n",
1356                                err);
1357                         break;
1358                 }
1359                 if (offset + size > new_size)
1360                         new_size = offset + size;
1361                 offset += size;
1362                 bufsize -= size;
1363                 buf += size;
1364         }
1365         if (bh)
1366                 brelse(bh);
1367
1368         /* correct in-core and on-disk sizes */
1369         if (new_size > i_size_read(inode)) {
1370                 cfs_lock_kernel();
1371                 if (new_size > i_size_read(inode))
1372                         i_size_write(inode, new_size);
1373                 if (i_size_read(inode) > EXT3_I(inode)->i_disksize)
1374                         EXT3_I(inode)->i_disksize = i_size_read(inode);
1375                 if (i_size_read(inode) > old_size)
1376                         mark_inode_dirty(inode);
1377                 cfs_unlock_kernel();
1378         }
1379
1380         if (err == 0)
1381                 *offs = offset;
1382         return err;
1383 }
1384 EXPORT_SYMBOL(fsfilt_ext3_write_handle);
1385
1386 static int fsfilt_ext3_write_record(struct file *file, void *buf, int bufsize,
1387                                     loff_t *offs, int force_sync)
1388 {
1389         struct inode *inode = file->f_dentry->d_inode;
1390         handle_t *handle;
1391         int err, block_count = 0, blocksize;
1392
1393         /* Determine how many transaction credits are needed */
1394         blocksize = 1 << inode->i_blkbits;
1395         block_count = (*offs & (blocksize - 1)) + bufsize;
1396         block_count = (block_count + blocksize - 1) >> inode->i_blkbits;
1397
1398         handle = ext3_journal_start(inode,
1399                                block_count * FSFILT_DATA_TRANS_BLOCKS(inode->i_sb) + 2);
1400         if (IS_ERR(handle)) {
1401                 CERROR("can't start transaction for %d blocks (%d bytes)\n",
1402                        block_count * FSFILT_DATA_TRANS_BLOCKS(inode->i_sb) + 2, bufsize);
1403                 return PTR_ERR(handle);
1404         }
1405
1406         err = fsfilt_ext3_write_handle(inode, buf, bufsize, offs, handle);
1407
1408         if (!err && force_sync)
1409                 handle->h_sync = 1; /* recovery likes this */
1410
1411         ext3_journal_stop(handle);
1412
1413         return err;
1414 }
1415
1416 static int fsfilt_ext3_setup(struct super_block *sb)
1417 {
1418 #if ((LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,6)) && \
1419      defined(HAVE_QUOTA_SUPPORT)) || defined(S_PDIROPS)
1420         struct ext3_sb_info *sbi = EXT3_SB(sb);
1421 #if 0
1422         sbi->dx_lock = fsfilt_ext3_dx_lock;
1423         sbi->dx_unlock = fsfilt_ext3_dx_unlock;
1424 #endif
1425 #endif
1426 #ifdef S_PDIROPS
1427         CWARN("Enabling PDIROPS\n");
1428         set_opt(sbi->s_mount_opt, PDIROPS);
1429         sb->s_flags |= S_PDIROPS;
1430 #endif
1431         if (!EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_DIR_INDEX))
1432                 CWARN("filesystem doesn't have dir_index feature enabled\n");
1433 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,6)) && defined(HAVE_QUOTA_SUPPORT)
1434         /* enable journaled quota support */
1435         /* kfreed in ext3_put_super() */
1436         sbi->s_qf_names[USRQUOTA] = kstrdup("lquota.user.reserved", GFP_KERNEL);
1437         if (!sbi->s_qf_names[USRQUOTA])
1438                 return -ENOMEM;
1439         sbi->s_qf_names[GRPQUOTA] = kstrdup("lquota.group.reserved", GFP_KERNEL);
1440         if (!sbi->s_qf_names[GRPQUOTA]) {
1441                 kfree(sbi->s_qf_names[USRQUOTA]);
1442                 sbi->s_qf_names[USRQUOTA] = NULL;
1443                 return -ENOMEM;
1444         }
1445         sbi->s_jquota_fmt = QFMT_LUSTRE;
1446 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13))
1447         set_opt(sbi->s_mount_opt, QUOTA);
1448 #endif
1449 #endif
1450         return 0;
1451 }
1452
1453 /* If fso is NULL, op is FSFILT operation, otherwise op is number of fso
1454    objects. Logs is number of logfiles to update */
1455 static int fsfilt_ext3_get_op_len(int op, struct fsfilt_objinfo *fso, int logs)
1456 {
1457         if ( !fso ) {
1458                 switch(op) {
1459                 case FSFILT_OP_CREATE:
1460                                  /* directory leaf, index & indirect & EA*/
1461                         return 4 + 3 * logs;
1462                 case FSFILT_OP_UNLINK:
1463                         return 3 * logs;
1464                 }
1465         } else {
1466                 int i;
1467                 int needed = 0;
1468                 struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
1469                 int blockpp = 1 << (CFS_PAGE_SHIFT - sb->s_blocksize_bits);
1470                 int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
1471                 for (i = 0; i < op; i++, fso++) {
1472                         int nblocks = fso->fso_bufcnt * blockpp;
1473                         int ndindirect = min(nblocks, addrpp + 1);
1474                         int nindir = nblocks + ndindirect + 1;
1475
1476                         needed += nindir;
1477                 }
1478                 return needed + 3 * logs;
1479         }
1480
1481         return 0;
1482 }
1483
1484 #ifdef HAVE_QUOTA_SUPPORT
1485 #define DQINFO_COPY(out, in)                    \
1486 do {                                            \
1487         Q_COPY(out, in, dqi_bgrace);            \
1488         Q_COPY(out, in, dqi_igrace);            \
1489         Q_COPY(out, in, dqi_flags);             \
1490         Q_COPY(out, in, dqi_valid);             \
1491 } while (0)
1492
1493 #define DQBLK_COPY(out, in)                     \
1494 do {                                            \
1495         Q_COPY(out, in, dqb_bhardlimit);        \
1496         Q_COPY(out, in, dqb_bsoftlimit);        \
1497         Q_COPY(out, in, dqb_curspace);          \
1498         Q_COPY(out, in, dqb_ihardlimit);        \
1499         Q_COPY(out, in, dqb_isoftlimit);        \
1500         Q_COPY(out, in, dqb_curinodes);         \
1501         Q_COPY(out, in, dqb_btime);             \
1502         Q_COPY(out, in, dqb_itime);             \
1503         Q_COPY(out, in, dqb_valid);             \
1504 } while (0)
1505
1506 static int fsfilt_ext3_quotactl(struct super_block *sb,
1507                                 struct obd_quotactl *oqc)
1508 {
1509         int i, rc = 0, error = 0;
1510         const struct quotactl_ops *qcop;
1511         struct if_dqinfo *info;
1512         struct if_dqblk *dqblk;
1513         ENTRY;
1514
1515         if (!sb->s_qcop)
1516                 RETURN(-ENOSYS);
1517
1518         OBD_ALLOC_PTR(info);
1519         if (!info)
1520                 RETURN(-ENOMEM);
1521         OBD_ALLOC_PTR(dqblk);
1522         if (!dqblk) {
1523                 OBD_FREE_PTR(info);
1524                 RETURN(-ENOMEM);
1525         }
1526
1527         DQINFO_COPY(info, &oqc->qc_dqinfo);
1528         DQBLK_COPY(dqblk, &oqc->qc_dqblk);
1529
1530         qcop = sb->s_qcop;
1531         if (oqc->qc_cmd == Q_QUOTAON || oqc->qc_cmd == Q_QUOTAOFF) {
1532                 for (i = 0; i < MAXQUOTAS; i++) {
1533                         if (!Q_TYPESET(oqc, i))
1534                                 continue;
1535
1536                         if (oqc->qc_cmd == Q_QUOTAON) {
1537                                 char *name[MAXQUOTAS] = LUSTRE_OPQFILES_NAMES_V2;
1538
1539                                 LASSERT(oqc->qc_id == LUSTRE_QUOTA_V2);
1540
1541                                 rc = ll_quota_on(sb, i, QFMT_LUSTRE,
1542                                                  name[i], 0);
1543                         } else if (oqc->qc_cmd == Q_QUOTAOFF) {
1544                                 rc = ll_quota_off(sb, i, 0);
1545                         }
1546
1547                         if (rc == -EBUSY)
1548                                 error = rc;
1549                         else if (rc)
1550                                 GOTO(out, rc);
1551                 }
1552                 GOTO(out, rc ?: error);
1553         }
1554
1555         switch (oqc->qc_cmd) {
1556         case Q_GETOINFO:
1557         case Q_GETINFO:
1558                 if (!qcop->get_info)
1559                         GOTO(out, rc = -ENOSYS);
1560                 rc = qcop->get_info(sb, oqc->qc_type, info);
1561                 break;
1562         case Q_SETQUOTA:
1563         case Q_INITQUOTA:
1564                 if (!qcop->set_dqblk)
1565                         GOTO(out, rc = -ENOSYS);
1566                 rc = qcop->set_dqblk(sb, oqc->qc_type, oqc->qc_id, dqblk);
1567                 break;
1568         case Q_GETOQUOTA:
1569         case Q_GETQUOTA:
1570                 if (!qcop->get_dqblk)
1571                         GOTO(out, rc = -ENOSYS);
1572                 rc = qcop->get_dqblk(sb, oqc->qc_type, oqc->qc_id, dqblk);
1573                 if (!rc)
1574                         dqblk->dqb_valid = QIF_LIMITS | QIF_USAGE;
1575                 break;
1576         case Q_SYNC:
1577                 if (!sb->s_qcop->quota_sync)
1578                         GOTO(out, rc = -ENOSYS);
1579                 qcop->quota_sync(sb, oqc->qc_type);
1580                 break;
1581         case Q_FINVALIDATE:
1582                 CDEBUG(D_WARNING, "invalidating operational quota files\n");
1583                 for (i = 0; i < MAXQUOTAS; i++) {
1584                         struct file *fp;
1585                         char *name[MAXQUOTAS] = LUSTRE_OPQFILES_NAMES_V2;
1586
1587                         LASSERT(oqc->qc_id == LUSTRE_QUOTA_V2);
1588
1589                         if (!Q_TYPESET(oqc, i))
1590                                 continue;
1591
1592                         fp = filp_open(name[i], O_CREAT | O_TRUNC | O_RDWR, 0644);
1593                         if (IS_ERR(fp)) {
1594                                 rc = PTR_ERR(fp);
1595                                 CERROR("error invalidating operational quota file"
1596                                        " %s (rc:%d)\n", name[i], rc);
1597                         } else {
1598                                 filp_close(fp, 0);
1599                         }
1600
1601                 }
1602                 break;
1603         default:
1604                 CERROR("unsupported quotactl command: %d\n", oqc->qc_cmd);
1605                 LBUG();
1606         }
1607 out:
1608         DQINFO_COPY(&oqc->qc_dqinfo, info);
1609         DQBLK_COPY(&oqc->qc_dqblk, dqblk);
1610
1611         OBD_FREE_PTR(info);
1612         OBD_FREE_PTR(dqblk);
1613
1614         if (rc)
1615                 CDEBUG(D_QUOTA, "quotactl command %#x, id %u, type %u "
1616                                 "failed: %d\n",
1617                        oqc->qc_cmd, oqc->qc_id, oqc->qc_type, rc);
1618         RETURN(rc);
1619 }
1620
1621 struct chk_dqblk{
1622         cfs_hlist_node_t        dqb_hash;        /** quotacheck hash */
1623         cfs_list_t              dqb_list;        /** in list also */
1624         qid_t                   dqb_id;          /** uid/gid */
1625         short                   dqb_type;        /** USRQUOTA/GRPQUOTA */
1626         qsize_t                 dqb_bhardlimit;  /** block hard limit */
1627         qsize_t                 dqb_bsoftlimit;  /** block soft limit */
1628         qsize_t                 dqb_curspace;    /** current space */
1629         qsize_t                 dqb_ihardlimit;  /** inode hard limit */
1630         qsize_t                 dqb_isoftlimit;  /** inode soft limit */
1631         qsize_t                 dqb_curinodes;   /** current inodes */
1632         obd_time                dqb_btime;       /** block grace time */
1633         obd_time                dqb_itime;       /** inode grace time */
1634         __u32                   dqb_valid;       /** flag for above fields */
1635 };
1636
1637 static inline unsigned int chkquot_hash(qid_t id, int type)
1638                                         __attribute__((__const__));
1639
1640 static inline unsigned int chkquot_hash(qid_t id, int type)
1641 {
1642         return (id * (MAXQUOTAS - type)) % NR_DQHASH;
1643 }
1644
1645 static inline struct chk_dqblk *
1646 find_chkquot(cfs_hlist_head_t *head, qid_t id, int type)
1647 {
1648         cfs_hlist_node_t *node;
1649         struct chk_dqblk *cdqb;
1650
1651         cfs_hlist_for_each(node, head) {
1652                 cdqb = cfs_hlist_entry(node, struct chk_dqblk, dqb_hash);
1653                 if (cdqb->dqb_id == id && cdqb->dqb_type == type)
1654                         return cdqb;
1655         }
1656
1657         return NULL;
1658 }
1659
1660 static struct chk_dqblk *alloc_chkquot(qid_t id, int type)
1661 {
1662         struct chk_dqblk *cdqb;
1663
1664         OBD_ALLOC_PTR(cdqb);
1665         if (cdqb) {
1666                 CFS_INIT_HLIST_NODE(&cdqb->dqb_hash);
1667                 CFS_INIT_LIST_HEAD(&cdqb->dqb_list);
1668                 cdqb->dqb_id = id;
1669                 cdqb->dqb_type = type;
1670         }
1671
1672         return cdqb;
1673 }
1674
1675 static struct chk_dqblk *
1676 cqget(struct super_block *sb, cfs_hlist_head_t *hash,
1677       cfs_list_t *list, qid_t id, int type, int first_check)
1678 {
1679         cfs_hlist_head_t *head = hash + chkquot_hash(id, type);
1680         struct if_dqblk dqb;
1681         struct chk_dqblk *cdqb;
1682         int rc;
1683
1684         cdqb = find_chkquot(head, id, type);
1685         if (cdqb)
1686                 return cdqb;
1687
1688         cdqb = alloc_chkquot(id, type);
1689         if (!cdqb)
1690                 return NULL;
1691
1692         if (!first_check) {
1693                 rc = sb->s_qcop->get_dqblk(sb, type, id, &dqb);
1694                 if (rc) {
1695                         CERROR("get_dqblk of id %u, type %d failed: %d\n",
1696                                id, type, rc);
1697                 } else {
1698                         DQBLK_COPY(cdqb, &dqb);
1699                         cdqb->dqb_curspace = 0;
1700                         cdqb->dqb_curinodes = 0;
1701                 }
1702         }
1703
1704         cfs_hlist_add_head(&cdqb->dqb_hash, head);
1705         cfs_list_add_tail(&cdqb->dqb_list, list);
1706
1707         return cdqb;
1708 }
1709
1710 static inline int quota_onoff(struct super_block *sb, int cmd, int type, int qfmt)
1711 {
1712         struct obd_quotactl *oqctl;
1713         int rc;
1714
1715         OBD_ALLOC_PTR(oqctl);
1716         if (!oqctl)
1717                 RETURN(-ENOMEM);
1718
1719         oqctl->qc_cmd = cmd;
1720         oqctl->qc_id = qfmt;
1721         oqctl->qc_type = type;
1722         rc = fsfilt_ext3_quotactl(sb, oqctl);
1723
1724         OBD_FREE_PTR(oqctl);
1725         return rc;
1726 }
1727
1728 static inline int read_old_dqinfo(struct super_block *sb, int type,
1729                                   struct if_dqinfo *dqinfo)
1730 {
1731         struct obd_quotactl *oqctl;
1732         int rc;
1733         ENTRY;
1734
1735         OBD_ALLOC_PTR(oqctl);
1736         if (!oqctl)
1737                 RETURN(-ENOMEM);
1738
1739         oqctl->qc_cmd = Q_GETINFO;
1740         oqctl->qc_type = type;
1741         rc = fsfilt_ext3_quotactl(sb, oqctl);
1742         if (!rc)
1743                 ((struct obd_dqinfo *)dqinfo)[type] = oqctl->qc_dqinfo;
1744
1745         OBD_FREE_PTR(oqctl);
1746         RETURN(rc);
1747 }
1748
1749 #ifndef HAVE_EXT4_LDISKFS
1750 static inline struct ext3_group_desc *
1751 get_group_desc(struct super_block *sb, int group, struct buffer_head **bh)
1752 {
1753         unsigned long desc_block, desc;
1754         struct ext3_group_desc *gdp;
1755
1756         desc_block = group / EXT3_DESC_PER_BLOCK(sb);
1757         desc = group % EXT3_DESC_PER_BLOCK(sb);
1758         gdp = (struct ext3_group_desc *)
1759               EXT3_SB(sb)->s_group_desc[desc_block]->b_data;
1760
1761         return gdp + desc;
1762 }
1763
1764 static inline struct buffer_head *
1765 ext3_read_inode_bitmap(struct super_block *sb, unsigned long group)
1766 {
1767         struct ext3_group_desc *desc;
1768         struct buffer_head *bh;
1769
1770         desc = get_group_desc(sb, group, NULL);
1771         bh = sb_bread(sb, ext3_inode_bitmap(sb, desc));
1772         return bh;
1773 }
1774
1775 static __u32 ext3_itable_unused_count(struct super_block *sb,
1776                                struct ext3_group_desc *bg) {
1777        return le16_to_cpu(bg->bg_itable_unused);
1778 }
1779 #else
1780 #define get_group_desc ext3_get_group_desc
1781 #endif
1782
1783 struct qchk_ctxt {
1784         cfs_hlist_head_t        qckt_hash[NR_DQHASH];      /* quotacheck hash */
1785         cfs_list_t              qckt_list;                 /* quotacheck list */
1786         int                     qckt_first_check[MAXQUOTAS]; /* 1 if no old quotafile */
1787         struct if_dqinfo        qckt_dqinfo[MAXQUOTAS];    /* old dqinfo */
1788 };
1789
1790 static int add_inode_quota(struct inode *inode, struct qchk_ctxt *qctxt,
1791                            struct obd_quotactl *oqc)
1792 {
1793         struct chk_dqblk *cdqb[MAXQUOTAS] = { NULL, };
1794         loff_t size = 0;
1795         qid_t qid[MAXQUOTAS];
1796         int cnt, i, rc = 0;
1797
1798         if (!inode)
1799                 return 0;
1800
1801         qid[USRQUOTA] = inode->i_uid;
1802         qid[GRPQUOTA] = inode->i_gid;
1803
1804         if (S_ISDIR(inode->i_mode) ||
1805             S_ISREG(inode->i_mode) ||
1806             S_ISLNK(inode->i_mode))
1807                 size = inode_get_bytes(inode);
1808
1809         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1810                 if (!Q_TYPESET(oqc, cnt))
1811                         continue;
1812
1813                 cdqb[cnt] = cqget(inode->i_sb, qctxt->qckt_hash,
1814                                 &qctxt->qckt_list, qid[cnt], cnt,
1815                                 qctxt->qckt_first_check[cnt]);
1816                 if (!cdqb[cnt]) {
1817                         rc = -ENOMEM;
1818                         break;
1819                 }
1820
1821                 cdqb[cnt]->dqb_curspace += size;
1822                 cdqb[cnt]->dqb_curinodes++;
1823         }
1824
1825         if (rc) {
1826                 for (i = 0; i < cnt; i++) {
1827                         if (!Q_TYPESET(oqc, i))
1828                                 continue;
1829                         LASSERT(cdqb[i]);
1830                         cdqb[i]->dqb_curspace -= size;
1831                         cdqb[i]->dqb_curinodes--;
1832                 }
1833         }
1834
1835         return rc;
1836 }
1837
1838 /* write dqinfo struct in a new quota file */
1839 static int v3_write_dqinfo(struct file *f, int type, struct if_dqinfo *info)
1840 {
1841         struct v2_disk_dqinfo dqinfo;
1842         __u32 blocks = V2_DQTREEOFF + 1;
1843         loff_t offset = V2_DQINFOOFF;
1844
1845         if (info) {
1846                 dqinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
1847                 dqinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
1848                 dqinfo.dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK &
1849                                                ~DQF_INFO_DIRTY);
1850         } else {
1851                 dqinfo.dqi_bgrace = cpu_to_le32(MAX_DQ_TIME);
1852                 dqinfo.dqi_igrace = cpu_to_le32(MAX_IQ_TIME);
1853                 dqinfo.dqi_flags = 0;
1854         }
1855
1856         dqinfo.dqi_blocks = cpu_to_le32(blocks);
1857         dqinfo.dqi_free_blk = 0;
1858         dqinfo.dqi_free_entry = 0;
1859
1860         return cfs_user_write(f, (char *)&dqinfo, sizeof(dqinfo), &offset);
1861 }
1862
1863 static int v3_write_dqheader(struct file *f, int type)
1864 {
1865         static const __u32 quota_magics[] = V2_INITQMAGICS;
1866         static const __u32 quota_versions[] = LUSTRE_INITQVERSIONS_V2;
1867         struct v2_disk_dqheader dqhead;
1868         loff_t offset = 0;
1869
1870         CLASSERT(ARRAY_SIZE(quota_magics) == ARRAY_SIZE(quota_versions));
1871         LASSERT(0 <= type && type < ARRAY_SIZE(quota_magics));
1872
1873         dqhead.dqh_magic = cpu_to_le32(quota_magics[type]);
1874         dqhead.dqh_version = cpu_to_le32(quota_versions[type]);
1875
1876         return cfs_user_write(f, (char *)&dqhead, sizeof(dqhead), &offset);
1877 }
1878
1879 static int create_new_quota_files(struct qchk_ctxt *qctxt,
1880                                   struct obd_quotactl *oqc)
1881 {
1882         int i, rc = 0;
1883         ENTRY;
1884
1885         for (i = 0; i < MAXQUOTAS; i++) {
1886                 struct if_dqinfo *info = qctxt->qckt_first_check[i]?
1887                                          NULL : &qctxt->qckt_dqinfo[i];
1888                 struct file *file;
1889                 const char *name[MAXQUOTAS] = LUSTRE_OPQFILES_NAMES_V2;
1890
1891                 if (!Q_TYPESET(oqc, i))
1892                         continue;
1893
1894                 LASSERT(oqc->qc_id == LUSTRE_QUOTA_V2);
1895
1896                 file = filp_open(name[i], O_RDWR | O_CREAT | O_TRUNC, 0644);
1897                 if (IS_ERR(file)) {
1898                         rc = PTR_ERR(file);
1899                         CERROR("can't create %s file: rc = %d\n",
1900                                name[i], rc);
1901                         GOTO(out, rc);
1902                 }
1903
1904                 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
1905                         CERROR("file %s is not regular", name[i]);
1906                         filp_close(file, 0);
1907                         GOTO(out, rc = -EINVAL);
1908                 }
1909
1910                 ll_vfs_dq_drop(file->f_dentry->d_inode);
1911
1912                 rc = v3_write_dqheader(file, i);
1913                 if (rc) {
1914                         filp_close(file, 0);
1915                         GOTO(out, rc);
1916                 }
1917
1918                 rc = v3_write_dqinfo(file, i, info);
1919                 filp_close(file, 0);
1920                 if (rc)
1921                         GOTO(out, rc);
1922         }
1923
1924 out:
1925         RETURN(rc);
1926 }
1927
1928
1929 static int commit_chkquot(struct super_block *sb, struct qchk_ctxt *qctxt,
1930                           struct chk_dqblk *cdqb)
1931 {
1932         struct obd_quotactl *oqc;
1933         long now;
1934         int rc;
1935         ENTRY;
1936
1937         OBD_ALLOC_PTR(oqc);
1938         if (!oqc)
1939                 RETURN(-ENOMEM);
1940
1941         now = cfs_time_current_sec();
1942
1943         if (cdqb->dqb_bsoftlimit &&
1944             toqb(cdqb->dqb_curspace) >= cdqb->dqb_bsoftlimit &&
1945             !cdqb->dqb_btime)
1946                 cdqb->dqb_btime =
1947                         now + qctxt->qckt_dqinfo[cdqb->dqb_type].dqi_bgrace;
1948
1949         if (cdqb->dqb_isoftlimit &&
1950             cdqb->dqb_curinodes >= cdqb->dqb_isoftlimit &&
1951             !cdqb->dqb_itime)
1952                 cdqb->dqb_itime =
1953                         now + qctxt->qckt_dqinfo[cdqb->dqb_type].dqi_igrace;
1954
1955         cdqb->dqb_valid = QIF_ALL;
1956
1957         oqc->qc_cmd = Q_SETQUOTA;
1958         oqc->qc_type = cdqb->dqb_type;
1959         oqc->qc_id = cdqb->dqb_id;
1960         DQBLK_COPY(&oqc->qc_dqblk, cdqb);
1961
1962         rc = fsfilt_ext3_quotactl(sb, oqc);
1963         OBD_FREE_PTR(oqc);
1964         RETURN(rc);
1965 }
1966
1967 static int prune_chkquots(struct super_block *sb,
1968                           struct qchk_ctxt *qctxt, int error)
1969 {
1970         struct chk_dqblk *cdqb, *tmp;
1971         int rc;
1972
1973         cfs_list_for_each_entry_safe(cdqb, tmp, &qctxt->qckt_list, dqb_list) {
1974                 if (!error) {
1975                         rc = commit_chkquot(sb, qctxt, cdqb);
1976                         if (rc)
1977                                 error = rc;
1978                 }
1979                 cfs_hlist_del_init(&cdqb->dqb_hash);
1980                 cfs_list_del(&cdqb->dqb_list);
1981                 OBD_FREE_PTR(cdqb);
1982         }
1983
1984         return error;
1985 }
1986
1987 #ifndef EXT3_FEATURE_RO_COMPAT_GDT_CSUM
1988 #define EXT3_FEATURE_RO_COMPAT_GDT_CSUM 0x0010
1989 #endif
1990
1991 static int fsfilt_ext3_quotacheck(struct super_block *sb,
1992                                   struct obd_quotactl *oqc)
1993 {
1994         struct ext3_sb_info *sbi = EXT3_SB(sb);
1995         int i, group, uninit_feat = 0;
1996         struct qchk_ctxt *qctxt;
1997         struct buffer_head *bitmap_bh = NULL;
1998         unsigned long ino, inode_inuse;
1999         struct inode *inode;
2000         int rc = 0;
2001         ENTRY;
2002
2003         /* turn on quota and read dqinfo if existed */
2004         OBD_ALLOC_PTR(qctxt);
2005         if (!qctxt) {
2006                 oqc->qc_stat = -ENOMEM;
2007                 RETURN(-ENOMEM);
2008         }
2009
2010         for (i = 0; i < NR_DQHASH; i++)
2011                 CFS_INIT_HLIST_HEAD(&qctxt->qckt_hash[i]);
2012         CFS_INIT_LIST_HEAD(&qctxt->qckt_list);
2013
2014         for (i = 0; i < MAXQUOTAS; i++) {
2015                 if (!Q_TYPESET(oqc, i))
2016                         continue;
2017
2018                 rc = quota_onoff(sb, Q_QUOTAON, i, oqc->qc_id);
2019                 if (!rc || rc == -EBUSY) {
2020                         rc = read_old_dqinfo(sb, i, qctxt->qckt_dqinfo);
2021                         if (rc)
2022                                 GOTO(out, rc);
2023                 } else if (rc == -ENOENT || rc == -EINVAL || rc == -EEXIST) {
2024                         qctxt->qckt_first_check[i] = 1;
2025                 } else if (rc) {
2026                         GOTO(out, rc);
2027                 }
2028         }
2029         if (EXT3_HAS_RO_COMPAT_FEATURE(sb, EXT3_FEATURE_RO_COMPAT_GDT_CSUM))
2030                 /* This filesystem supports the uninit group feature */
2031                 uninit_feat = 1;
2032
2033         /* number of inodes that have been allocated */
2034         inode_inuse = sbi->s_inodes_per_group * sbi->s_groups_count -
2035                       percpu_counter_sum(&sbi->s_freeinodes_counter);
2036
2037         /* check quota and update in hash */
2038         for (group = 0; group < sbi->s_groups_count && inode_inuse > 0;
2039              group++) {
2040                 unsigned long used_count = sbi->s_inodes_per_group;
2041
2042                 if (uninit_feat) {
2043                         struct ext3_group_desc *desc;
2044                         desc = get_group_desc(sb, group, NULL);
2045                         if (!desc)
2046                                 GOTO(out, -EIO);
2047
2048                         /* we don't really need to take the group lock here,
2049                          * but it may be useful if one day we support online
2050                          * quotacheck */
2051 #ifdef HAVE_EXT4_LDISKFS
2052                         ext4_lock_group(sb, group);
2053 #else
2054                         spin_lock(sb_bgl_lock(sbi, group));
2055 #endif
2056                         if (desc->bg_flags & cpu_to_le16(EXT3_BG_INODE_UNINIT)) {
2057                                 /* no inode in use in this group, just skip it */
2058 #ifdef HAVE_EXT4_LDISKFS
2059                                 ext3_unlock_group(sb, group);
2060 #else
2061                                 spin_unlock(sb_bgl_lock(sbi, group));
2062 #endif
2063                                 continue;
2064                         }
2065
2066                         used_count -= ext3_itable_unused_count(sb, desc);
2067 #ifdef HAVE_EXT4_LDISKFS
2068                         ext3_unlock_group(sb, group);
2069 #else
2070                         spin_unlock(sb_bgl_lock(sbi, group));
2071 #endif
2072                 }
2073
2074                 ino = group * sbi->s_inodes_per_group + 1;
2075                 bitmap_bh = ext3_read_inode_bitmap(sb, group);
2076                 if (!bitmap_bh) {
2077                         CERROR("%s: ext3_read_inode_bitmap group %d failed\n",
2078                                sb->s_id, group);
2079                         GOTO(out, -EIO);
2080                 }
2081
2082                 i = 0;
2083                 while (i < used_count &&
2084                        (i = ext3_find_next_bit(bitmap_bh->b_data,
2085                                                used_count, i)) < used_count) {
2086                         inode_inuse--;
2087                         i++;
2088                         ino = i + group * sbi->s_inodes_per_group;
2089                         if (ino < sbi->s_first_ino)
2090                                 continue;
2091 #if defined(HAVE_EXT4_LDISKFS) || !defined(HAVE_READ_INODE_IN_SBOPS)
2092                         inode = ext3_iget(sb, ino);
2093 #else
2094                         inode = iget(sb, ino);
2095 #endif
2096                         if (!inode || IS_ERR(inode))
2097                                 continue;
2098
2099                         rc = add_inode_quota(inode, qctxt, oqc);
2100                         iput(inode);
2101                         if (rc) {
2102                                 brelse(bitmap_bh);
2103                                 GOTO(out, rc);
2104                         }
2105                 }
2106                 brelse(bitmap_bh);
2107         }
2108
2109         /* read old quota limits from old quota file. (only for the user
2110          * has limits but hasn't file) */
2111 #ifdef HAVE_QUOTA_SUPPORT
2112         for (i = 0; i < MAXQUOTAS; i++) {
2113                 cfs_list_t id_list;
2114                 struct dquot_id *dqid, *tmp;
2115
2116                 if (!Q_TYPESET(oqc, i))
2117                         continue;
2118
2119                 if (qctxt->qckt_first_check[i])
2120                         continue;
2121
2122
2123                 LASSERT(sb_dqopt(sb)->files[i] != NULL);
2124                 CFS_INIT_LIST_HEAD(&id_list);
2125 #ifndef KERNEL_SUPPORTS_QUOTA_READ
2126                 rc = lustre_get_qids(sb_dqopt(sb)->files[i], NULL, i, &id_list);
2127 #else
2128                 rc = lustre_get_qids(NULL, sb_dqopt(sb)->files[i], i, &id_list);
2129 #endif
2130                 if (rc)
2131                         CERROR("read old limits failed. (rc:%d)\n", rc);
2132
2133                 cfs_list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
2134                         cfs_list_del_init(&dqid->di_link);
2135
2136                         if (!rc)
2137                                 cqget(sb, qctxt->qckt_hash, &qctxt->qckt_list,
2138                                       dqid->di_id, i,
2139                                       qctxt->qckt_first_check[i]);
2140                         OBD_FREE_PTR(dqid);
2141                 }
2142         }
2143 #endif
2144         /* turn off quota cause we are to dump chk_dqblk to files */
2145         quota_onoff(sb, Q_QUOTAOFF, oqc->qc_type, oqc->qc_id);
2146
2147         rc = create_new_quota_files(qctxt, oqc);
2148         if (rc)
2149                 GOTO(out, rc);
2150
2151         /* we use vfs functions to set dqblk, so turn quota on */
2152         rc = quota_onoff(sb, Q_QUOTAON, oqc->qc_type, oqc->qc_id);
2153 out:
2154         /* dump and free chk_dqblk */
2155         rc = prune_chkquots(sb, qctxt, rc);
2156         OBD_FREE_PTR(qctxt);
2157
2158         /* turn off quota, `lfs quotacheck` will turn on when all
2159          * nodes quotacheck finish. */
2160         quota_onoff(sb, Q_QUOTAOFF, oqc->qc_type, oqc->qc_id);
2161
2162         oqc->qc_stat = rc;
2163         if (rc)
2164                 CERROR("quotacheck failed: rc = %d\n", rc);
2165
2166         RETURN(rc);
2167 }
2168
2169 static int fsfilt_ext3_quotainfo(struct lustre_quota_info *lqi, int type,
2170                                  int cmd)
2171 {
2172         int rc = 0;
2173         ENTRY;
2174
2175         if (lqi->qi_files[type] == NULL) {
2176                 CERROR("operate qinfo before it's enabled!\n");
2177                 RETURN(-ESRCH);
2178         }
2179
2180         switch (cmd) {
2181         case QFILE_CHK:
2182                 rc = lustre_check_quota_file(lqi, type);
2183                 break;
2184         case QFILE_RD_INFO:
2185                 rc = lustre_read_quota_info(lqi, type);
2186                 break;
2187         case QFILE_WR_INFO:
2188                 rc = lustre_write_quota_info(lqi, type);
2189                 break;
2190         case QFILE_INIT_INFO:
2191                 rc = lustre_init_quota_info(lqi, type);
2192                 break;
2193         case QFILE_CONVERT:
2194                 rc = -ENOTSUPP;
2195                 CERROR("quota CONVERT command is not supported\n");
2196                 break;
2197         default:
2198                 rc = -ENOTSUPP;
2199                 CERROR("Unsupported admin quota file cmd %d\n"
2200                        "Are lquota.ko and fsfilt_ldiskfs.ko modules in sync?\n",
2201                        cmd);
2202                 break;
2203         }
2204         RETURN(rc);
2205 }
2206
2207 static int fsfilt_ext3_qids(struct file *file, struct inode *inode, int type,
2208                             cfs_list_t *list)
2209 {
2210         return lustre_get_qids(file, inode, type, list);
2211 }
2212
2213 static int fsfilt_ext3_dquot(struct lustre_dquot *dquot, int cmd)
2214 {
2215         int rc = 0;
2216         ENTRY;
2217
2218         if (dquot->dq_info->qi_files[dquot->dq_type] == NULL) {
2219                 CERROR("operate dquot before it's enabled!\n");
2220                 RETURN(-ESRCH);
2221         }
2222
2223         switch (cmd) {
2224         case QFILE_RD_DQUOT:
2225                 rc = lustre_read_dquot(dquot);
2226                 break;
2227         case QFILE_WR_DQUOT:
2228                 if (dquot->dq_dqb.dqb_ihardlimit ||
2229                     dquot->dq_dqb.dqb_isoftlimit ||
2230                     dquot->dq_dqb.dqb_bhardlimit ||
2231                     dquot->dq_dqb.dqb_bsoftlimit)
2232                         cfs_clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2233                 else
2234                         cfs_set_bit(DQ_FAKE_B, &dquot->dq_flags);
2235
2236                 rc = lustre_commit_dquot(dquot);
2237                 if (rc >= 0)
2238                         rc = 0;
2239                 break;
2240         default:
2241                 CERROR("Unsupported admin quota file cmd %d\n", cmd);
2242                 LBUG();
2243                 break;
2244         }
2245         RETURN(rc);
2246 }
2247
2248 static int fsfilt_ext3_get_mblk(struct super_block *sb, int *count,
2249                                 struct inode *inode, int frags)
2250 {
2251 #ifdef EXT3_EXT_HAS_NO_TREE
2252         struct ext3_ext_base *base = inode;
2253 #else
2254         struct ext3_extents_tree tree;
2255         struct ext3_ext_base *base = &tree;
2256
2257         ext3_init_tree_desc(base, inode);
2258 #endif
2259         /* for an ost_write request, it needs <#fragments> * <tree depth + 1>
2260          * metablocks at maxium b=16542 */
2261         *count = frags * (EXT_DEPTH(base) + 1) * EXT3_BLOCK_SIZE(sb);
2262         return 0;
2263 }
2264
2265 #endif
2266
2267 lvfs_sbdev_type fsfilt_ext3_journal_sbdev(struct super_block *sb)
2268 {
2269         return (EXT3_SB(sb)->journal_bdev);
2270 }
2271 EXPORT_SYMBOL(fsfilt_ext3_journal_sbdev);
2272
2273 ssize_t lustre_read_quota(struct file *f, struct inode *inode, int type,
2274                           char *buf, int count, loff_t pos)
2275 {
2276         loff_t p = pos;
2277         int rc;
2278
2279         if (!f && !inode) {
2280                 CERROR("lustre_read_quota failed for no quota file!\n");
2281                 libcfs_debug_dumpstack(NULL);
2282                 return -EINVAL;
2283         }
2284
2285         /* Support for both adm and op quota files must be provided */
2286         if (f) {
2287                 rc = fsfilt_ext3_read_record(f, buf, count, &p);
2288                 rc = rc < 0 ? rc : p - pos;
2289         } else {
2290                 struct super_block *sb = inode->i_sb;
2291                 rc = sb->s_op->quota_read(sb, type, buf, count, pos);
2292         }
2293         return rc;
2294 }
2295
2296 ssize_t lustre_write_quota(struct file *f, char *buf, int count, loff_t pos)
2297 {
2298         loff_t p = pos;
2299         int rc;
2300
2301         /* Only adm quota files are supported, op updates are handled by vfs */
2302         rc = fsfilt_ext3_write_record(f, buf, count, &p, 0);
2303         rc = rc < 0 ? rc : p - pos;
2304
2305         return rc;
2306 }
2307
2308 void *lustre_quota_journal_start(struct inode *inode, int delete)
2309 {
2310         handle_t *handle;
2311         unsigned block_count;
2312
2313         if (delete) {
2314                 /* each indirect block (+4) may become free, attaching to the
2315                  * header list of free blocks (+1); the data block (+1) may
2316                  * become a free block (+0) or a block with free dqentries (+0) */
2317                 block_count = (4 + 1) + 1;
2318                 handle = ext3_journal_start(inode,
2319                             block_count*FSFILT_DATA_TRANS_BLOCKS(inode->i_sb)+2);
2320         } else {
2321                 /* indirect blocks are touched (+4), each causes file expansion (+0) or
2322                  * freeblk reusage with a header update (+1); dqentry is either reused
2323                  * causing update of the entry block (+1), prev (+1) and next (+1) or
2324                  * a new block allocation (+1) with a header update (+1)              */
2325                 block_count = (4 + 1) + 3;
2326                 handle = ext3_journal_start(inode,
2327                              block_count*FSFILT_DATA_TRANS_BLOCKS(inode->i_sb)+2);
2328
2329         }
2330
2331         return handle;
2332 }
2333
2334 void lustre_quota_journal_stop(void *handle)
2335 {
2336         ext3_journal_stop((handle_t *)handle);
2337 }
2338
2339 static int ll_decode_fh_accept(void *context, struct dentry *de)
2340 {
2341         return 1;
2342 }
2343
2344 #ifdef HAVE_EXPORTFS_DECODE_FH
2345 # define ll_exportfs_decode_fh(mnt, fid, len, type, acceptable, context) \
2346          exportfs_decode_fh(mnt, (struct fid*)(fid), len, type,          \
2347                             acceptable, context)
2348 #else
2349 # define ll_exportfs_decode_fh(mnt, fid, len, type, acceptable, context) \
2350          export_op_default.decode_fh((mnt)->mnt_sb, &(fid)->ino, len,    \
2351                                      type, acceptable, context)
2352 # define FILEID_INO32_GEN 1
2353 extern struct export_operations export_op_default;
2354 #endif
2355
2356 struct dentry *fsfilt_ext3_fid2dentry(struct vfsmount *mnt,
2357                                       struct fsfilt_fid *fid, int ignore_gen)
2358 {
2359         struct inode  *inode;
2360         struct dentry *result;
2361         
2362         result = ll_exportfs_decode_fh(mnt, fid, 2, FILEID_INO32_GEN,
2363                                        ll_decode_fh_accept, NULL);
2364         if (IS_ERR(result)) {
2365                 CDEBUG(D_DENTRY, "%s of %u/%u failed %ld\n", __func__,
2366                        fid->ino, fid->gen, PTR_ERR(result));
2367                 return result;
2368         }
2369
2370         CDEBUG(D_DENTRY, "%s of %u/%u succeeded\n", __func__,
2371                fid->ino, fid->gen);
2372         inode = result->d_inode;
2373         if (inode == NULL)
2374                 goto err_out;
2375
2376         if (inode->i_nlink == 0 &&
2377             inode->i_mode == 0 && LTIME_S(inode->i_ctime) == 0) {
2378                 LCONSOLE_WARN("Found inode with zero nlink, mode and"
2379                               " ctime -- this may indicate disk "
2380                               "corruption (inode: %lu, link: %lu, "
2381                               "count: %d)\n", inode->i_ino,
2382                               (unsigned long)inode->i_nlink,
2383                               atomic_read(&inode->i_count));
2384                 goto err_out;
2385         }
2386         if (fid->gen && inode->i_generation != fid->gen) {
2387                 /* we didn't find the right inode.. */
2388                 CDEBUG(D_INODE, "found wrong generation: inode %lu, link: %lu, "
2389                        "count: %d, generation %u/%u\n",
2390                        inode->i_ino, (unsigned long)inode->i_nlink,
2391                        atomic_read(&inode->i_count), inode->i_generation,
2392                        fid->gen);
2393                 goto err_out;
2394         }
2395
2396         return result;
2397 err_out:
2398         l_dput(result);
2399         return ERR_PTR(-ENOENT);
2400 }
2401
2402 static struct fsfilt_operations fsfilt_ext3_ops = {
2403         .fs_type                = "ext3",
2404         .fs_owner               = THIS_MODULE,
2405         .fs_getlabel            = fsfilt_ext3_get_label,
2406         .fs_setlabel            = fsfilt_ext3_set_label,
2407         .fs_uuid                = fsfilt_ext3_uuid,
2408         .fs_start               = fsfilt_ext3_start,
2409         .fs_brw_start           = fsfilt_ext3_brw_start,
2410         .fs_extend              = fsfilt_ext3_extend,
2411         .fs_commit              = fsfilt_ext3_commit,
2412         .fs_commit_async        = fsfilt_ext3_commit_async,
2413         .fs_commit_wait         = fsfilt_ext3_commit_wait,
2414         .fs_setattr             = fsfilt_ext3_setattr,
2415         .fs_iocontrol           = fsfilt_ext3_iocontrol,
2416         .fs_set_md              = fsfilt_ext3_set_md,
2417         .fs_get_md              = fsfilt_ext3_get_md,
2418         .fs_readpage            = fsfilt_ext3_readpage,
2419         .fs_add_journal_cb      = fsfilt_ext3_add_journal_cb,
2420         .fs_statfs              = fsfilt_ext3_statfs,
2421         .fs_sync                = fsfilt_ext3_sync,
2422         .fs_map_inode_pages     = fsfilt_ext3_map_inode_pages,
2423         .fs_write_record        = fsfilt_ext3_write_record,
2424         .fs_read_record         = fsfilt_ext3_read_record,
2425         .fs_setup               = fsfilt_ext3_setup,
2426         .fs_send_bio            = fsfilt_ext3_send_bio,
2427         .fs_get_op_len          = fsfilt_ext3_get_op_len,
2428 #ifdef HAVE_DISK_INODE_VERSION
2429         .fs_get_version         = fsfilt_ext3_get_version,
2430         .fs_set_version         = fsfilt_ext3_set_version,
2431 #endif
2432 #ifdef HAVE_QUOTA_SUPPORT
2433         .fs_quotactl            = fsfilt_ext3_quotactl,
2434         .fs_quotacheck          = fsfilt_ext3_quotacheck,
2435         .fs_quotainfo           = fsfilt_ext3_quotainfo,
2436         .fs_qids                = fsfilt_ext3_qids,
2437         .fs_dquot               = fsfilt_ext3_dquot,
2438         .fs_get_mblk            = fsfilt_ext3_get_mblk,
2439 #endif
2440         .fs_journal_sbdev       = fsfilt_ext3_journal_sbdev,
2441         .fs_fid2dentry          = fsfilt_ext3_fid2dentry,
2442 };
2443
2444 static int __init fsfilt_ext3_init(void)
2445 {
2446         int rc;
2447
2448         fcb_cache = cfs_mem_cache_create("fsfilt_ext3_fcb",
2449                                          sizeof(struct fsfilt_cb_data), 0, 0);
2450         if (!fcb_cache) {
2451                 CERROR("error allocating fsfilt journal callback cache\n");
2452                 GOTO(out, rc = -ENOMEM);
2453         }
2454
2455         rc = fsfilt_register_ops(&fsfilt_ext3_ops);
2456
2457         if (rc) {
2458                 int err = cfs_mem_cache_destroy(fcb_cache);
2459                 LASSERTF(err == 0, "error destroying new cache: rc %d\n", err);
2460         }
2461 out:
2462         return rc;
2463 }
2464
2465 static void __exit fsfilt_ext3_exit(void)
2466 {
2467         int rc;
2468
2469         fsfilt_unregister_ops(&fsfilt_ext3_ops);
2470         rc = cfs_mem_cache_destroy(fcb_cache);
2471         LASSERTF(rc == 0, "couldn't destroy fcb_cache slab\n");
2472 }
2473
2474 module_init(fsfilt_ext3_init);
2475 module_exit(fsfilt_ext3_exit);
2476
2477 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2478 MODULE_DESCRIPTION("Lustre ext3 Filesystem Helper v0.1");
2479 MODULE_LICENSE("GPL");