1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * lustre/lib/fsfilt_ext3.c
5 * Lustre filesystem abstraction routines
7 * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
8 * Author: Andreas Dilger <adilger@clusterfs.com>
10 * This file is part of Lustre, http://www.lustre.org.
12 * Lustre is free software; you can redistribute it and/or
13 * modify it under the terms of version 2 of the GNU General Public
14 * License as published by the Free Software Foundation.
16 * Lustre is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Lustre; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define DEBUG_SUBSYSTEM S_FILTER
28 #include <linux/init.h>
29 #include <linux/module.h>
31 #include <linux/jbd.h>
32 #include <linux/slab.h>
33 #include <linux/pagemap.h>
34 #include <linux/quotaops.h>
35 #include <linux/ext3_fs.h>
36 #include <linux/ext3_jbd.h>
37 #include <linux/ext3_extents.h>
38 #include <linux/version.h>
39 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
40 #include <linux/ext3_xattr.h>
42 #include <ext3/xattr.h>
45 #include <libcfs/kp30.h>
46 #include <linux/lustre_fsfilt.h>
47 #include <linux/obd.h>
48 #include <linux/obd_class.h>
49 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
50 #include <linux/module.h>
51 #include <linux/iobuf.h>
55 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,7))
56 # define lock_24kernel() lock_kernel()
57 # define unlock_24kernel() unlock_kernel()
59 # define lock_24kernel() do {} while (0)
60 # define unlock_24kernel() do {} while (0)
63 static kmem_cache_t *fcb_cache;
64 static atomic_t fcb_cache_count = ATOMIC_INIT(0);
66 struct fsfilt_cb_data {
67 struct journal_callback cb_jcb; /* jbd private data - MUST BE FIRST */
68 fsfilt_cb_t cb_func; /* MDS/OBD completion function */
69 struct obd_device *cb_obd; /* MDS/OBD completion device */
70 __u64 cb_last_num; /* MDS/OST last committed operation */
71 void *cb_data; /* MDS/OST completion function data */
74 #ifndef EXT3_XATTR_INDEX_TRUSTED /* temporary until we hit l28 kernel */
75 #define EXT3_XATTR_INDEX_TRUSTED 4
78 #define XATTR_LUSTRE_MDS_LOV_EA "lov"
79 #define XATTR_LUSTRE_MDS_MEA_EA "mea"
80 #define XATTR_LUSTRE_MDS_MID_EA "mid"
81 #define XATTR_LUSTRE_MDS_SID_EA "sid"
84 * We don't currently need any additional blocks for rmdir and
85 * unlink transactions because we are storing the OST oa_id inside
86 * the inode (which we will be changing anyways as part of this
89 static void *fsfilt_ext3_start(struct inode *inode, int op, void *desc_private,
92 /* For updates to the last recieved file */
93 int nblocks = EXT3_SINGLEDATA_TRANS_BLOCKS;
97 if (current->journal_info) {
98 CDEBUG(D_INODE, "increasing refcount on %p\n",
99 current->journal_info);
103 /* XXX BUG 3188 -- must return to one set of opcodes */
104 /* FIXME - cache hook */
106 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
112 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
117 case FSFILT_OP_RMDIR:
118 case FSFILT_OP_UNLINK:
119 /* delete one file + create/update logs for each stripe */
120 nblocks += EXT3_DELETE_TRANS_BLOCKS;
121 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
122 EXT3_SINGLEDATA_TRANS_BLOCKS) * logs;
124 case FSFILT_OP_RENAME:
125 /* modify additional directory */
126 nblocks += EXT3_SINGLEDATA_TRANS_BLOCKS;
128 case FSFILT_OP_SYMLINK:
129 /* additional block + block bitmap + GDT for long symlink */
132 case FSFILT_OP_CREATE:
133 /* create/update logs for each stripe */
134 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
135 EXT3_SINGLEDATA_TRANS_BLOCKS) * logs;
137 case FSFILT_OP_MKDIR:
138 case FSFILT_OP_MKNOD:
139 /* modify one inode + block bitmap + GDT */
143 /* modify parent directory */
144 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS +
145 EXT3_DATA_TRANS_BLOCKS;
147 case FSFILT_OP_SETATTR:
148 /* Setattr on inode */
151 case FSFILT_OP_CANCEL_UNLINK:
152 /* blocks for log header bitmap update OR
153 * blocks for catalog header bitmap update + unlink of logs */
154 nblocks = (LLOG_CHUNK_SIZE >> inode->i_blkbits) +
155 EXT3_DELETE_TRANS_BLOCKS * logs;
158 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
160 default: CERROR("unknown transaction start op %d\n", op);
164 LASSERT(current->journal_info == desc_private);
165 journal = EXT3_SB(inode->i_sb)->s_journal;
166 if (nblocks > journal->j_max_transaction_buffers) {
167 CERROR("too many credits %d for op %ux%u using %d instead\n",
168 nblocks, op, logs, journal->j_max_transaction_buffers);
169 nblocks = journal->j_max_transaction_buffers;
173 LASSERTF(nblocks > 0, "can't start %d credit transaction\n", nblocks);
175 handle = journal_start(EXT3_JOURNAL(inode), nblocks);
179 LASSERT(current->journal_info == handle);
181 CERROR("error starting handle for op %u (%u credits): rc %ld\n",
182 op, nblocks, PTR_ERR(handle));
187 * Calculate the number of buffer credits needed to write multiple pages in
188 * a single ext3 transaction. No, this shouldn't be here, but as yet ext3
189 * doesn't have a nice API for calculating this sort of thing in advance.
191 * See comment above ext3_writepage_trans_blocks for details. We assume
192 * no data journaling is being done, but it does allow for all of the pages
193 * being non-contiguous. If we are guaranteed contiguous pages we could
194 * reduce the number of (d)indirect blocks a lot.
196 * With N blocks per page and P pages, for each inode we have at most:
198 * min(N*P, blocksize/4 + 1) dindirect blocks
201 * For the entire filesystem, we have at most:
202 * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
203 * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
204 * objcount inode blocks
206 * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
208 * 1 EXT3_DATA_TRANS_BLOCKS for the last_rcvd update.
210 static int fsfilt_ext3_credits_needed(int objcount, struct fsfilt_objinfo *fso,
211 int niocount, struct niobuf_local *nb)
213 struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
215 const int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
216 int nbitmaps = 0, ngdblocks;
217 int needed = objcount + 1; /* inodes + superblock */
220 for (i = 0, j = 0; i < objcount; i++, fso++) {
221 /* two or more dindirect blocks in case we cross boundary */
222 int ndind = (long)((nb[j + fso->fso_bufcnt - 1].offset -
224 sb->s_blocksize_bits) /
225 (EXT3_ADDR_PER_BLOCK(sb) * EXT3_ADDR_PER_BLOCK(sb));
226 nbitmaps += min(fso->fso_bufcnt, ndind > 0 ? ndind : 2);
228 /* leaf, indirect, tindirect blocks for first block */
229 nbitmaps += blockpp + 2;
231 j += fso->fso_bufcnt;
234 next_indir = nb[0].offset +
235 (EXT3_ADDR_PER_BLOCK(sb) << sb->s_blocksize_bits);
236 for (i = 1; i < niocount; i++) {
237 if (nb[i].offset >= next_indir) {
238 nbitmaps++; /* additional indirect */
239 next_indir = nb[i].offset +
240 (EXT3_ADDR_PER_BLOCK(sb)<<sb->s_blocksize_bits);
241 } else if (nb[i].offset != nb[i - 1].offset + sb->s_blocksize) {
242 nbitmaps++; /* additional indirect */
244 nbitmaps += blockpp; /* each leaf in different group? */
247 ngdblocks = nbitmaps;
248 if (nbitmaps > EXT3_SB(sb)->s_groups_count)
249 nbitmaps = EXT3_SB(sb)->s_groups_count;
250 if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
251 ngdblocks = EXT3_SB(sb)->s_gdb_count;
253 needed += nbitmaps + ngdblocks;
255 /* last_rcvd update */
256 needed += EXT3_DATA_TRANS_BLOCKS;
258 #if defined(CONFIG_QUOTA) && !defined(__x86_64__) /* XXX */
259 /* We assume that there will be 1 bit set in s_dquot.flags for each
260 * quota file that is active. This is at least true for now.
262 needed += hweight32(sb_any_quota_enabled(sb)) *
263 EXT3_SINGLEDATA_TRANS_BLOCKS;
269 /* We have to start a huge journal transaction here to hold all of the
270 * metadata for the pages being written here. This is necessitated by
271 * the fact that we do lots of prepare_write operations before we do
272 * any of the matching commit_write operations, so even if we split
273 * up to use "smaller" transactions none of them could complete until
274 * all of them were opened. By having a single journal transaction,
275 * we eliminate duplicate reservations for common blocks like the
276 * superblock and group descriptors or bitmaps.
278 * We will start the transaction here, but each prepare_write will
279 * add a refcount to the transaction, and each commit_write will
280 * remove a refcount. The transaction will be closed when all of
281 * the pages have been written.
283 static void *fsfilt_ext3_brw_start(int objcount, struct fsfilt_objinfo *fso,
284 int niocount, struct niobuf_local *nb,
285 void *desc_private, int logs)
292 LASSERT(current->journal_info == desc_private);
293 journal = EXT3_SB(fso->fso_dentry->d_inode->i_sb)->s_journal;
294 needed = fsfilt_ext3_credits_needed(objcount, fso, niocount, nb);
296 /* The number of blocks we could _possibly_ dirty can very large.
297 * We reduce our request if it is absurd (and we couldn't get that
298 * many credits for a single handle anyways).
300 * At some point we have to limit the size of I/Os sent at one time,
301 * increase the size of the journal, or we have to calculate the
302 * actual journal requirements more carefully by checking all of
303 * the blocks instead of being maximally pessimistic. It remains to
304 * be seen if this is a real problem or not.
306 if (needed > journal->j_max_transaction_buffers) {
307 CERROR("want too many journal credits (%d) using %d instead\n",
308 needed, journal->j_max_transaction_buffers);
309 needed = journal->j_max_transaction_buffers;
312 LASSERTF(needed > 0, "can't start %d credit transaction\n", needed);
314 handle = journal_start(journal, needed);
316 if (IS_ERR(handle)) {
317 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
320 LASSERT(handle->h_buffer_credits >= needed);
321 LASSERT(current->journal_info == handle);
327 static int fsfilt_ext3_commit(struct super_block *sb, struct inode *inode,
328 void *h, int force_sync)
331 handle_t *handle = h;
333 LASSERT(current->journal_info == handle);
335 handle->h_sync = 1; /* recovery likes this */
338 rc = journal_stop(handle);
344 static int fsfilt_ext3_commit_async(struct inode *inode, void *h,
348 transaction_t *transaction;
349 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
352 handle_t *handle = h;
356 LASSERT(current->journal_info == handle);
359 transaction = handle->h_transaction;
360 journal = transaction->t_journal;
361 tid = transaction->t_tid;
362 /* we don't want to be blocked */
364 rc = journal_stop(handle);
366 CERROR("error while stopping transaction: %d\n", rc);
370 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
371 rtid = log_start_commit(journal, transaction);
373 CERROR("strange race: %lu != %lu\n",
374 (unsigned long) tid, (unsigned long) rtid);
376 log_start_commit(journal, transaction->t_tid);
380 *wait_handle = (void *) tid;
381 CDEBUG(D_INODE, "commit async: %lu\n", (unsigned long) tid);
385 static int fsfilt_ext3_commit_wait(struct inode *inode, void *h)
387 tid_t tid = (tid_t)(long)h;
389 CDEBUG(D_INODE, "commit wait: %lu\n", (unsigned long) tid);
390 if (is_journal_aborted(EXT3_JOURNAL(inode)))
393 log_wait_commit(EXT3_JOURNAL(inode), tid);
398 static int fsfilt_ext3_setattr(struct dentry *dentry, void *handle,
399 struct iattr *iattr, int do_trunc)
401 struct inode *inode = dentry->d_inode;
406 /* A _really_ horrible hack to avoid removing the data stored
407 * in the block pointers; this is really the "small" stripe MD data.
408 * We can avoid further hackery by virtue of the MDS file size being
409 * zero all the time (which doesn't invoke block truncate at unlink
410 * time), so we assert we never change the MDS file size from zero. */
411 if (iattr->ia_valid & ATTR_SIZE && !do_trunc) {
412 /* ATTR_SIZE would invoke truncate: clear it */
413 iattr->ia_valid &= ~ATTR_SIZE;
414 EXT3_I(inode)->i_disksize = inode->i_size = iattr->ia_size;
416 /* make sure _something_ gets set - so new inode
417 * goes to disk (probably won't work over XFS */
418 if (!(iattr->ia_valid & (ATTR_MODE | ATTR_MTIME | ATTR_CTIME))){
419 iattr->ia_valid |= ATTR_MODE;
420 iattr->ia_mode = inode->i_mode;
424 /* Don't allow setattr to change file type */
425 iattr->ia_mode = (inode->i_mode & S_IFMT)|(iattr->ia_mode & ~S_IFMT);
427 /* We set these flags on the client, but have already checked perms
428 * so don't confuse inode_change_ok. */
429 iattr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET);
431 if (inode->i_op->setattr) {
432 rc = inode->i_op->setattr(dentry, iattr);
434 rc = inode_change_ok(inode, iattr);
436 rc = inode_setattr(inode, iattr);
444 static int fsfilt_ext3_iocontrol(struct inode * inode, struct file *file,
445 unsigned int cmd, unsigned long arg)
450 if (inode->i_fop->ioctl)
451 rc = inode->i_fop->ioctl(inode, file, cmd, arg);
458 static int fsfilt_ext3_set_xattr(struct inode * inode, void *handle, char *name,
459 void *buffer, int buffer_size)
465 rc = ext3_xattr_set_handle(handle, inode, EXT3_XATTR_INDEX_TRUSTED,
466 name, buffer, buffer_size, 0);
469 CERROR("set xattr %s from inode %lu: rc %d\n",
470 name, inode->i_ino, rc);
474 static int fsfilt_ext3_get_xattr(struct inode *inode, char *name,
475 void *buffer, int buffer_size)
481 rc = ext3_xattr_get(inode, EXT3_XATTR_INDEX_TRUSTED,
482 name, buffer, buffer_size);
486 return (rc == -ENODATA) ? 0 : rc;
488 CDEBUG(D_INFO, "error getting EA %s from inode %lu: rc %d\n",
489 name, inode->i_ino, rc);
490 memset(buffer, 0, buffer_size);
491 return (rc == -ENODATA) ? 0 : rc;
497 static int fsfilt_ext3_set_md(struct inode *inode, void *handle,
498 void *lmm, int lmm_size,
505 rc = fsfilt_ext3_set_xattr(inode, handle,
506 XATTR_LUSTRE_MDS_LOV_EA,
510 rc = fsfilt_ext3_set_xattr(inode, handle,
511 XATTR_LUSTRE_MDS_MEA_EA,
515 rc = fsfilt_ext3_set_xattr(inode, handle,
516 XATTR_LUSTRE_MDS_SID_EA,
520 rc = fsfilt_ext3_set_xattr(inode, handle,
521 XATTR_LUSTRE_MDS_MID_EA,
531 static int fsfilt_ext3_get_md(struct inode *inode, void *lmm,
532 int lmm_size, enum ea_type type)
538 rc = fsfilt_ext3_get_xattr(inode,
539 XATTR_LUSTRE_MDS_LOV_EA,
543 rc = fsfilt_ext3_get_xattr(inode,
544 XATTR_LUSTRE_MDS_MEA_EA,
548 rc = fsfilt_ext3_get_xattr(inode,
549 XATTR_LUSTRE_MDS_SID_EA,
553 rc = fsfilt_ext3_get_xattr(inode,
554 XATTR_LUSTRE_MDS_MID_EA,
564 static int fsfilt_ext3_send_bio(int rw, struct inode *inode, void *bio)
567 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
568 submit_bio(rw, (struct bio *)bio);
570 struct bio *b = (struct kiobuf *)bio;
573 rc = brw_kiovec(rw, 1, &b, inode->i_dev,
574 b->blocks, 1 << inode->i_blkbits);
576 blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
578 if (rc != (1 << inode->i_blkbits) * b->nr_pages * blocks_per_page) {
579 CERROR("short write? expected %d, wrote %d\n",
580 (1 << inode->i_blkbits) * b->nr_pages *
581 blocks_per_page, rc);
587 static struct page *fsfilt_ext3_getpage(struct inode *inode, long int index)
592 page = grab_cache_page(inode->i_mapping, index);
594 return ERR_PTR(-ENOMEM);
596 if (PageUptodate(page)) {
601 rc = inode->i_mapping->a_ops->readpage(NULL, page);
603 page_cache_release(page);
610 static ssize_t fsfilt_ext3_readpage(struct file *file, char *buf, size_t count,
613 struct inode *inode = file->f_dentry->d_inode;
616 if (S_ISREG(inode->i_mode))
617 rc = file->f_op->read(file, buf, count, off);
619 const int blkbits = inode->i_sb->s_blocksize_bits;
620 const int blksize = inode->i_sb->s_blocksize;
622 CDEBUG(D_EXT2, "reading "LPSZ" at dir %lu+%llu\n",
623 count, inode->i_ino, *off);
625 struct buffer_head *bh;
628 if (*off < inode->i_size) {
631 bh = ext3_bread(NULL, inode, *off >> blkbits,
634 CDEBUG(D_EXT2, "read %u@%llu\n", blksize, *off);
637 memcpy(buf, bh->b_data, blksize);
640 /* XXX in theory we should just fake
641 * this buffer and continue like ext3,
642 * especially if this is a partial read
644 CERROR("error read dir %lu+%llu: %d\n",
645 inode->i_ino, *off, err);
650 struct ext3_dir_entry_2 *fake = (void *)buf;
652 CDEBUG(D_EXT2, "fake %u@%llu\n", blksize, *off);
653 memset(fake, 0, sizeof(*fake));
654 fake->rec_len = cpu_to_le32(blksize);
666 static void fsfilt_ext3_cb_func(struct journal_callback *jcb, int error)
668 struct fsfilt_cb_data *fcb = (struct fsfilt_cb_data *)jcb;
670 fcb->cb_func(fcb->cb_obd, fcb->cb_last_num, fcb->cb_data, error);
672 OBD_SLAB_FREE(fcb, fcb_cache, sizeof *fcb);
673 atomic_dec(&fcb_cache_count);
676 static int fsfilt_ext3_add_journal_cb(struct obd_device *obd,
677 struct super_block *sb,
678 __u64 last_num, void *handle,
682 struct fsfilt_cb_data *fcb;
684 OBD_SLAB_ALLOC(fcb, fcb_cache, GFP_NOFS, sizeof *fcb);
688 atomic_inc(&fcb_cache_count);
689 fcb->cb_func = cb_func;
691 fcb->cb_last_num = last_num;
692 fcb->cb_data = cb_data;
694 CDEBUG(D_EXT2, "set callback for last_num: "LPD64"\n", last_num);
696 journal_callback_set(handle, fsfilt_ext3_cb_func,
697 (struct journal_callback *)fcb);
703 * We need to hack the return value for the free inode counts because
704 * the current EA code requires one filesystem block per inode with EAs,
705 * so it is possible to run out of blocks before we run out of inodes.
707 * This can be removed when the ext3 EA code is fixed.
709 static int fsfilt_ext3_statfs(struct super_block *sb, struct obd_statfs *osfs)
714 memset(&sfs, 0, sizeof(sfs));
716 rc = sb->s_op->statfs(sb, &sfs);
718 if (!rc && sfs.f_bfree < sfs.f_ffree) {
719 sfs.f_files = (sfs.f_files - sfs.f_ffree) + sfs.f_bfree;
720 sfs.f_ffree = sfs.f_bfree;
723 statfs_pack(osfs, &sfs);
727 static int fsfilt_ext3_sync(struct super_block *sb)
729 return ext3_force_commit(sb);
732 #ifdef EXT3_MULTIBLOCK_ALLOCATOR
733 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
734 #define ext3_up_truncate_sem(inode) up_write(&EXT3_I(inode)->truncate_sem);
735 #define ext3_down_truncate_sem(inode) down_write(&EXT3_I(inode)->truncate_sem);
737 #define ext3_up_truncate_sem(inode) up(&EXT3_I(inode)->truncate_sem);
738 #define ext3_down_truncate_sem(inode) down(&EXT3_I(inode)->truncate_sem);
741 #include <linux/lustre_version.h>
742 #if EXT3_EXT_MAGIC == 0xf301
743 #define ee_start e_start
744 #define ee_block e_block
747 #ifndef EXT3_BB_MAX_BLOCKS
748 #define ext3_mb_new_blocks(handle, inode, goal, count, aflags, err) \
749 ext3_new_blocks(handle, inode, count, goal, err)
753 unsigned long *blocks;
760 static int ext3_ext_find_goal(struct inode *inode, struct ext3_ext_path *path,
761 unsigned long block, int *aflags)
763 struct ext3_inode_info *ei = EXT3_I(inode);
764 unsigned long bg_start;
765 unsigned long colour;
769 struct ext3_extent *ex;
770 depth = path->p_depth;
772 /* try to predict block placement */
773 if ((ex = path[depth].p_ext)) {
774 if (ex->ee_block + ex->ee_len == block)
776 return ex->ee_start + (block - ex->ee_block);
779 /* it looks index is empty
780 * try to find starting from index itself */
781 if (path[depth].p_bh)
782 return path[depth].p_bh->b_blocknr;
785 /* OK. use inode's group */
786 bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
787 le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
788 colour = (current->pid % 16) *
789 (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
790 return bg_start + colour + block;
793 static int ext3_ext_new_extent_cb(struct ext3_extents_tree *tree,
794 struct ext3_ext_path *path,
795 struct ext3_extent *newex, int exist)
797 struct inode *inode = tree->inode;
798 struct bpointers *bp = tree->private;
799 int count, err, goal;
800 unsigned long pblock;
807 EXT_ASSERT(i == path->p_depth);
808 EXT_ASSERT(path[i].p_hdr);
815 if (bp->create == 0) {
817 if (newex->ee_block < bp->start)
818 i = bp->start - newex->ee_block;
819 if (i >= newex->ee_len)
820 CERROR("nothing to do?! i = %d, e_num = %u\n",
822 for (; i < newex->ee_len && bp->num; i++) {
833 tgen = EXT_GENERATION(tree);
834 count = ext3_ext_calc_credits_for_insert(tree, path);
835 ext3_up_truncate_sem(inode);
837 handle = journal_start(EXT3_JOURNAL(inode), count + EXT3_ALLOC_NEEDED + 1);
839 if (IS_ERR(handle)) {
840 ext3_down_truncate_sem(inode);
841 return PTR_ERR(handle);
844 if (tgen != EXT_GENERATION(tree)) {
845 /* the tree has changed. so path can be invalid at moment */
847 journal_stop(handle);
849 ext3_down_truncate_sem(inode);
852 ext3_down_truncate_sem(inode);
853 count = newex->ee_len;
854 goal = ext3_ext_find_goal(inode, path, newex->ee_block, &aflags);
855 aflags |= 2; /* block have been already reserved */
856 pblock = ext3_mb_new_blocks(handle, inode, goal, &count, aflags, &err);
859 EXT_ASSERT(count <= newex->ee_len);
861 /* insert new extent */
862 newex->ee_start = pblock;
863 newex->ee_len = count;
864 err = ext3_ext_insert_extent(handle, tree, path, newex);
868 /* correct on-disk inode size */
869 if (newex->ee_len > 0) {
870 new_i_size = (loff_t) newex->ee_block + newex->ee_len;
871 new_i_size = new_i_size << inode->i_blkbits;
872 if (new_i_size > EXT3_I(inode)->i_disksize) {
873 EXT3_I(inode)->i_disksize = new_i_size;
874 err = ext3_mark_inode_dirty(handle, inode);
879 journal_stop(handle);
885 CERROR("hmm. why do we find this extent?\n");
886 CERROR("initial space: %lu:%u\n",
887 bp->start, bp->init_num);
888 CERROR("current extent: %u/%u/%u %d\n",
889 newex->ee_block, newex->ee_len,
890 newex->ee_start, exist);
893 if (newex->ee_block < bp->start)
894 i = bp->start - newex->ee_block;
895 if (i >= newex->ee_len)
896 CERROR("nothing to do?! i = %d, e_num = %u\n",
898 for (; i < newex->ee_len && bp->num; i++) {
899 *(bp->created) = (exist == 0 ? 1 : 0);
901 *(bp->blocks) = newex->ee_start + i;
910 int fsfilt_map_nblocks(struct inode *inode, unsigned long block,
911 unsigned long num, unsigned long *blocks,
912 int *created, int create)
914 struct ext3_extents_tree tree;
918 CDEBUG(D_OTHER, "blocks %lu-%lu requested for inode %u\n",
919 block, block + num, (unsigned) inode->i_ino);
921 ext3_init_tree_desc(&tree, inode);
924 bp.created = created;
926 bp.init_num = bp.num = num;
929 ext3_down_truncate_sem(inode);
930 err = ext3_ext_walk_space(&tree, block, num, ext3_ext_new_extent_cb);
931 ext3_ext_invalidate_cache(&tree);
932 ext3_up_truncate_sem(inode);
936 int fsfilt_ext3_map_ext_inode_pages(struct inode *inode, struct page **page,
937 int pages, unsigned long *blocks,
938 int *created, int create)
940 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
942 struct page *fp = NULL;
945 CDEBUG(D_OTHER, "inode %lu: map %d pages from %lu\n",
946 inode->i_ino, pages, (*page)->index);
948 /* pages are sorted already. so, we just have to find
949 * contig. space and process them properly */
952 /* start new extent */
957 } else if (fp->index + clen == (*page)->index) {
958 /* continue the extent */
965 /* process found extent */
966 rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page,
967 clen * blocks_per_page, blocks,
972 /* look for next extent */
974 blocks += blocks_per_page * clen;
975 created += blocks_per_page * clen;
979 rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page,
980 clen * blocks_per_page, blocks,
987 extern int ext3_map_inode_page(struct inode *inode, struct page *page,
988 unsigned long *blocks, int *created, int create);
989 int fsfilt_ext3_map_bm_inode_pages(struct inode *inode, struct page **page,
990 int pages, unsigned long *blocks,
991 int *created, int create)
993 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
997 for (i = 0, cr = created, b = blocks; i < pages; i++, page++) {
998 rc = ext3_map_inode_page(inode, *page, b, cr, create);
1000 CERROR("ino %lu, blk %lu cr %u create %d: rc %d\n",
1001 inode->i_ino, *b, *cr, create, rc);
1005 b += blocks_per_page;
1006 cr += blocks_per_page;
1011 int fsfilt_ext3_map_inode_pages(struct inode *inode, struct page **page,
1012 int pages, unsigned long *blocks,
1013 int *created, int create,
1014 struct semaphore *optional_sem)
1017 #ifdef EXT3_MULTIBLOCK_ALLOCATOR
1018 if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL) {
1019 rc = fsfilt_ext3_map_ext_inode_pages(inode, page, pages,
1020 blocks, created, create);
1024 if (optional_sem != NULL)
1026 rc = fsfilt_ext3_map_bm_inode_pages(inode, page, pages, blocks,
1028 if (optional_sem != NULL)
1034 extern int ext3_prep_san_write(struct inode *inode, long *blocks,
1035 int nblocks, loff_t newsize);
1036 static int fsfilt_ext3_prep_san_write(struct inode *inode, long *blocks,
1037 int nblocks, loff_t newsize)
1039 return ext3_prep_san_write(inode, blocks, nblocks, newsize);
1042 static int fsfilt_ext3_read_record(struct file * file, void *buf,
1043 int size, loff_t *offs)
1045 struct inode *inode = file->f_dentry->d_inode;
1046 unsigned long block;
1047 struct buffer_head *bh;
1048 int err, blocksize, csize, boffs;
1050 /* prevent reading after eof */
1052 if (inode->i_size < *offs + size) {
1053 size = inode->i_size - *offs;
1056 CERROR("size %llu is too short for read %u@%llu\n",
1057 inode->i_size, size, *offs);
1059 } else if (size == 0) {
1066 blocksize = 1 << inode->i_blkbits;
1069 block = *offs >> inode->i_blkbits;
1070 boffs = *offs & (blocksize - 1);
1071 csize = min(blocksize - boffs, size);
1072 bh = ext3_bread(NULL, inode, block, 0, &err);
1074 CERROR("can't read block: %d\n", err);
1078 memcpy(buf, bh->b_data + boffs, csize);
1088 static int fsfilt_ext3_write_record(struct file *file, void *buf, int bufsize,
1089 loff_t *offs, int force_sync)
1091 struct buffer_head *bh = NULL;
1092 unsigned long block;
1093 struct inode *inode = file->f_dentry->d_inode;
1094 loff_t old_size = inode->i_size, offset = *offs;
1095 loff_t new_size = inode->i_size;
1098 int err = 0, block_count = 0, blocksize, size, boffs;
1100 /* Determine how many transaction credits are needed */
1101 blocksize = 1 << inode->i_blkbits;
1102 block_count = (*offs & (blocksize - 1)) + bufsize;
1103 block_count = (block_count + blocksize - 1) >> inode->i_blkbits;
1105 journal = EXT3_SB(inode->i_sb)->s_journal;
1107 handle = journal_start(journal,
1108 block_count * EXT3_DATA_TRANS_BLOCKS + 2);
1110 if (IS_ERR(handle)) {
1111 CERROR("can't start transaction\n");
1112 return PTR_ERR(handle);
1115 while (bufsize > 0) {
1119 block = offset >> inode->i_blkbits;
1120 boffs = offset & (blocksize - 1);
1121 size = min(blocksize - boffs, bufsize);
1122 bh = ext3_bread(handle, inode, block, 1, &err);
1124 CERROR("can't read/create block: %d\n", err);
1128 err = ext3_journal_get_write_access(handle, bh);
1130 CERROR("journal_get_write_access() returned error %d\n",
1134 LASSERT(bh->b_data + boffs + size <= bh->b_data + bh->b_size);
1135 memcpy(bh->b_data + boffs, buf, size);
1136 err = ext3_journal_dirty_metadata(handle, bh);
1138 CERROR("journal_dirty_metadata() returned error %d\n",
1142 if (offset + size > new_size)
1143 new_size = offset + size;
1150 handle->h_sync = 1; /* recovery likes this */
1155 /* correct in-core and on-disk sizes */
1156 if (new_size > inode->i_size) {
1158 if (new_size > inode->i_size)
1159 inode->i_size = new_size;
1160 if (inode->i_size > EXT3_I(inode)->i_disksize)
1161 EXT3_I(inode)->i_disksize = inode->i_size;
1162 if (inode->i_size > old_size)
1163 mark_inode_dirty(inode);
1168 journal_stop(handle);
1176 static int fsfilt_ext3_setup(struct obd_device *obd, struct super_block *sb)
1178 #ifdef EXT3_FEATURE_INCOMPAT_MDSNUM
1179 struct mds_obd *mds = &obd->u.mds;
1182 EXT3_SB(sb)->dx_lock = fsfilt_ext3_dx_lock;
1183 EXT3_SB(sb)->dx_unlock = fsfilt_ext3_dx_unlock;
1186 CWARN("Enabling PDIROPS\n");
1187 set_opt(EXT3_SB(sb)->s_mount_opt, PDIROPS);
1188 sb->s_flags |= S_PDIROPS;
1190 /* setup mdsnum in underlying fs */
1191 #ifdef EXT3_FEATURE_INCOMPAT_MDSNUM
1192 if (mds->mds_md_obd) {
1193 struct ext3_sb_info *sbi = EXT3_SB(sb);
1194 struct ext3_super_block *es = sbi->s_es;
1198 if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_MDSNUM)) {
1199 CWARN("%s: set mdsnum %d in ext3\n",
1200 obd->obd_name, mds->mds_num);
1202 handle = journal_start(sbi->s_journal, 1);
1204 LASSERT(!IS_ERR(handle));
1205 err = ext3_journal_get_write_access(handle, sbi->s_sbh);
1207 EXT3_SET_INCOMPAT_FEATURE(sb,
1208 EXT3_FEATURE_INCOMPAT_MDSNUM);
1209 es->s_mdsnum = mds->mds_num;
1210 err = ext3_journal_dirty_metadata(handle, sbi->s_sbh);
1213 journal_stop(handle);
1216 CWARN("%s: mdsnum initialized to %u in ext3fs\n",
1217 obd->obd_name, es->s_mdsnum);
1219 sbi->s_mdsnum = es->s_mdsnum;
1225 extern int ext3_add_dir_entry(struct dentry *dentry);
1226 extern int ext3_del_dir_entry(struct dentry *dentry);
1228 static int fsfilt_ext3_add_dir_entry(struct obd_device *obd,
1229 struct dentry *parent,
1230 char *name, int namelen,
1232 unsigned long generation,
1236 #ifdef EXT3_FEATURE_INCOMPAT_MDSNUM
1237 struct dentry *dentry;
1240 LASSERT(namelen != 0);
1241 dentry = ll_lookup_one_len(name, parent, namelen);
1242 if (IS_ERR(dentry)) {
1243 CERROR("can't lookup %*s in %lu/%lu: %d\n", dentry->d_name.len,
1244 dentry->d_name.name, dentry->d_inode->i_ino,
1245 (unsigned long) dentry->d_inode->i_generation,
1246 (int) PTR_ERR(dentry));
1247 RETURN(PTR_ERR(dentry));
1249 if (dentry->d_inode != NULL || dentry->d_flags & DCACHE_CROSS_REF) {
1250 CERROR("dentry %*s(0x%p) found\n", dentry->d_name.len,
1251 dentry->d_name.name, dentry);
1256 /* mds_reint_rename() may use this method to add dir entry
1257 * that points onto local inode. and we don't want to find
1258 * it cross-ref by subsequent lookups */
1261 dentry->d_flags |= DCACHE_CROSS_REF;
1262 dentry->d_inum = ino;
1263 dentry->d_mdsnum = mds;
1264 dentry->d_generation = generation;
1265 dentry->d_fid = fid;
1267 err = ext3_add_dir_entry(dentry);
1274 #error "rebuild kernel and lustre with ext3-mds-num patch!"
1279 static int fsfilt_ext3_del_dir_entry(struct obd_device *obd,
1280 struct dentry *dentry)
1282 #ifdef EXT3_FEATURE_INCOMPAT_MDSNUM
1285 err = ext3_del_dir_entry(dentry);
1291 #error "rebuild kernel and lustre with ext3-mds-num patch!"
1296 /* If fso is NULL, op is FSFILT operation, otherwise op is number of fso
1297 objects. Logs is number of logfiles to update */
1298 static int fsfilt_ext3_get_op_len(int op, struct fsfilt_objinfo *fso, int logs)
1302 case FSFILT_OP_CREATE:
1303 /* directory leaf, index & indirect & EA*/
1304 return 4 + 3 * logs;
1305 case FSFILT_OP_UNLINK:
1311 struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
1312 int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
1313 int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
1314 for (i = 0; i < op; i++, fso++) {
1315 int nblocks = fso->fso_bufcnt * blockpp;
1316 int ndindirect = min(nblocks, addrpp + 1);
1317 int nindir = nblocks + ndindirect + 1;
1321 return needed + 3 * logs;
1328 #define EXTENTS_EA "write_extents"
1329 #define EXTENTS_EA_SIZE 64
1331 int ext3_ext_in_ea_alloc_space(struct inode *, int, const char *, unsigned long, unsigned long);
1332 int ext3_ext_in_ea_remove_space(struct inode *, int, const char *, unsigned long, unsigned long);
1333 int ext3_ext_in_ea_get_extents(struct inode *, int, const char *, char **, int *);
1334 int ext3_ext_in_ea_get_extents_num(struct inode *, int, const char *, int *);
1336 static int fsfilt_ext3_insert_extents_ea(struct inode *inode,
1342 rc = ext3_ext_in_ea_alloc_space(inode, EXT3_XATTR_INDEX_TRUSTED,
1343 EXTENTS_EA, from, num);
1347 static int fsfilt_ext3_remove_extents_ea(struct inode *inode,
1353 rc = ext3_ext_in_ea_remove_space(inode, EXT3_XATTR_INDEX_TRUSTED,
1354 EXTENTS_EA, from, num);
1358 extern int ext3_init_tree_in_ea(struct inode *inode, int name_index,
1359 const char *eaname, int size);
1361 static int fsfilt_ext3_init_extents_ea(struct inode *inode)
1365 rc = ext3_init_tree_in_ea(inode, EXT3_XATTR_INDEX_TRUSTED,
1370 static int fsfilt_ext3_get_inode_write_extents(struct inode *inode,
1371 char **pbuf, int *size)
1375 rc = ext3_ext_in_ea_get_extents(inode, EXT3_XATTR_INDEX_TRUSTED,
1376 EXTENTS_EA, pbuf, size);
1380 static int fsfilt_ext3_get_write_extents_num(struct inode *inode, int *size)
1384 rc = ext3_ext_in_ea_get_extents_num(inode, EXT3_XATTR_INDEX_TRUSTED,
1389 static struct fsfilt_operations fsfilt_ext3_ops = {
1391 .fs_owner = THIS_MODULE,
1392 .fs_start = fsfilt_ext3_start,
1393 .fs_brw_start = fsfilt_ext3_brw_start,
1394 .fs_commit = fsfilt_ext3_commit,
1395 .fs_commit_async = fsfilt_ext3_commit_async,
1396 .fs_commit_wait = fsfilt_ext3_commit_wait,
1397 .fs_setattr = fsfilt_ext3_setattr,
1398 .fs_iocontrol = fsfilt_ext3_iocontrol,
1399 .fs_set_md = fsfilt_ext3_set_md,
1400 .fs_get_md = fsfilt_ext3_get_md,
1401 .fs_readpage = fsfilt_ext3_readpage,
1402 .fs_add_journal_cb = fsfilt_ext3_add_journal_cb,
1403 .fs_statfs = fsfilt_ext3_statfs,
1404 .fs_sync = fsfilt_ext3_sync,
1405 .fs_map_inode_pages = fsfilt_ext3_map_inode_pages,
1406 .fs_prep_san_write = fsfilt_ext3_prep_san_write,
1407 .fs_write_record = fsfilt_ext3_write_record,
1408 .fs_read_record = fsfilt_ext3_read_record,
1409 .fs_setup = fsfilt_ext3_setup,
1410 .fs_getpage = fsfilt_ext3_getpage,
1411 .fs_send_bio = fsfilt_ext3_send_bio,
1412 .fs_set_xattr = fsfilt_ext3_set_xattr,
1413 .fs_get_xattr = fsfilt_ext3_get_xattr,
1414 .fs_get_op_len = fsfilt_ext3_get_op_len,
1415 .fs_add_dir_entry = fsfilt_ext3_add_dir_entry,
1416 .fs_del_dir_entry = fsfilt_ext3_del_dir_entry,
1417 .fs_init_extents_ea = fsfilt_ext3_init_extents_ea,
1418 .fs_insert_extents_ea = fsfilt_ext3_insert_extents_ea,
1419 .fs_remove_extents_ea = fsfilt_ext3_remove_extents_ea,
1420 .fs_get_inode_write_extents = fsfilt_ext3_get_inode_write_extents,
1421 .fs_get_write_extents_num = fsfilt_ext3_get_write_extents_num,
1424 static int __init fsfilt_ext3_init(void)
1428 fcb_cache = kmem_cache_create("fsfilt_ext3_fcb",
1429 sizeof(struct fsfilt_cb_data), 0,
1432 CERROR("error allocating fsfilt journal callback cache\n");
1433 GOTO(out, rc = -ENOMEM);
1436 rc = fsfilt_register_ops(&fsfilt_ext3_ops);
1439 kmem_cache_destroy(fcb_cache);
1444 static void __exit fsfilt_ext3_exit(void)
1446 fsfilt_unregister_ops(&fsfilt_ext3_ops);
1447 LASSERTF(kmem_cache_destroy(fcb_cache) == 0,
1448 "can't free fsfilt callback cache: count %d\n",
1449 atomic_read(&fcb_cache_count));
1452 module_init(fsfilt_ext3_init);
1453 module_exit(fsfilt_ext3_exit);
1455 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1456 MODULE_DESCRIPTION("Lustre ext3 Filesystem Helper v0.1");
1457 MODULE_LICENSE("GPL");