Whamcloud - gitweb
New tag 2.3.52
[fs/lustre-release.git] / lustre / lvfs / fsfilt_ext3.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
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 #include <ext4/ext4.h>
53 #include <ext4/ext4_jbd2.h>
54 #include <linux/version.h>
55 #include <linux/bitops.h>
56 #include <linux/quota.h>
57
58 #if defined(HAVE_EXT3_XATTR_H)
59 # include <ext3/xattr.h>
60 #elif !defined(EXT3_XATTR_INDEX_TRUSTED)
61 /* ext3 xattr.h not available in rh style kernel-devel rpm */
62 /* CHAOS kernel-devel package will not include fs/ldiskfs/xattr.h */
63 # define EXT3_XATTR_INDEX_TRUSTED        4
64 extern int ext3_xattr_get(struct inode *, int, const char *, void *, size_t);
65 extern int ext3_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
66 #endif
67
68 #include <libcfs/libcfs.h>
69 #include <lustre_fsfilt.h>
70 #include <obd.h>
71 #include <linux/lustre_compat25.h>
72 #include <linux/lprocfs_status.h>
73
74 #include <ext4/ext4_extents.h>
75
76 /* for kernels 2.6.18 and later */
77 #define FSFILT_SINGLEDATA_TRANS_BLOCKS(sb) EXT3_SINGLEDATA_TRANS_BLOCKS(sb)
78
79 #define fsfilt_ext3_ext_insert_extent(handle, inode, path, newext, flag) \
80                ext3_ext_insert_extent(handle, inode, path, newext, flag)
81
82 #define ext3_mb_discard_inode_preallocations(inode) \
83                  ext3_discard_preallocations(inode)
84
85 #define fsfilt_log_start_commit(journal, tid) jbd2_log_start_commit(journal, tid)
86 #define fsfilt_log_wait_commit(journal, tid) jbd2_log_wait_commit(journal, tid)
87
88 #ifdef HAVE_EXT4_JOURNAL_CALLBACK_ADD
89 # define journal_callback ext4_journal_cb_entry
90 # define fsfilt_journal_callback_set(handle, func, jcb) \
91          ext4_journal_callback_add(handle, func, jcb)
92 #elif defined(HAVE_JBD2_JOURNAL_CALLBACK_SET)
93 # define fsfilt_journal_callback_set(handle, func, jcb) \
94          jbd2_journal_callback_set(handle, func, jcb)
95 #elif defined(HAVE_JOURNAL_CALLBACK_SET)
96 # define fsfilt_journal_callback_set(handle, func, jcb) \
97          journal_callback_set(handle, func, jcb)
98 #else
99 # error missing journal commit callback
100 #endif /* HAVE_EXT4_JOURNAL_CALLBACK_ADD */
101
102 static cfs_mem_cache_t *fcb_cache;
103
104 struct fsfilt_cb_data {
105         struct journal_callback cb_jcb; /* jbd private data - MUST BE FIRST */
106         fsfilt_cb_t cb_func;            /* MDS/OBD completion function */
107         struct obd_device *cb_obd;      /* MDS/OBD completion device */
108         __u64 cb_last_rcvd;             /* MDS/OST last committed operation */
109         void *cb_data;                  /* MDS/OST completion function data */
110 };
111
112 #ifndef ext3_find_next_bit
113 #define ext3_find_next_bit           ext2_find_next_bit
114 #endif
115
116 #ifndef ext2_find_next_bit
117 #ifdef __LITTLE_ENDIAN
118 #define ext2_find_next_bit(addr, size, off) find_next_bit((unsigned long *)(addr), (size), (off))
119 #else
120 error "Need implementation of find_next_bit on big-endian systems"
121 #endif  /* __LITTLE_ENDIAN */
122 #endif  /* !ext2_find_next_le_bit */
123
124 static char *fsfilt_ext3_get_label(struct super_block *sb)
125 {
126         return EXT3_SB(sb)->s_es->s_volume_name;
127 }
128
129 static int fsfilt_ext3_set_label(struct super_block *sb, char *label)
130 {
131         /* see e.g. fsfilt_ext3_write_record() */
132         journal_t *journal;
133         handle_t *handle;
134         int err;
135
136         journal = EXT3_SB(sb)->s_journal;
137         handle = ext3_journal_start_sb(sb, 1);
138         if (IS_ERR(handle)) {
139                 CERROR("can't start transaction\n");
140                 return(PTR_ERR(handle));
141         }
142
143         err = ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
144         if (err)
145                 goto out;
146
147         memcpy(EXT3_SB(sb)->s_es->s_volume_name, label,
148                sizeof(EXT3_SB(sb)->s_es->s_volume_name));
149
150         err = ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
151
152 out:
153         ext3_journal_stop(handle);
154
155         return(err);
156 }
157
158 static char *fsfilt_ext3_uuid(struct super_block *sb)
159 {
160         return EXT3_SB(sb)->s_es->s_uuid;
161 }
162
163 #ifdef HAVE_DISK_INODE_VERSION
164
165 static __u64 get_i_version(struct inode *inode)
166 {
167         return EXT3_I(inode)->i_fs_version;
168 }
169
170 static void set_i_version(struct inode *inode, __u64 new_version)
171 {
172         (EXT3_I(inode))->i_fs_version = new_version;
173 }
174
175 /*
176  * Get the 64-bit version for an inode.
177  */
178 static __u64 fsfilt_ext3_get_version(struct inode *inode)
179 {
180         CDEBUG(D_INFO, "Get version "LPX64" for inode %lu\n",
181                get_i_version(inode), inode->i_ino);
182         return get_i_version(inode);
183 }
184
185 /*
186  * Set the 64-bit version and return the old version.
187  */
188 static __u64 fsfilt_ext3_set_version(struct inode *inode, __u64 new_version)
189 {
190         __u64 old_version = get_i_version(inode);
191
192         CDEBUG(D_INFO, "Set version "LPX64" (old "LPX64") for inode %lu\n",
193                new_version, old_version, inode->i_ino);
194         set_i_version(inode, new_version);
195         /* version is set after all inode operations are finished, so we should
196          * mark it dirty here */
197         inode->i_sb->s_op->dirty_inode(inode);
198         return old_version;
199 }
200
201 #endif
202
203 /* kernel has ext4_blocks_for_truncate since linux-3.1.1 */
204 #ifdef HAVE_BLOCKS_FOR_TRUNCATE
205 # include <ext4/truncate.h>
206 #else
207 static inline unsigned long ext4_blocks_for_truncate(struct inode *inode)
208 {
209         ext4_lblk_t needed;
210
211         needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
212         if (needed < 2)
213                 needed = 2;
214         if (needed > EXT4_MAX_TRANS_DATA)
215                 needed = EXT4_MAX_TRANS_DATA;
216         return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed;
217 }
218 #endif
219
220 /*
221  * We don't currently need any additional blocks for rmdir and
222  * unlink transactions because we are storing the OST oa_id inside
223  * the inode (which we will be changing anyways as part of this
224  * transaction).
225  */
226 static void *fsfilt_ext3_start(struct inode *inode, int op, void *desc_private,
227                                int logs)
228 {
229         /* For updates to the last received file */
230         int nblocks = FSFILT_SINGLEDATA_TRANS_BLOCKS(inode->i_sb);
231         journal_t *journal;
232         void *handle;
233
234         if (current->journal_info) {
235                 CDEBUG(D_INODE, "increasing refcount on %p\n",
236                        current->journal_info);
237                 goto journal_start;
238         }
239
240         switch(op) {
241         case FSFILT_OP_RMDIR:
242         case FSFILT_OP_UNLINK:
243                 /* delete one file + create/update logs for each stripe */
244                 nblocks += EXT3_DELETE_TRANS_BLOCKS(inode->i_sb);
245                 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
246                             FSFILT_SINGLEDATA_TRANS_BLOCKS(inode->i_sb)) * logs;
247                 break;
248         case FSFILT_OP_RENAME:
249                 /* modify additional directory */
250                 nblocks += FSFILT_SINGLEDATA_TRANS_BLOCKS(inode->i_sb);
251                 /* no break */
252         case FSFILT_OP_SYMLINK:
253                 /* additional block + block bitmap + GDT for long symlink */
254                 nblocks += 3;
255                 /* no break */
256         case FSFILT_OP_CREATE: {
257                 /* no break */
258         }
259         case FSFILT_OP_MKDIR:
260         case FSFILT_OP_MKNOD:
261                 /* modify one inode + block bitmap + GDT */
262                 nblocks += 3;
263                 /* no break */
264         case FSFILT_OP_LINK:
265                 /* modify parent directory */
266                 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS +
267                            EXT3_DATA_TRANS_BLOCKS(inode->i_sb);
268                 /* create/update logs for each stripe */
269                 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
270                             FSFILT_SINGLEDATA_TRANS_BLOCKS(inode->i_sb)) * logs;
271                 break;
272         case FSFILT_OP_SETATTR:
273                 /* Setattr on inode */
274                 nblocks += 1;
275                 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS +
276                            EXT3_DATA_TRANS_BLOCKS(inode->i_sb);
277                 /* quota chown log for each stripe */
278                 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
279                             FSFILT_SINGLEDATA_TRANS_BLOCKS(inode->i_sb)) * logs;
280                 break;
281         case FSFILT_OP_CANCEL_UNLINK:
282                 LASSERT(logs == 1);
283
284                 /* blocks for log header bitmap update OR
285                  * blocks for catalog header bitmap update + unlink of logs +
286                  * blocks for delete the inode (include blocks truncating). */
287                 nblocks = (LLOG_CHUNK_SIZE >> inode->i_blkbits) +
288                           EXT3_DELETE_TRANS_BLOCKS(inode->i_sb) +
289                           ext4_blocks_for_truncate(inode) + 3;
290                 break;
291         default: CERROR("unknown transaction start op %d\n", op);
292                 LBUG();
293         }
294
295         LASSERT(current->journal_info == desc_private);
296         journal = EXT3_SB(inode->i_sb)->s_journal;
297         if (nblocks > journal->j_max_transaction_buffers) {
298                 CWARN("too many credits %d for op %ux%u using %d instead\n",
299                        nblocks, op, logs, journal->j_max_transaction_buffers);
300                 nblocks = journal->j_max_transaction_buffers;
301         }
302
303  journal_start:
304         LASSERTF(nblocks > 0, "can't start %d credit transaction\n", nblocks);
305         handle = ext3_journal_start(inode, nblocks);
306
307         if (!IS_ERR(handle))
308                 LASSERT(current->journal_info == handle);
309         else
310                 CERROR("error starting handle for op %u (%u credits): rc %ld\n",
311                        op, nblocks, PTR_ERR(handle));
312         return handle;
313 }
314
315 /*
316  * Calculate the number of buffer credits needed to write multiple pages in
317  * a single ext3 transaction.  No, this shouldn't be here, but as yet ext3
318  * doesn't have a nice API for calculating this sort of thing in advance.
319  *
320  * See comment above ext3_writepage_trans_blocks for details.  We assume
321  * no data journaling is being done, but it does allow for all of the pages
322  * being non-contiguous.  If we are guaranteed contiguous pages we could
323  * reduce the number of (d)indirect blocks a lot.
324  *
325  * With N blocks per page and P pages, for each inode we have at most:
326  * N*P indirect
327  * min(N*P, blocksize/4 + 1) dindirect blocks
328  * niocount tindirect
329  *
330  * For the entire filesystem, we have at most:
331  * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
332  * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
333  * objcount inode blocks
334  * 1 superblock
335  * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
336  *
337  * 1 EXT3_DATA_TRANS_BLOCKS for the last_rcvd update.
338  */
339 static int fsfilt_ext3_credits_needed(int objcount, struct fsfilt_objinfo *fso,
340                                       int niocount, struct niobuf_local *nb)
341 {
342         struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
343         __u64 next_indir;
344         const int blockpp = 1 << (CFS_PAGE_SHIFT - sb->s_blocksize_bits);
345         int nbitmaps = 0, ngdblocks;
346         int needed = objcount + 1; /* inodes + superblock */
347         int i, j;
348
349         for (i = 0, j = 0; i < objcount; i++, fso++) {
350                 /* two or more dindirect blocks in case we cross boundary */
351                 int ndind =
352                         (long)((nb[j + fso->fso_bufcnt - 1].lnb_file_offset -
353                                 nb[j].lnb_file_offset) >>
354                                sb->s_blocksize_bits) /
355                         (EXT3_ADDR_PER_BLOCK(sb) * EXT3_ADDR_PER_BLOCK(sb));
356                 nbitmaps += min(fso->fso_bufcnt, ndind > 0 ? ndind : 2);
357
358                 /* leaf, indirect, tindirect blocks for first block */
359                 nbitmaps += blockpp + 2;
360
361                 j += fso->fso_bufcnt;
362         }
363
364         next_indir = nb[0].lnb_file_offset +
365                      (EXT3_ADDR_PER_BLOCK(sb) << sb->s_blocksize_bits);
366         for (i = 1; i < niocount; i++) {
367                 if (nb[i].lnb_file_offset >= next_indir) {
368                         nbitmaps++;     /* additional indirect */
369                         next_indir = nb[i].lnb_file_offset +
370                                      (EXT3_ADDR_PER_BLOCK(sb) <<
371                                       sb->s_blocksize_bits);
372                 } else if (nb[i].lnb_file_offset !=
373                            nb[i - 1].lnb_file_offset + sb->s_blocksize) {
374                         nbitmaps++;     /* additional indirect */
375                 }
376                 nbitmaps += blockpp;    /* each leaf in different group? */
377         }
378
379         ngdblocks = nbitmaps;
380         if (nbitmaps > EXT3_SB(sb)->s_groups_count)
381                 nbitmaps = EXT3_SB(sb)->s_groups_count;
382         if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
383                 ngdblocks = EXT3_SB(sb)->s_gdb_count;
384
385         needed += nbitmaps + ngdblocks;
386
387         /* last_rcvd update */
388         needed += EXT3_DATA_TRANS_BLOCKS(sb);
389
390 #if defined(CONFIG_QUOTA)
391         /* We assume that there will be 1 bit set in s_dquot.flags for each
392          * quota file that is active.  This is at least true for now.
393          */
394         needed += hweight32(sb_any_quota_loaded(sb)) *
395                 FSFILT_SINGLEDATA_TRANS_BLOCKS(sb);
396 #endif
397
398         return needed;
399 }
400
401 /* We have to start a huge journal transaction here to hold all of the
402  * metadata for the pages being written here.  This is necessitated by
403  * the fact that we do lots of prepare_write operations before we do
404  * any of the matching commit_write operations, so even if we split
405  * up to use "smaller" transactions none of them could complete until
406  * all of them were opened.  By having a single journal transaction,
407  * we eliminate duplicate reservations for common blocks like the
408  * superblock and group descriptors or bitmaps.
409  *
410  * We will start the transaction here, but each prepare_write will
411  * add a refcount to the transaction, and each commit_write will
412  * remove a refcount.  The transaction will be closed when all of
413  * the pages have been written.
414  */
415 static void *fsfilt_ext3_brw_start(int objcount, struct fsfilt_objinfo *fso,
416                                    int niocount, struct niobuf_local *nb,
417                                    void *desc_private, int logs)
418 {
419         journal_t *journal;
420         handle_t *handle;
421         int needed;
422         ENTRY;
423
424         LASSERT(current->journal_info == desc_private);
425         journal = EXT3_SB(fso->fso_dentry->d_inode->i_sb)->s_journal;
426         needed = fsfilt_ext3_credits_needed(objcount, fso, niocount, nb);
427
428         /* The number of blocks we could _possibly_ dirty can very large.
429          * We reduce our request if it is absurd (and we couldn't get that
430          * many credits for a single handle anyways).
431          *
432          * At some point we have to limit the size of I/Os sent at one time,
433          * increase the size of the journal, or we have to calculate the
434          * actual journal requirements more carefully by checking all of
435          * the blocks instead of being maximally pessimistic.  It remains to
436          * be seen if this is a real problem or not.
437          */
438         if (needed > journal->j_max_transaction_buffers) {
439                 CERROR("want too many journal credits (%d) using %d instead\n",
440                        needed, journal->j_max_transaction_buffers);
441                 needed = journal->j_max_transaction_buffers;
442         }
443
444         LASSERTF(needed > 0, "can't start %d credit transaction\n", needed);
445         handle = ext3_journal_start(fso->fso_dentry->d_inode, needed);
446         if (IS_ERR(handle)) {
447                 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
448                        PTR_ERR(handle));
449         } else {
450                 LASSERT(handle->h_buffer_credits >= needed);
451                 LASSERT(current->journal_info == handle);
452         }
453
454         RETURN(handle);
455 }
456
457 static int fsfilt_ext3_extend(struct inode *inode, unsigned int nblocks,void *h)
458 {
459        handle_t *handle = h;
460
461        /* fsfilt_extend called with nblocks = 0 for testing in special cases */
462        if (nblocks == 0) {
463                handle->h_buffer_credits = 0;
464                CWARN("setting credits of handle %p to zero by request\n", h);
465        }
466
467        if (handle->h_buffer_credits > nblocks)
468                 return 0;
469        if (ext3_journal_extend(handle, nblocks) == 0)
470                 return 0;
471
472        ext3_mark_inode_dirty(handle, inode);
473        return ext3_journal_restart(handle, nblocks);
474 }
475
476 static int fsfilt_ext3_commit(struct inode *inode, void *h, int force_sync)
477 {
478         int rc;
479         handle_t *handle = h;
480
481         LASSERT(current->journal_info == handle);
482         if (force_sync)
483                 handle->h_sync = 1; /* recovery likes this */
484
485         rc = ext3_journal_stop(handle);
486
487         return rc;
488 }
489
490 static int fsfilt_ext3_commit_async(struct inode *inode, void *h,
491                                     void **wait_handle)
492 {
493         unsigned long tid;
494         transaction_t *transaction;
495         handle_t *handle = h;
496         journal_t *journal;
497         int rc;
498
499         LASSERT(current->journal_info == handle);
500
501         transaction = handle->h_transaction;
502         journal = transaction->t_journal;
503         tid = transaction->t_tid;
504         /* we don't want to be blocked */
505         handle->h_sync = 0;
506         rc = ext3_journal_stop(handle);
507         if (rc) {
508                 CERROR("error while stopping transaction: %d\n", rc);
509                 return rc;
510         }
511         fsfilt_log_start_commit(journal, tid);
512
513         *wait_handle = (void *) tid;
514         CDEBUG(D_INODE, "commit async: %lu\n", (unsigned long) tid);
515         return 0;
516 }
517
518 static int fsfilt_ext3_commit_wait(struct inode *inode, void *h)
519 {
520         journal_t *journal = EXT3_JOURNAL(inode);
521         tid_t tid = (tid_t)(long)h;
522
523         CDEBUG(D_INODE, "commit wait: %lu\n", (unsigned long) tid);
524         if (unlikely(is_journal_aborted(journal)))
525                 return -EIO;
526
527         fsfilt_log_wait_commit(EXT3_JOURNAL(inode), tid);
528
529         if (unlikely(is_journal_aborted(journal)))
530                 return -EIO;
531         return 0;
532 }
533
534 static int fsfilt_ext3_setattr(struct dentry *dentry, void *handle,
535                                struct iattr *iattr, int do_trunc)
536 {
537         struct inode *inode = dentry->d_inode;
538         int rc = 0;
539
540 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0)
541         /* Try to correct for a bug in 2.1.0 (LU-221) that caused negative
542          * timestamps to appear to be in the far future, due old timestamp
543          * being stored on disk as an unsigned value.  This fixes up any
544          * bad values held by the client before storing them on disk,
545          * and ensures any timestamp updates are correct.  LU-1042 */
546         if (unlikely(LTIME_S(inode->i_atime) == LU221_BAD_TIME &&
547                      !(iattr->ia_valid & ATTR_ATIME))) {
548                 iattr->ia_valid |= ATTR_ATIME;
549                 LTIME_S(iattr->ia_atime) = 0;
550         }
551         if (unlikely(LTIME_S(inode->i_mtime) == LU221_BAD_TIME &&
552                      !(iattr->ia_valid & ATTR_MTIME))) {
553                 iattr->ia_valid |= ATTR_MTIME;
554                 LTIME_S(iattr->ia_mtime) = 0;
555         }
556         if (unlikely((LTIME_S(inode->i_ctime) == LU221_BAD_TIME ||
557                       LTIME_S(inode->i_ctime) == 0) &&
558                      !(iattr->ia_valid & ATTR_CTIME))) {
559                 iattr->ia_valid |= ATTR_CTIME;
560                 LTIME_S(iattr->ia_ctime) = 0;
561         }
562 #else
563 #warning "remove old LU-221/LU-1042 workaround code"
564 #endif
565
566         /* When initializating timestamps for new inodes, use the filesystem
567          * mkfs time for ctime to avoid e2fsck ibadness incorrectly thinking
568          * that this is potentially an invalid inode.  Files with an old ctime
569          * migrated to a newly-formatted OST with a newer s_mkfs_time will not
570          * hit this check, since it is only for ctime == 0.  LU-1010/LU-1042 */
571         if ((iattr->ia_valid & ATTR_CTIME) && LTIME_S(iattr->ia_ctime) == 0)
572                 LTIME_S(iattr->ia_ctime) =
573                         EXT4_SB(inode->i_sb)->s_es->s_mkfs_time;
574
575         /* Avoid marking the inode dirty on the superblock list unnecessarily.
576          * We are already writing the inode to disk as part of this
577          * transaction and want to avoid a lot of extra inode writeout
578          * later on. b=9828 */
579         if (iattr->ia_valid & ATTR_SIZE && !do_trunc) {
580                 /* ATTR_SIZE would invoke truncate: clear it */
581                 iattr->ia_valid &= ~ATTR_SIZE;
582                 EXT3_I(inode)->i_disksize = iattr->ia_size;
583                 i_size_write(inode, iattr->ia_size);
584
585                 if (iattr->ia_valid & ATTR_UID)
586                         inode->i_uid = iattr->ia_uid;
587                 if (iattr->ia_valid & ATTR_GID)
588                         inode->i_gid = iattr->ia_gid;
589                 if (iattr->ia_valid & ATTR_ATIME)
590                         inode->i_atime = iattr->ia_atime;
591                 if (iattr->ia_valid & ATTR_MTIME)
592                         inode->i_mtime = iattr->ia_mtime;
593                 if (iattr->ia_valid & ATTR_CTIME)
594                         inode->i_ctime = iattr->ia_ctime;
595                 if (iattr->ia_valid & ATTR_MODE) {
596                         inode->i_mode = iattr->ia_mode;
597
598                         if (!cfs_curproc_is_in_groups(inode->i_gid) &&
599                             !cfs_capable(CFS_CAP_FSETID))
600                                 inode->i_mode &= ~S_ISGID;
601                 }
602
603                 inode->i_sb->s_op->dirty_inode(inode);
604
605                 goto out;
606         }
607
608         /* Don't allow setattr to change file type */
609         if (iattr->ia_valid & ATTR_MODE)
610                 iattr->ia_mode = (inode->i_mode & S_IFMT) |
611                                  (iattr->ia_mode & ~S_IFMT);
612
613         /* We set these flags on the client, but have already checked perms
614          * so don't confuse inode_change_ok. */
615         iattr->ia_valid &= ~TIMES_SET_FLAGS;
616
617         if (inode->i_op->setattr) {
618                 rc = inode->i_op->setattr(dentry, iattr);
619         } else {
620 #ifndef HAVE_SIMPLE_SETATTR /* simple_setattr() already call it */
621                 rc = inode_change_ok(inode, iattr);
622                 if (!rc)
623 #endif
624                         rc = simple_setattr(dentry, iattr);
625         }
626
627  out:
628         RETURN(rc);
629 }
630
631 static int fsfilt_ext3_iocontrol(struct inode *inode, struct file *file,
632                                  unsigned int cmd, unsigned long arg)
633 {
634         int rc = 0;
635         ENTRY;
636
637         /* FIXME: Can't do this because of nested transaction deadlock */
638         if (cmd == EXT3_IOC_SETFLAGS) {
639                 /* We can't enable data journaling on OST objects, because
640                 * this forces the transaction to be closed in order to
641                 * flush the journal, but the caller will already have a
642                 * compound transaction open to update the last_rcvd file,
643                 * and this thread would deadlock trying to set the flag. */
644                 if ((*(int *)arg) & EXT3_JOURNAL_DATA_FL) {
645                         CERROR("can't set data journal flag on file\n");
646                         RETURN(-EPERM);
647                 }
648                 /* Because the MDS does not see the EXTENTS_FL set on the
649                  * OST objects, mask this flag into all set flags.  It is
650                  * not legal to clear this flag in any case, so we are not
651                  * changing the functionality by doing this.  b=22911 */
652                 *(int *)arg |= EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL;
653         }
654
655         /* ext4_ioctl does not have a inode argument */
656         if (inode->i_fop->unlocked_ioctl)
657                 rc = inode->i_fop->unlocked_ioctl(file, cmd, arg);
658         else
659                 RETURN(-ENOTTY);
660
661         RETURN(rc);
662 }
663
664 static int fsfilt_ext3_set_md(struct inode *inode, void *handle,
665                               void *lmm, int lmm_size, const char *name)
666 {
667         int rc;
668
669         LASSERT(mutex_trylock(&inode->i_mutex) == 0);
670
671         rc = ext3_xattr_set_handle(handle, inode, EXT3_XATTR_INDEX_TRUSTED,
672                                    name, lmm, lmm_size, XATTR_NO_CTIME);
673
674         if (rc && rc != -EROFS)
675                 CERROR("error adding MD data to inode %lu: rc = %d\n",
676                        inode->i_ino, rc);
677         return rc;
678 }
679
680 /* Must be called with i_mutex held */
681 static int fsfilt_ext3_get_md(struct inode *inode, void *lmm, int lmm_size,
682                               const char *name)
683 {
684         int rc;
685
686         LASSERT(mutex_trylock(&inode->i_mutex) == 0);
687
688         rc = ext3_xattr_get(inode, EXT3_XATTR_INDEX_TRUSTED,
689                             name, lmm, lmm_size);
690
691         /* This gives us the MD size */
692         if (lmm == NULL)
693                 return (rc == -ENODATA) ? 0 : rc;
694
695         if (rc < 0) {
696                 CDEBUG(D_INFO, "error getting EA %d/%s from inode %lu: rc %d\n",
697                        EXT3_XATTR_INDEX_TRUSTED, name,
698                        inode->i_ino, rc);
699                 memset(lmm, 0, lmm_size);
700                 return (rc == -ENODATA) ? 0 : rc;
701         }
702
703         return rc;
704 }
705
706 static int fsfilt_ext3_send_bio(int rw, struct inode *inode, struct bio *bio)
707 {
708         submit_bio(rw, bio);
709         return 0;
710 }
711
712 static ssize_t fsfilt_ext3_readpage(struct file *file, char *buf, size_t count,
713                                     loff_t *off)
714 {
715         struct inode *inode = file->f_dentry->d_inode;
716         int rc = 0;
717
718         if (S_ISREG(inode->i_mode))
719                 rc = file->f_op->read(file, buf, count, off);
720         else {
721                 const int blkbits = inode->i_sb->s_blocksize_bits;
722                 const int blksize = inode->i_sb->s_blocksize;
723
724                 CDEBUG(D_EXT2, "reading %lu at dir %lu+%llu\n",
725                        (unsigned long)count, inode->i_ino, *off);
726                 while (count > 0) {
727                         struct buffer_head *bh;
728
729                         bh = NULL;
730                         if (*off < i_size_read(inode)) {
731                                 int err = 0;
732
733                                 bh = ext3_bread(NULL, inode, *off >> blkbits,
734                                                 0, &err);
735
736                                 CDEBUG(D_EXT2, "read %u@%llu\n", blksize, *off);
737
738                                 if (bh) {
739                                         memcpy(buf, bh->b_data, blksize);
740                                         brelse(bh);
741                                 } else if (err) {
742                                         /* XXX in theory we should just fake
743                                          * this buffer and continue like ext3,
744                                          * especially if this is a partial read
745                                          */
746                                         CERROR("error read dir %lu+%llu: %d\n",
747                                                inode->i_ino, *off, err);
748                                         RETURN(err);
749                                 }
750                         }
751                         if (!bh) {
752                                 struct ext3_dir_entry_2 *fake = (void *)buf;
753
754                                 CDEBUG(D_EXT2, "fake %u@%llu\n", blksize, *off);
755                                 memset(fake, 0, sizeof(*fake));
756                                 fake->rec_len = cpu_to_le16(blksize);
757                         }
758                         count -= blksize;
759                         buf += blksize;
760                         *off += blksize;
761                         rc += blksize;
762                 }
763         }
764
765         return rc;
766 }
767
768 #ifdef HAVE_EXT4_JOURNAL_CALLBACK_ADD
769 static void fsfilt_ext3_cb_func(struct super_block *sb,
770                                 struct journal_callback *jcb, int error)
771 #else
772 static void fsfilt_ext3_cb_func(struct journal_callback *jcb, int error)
773 #endif
774 {
775         struct fsfilt_cb_data *fcb = container_of(jcb, typeof(*fcb), cb_jcb);
776
777         fcb->cb_func(fcb->cb_obd, fcb->cb_last_rcvd, fcb->cb_data, error);
778
779         OBD_SLAB_FREE(fcb, fcb_cache, sizeof *fcb);
780 }
781
782 static int fsfilt_ext3_add_journal_cb(struct obd_device *obd, __u64 last_rcvd,
783                                       void *handle, fsfilt_cb_t cb_func,
784                                       void *cb_data)
785 {
786         struct fsfilt_cb_data *fcb;
787
788         OBD_SLAB_ALLOC_PTR_GFP(fcb, fcb_cache, CFS_ALLOC_IO);
789         if (fcb == NULL)
790                 RETURN(-ENOMEM);
791
792         fcb->cb_func = cb_func;
793         fcb->cb_obd = obd;
794         fcb->cb_last_rcvd = last_rcvd;
795         fcb->cb_data = cb_data;
796
797         CDEBUG(D_EXT2, "set callback for last_rcvd: "LPD64"\n", last_rcvd);
798         fsfilt_journal_callback_set(handle, fsfilt_ext3_cb_func, &fcb->cb_jcb);
799
800         return 0;
801 }
802
803 static int fsfilt_ext3_statfs(struct super_block *sb, struct obd_statfs *osfs)
804 {
805         struct kstatfs sfs;
806         int rc;
807
808         memset(&sfs, 0, sizeof(sfs));
809         rc = sb->s_op->statfs(sb->s_root, &sfs);
810         statfs_pack(osfs, &sfs);
811         return rc;
812 }
813
814 static int fsfilt_ext3_sync(struct super_block *sb)
815 {
816         return ext3_force_commit(sb);
817 }
818
819 #ifndef EXT3_EXTENTS_FL
820 #define EXT3_EXTENTS_FL                 0x00080000 /* Inode uses extents */
821 #endif
822
823 #ifndef EXT_ASSERT
824 #define EXT_ASSERT(cond)  BUG_ON(!(cond))
825 #endif
826
827 #define EXT_GENERATION(inode)           (EXT4_I(inode)->i_ext_generation)
828 #define ext3_ext_base                   inode
829 #define ext3_ext_base2inode(inode)      (inode)
830 #define EXT_DEPTH(inode)                ext_depth(inode)
831 #define fsfilt_ext3_ext_walk_space(inode, block, num, cb, cbdata) \
832                         ext3_ext_walk_space(inode, block, num, cb, cbdata);
833
834 struct bpointers {
835         unsigned long *blocks;
836         int *created;
837         unsigned long start;
838         int num;
839         int init_num;
840         int create;
841 };
842
843 static long ext3_ext_find_goal(struct inode *inode, struct ext3_ext_path *path,
844                                unsigned long block, int *aflags)
845 {
846         struct ext3_inode_info *ei = EXT3_I(inode);
847         unsigned long bg_start;
848         unsigned long colour;
849         int depth;
850
851         if (path) {
852                 struct ext3_extent *ex;
853                 depth = path->p_depth;
854
855                 /* try to predict block placement */
856                 if ((ex = path[depth].p_ext))
857                         return ext_pblock(ex) + (block - le32_to_cpu(ex->ee_block));
858
859                 /* it looks index is empty
860                  * try to find starting from index itself */
861                 if (path[depth].p_bh)
862                         return path[depth].p_bh->b_blocknr;
863         }
864
865         /* OK. use inode's group */
866         bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
867                 le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
868         colour = (current->pid % 16) *
869                 (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
870         return bg_start + colour + block;
871 }
872
873 #define ll_unmap_underlying_metadata(sb, blocknr) \
874         unmap_underlying_metadata((sb)->s_bdev, blocknr)
875
876 #ifndef EXT3_MB_HINT_GROUP_ALLOC
877 static unsigned long new_blocks(handle_t *handle, struct ext3_ext_base *base,
878                                 struct ext3_ext_path *path, unsigned long block,
879                                 unsigned long *count, int *err)
880 {
881         unsigned long pblock, goal;
882         int aflags = 0;
883         struct inode *inode = ext3_ext_base2inode(base);
884
885         goal = ext3_ext_find_goal(inode, path, block, &aflags);
886         aflags |= 2; /* block have been already reserved */
887         pblock = ext3_mb_new_blocks(handle, inode, goal, count, aflags, err);
888         return pblock;
889
890 }
891 #else
892 static unsigned long new_blocks(handle_t *handle, struct ext3_ext_base *base,
893                                 struct ext3_ext_path *path, unsigned long block,
894                                 unsigned long *count, int *err)
895 {
896         struct inode *inode = ext3_ext_base2inode(base);
897         struct ext3_allocation_request ar;
898         unsigned long pblock;
899         int aflags;
900
901         /* find neighbour allocated blocks */
902         ar.lleft = block;
903         *err = ext3_ext_search_left(base, path, &ar.lleft, &ar.pleft);
904         if (*err)
905                 return 0;
906         ar.lright = block;
907         *err = ext3_ext_search_right(base, path, &ar.lright, &ar.pright);
908         if (*err)
909                 return 0;
910
911         /* allocate new block */
912         ar.goal = ext3_ext_find_goal(inode, path, block, &aflags);
913         ar.inode = inode;
914         ar.logical = block;
915         ar.len = *count;
916         ar.flags = EXT3_MB_HINT_DATA;
917         pblock = ext3_mb_new_blocks(handle, &ar, err);
918         *count = ar.len;
919         return pblock;
920 }
921 #endif
922
923 static int ext3_ext_new_extent_cb(struct ext3_ext_base *base,
924                                   struct ext3_ext_path *path,
925                                   struct ext3_ext_cache *cex,
926 #ifdef HAVE_EXT_PREPARE_CB_EXTENT
927                                    struct ext3_extent *ex,
928 #endif
929                                   void *cbdata)
930 {
931         struct bpointers *bp = cbdata;
932         struct inode *inode = ext3_ext_base2inode(base);
933         struct ext3_extent nex;
934         unsigned long pblock;
935         unsigned long tgen;
936         int err, i;
937         unsigned long count;
938         handle_t *handle;
939
940         if (cex->ec_type == EXT3_EXT_CACHE_EXTENT) {
941                 err = EXT_CONTINUE;
942                 goto map;
943         }
944
945         if (bp->create == 0) {
946                 i = 0;
947                 if (cex->ec_block < bp->start)
948                         i = bp->start - cex->ec_block;
949                 if (i >= cex->ec_len)
950                         CERROR("nothing to do?! i = %d, e_num = %u\n",
951                                         i, cex->ec_len);
952                 for (; i < cex->ec_len && bp->num; i++) {
953                         *(bp->created) = 0;
954                         bp->created++;
955                         *(bp->blocks) = 0;
956                         bp->blocks++;
957                         bp->num--;
958                         bp->start++;
959                 }
960
961                 return EXT_CONTINUE;
962         }
963
964         tgen = EXT_GENERATION(base);
965         count = ext3_ext_calc_credits_for_insert(base, path);
966
967         handle = ext3_journal_start(inode, count+EXT3_ALLOC_NEEDED+1);
968         if (IS_ERR(handle)) {
969                 return PTR_ERR(handle);
970         }
971
972         if (tgen != EXT_GENERATION(base)) {
973                 /* the tree has changed. so path can be invalid at moment */
974                 ext3_journal_stop(handle);
975                 return EXT_REPEAT;
976         }
977
978         /* In 2.6.32 kernel, ext4_ext_walk_space()'s callback func is not
979          * protected by i_data_sem as whole. so we patch it to store
980          * generation to path and now verify the tree hasn't changed */
981         down_write((&EXT4_I(inode)->i_data_sem));
982
983         /* validate extent, make sure the extent tree does not changed */
984         if (EXT_GENERATION(base) != path[0].p_generation) {
985                 /* cex is invalid, try again */
986                 up_write(&EXT4_I(inode)->i_data_sem);
987                 ext3_journal_stop(handle);
988                 return EXT_REPEAT;
989         }
990
991         count = cex->ec_len;
992         pblock = new_blocks(handle, base, path, cex->ec_block, &count, &err);
993         if (!pblock)
994                 goto out;
995         EXT_ASSERT(count <= cex->ec_len);
996
997         /* insert new extent */
998         nex.ee_block = cpu_to_le32(cex->ec_block);
999         ext3_ext_store_pblock(&nex, pblock);
1000         nex.ee_len = cpu_to_le16(count);
1001         err = fsfilt_ext3_ext_insert_extent(handle, base, path, &nex, 0);
1002         if (err) {
1003                 /* free data blocks we just allocated */
1004                 /* not a good idea to call discard here directly,
1005                  * but otherwise we'd need to call it every free() */
1006 #ifdef EXT3_MB_HINT_GROUP_ALLOC
1007                 ext3_mb_discard_inode_preallocations(inode);
1008 #endif
1009                 ext3_free_blocks(handle, inode, ext_pblock(&nex),
1010                                  cpu_to_le16(nex.ee_len), 0);
1011                 goto out;
1012         }
1013
1014         /*
1015          * Putting len of the actual extent we just inserted,
1016          * we are asking ext3_ext_walk_space() to continue
1017          * scaning after that block
1018          */
1019         cex->ec_len = le16_to_cpu(nex.ee_len);
1020         cex->ec_start = ext_pblock(&nex);
1021         BUG_ON(le16_to_cpu(nex.ee_len) == 0);
1022         BUG_ON(le32_to_cpu(nex.ee_block) != cex->ec_block);
1023
1024 out:
1025         up_write((&EXT4_I(inode)->i_data_sem));
1026         ext3_journal_stop(handle);
1027 map:
1028         if (err >= 0) {
1029                 /* map blocks */
1030                 if (bp->num == 0) {
1031                         CERROR("hmm. why do we find this extent?\n");
1032                         CERROR("initial space: %lu:%u\n",
1033                                 bp->start, bp->init_num);
1034                         CERROR("current extent: %u/%u/%llu %d\n",
1035                                 cex->ec_block, cex->ec_len,
1036                                 (unsigned long long)cex->ec_start,
1037                                 cex->ec_type);
1038                 }
1039                 i = 0;
1040                 if (cex->ec_block < bp->start)
1041                         i = bp->start - cex->ec_block;
1042                 if (i >= cex->ec_len)
1043                         CERROR("nothing to do?! i = %d, e_num = %u\n",
1044                                         i, cex->ec_len);
1045                 for (; i < cex->ec_len && bp->num; i++) {
1046                         *(bp->blocks) = cex->ec_start + i;
1047                         if (cex->ec_type == EXT3_EXT_CACHE_EXTENT) {
1048                                 *(bp->created) = 0;
1049                         } else {
1050                                 *(bp->created) = 1;
1051                                 /* unmap any possible underlying metadata from
1052                                  * the block device mapping.  bug 6998. */
1053                                 ll_unmap_underlying_metadata(inode->i_sb,
1054                                                              *(bp->blocks));
1055                         }
1056                         bp->created++;
1057                         bp->blocks++;
1058                         bp->num--;
1059                         bp->start++;
1060                 }
1061         }
1062         return err;
1063 }
1064
1065 int fsfilt_map_nblocks(struct inode *inode, unsigned long block,
1066                        unsigned long num, unsigned long *blocks,
1067                        int *created, int create)
1068 {
1069         struct ext3_ext_base *base = inode;
1070         struct bpointers bp;
1071         int err;
1072
1073         CDEBUG(D_OTHER, "blocks %lu-%lu requested for inode %u\n",
1074                block, block + num - 1, (unsigned) inode->i_ino);
1075
1076         bp.blocks = blocks;
1077         bp.created = created;
1078         bp.start = block;
1079         bp.init_num = bp.num = num;
1080         bp.create = create;
1081
1082         err = fsfilt_ext3_ext_walk_space(base, block, num,
1083                                          ext3_ext_new_extent_cb, &bp);
1084         ext3_ext_invalidate_cache(base);
1085
1086         return err;
1087 }
1088
1089 int fsfilt_ext3_map_ext_inode_pages(struct inode *inode, struct page **page,
1090                                     int pages, unsigned long *blocks,
1091                                     int *created, int create)
1092 {
1093         int blocks_per_page = CFS_PAGE_SIZE >> inode->i_blkbits;
1094         int rc = 0, i = 0;
1095         struct page *fp = NULL;
1096         int clen = 0;
1097
1098         CDEBUG(D_OTHER, "inode %lu: map %d pages from %lu\n",
1099                 inode->i_ino, pages, (*page)->index);
1100
1101         /* pages are sorted already. so, we just have to find
1102          * contig. space and process them properly */
1103         while (i < pages) {
1104                 if (fp == NULL) {
1105                         /* start new extent */
1106                         fp = *page++;
1107                         clen = 1;
1108                         i++;
1109                         continue;
1110                 } else if (fp->index + clen == (*page)->index) {
1111                         /* continue the extent */
1112                         page++;
1113                         clen++;
1114                         i++;
1115                         continue;
1116                 }
1117
1118                 /* process found extent */
1119                 rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page,
1120                                         clen * blocks_per_page, blocks,
1121                                         created, create);
1122                 if (rc)
1123                         GOTO(cleanup, rc);
1124
1125                 /* look for next extent */
1126                 fp = NULL;
1127                 blocks += blocks_per_page * clen;
1128                 created += blocks_per_page * clen;
1129         }
1130
1131         if (fp)
1132                 rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page,
1133                                         clen * blocks_per_page, blocks,
1134                                         created, create);
1135 cleanup:
1136         return rc;
1137 }
1138
1139 extern int ext3_map_inode_page(struct inode *inode, struct page *page,
1140                                unsigned long *blocks, int *created, int create);
1141 int fsfilt_ext3_map_bm_inode_pages(struct inode *inode, struct page **page,
1142                                    int pages, unsigned long *blocks,
1143                                    int *created, int create)
1144 {
1145         int blocks_per_page = CFS_PAGE_SIZE >> inode->i_blkbits;
1146         unsigned long *b;
1147         int rc = 0, i, *cr;
1148
1149         for (i = 0, cr = created, b = blocks; i < pages; i++, page++) {
1150                 rc = ext3_map_inode_page(inode, *page, b, cr, create);
1151                 if (rc) {
1152                         CERROR("ino %lu, blk %lu cr %u create %d: rc %d\n",
1153                                inode->i_ino, *b, *cr, create, rc);
1154                         break;
1155                 }
1156
1157                 b += blocks_per_page;
1158                 cr += blocks_per_page;
1159         }
1160         return rc;
1161 }
1162
1163 int fsfilt_ext3_map_inode_pages(struct inode *inode, struct page **page,
1164                                 int pages, unsigned long *blocks,
1165                                 int *created, int create,
1166                                 cfs_mutex_t *optional_mutex)
1167 {
1168         int rc;
1169
1170         if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL) {
1171                 rc = fsfilt_ext3_map_ext_inode_pages(inode, page, pages,
1172                                                      blocks, created, create);
1173                 return rc;
1174         }
1175         if (optional_mutex != NULL)
1176                 cfs_mutex_lock(optional_mutex);
1177         rc = fsfilt_ext3_map_bm_inode_pages(inode, page, pages, blocks,
1178                                             created, create);
1179         if (optional_mutex != NULL)
1180                 cfs_mutex_unlock(optional_mutex);
1181
1182         return rc;
1183 }
1184
1185 int fsfilt_ext3_read(struct inode *inode, void *buf, int size, loff_t *offs)
1186 {
1187         unsigned long block;
1188         struct buffer_head *bh;
1189         int err, blocksize, csize, boffs, osize = size;
1190
1191         /* prevent reading after eof */
1192         spin_lock(&inode->i_lock);
1193         if (i_size_read(inode) < *offs + size) {
1194                 size = i_size_read(inode) - *offs;
1195                 spin_unlock(&inode->i_lock);
1196                 if (size < 0) {
1197                         CDEBUG(D_EXT2, "size %llu is too short for read @%llu\n",
1198                                i_size_read(inode), *offs);
1199                         return -EBADR;
1200                 } else if (size == 0) {
1201                         return 0;
1202                 }
1203         } else {
1204                 spin_unlock(&inode->i_lock);
1205         }
1206
1207         blocksize = 1 << inode->i_blkbits;
1208
1209         while (size > 0) {
1210                 block = *offs >> inode->i_blkbits;
1211                 boffs = *offs & (blocksize - 1);
1212                 csize = min(blocksize - boffs, size);
1213                 bh = ext3_bread(NULL, inode, block, 0, &err);
1214                 if (!bh) {
1215                         CERROR("can't read block: %d\n", err);
1216                         return err;
1217                 }
1218
1219                 memcpy(buf, bh->b_data + boffs, csize);
1220                 brelse(bh);
1221
1222                 *offs += csize;
1223                 buf += csize;
1224                 size -= csize;
1225         }
1226         return osize;
1227 }
1228 EXPORT_SYMBOL(fsfilt_ext3_read);
1229
1230 static int fsfilt_ext3_read_record(struct file * file, void *buf,
1231                                    int size, loff_t *offs)
1232 {
1233         int rc;
1234         rc = fsfilt_ext3_read(file->f_dentry->d_inode, buf, size, offs);
1235         if (rc > 0)
1236                 rc = 0;
1237         return rc;
1238 }
1239
1240 int fsfilt_ext3_write_handle(struct inode *inode, void *buf, int bufsize,
1241                                 loff_t *offs, handle_t *handle)
1242 {
1243         struct buffer_head *bh = NULL;
1244         loff_t old_size = i_size_read(inode), offset = *offs;
1245         loff_t new_size = i_size_read(inode);
1246         unsigned long block;
1247         int err = 0, blocksize = 1 << inode->i_blkbits, size, boffs;
1248
1249         while (bufsize > 0) {
1250                 if (bh != NULL)
1251                         brelse(bh);
1252
1253                 block = offset >> inode->i_blkbits;
1254                 boffs = offset & (blocksize - 1);
1255                 size = min(blocksize - boffs, bufsize);
1256                 bh = ext3_bread(handle, inode, block, 1, &err);
1257                 if (!bh) {
1258                         CERROR("can't read/create block: %d\n", err);
1259                         break;
1260                 }
1261
1262                 err = ext3_journal_get_write_access(handle, bh);
1263                 if (err) {
1264                         CERROR("journal_get_write_access() returned error %d\n",
1265                                err);
1266                         break;
1267                 }
1268                 LASSERT(bh->b_data + boffs + size <= bh->b_data + bh->b_size);
1269                 memcpy(bh->b_data + boffs, buf, size);
1270                 err = ext3_journal_dirty_metadata(handle, bh);
1271                 if (err) {
1272                         CERROR("journal_dirty_metadata() returned error %d\n",
1273                                err);
1274                         break;
1275                 }
1276                 if (offset + size > new_size)
1277                         new_size = offset + size;
1278                 offset += size;
1279                 bufsize -= size;
1280                 buf += size;
1281         }
1282         if (bh)
1283                 brelse(bh);
1284
1285         /* correct in-core and on-disk sizes */
1286         if (new_size > i_size_read(inode)) {
1287                 spin_lock(&inode->i_lock);
1288                 if (new_size > i_size_read(inode))
1289                         i_size_write(inode, new_size);
1290                 if (i_size_read(inode) > EXT3_I(inode)->i_disksize)
1291                         EXT3_I(inode)->i_disksize = i_size_read(inode);
1292                 if (i_size_read(inode) > old_size) {
1293                         spin_unlock(&inode->i_lock);
1294                         mark_inode_dirty(inode);
1295                 } else {
1296                         spin_unlock(&inode->i_lock);
1297                 }
1298         }
1299
1300         if (err == 0)
1301                 *offs = offset;
1302         return err;
1303 }
1304 EXPORT_SYMBOL(fsfilt_ext3_write_handle);
1305
1306 static int fsfilt_ext3_write_record(struct file *file, void *buf, int bufsize,
1307                                     loff_t *offs, int force_sync)
1308 {
1309         struct inode *inode = file->f_dentry->d_inode;
1310         handle_t *handle;
1311         int err, block_count = 0, blocksize;
1312
1313         /* Determine how many transaction credits are needed */
1314         blocksize = 1 << inode->i_blkbits;
1315         block_count = (*offs & (blocksize - 1)) + bufsize;
1316         block_count = (block_count + blocksize - 1) >> inode->i_blkbits;
1317
1318         handle = ext3_journal_start(inode,
1319                         block_count * EXT3_DATA_TRANS_BLOCKS(inode->i_sb) + 2);
1320         if (IS_ERR(handle)) {
1321                 CERROR("can't start transaction for %d blocks (%d bytes)\n",
1322                        block_count * EXT3_DATA_TRANS_BLOCKS(inode->i_sb) + 2,
1323                        bufsize);
1324                 return PTR_ERR(handle);
1325         }
1326
1327         err = fsfilt_ext3_write_handle(inode, buf, bufsize, offs, handle);
1328
1329         if (!err && force_sync)
1330                 handle->h_sync = 1; /* recovery likes this */
1331
1332         ext3_journal_stop(handle);
1333
1334         return err;
1335 }
1336
1337 static int fsfilt_ext3_setup(struct super_block *sb)
1338 {
1339         if (!EXT3_HAS_COMPAT_FEATURE(sb,
1340                                 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
1341                 CERROR("ext3 mounted without journal\n");
1342                 return -EINVAL;
1343         }
1344
1345 #ifdef S_PDIROPS
1346         CWARN("Enabling PDIROPS\n");
1347         set_opt(EXT3_SB(sb)->s_mount_opt, PDIROPS);
1348         sb->s_flags |= S_PDIROPS;
1349 #endif
1350         if (!EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_DIR_INDEX))
1351                 CWARN("filesystem doesn't have dir_index feature enabled\n");
1352         return 0;
1353 }
1354
1355 /* If fso is NULL, op is FSFILT operation, otherwise op is number of fso
1356    objects. Logs is number of logfiles to update */
1357 static int fsfilt_ext3_get_op_len(int op, struct fsfilt_objinfo *fso, int logs)
1358 {
1359         if ( !fso ) {
1360                 switch(op) {
1361                 case FSFILT_OP_CREATE:
1362                                  /* directory leaf, index & indirect & EA*/
1363                         return 4 + 3 * logs;
1364                 case FSFILT_OP_UNLINK:
1365                         return 3 * logs;
1366                 }
1367         } else {
1368                 int i;
1369                 int needed = 0;
1370                 struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
1371                 int blockpp = 1 << (CFS_PAGE_SHIFT - sb->s_blocksize_bits);
1372                 int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
1373                 for (i = 0; i < op; i++, fso++) {
1374                         int nblocks = fso->fso_bufcnt * blockpp;
1375                         int ndindirect = min(nblocks, addrpp + 1);
1376                         int nindir = nblocks + ndindirect + 1;
1377
1378                         needed += nindir;
1379                 }
1380                 return needed + 3 * logs;
1381         }
1382
1383         return 0;
1384 }
1385
1386 lvfs_sbdev_type fsfilt_ext3_journal_sbdev(struct super_block *sb)
1387 {
1388         return (EXT3_SB(sb)->journal_bdev);
1389 }
1390 EXPORT_SYMBOL(fsfilt_ext3_journal_sbdev);
1391
1392 static int ll_decode_fh_accept(void *context, struct dentry *de)
1393 {
1394         return 1;
1395 }
1396
1397 #ifdef HAVE_EXPORTFS_DECODE_FH
1398 # define ll_exportfs_decode_fh(mnt, fid, len, type, acceptable, context) \
1399          exportfs_decode_fh(mnt, (struct fid*)(fid), len, type,          \
1400                             acceptable, context)
1401 #else
1402 # define ll_exportfs_decode_fh(mnt, fid, len, type, acceptable, context) \
1403          export_op_default.decode_fh((mnt)->mnt_sb, &(fid)->ino, len,    \
1404                                      type, acceptable, context)
1405 # define FILEID_INO32_GEN 1
1406 extern struct export_operations export_op_default;
1407 #endif
1408
1409 struct dentry *fsfilt_ext3_fid2dentry(struct vfsmount *mnt,
1410                                       struct fsfilt_fid *fid, int ignore_gen)
1411 {
1412         struct inode  *inode;
1413         struct dentry *result;
1414
1415         result = ll_exportfs_decode_fh(mnt, fid, 2, FILEID_INO32_GEN,
1416                                        ll_decode_fh_accept, NULL);
1417         if (IS_ERR(result)) {
1418                 CDEBUG(D_DENTRY, "%s of %u/%u failed %ld\n", __func__,
1419                        fid->ino, fid->gen, PTR_ERR(result));
1420                 return result;
1421         }
1422
1423         CDEBUG(D_DENTRY, "%s of %u/%u succeeded\n", __func__,
1424                fid->ino, fid->gen);
1425         inode = result->d_inode;
1426         if (inode == NULL)
1427                 goto err_out;
1428
1429         if (inode->i_nlink == 0 &&
1430             inode->i_mode == 0 && LTIME_S(inode->i_ctime) == 0) {
1431                 LCONSOLE_WARN("Found inode with zero nlink, mode and"
1432                               " ctime -- this may indicate disk "
1433                               "corruption (inode: %lu, link: %lu, "
1434                               "count: %d)\n", inode->i_ino,
1435                               (unsigned long)inode->i_nlink,
1436                               atomic_read(&inode->i_count));
1437                 goto err_out;
1438         }
1439         if (fid->gen && inode->i_generation != fid->gen) {
1440                 /* we didn't find the right inode.. */
1441                 CDEBUG(D_INODE, "found wrong generation: inode %lu, link: %lu, "
1442                        "count: %d, generation %u/%u\n",
1443                        inode->i_ino, (unsigned long)inode->i_nlink,
1444                        atomic_read(&inode->i_count), inode->i_generation,
1445                        fid->gen);
1446                 goto err_out;
1447         }
1448
1449         return result;
1450 err_out:
1451         l_dput(result);
1452         return ERR_PTR(-ENOENT);
1453 }
1454
1455 static struct fsfilt_operations fsfilt_ext3_ops = {
1456         .fs_type                = "ext3",
1457         .fs_owner               = THIS_MODULE,
1458         .fs_getlabel            = fsfilt_ext3_get_label,
1459         .fs_setlabel            = fsfilt_ext3_set_label,
1460         .fs_uuid                = fsfilt_ext3_uuid,
1461         .fs_start               = fsfilt_ext3_start,
1462         .fs_brw_start           = fsfilt_ext3_brw_start,
1463         .fs_extend              = fsfilt_ext3_extend,
1464         .fs_commit              = fsfilt_ext3_commit,
1465         .fs_commit_async        = fsfilt_ext3_commit_async,
1466         .fs_commit_wait         = fsfilt_ext3_commit_wait,
1467         .fs_setattr             = fsfilt_ext3_setattr,
1468         .fs_iocontrol           = fsfilt_ext3_iocontrol,
1469         .fs_set_md              = fsfilt_ext3_set_md,
1470         .fs_get_md              = fsfilt_ext3_get_md,
1471         .fs_readpage            = fsfilt_ext3_readpage,
1472         .fs_add_journal_cb      = fsfilt_ext3_add_journal_cb,
1473         .fs_statfs              = fsfilt_ext3_statfs,
1474         .fs_sync                = fsfilt_ext3_sync,
1475         .fs_map_inode_pages     = fsfilt_ext3_map_inode_pages,
1476         .fs_write_record        = fsfilt_ext3_write_record,
1477         .fs_read_record         = fsfilt_ext3_read_record,
1478         .fs_setup               = fsfilt_ext3_setup,
1479         .fs_send_bio            = fsfilt_ext3_send_bio,
1480         .fs_get_op_len          = fsfilt_ext3_get_op_len,
1481 #ifdef HAVE_DISK_INODE_VERSION
1482         .fs_get_version         = fsfilt_ext3_get_version,
1483         .fs_set_version         = fsfilt_ext3_set_version,
1484 #endif
1485         .fs_journal_sbdev       = fsfilt_ext3_journal_sbdev,
1486         .fs_fid2dentry          = fsfilt_ext3_fid2dentry,
1487 };
1488
1489 static int __init fsfilt_ext3_init(void)
1490 {
1491         int rc;
1492
1493         fcb_cache = cfs_mem_cache_create("fsfilt_ext3_fcb",
1494                                          sizeof(struct fsfilt_cb_data), 0, 0);
1495         if (!fcb_cache) {
1496                 CERROR("error allocating fsfilt journal callback cache\n");
1497                 GOTO(out, rc = -ENOMEM);
1498         }
1499
1500         rc = fsfilt_register_ops(&fsfilt_ext3_ops);
1501
1502         if (rc) {
1503                 int err = cfs_mem_cache_destroy(fcb_cache);
1504                 LASSERTF(err == 0, "error destroying new cache: rc %d\n", err);
1505         }
1506 out:
1507         return rc;
1508 }
1509
1510 static void __exit fsfilt_ext3_exit(void)
1511 {
1512         int rc;
1513
1514         fsfilt_unregister_ops(&fsfilt_ext3_ops);
1515         rc = cfs_mem_cache_destroy(fcb_cache);
1516         LASSERTF(rc == 0, "couldn't destroy fcb_cache slab\n");
1517 }
1518
1519 module_init(fsfilt_ext3_init);
1520 module_exit(fsfilt_ext3_exit);
1521
1522 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1523 MODULE_DESCRIPTION("Lustre ext3 Filesystem Helper v0.1");
1524 MODULE_LICENSE("GPL");