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