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>
54 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,7))
55 # define lock_24kernel() lock_kernel()
56 # define unlock_24kernel() unlock_kernel()
58 # define lock_24kernel() do {} while (0)
59 # define unlock_24kernel() do {} while (0)
62 static kmem_cache_t *fcb_cache;
63 static atomic_t fcb_cache_count = ATOMIC_INIT(0);
65 struct fsfilt_cb_data {
66 struct journal_callback cb_jcb; /* jbd private data - MUST BE FIRST */
67 fsfilt_cb_t cb_func; /* MDS/OBD completion function */
68 struct obd_device *cb_obd; /* MDS/OBD completion device */
69 __u64 cb_last_num; /* MDS/OST last committed operation */
70 void *cb_data; /* MDS/OST completion function data */
73 #ifndef EXT3_XATTR_INDEX_TRUSTED /* temporary until we hit l28 kernel */
74 #define EXT3_XATTR_INDEX_TRUSTED 4
78 * We don't currently need any additional blocks for rmdir and
79 * unlink transactions because we are storing the OST oa_id inside
80 * the inode (which we will be changing anyways as part of this
83 static void *fsfilt_ext3_start(struct inode *inode, int op, void *desc_private,
86 /* For updates to the last recieved file */
87 int nblocks = EXT3_SINGLEDATA_TRANS_BLOCKS;
91 if (current->journal_info) {
92 CDEBUG(D_INODE, "increasing refcount on %p\n",
93 current->journal_info);
98 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
99 EXT3_SINGLEDATA_TRANS_BLOCKS) * logs;
102 case FSFILT_OP_RMDIR:
103 case FSFILT_OP_UNLINK:
104 /* delete one file + create/update logs for each stripe */
105 nblocks += EXT3_DELETE_TRANS_BLOCKS;
106 /*nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
107 EXT3_SINGLEDATA_TRANS_BLOCKS) * logs;*/
109 case FSFILT_OP_RENAME:
110 /* modify additional directory */
111 nblocks += EXT3_SINGLEDATA_TRANS_BLOCKS;
113 case FSFILT_OP_SYMLINK:
114 /* additional block + block bitmap + GDT for long symlink */
117 case FSFILT_OP_CREATE:
118 /* create/update logs for each stripe */
119 /*nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
120 EXT3_SINGLEDATA_TRANS_BLOCKS) * logs;*/
122 case FSFILT_OP_MKDIR:
123 case FSFILT_OP_MKNOD:
124 /* modify one inode + block bitmap + GDT */
128 /* modify parent directory */
129 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS +
130 EXT3_DATA_TRANS_BLOCKS;
132 case FSFILT_OP_SETATTR:
133 /* Setattr on inode */
136 case FSFILT_OP_CANCEL_UNLINK:
137 /* blocks for log header bitmap update OR
138 * blocks for catalog header bitmap update + unlink of logs */
139 nblocks = (LLOG_CHUNK_SIZE >> inode->i_blkbits) +
140 EXT3_DELETE_TRANS_BLOCKS * logs;
143 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
145 default: CERROR("unknown transaction start op %d\n", op);
149 LASSERT(current->journal_info == desc_private);
150 journal = EXT3_SB(inode->i_sb)->s_journal;
151 if (nblocks > journal->j_max_transaction_buffers) {
152 CERROR("too many credits %d for op %ux%u using %d instead\n",
153 nblocks, op, logs, journal->j_max_transaction_buffers);
154 nblocks = journal->j_max_transaction_buffers;
158 LASSERTF(nblocks > 0, "can't start %d credit transaction\n", nblocks);
160 handle = journal_start(EXT3_JOURNAL(inode), nblocks);
164 LASSERT(current->journal_info == handle);
166 CERROR("error starting handle for op %u (%u credits): rc %ld\n",
167 op, nblocks, PTR_ERR(handle));
172 * Calculate the number of buffer credits needed to write multiple pages in
173 * a single ext3 transaction. No, this shouldn't be here, but as yet ext3
174 * doesn't have a nice API for calculating this sort of thing in advance.
176 * See comment above ext3_writepage_trans_blocks for details. We assume
177 * no data journaling is being done, but it does allow for all of the pages
178 * being non-contiguous. If we are guaranteed contiguous pages we could
179 * reduce the number of (d)indirect blocks a lot.
181 * With N blocks per page and P pages, for each inode we have at most:
183 * min(N*P, blocksize/4 + 1) dindirect blocks
186 * For the entire filesystem, we have at most:
187 * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
188 * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
189 * objcount inode blocks
191 * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
193 * 1 EXT3_DATA_TRANS_BLOCKS for the last_rcvd update.
195 static int fsfilt_ext3_credits_needed(int objcount, struct fsfilt_objinfo *fso,
196 int niocount, struct niobuf_local *nb)
198 struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
200 const int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
201 int nbitmaps = 0, ngdblocks;
202 int needed = objcount + 1; /* inodes + superblock */
205 for (i = 0, j = 0; i < objcount; i++, fso++) {
206 /* two or more dindirect blocks in case we cross boundary */
207 int ndind = (long)((nb[j + fso->fso_bufcnt - 1].offset -
209 sb->s_blocksize_bits) /
210 (EXT3_ADDR_PER_BLOCK(sb) * EXT3_ADDR_PER_BLOCK(sb));
211 nbitmaps += min(fso->fso_bufcnt, ndind > 0 ? ndind : 2);
213 /* leaf, indirect, tindirect blocks for first block */
214 nbitmaps += blockpp + 2;
216 j += fso->fso_bufcnt;
219 next_indir = nb[0].offset +
220 (EXT3_ADDR_PER_BLOCK(sb) << sb->s_blocksize_bits);
221 for (i = 1; i < niocount; i++) {
222 if (nb[i].offset >= next_indir) {
223 nbitmaps++; /* additional indirect */
224 next_indir = nb[i].offset +
225 (EXT3_ADDR_PER_BLOCK(sb)<<sb->s_blocksize_bits);
226 } else if (nb[i].offset != nb[i - 1].offset + sb->s_blocksize) {
227 nbitmaps++; /* additional indirect */
229 nbitmaps += blockpp; /* each leaf in different group? */
232 ngdblocks = nbitmaps;
233 if (nbitmaps > EXT3_SB(sb)->s_groups_count)
234 nbitmaps = EXT3_SB(sb)->s_groups_count;
235 if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
236 ngdblocks = EXT3_SB(sb)->s_gdb_count;
238 needed += nbitmaps + ngdblocks;
240 /* last_rcvd update */
241 needed += EXT3_DATA_TRANS_BLOCKS;
243 #if defined(CONFIG_QUOTA) && !defined(__x86_64__) /* XXX */
244 /* We assume that there will be 1 bit set in s_dquot.flags for each
245 * quota file that is active. This is at least true for now.
247 needed += hweight32(sb_any_quota_enabled(sb)) *
248 EXT3_SINGLEDATA_TRANS_BLOCKS;
254 /* We have to start a huge journal transaction here to hold all of the
255 * metadata for the pages being written here. This is necessitated by
256 * the fact that we do lots of prepare_write operations before we do
257 * any of the matching commit_write operations, so even if we split
258 * up to use "smaller" transactions none of them could complete until
259 * all of them were opened. By having a single journal transaction,
260 * we eliminate duplicate reservations for common blocks like the
261 * superblock and group descriptors or bitmaps.
263 * We will start the transaction here, but each prepare_write will
264 * add a refcount to the transaction, and each commit_write will
265 * remove a refcount. The transaction will be closed when all of
266 * the pages have been written.
268 static void *fsfilt_ext3_brw_start(int objcount, struct fsfilt_objinfo *fso,
269 int niocount, struct niobuf_local *nb,
270 void *desc_private, int logs)
277 LASSERT(current->journal_info == desc_private);
278 journal = EXT3_SB(fso->fso_dentry->d_inode->i_sb)->s_journal;
279 needed = fsfilt_ext3_credits_needed(objcount, fso, niocount, nb);
281 /* The number of blocks we could _possibly_ dirty can very large.
282 * We reduce our request if it is absurd (and we couldn't get that
283 * many credits for a single handle anyways).
285 * At some point we have to limit the size of I/Os sent at one time,
286 * increase the size of the journal, or we have to calculate the
287 * actual journal requirements more carefully by checking all of
288 * the blocks instead of being maximally pessimistic. It remains to
289 * be seen if this is a real problem or not.
291 if (needed > journal->j_max_transaction_buffers) {
292 CERROR("want too many journal credits (%d) using %d instead\n",
293 needed, journal->j_max_transaction_buffers);
294 needed = journal->j_max_transaction_buffers;
297 LASSERTF(needed > 0, "can't start %d credit transaction\n", needed);
299 handle = journal_start(journal, needed);
301 if (IS_ERR(handle)) {
302 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
305 LASSERT(handle->h_buffer_credits >= needed);
306 LASSERT(current->journal_info == handle);
312 static int fsfilt_ext3_commit(struct super_block *sb, struct inode *inode,
313 void *h, int force_sync)
316 handle_t *handle = h;
318 LASSERT(current->journal_info == handle);
320 handle->h_sync = 1; /* recovery likes this */
323 rc = journal_stop(handle);
329 static int fsfilt_ext3_commit_async(struct inode *inode, void *h,
333 transaction_t *transaction;
334 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
337 handle_t *handle = h;
341 LASSERT(current->journal_info == handle);
344 transaction = handle->h_transaction;
345 journal = transaction->t_journal;
346 tid = transaction->t_tid;
347 /* we don't want to be blocked */
349 rc = journal_stop(handle);
351 CERROR("error while stopping transaction: %d\n", rc);
355 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
356 rtid = log_start_commit(journal, transaction);
358 CERROR("strange race: %lu != %lu\n",
359 (unsigned long) tid, (unsigned long) rtid);
361 log_start_commit(journal, tid);
365 *wait_handle = (void *) tid;
366 CDEBUG(D_INODE, "commit async: %lu\n", (unsigned long) tid);
370 static int fsfilt_ext3_commit_wait(struct inode *inode, void *h)
372 tid_t tid = (tid_t)(long)h;
374 CDEBUG(D_INODE, "commit wait: %lu\n", (unsigned long) tid);
375 if (is_journal_aborted(EXT3_JOURNAL(inode)))
378 log_wait_commit(EXT3_JOURNAL(inode), tid);
383 static int fsfilt_ext3_setattr(struct dentry *dentry, void *handle,
384 struct iattr *iattr, int do_trunc)
386 struct inode *inode = dentry->d_inode;
391 /* A _really_ horrible hack to avoid removing the data stored
392 * in the block pointers; this is really the "small" stripe MD data.
393 * We can avoid further hackery by virtue of the MDS file size being
394 * zero all the time (which doesn't invoke block truncate at unlink
395 * time), so we assert we never change the MDS file size from zero. */
396 if (iattr->ia_valid & ATTR_SIZE && !do_trunc) {
397 /* ATTR_SIZE would invoke truncate: clear it */
398 iattr->ia_valid &= ~ATTR_SIZE;
399 EXT3_I(inode)->i_disksize = inode->i_size = iattr->ia_size;
401 /* make sure _something_ gets set - so new inode
402 * goes to disk (probably won't work over XFS */
403 if (!(iattr->ia_valid & (ATTR_MODE | ATTR_MTIME | ATTR_CTIME))){
404 iattr->ia_valid |= ATTR_MODE;
405 iattr->ia_mode = inode->i_mode;
409 /* Don't allow setattr to change file type */
410 iattr->ia_mode = (inode->i_mode & S_IFMT)|(iattr->ia_mode & ~S_IFMT);
412 /* We set these flags on the client, but have already checked perms
413 * so don't confuse inode_change_ok. */
414 iattr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET);
416 if (inode->i_op->setattr) {
417 rc = inode->i_op->setattr(dentry, iattr);
419 rc = inode_change_ok(inode, iattr);
421 rc = inode_setattr(inode, iattr);
429 static int fsfilt_ext3_iocontrol(struct inode * inode, struct file *file,
430 unsigned int cmd, unsigned long arg)
435 if (inode->i_fop->ioctl)
436 rc = inode->i_fop->ioctl(inode, file, cmd, arg);
443 static int fsfilt_ext3_set_xattr(struct inode * inode, void *handle, char *name,
444 void *buffer, int buffer_size)
450 rc = ext3_xattr_set_handle(handle, inode, EXT3_XATTR_INDEX_TRUSTED,
451 name, buffer, buffer_size, 0);
454 CERROR("set xattr %s from inode %lu: rc %d\n",
455 name, inode->i_ino, rc);
459 static int fsfilt_ext3_get_xattr(struct inode *inode, char *name,
460 void *buffer, int buffer_size)
466 rc = ext3_xattr_get(inode, EXT3_XATTR_INDEX_TRUSTED,
467 name, buffer, buffer_size);
471 return (rc == -ENODATA) ? 0 : rc;
473 CDEBUG(D_INFO, "error getting EA %s from inode %lu: rc %d\n",
474 name, inode->i_ino, rc);
475 memset(buffer, 0, buffer_size);
476 return (rc == -ENODATA) ? 0 : rc;
482 static int fsfilt_ext3_set_md(struct inode *inode, void *handle,
483 void *lmm, int lmm_size,
490 rc = fsfilt_ext3_set_xattr(inode, handle,
491 XATTR_LUSTRE_MDS_LOV_EA,
495 rc = fsfilt_ext3_set_xattr(inode, handle,
496 XATTR_LUSTRE_MDS_MEA_EA,
500 rc = fsfilt_ext3_set_xattr(inode, handle,
501 XATTR_LUSTRE_MDS_SID_EA,
505 rc = fsfilt_ext3_set_xattr(inode, handle,
506 XATTR_LUSTRE_MDS_PID_EA,
510 rc = fsfilt_ext3_set_xattr(inode, handle,
511 XATTR_LUSTRE_MDS_KEY_EA,
521 static int fsfilt_ext3_get_md(struct inode *inode, void *lmm,
522 int lmm_size, enum ea_type type)
528 rc = fsfilt_ext3_get_xattr(inode,
529 XATTR_LUSTRE_MDS_LOV_EA,
533 rc = fsfilt_ext3_get_xattr(inode,
534 XATTR_LUSTRE_MDS_MEA_EA,
538 rc = fsfilt_ext3_get_xattr(inode,
539 XATTR_LUSTRE_MDS_SID_EA,
543 rc = fsfilt_ext3_get_xattr(inode,
544 XATTR_LUSTRE_MDS_PID_EA,
548 rc = fsfilt_ext3_get_xattr(inode,
549 XATTR_LUSTRE_MDS_KEY_EA,
559 static int fsfilt_ext3_send_bio(int rw, struct inode *inode, void *bio)
562 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
563 submit_bio(rw, (struct bio *)bio);
565 struct bio *b = (struct kiobuf *)bio;
568 rc = brw_kiovec(rw, 1, &b, inode->i_dev,
569 b->blocks, 1 << inode->i_blkbits);
571 blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
573 if (rc != (1 << inode->i_blkbits) * b->nr_pages * blocks_per_page) {
574 CERROR("short write? expected %d, wrote %d\n",
575 (1 << inode->i_blkbits) * b->nr_pages *
576 blocks_per_page, rc);
582 static struct page *fsfilt_ext3_getpage(struct inode *inode, long int index)
587 page = grab_cache_page(inode->i_mapping, index);
589 return ERR_PTR(-ENOMEM);
591 if (PageUptodate(page)) {
596 rc = inode->i_mapping->a_ops->readpage(NULL, page);
598 page_cache_release(page);
605 static ssize_t fsfilt_ext3_readpage(struct file *file, char *buf, size_t count,
608 struct inode *inode = file->f_dentry->d_inode;
611 if (S_ISREG(inode->i_mode))
612 rc = file->f_op->read(file, buf, count, off);
614 const int blkbits = inode->i_sb->s_blocksize_bits;
615 const int blksize = inode->i_sb->s_blocksize;
617 CDEBUG(D_EXT2, "reading "LPSZ" at dir %lu+%llu\n",
618 count, inode->i_ino, *off);
620 struct buffer_head *bh;
623 if (*off < inode->i_size) {
626 bh = ext3_bread(NULL, inode, *off >> blkbits,
629 CDEBUG(D_EXT2, "read %u@%llu\n", blksize, *off);
632 memcpy(buf, bh->b_data, blksize);
635 /* XXX in theory we should just fake
636 * this buffer and continue like ext3,
637 * especially if this is a partial read
639 CERROR("error read dir %lu+%llu: %d\n",
640 inode->i_ino, *off, err);
645 struct ext3_dir_entry_2 *fake = (void *)buf;
647 CDEBUG(D_EXT2, "fake %u@%llu\n", blksize, *off);
648 memset(fake, 0, sizeof(*fake));
649 fake->rec_len = cpu_to_le32(blksize);
661 static void fsfilt_ext3_cb_func(struct journal_callback *jcb, int error)
663 struct fsfilt_cb_data *fcb = (struct fsfilt_cb_data *)jcb;
665 fcb->cb_func(fcb->cb_obd, fcb->cb_last_num, fcb->cb_data, error);
667 OBD_SLAB_FREE(fcb, fcb_cache, sizeof *fcb);
668 atomic_dec(&fcb_cache_count);
671 static int fsfilt_ext3_add_journal_cb(struct obd_device *obd,
672 struct super_block *sb,
673 __u64 last_num, void *handle,
677 struct fsfilt_cb_data *fcb;
679 OBD_SLAB_ALLOC(fcb, fcb_cache, GFP_NOFS, sizeof *fcb);
683 atomic_inc(&fcb_cache_count);
684 fcb->cb_func = cb_func;
686 fcb->cb_last_num = last_num;
687 fcb->cb_data = cb_data;
689 CDEBUG(D_EXT2, "set callback for last_num: "LPD64"\n", last_num);
691 journal_callback_set(handle, fsfilt_ext3_cb_func,
692 (struct journal_callback *)fcb);
698 * We need to hack the return value for the free inode counts because
699 * the current EA code requires one filesystem block per inode with EAs,
700 * so it is possible to run out of blocks before we run out of inodes.
702 * This can be removed when the ext3 EA code is fixed.
704 static int fsfilt_ext3_statfs(struct super_block *sb, struct obd_statfs *osfs)
709 memset(&sfs, 0, sizeof(sfs));
711 rc = sb->s_op->statfs(sb, &sfs);
713 if (!rc && sfs.f_bfree < sfs.f_ffree) {
714 sfs.f_files = (sfs.f_files - sfs.f_ffree) + sfs.f_bfree;
715 sfs.f_ffree = sfs.f_bfree;
718 statfs_pack(osfs, &sfs);
722 static int fsfilt_ext3_sync(struct super_block *sb)
724 return ext3_force_commit(sb);
727 #ifdef EXT3_MULTIBLOCK_ALLOCATOR
728 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
729 #define ext3_up_truncate_sem(inode) up_write(&EXT3_I(inode)->truncate_sem);
730 #define ext3_down_truncate_sem(inode) down_write(&EXT3_I(inode)->truncate_sem);
732 #define ext3_up_truncate_sem(inode) up(&EXT3_I(inode)->truncate_sem);
733 #define ext3_down_truncate_sem(inode) down(&EXT3_I(inode)->truncate_sem);
736 #include <linux/lustre_version.h>
737 #if EXT3_EXT_MAGIC == 0xf301
738 #define ee_start e_start
739 #define ee_block e_block
742 #ifndef EXT3_BB_MAX_BLOCKS
743 #define ext3_mb_new_blocks(handle, inode, goal, count, aflags, err) \
744 ext3_new_blocks(handle, inode, count, goal, err)
748 unsigned long *blocks;
755 static int ext3_ext_find_goal(struct inode *inode, struct ext3_ext_path *path,
756 unsigned long block, int *aflags)
758 struct ext3_inode_info *ei = EXT3_I(inode);
759 unsigned long bg_start;
760 unsigned long colour;
764 struct ext3_extent *ex;
765 depth = path->p_depth;
767 /* try to predict block placement */
768 if ((ex = path[depth].p_ext)) {
770 /* This prefers to eat into a contiguous extent
771 * rather than find an extent that the whole
772 * request will fit into. This can fragment data
773 * block allocation and prevents our lovely 1M I/Os
774 * from reaching the disk intact. */
776 if (ex->ee_block + ex->ee_len == block)
779 return ex->ee_start + (block - ex->ee_block);
782 /* it looks index is empty
783 * try to find starting from index itself */
784 if (path[depth].p_bh)
785 return path[depth].p_bh->b_blocknr;
788 /* OK. use inode's group */
789 bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
790 le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
791 colour = (current->pid % 16) *
792 (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
793 return bg_start + colour + block;
796 static int ext3_ext_new_extent_cb(struct ext3_extents_tree *tree,
797 struct ext3_ext_path *path,
798 struct ext3_extent *newex, int exist)
800 struct inode *inode = tree->inode;
801 struct bpointers *bp = tree->private;
802 int count, err, goal;
803 unsigned long pblock;
810 EXT_ASSERT(i == path->p_depth);
811 EXT_ASSERT(path[i].p_hdr);
818 if (bp->create == 0) {
820 if (newex->ee_block < bp->start)
821 i = bp->start - newex->ee_block;
822 if (i >= newex->ee_len)
823 CERROR("nothing to do?! i = %d, e_num = %u\n",
825 for (; i < newex->ee_len && bp->num; i++) {
836 tgen = EXT_GENERATION(tree);
837 count = ext3_ext_calc_credits_for_insert(tree, path);
838 ext3_up_truncate_sem(inode);
840 handle = journal_start(EXT3_JOURNAL(inode), count + EXT3_ALLOC_NEEDED + 1);
842 if (IS_ERR(handle)) {
843 ext3_down_truncate_sem(inode);
844 return PTR_ERR(handle);
847 if (tgen != EXT_GENERATION(tree)) {
848 /* the tree has changed. so path can be invalid at moment */
850 journal_stop(handle);
852 ext3_down_truncate_sem(inode);
855 ext3_down_truncate_sem(inode);
856 count = newex->ee_len;
857 goal = ext3_ext_find_goal(inode, path, newex->ee_block, &aflags);
858 aflags |= 2; /* block have been already reserved */
859 pblock = ext3_mb_new_blocks(handle, inode, goal, &count, aflags, &err);
862 EXT_ASSERT(count <= newex->ee_len);
864 /* insert new extent */
865 newex->ee_start = pblock;
866 newex->ee_len = count;
867 err = ext3_ext_insert_extent(handle, tree, path, newex);
871 /* correct on-disk inode size */
872 if (newex->ee_len > 0) {
873 new_i_size = (loff_t) newex->ee_block + newex->ee_len;
874 new_i_size = new_i_size << inode->i_blkbits;
875 if (new_i_size > EXT3_I(inode)->i_disksize) {
876 EXT3_I(inode)->i_disksize = new_i_size;
877 err = ext3_mark_inode_dirty(handle, inode);
882 journal_stop(handle);
888 CERROR("hmm. why do we find this extent?\n");
889 CERROR("initial space: %lu:%u\n",
890 bp->start, bp->init_num);
891 CERROR("current extent: %u/%u/%u %d\n",
892 newex->ee_block, newex->ee_len,
893 newex->ee_start, exist);
896 if (newex->ee_block < bp->start)
897 i = bp->start - newex->ee_block;
898 if (i >= newex->ee_len)
899 CERROR("nothing to do?! i = %d, e_num = %u\n",
901 for (; i < newex->ee_len && bp->num; i++) {
902 *(bp->created) = (exist == 0 ? 1 : 0);
904 *(bp->blocks) = newex->ee_start + i;
913 int fsfilt_map_nblocks(struct inode *inode, unsigned long block,
914 unsigned long num, unsigned long *blocks,
915 int *created, int create)
917 struct ext3_extents_tree tree;
921 CDEBUG(D_OTHER, "blocks %lu-%lu requested for inode %u\n",
922 block, block + num, (unsigned) inode->i_ino);
924 ext3_init_tree_desc(&tree, inode);
927 bp.created = created;
929 bp.init_num = bp.num = num;
932 ext3_down_truncate_sem(inode);
933 err = ext3_ext_walk_space(&tree, block, num, ext3_ext_new_extent_cb);
934 ext3_ext_invalidate_cache(&tree);
935 ext3_up_truncate_sem(inode);
937 /* unmap underlying pages/buffers from blockdevice mapping */
939 struct block_device *bdev = inode->i_sb->s_bdev;
940 for (i = 0; i < num; i++) {
943 unmap_underlying_metadata(bdev, blocks[i]);
949 int fsfilt_ext3_map_ext_inode_pages(struct inode *inode, struct page **page,
950 int pages, unsigned long *blocks,
951 int *created, int create)
953 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
955 struct page *fp = NULL;
958 CDEBUG(D_OTHER, "inode %lu: map %d pages from %lu\n",
959 inode->i_ino, pages, (*page)->index);
961 /* pages are sorted already. so, we just have to find
962 * contig. space and process them properly */
965 /* start new extent */
970 } else if (fp->index + clen == (*page)->index) {
971 /* continue the extent */
978 /* process found extent */
979 rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page,
980 clen * blocks_per_page, blocks,
985 /* look for next extent */
987 blocks += blocks_per_page * clen;
988 created += blocks_per_page * clen;
992 rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page,
993 clen * blocks_per_page, blocks,
1000 extern int ext3_map_inode_page(struct inode *inode, struct page *page,
1001 unsigned long *blocks, int *created, int create);
1002 int fsfilt_ext3_map_bm_inode_pages(struct inode *inode, struct page **page,
1003 int pages, unsigned long *blocks,
1004 int *created, int create)
1006 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
1010 for (i = 0, cr = created, b = blocks; i < pages; i++, page++) {
1011 rc = ext3_map_inode_page(inode, *page, b, cr, create);
1013 CERROR("ino %lu, blk %lu cr %u create %d: rc %d\n",
1014 inode->i_ino, *b, *cr, create, rc);
1018 b += blocks_per_page;
1019 cr += blocks_per_page;
1024 int fsfilt_ext3_map_inode_pages(struct inode *inode, struct page **page,
1025 int pages, unsigned long *blocks,
1026 int *created, int create,
1027 struct semaphore *optional_sem)
1030 #ifdef EXT3_MULTIBLOCK_ALLOCATOR
1031 if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL) {
1032 rc = fsfilt_ext3_map_ext_inode_pages(inode, page, pages,
1033 blocks, created, create);
1037 if (optional_sem != NULL)
1039 rc = fsfilt_ext3_map_bm_inode_pages(inode, page, pages, blocks,
1041 if (optional_sem != NULL)
1047 extern int ext3_prep_san_write(struct inode *inode, long *blocks,
1048 int nblocks, loff_t newsize);
1049 static int fsfilt_ext3_prep_san_write(struct inode *inode, long *blocks,
1050 int nblocks, loff_t newsize)
1052 return ext3_prep_san_write(inode, blocks, nblocks, newsize);
1055 static int fsfilt_ext3_read_record(struct file * file, void *buf,
1056 int size, loff_t *offs)
1058 struct inode *inode = file->f_dentry->d_inode;
1059 unsigned long block;
1060 struct buffer_head *bh;
1061 int err, blocksize, csize, boffs;
1063 /* prevent reading after eof */
1065 if (inode->i_size < *offs + size) {
1066 size = inode->i_size - *offs;
1069 CERROR("size %llu is too short for read %u@%llu\n",
1070 inode->i_size, size, *offs);
1072 } else if (size == 0) {
1079 blocksize = 1 << inode->i_blkbits;
1082 block = *offs >> inode->i_blkbits;
1083 boffs = *offs & (blocksize - 1);
1084 csize = min(blocksize - boffs, size);
1085 bh = ext3_bread(NULL, inode, block, 0, &err);
1087 CERROR("can't read block: %d\n", err);
1091 memcpy(buf, bh->b_data + boffs, csize);
1101 static int fsfilt_ext3_write_record(struct file *file, void *buf, int bufsize,
1102 loff_t *offs, int force_sync)
1104 struct buffer_head *bh = NULL;
1105 unsigned long block;
1106 struct inode *inode = file->f_dentry->d_inode;
1107 loff_t old_size = inode->i_size, offset = *offs;
1108 loff_t new_size = inode->i_size;
1111 int err = 0, block_count = 0, blocksize, size, boffs;
1113 /* Determine how many transaction credits are needed */
1114 blocksize = 1 << inode->i_blkbits;
1115 block_count = (*offs & (blocksize - 1)) + bufsize;
1116 block_count = (block_count + blocksize - 1) >> inode->i_blkbits;
1118 journal = EXT3_SB(inode->i_sb)->s_journal;
1120 handle = journal_start(journal,
1121 block_count * EXT3_DATA_TRANS_BLOCKS + 2);
1123 if (IS_ERR(handle)) {
1124 CERROR("can't start transaction\n");
1125 return PTR_ERR(handle);
1128 while (bufsize > 0) {
1132 block = offset >> inode->i_blkbits;
1133 boffs = offset & (blocksize - 1);
1134 size = min(blocksize - boffs, bufsize);
1135 bh = ext3_bread(handle, inode, block, 1, &err);
1137 CERROR("can't read/create block: %d\n", err);
1141 err = ext3_journal_get_write_access(handle, bh);
1143 CERROR("journal_get_write_access() returned error %d\n",
1147 LASSERT(bh->b_data + boffs + size <= bh->b_data + bh->b_size);
1148 memcpy(bh->b_data + boffs, buf, size);
1149 err = ext3_journal_dirty_metadata(handle, bh);
1151 CERROR("journal_dirty_metadata() returned error %d\n",
1155 if (offset + size > new_size)
1156 new_size = offset + size;
1163 handle->h_sync = 1; /* recovery likes this */
1168 /* correct in-core and on-disk sizes */
1169 if (new_size > inode->i_size) {
1171 if (new_size > inode->i_size)
1172 inode->i_size = new_size;
1173 if (inode->i_size > EXT3_I(inode)->i_disksize)
1174 EXT3_I(inode)->i_disksize = inode->i_size;
1175 if (inode->i_size > old_size)
1176 mark_inode_dirty(inode);
1181 journal_stop(handle);
1189 static int fsfilt_ext3_setup(struct obd_device *obd, struct super_block *sb)
1191 #ifdef EXT3_FEATURE_INCOMPAT_MDSNUM
1192 struct mds_obd *mds = &obd->u.mds;
1195 EXT3_SB(sb)->dx_lock = fsfilt_ext3_dx_lock;
1196 EXT3_SB(sb)->dx_unlock = fsfilt_ext3_dx_unlock;
1199 CWARN("Enabling PDIROPS\n");
1200 set_opt(EXT3_SB(sb)->s_mount_opt, PDIROPS);
1201 sb->s_flags |= S_PDIROPS;
1203 /* setup mdsnum in underlying fs */
1204 #ifdef EXT3_FEATURE_INCOMPAT_MDSNUM
1205 if (mds->mds_md_obd) {
1206 struct ext3_sb_info *sbi = EXT3_SB(sb);
1207 struct ext3_super_block *es = sbi->s_es;
1211 if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_MDSNUM)) {
1212 CWARN("%s: set mdsnum %d in ext3\n",
1213 obd->obd_name, mds->mds_num);
1215 handle = journal_start(sbi->s_journal, 1);
1217 LASSERT(!IS_ERR(handle));
1218 err = ext3_journal_get_write_access(handle, sbi->s_sbh);
1220 EXT3_SET_INCOMPAT_FEATURE(sb,
1221 EXT3_FEATURE_INCOMPAT_MDSNUM);
1222 es->s_mdsnum = mds->mds_num;
1223 err = ext3_journal_dirty_metadata(handle, sbi->s_sbh);
1226 journal_stop(handle);
1229 CWARN("%s: mdsnum initialized to %u in ext3fs\n",
1230 obd->obd_name, es->s_mdsnum);
1232 sbi->s_mdsnum = es->s_mdsnum;
1238 extern int ext3_add_dir_entry(struct dentry *dentry);
1239 extern int ext3_del_dir_entry(struct dentry *dentry);
1241 static int fsfilt_ext3_add_dir_entry(struct obd_device *obd,
1242 struct dentry *parent,
1243 char *name, int namelen,
1245 unsigned long generation,
1249 #ifdef EXT3_FEATURE_INCOMPAT_MDSNUM
1250 struct dentry *dentry;
1253 LASSERT(namelen != 0);
1254 dentry = ll_lookup_one_len(name, parent, namelen);
1255 if (IS_ERR(dentry)) {
1256 CERROR("can't lookup %*s in %lu/%lu: %d\n", dentry->d_name.len,
1257 dentry->d_name.name, dentry->d_inode->i_ino,
1258 (unsigned long) dentry->d_inode->i_generation,
1259 (int) PTR_ERR(dentry));
1260 RETURN(PTR_ERR(dentry));
1262 if (dentry->d_inode != NULL || dentry->d_flags & DCACHE_CROSS_REF) {
1263 CERROR("dentry %*s(0x%p) found\n", dentry->d_name.len,
1264 dentry->d_name.name, dentry);
1269 /* mds_reint_rename() may use this method to add dir entry
1270 * that points onto local inode. and we don't want to find
1271 * it cross-ref by subsequent lookups */
1274 dentry->d_flags |= DCACHE_CROSS_REF;
1275 dentry->d_inum = ino;
1276 dentry->d_mdsnum = mds;
1277 dentry->d_generation = generation;
1278 dentry->d_fid = fid;
1280 err = ext3_add_dir_entry(dentry);
1287 #error "rebuild kernel and lustre with ext3-mds-num patch!"
1292 static int fsfilt_ext3_del_dir_entry(struct obd_device *obd,
1293 struct dentry *dentry)
1295 #ifdef EXT3_FEATURE_INCOMPAT_MDSNUM
1298 err = ext3_del_dir_entry(dentry);
1304 #error "rebuild kernel and lustre with ext3-mds-num patch!"
1309 /* If fso is NULL, op is FSFILT operation, otherwise op is number of fso
1310 objects. Logs is number of logfiles to update */
1311 static int fsfilt_ext3_get_op_len(int op, struct fsfilt_objinfo *fso, int logs)
1315 case FSFILT_OP_CREATE:
1316 /* directory leaf, index & indirect & EA*/
1317 return 4 + 3 * logs;
1318 case FSFILT_OP_UNLINK:
1324 struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
1325 int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
1326 int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
1327 for (i = 0; i < op; i++, fso++) {
1328 int nblocks = fso->fso_bufcnt * blockpp;
1329 int ndindirect = min(nblocks, addrpp + 1);
1330 int nindir = nblocks + ndindirect + 1;
1334 return needed + 3 * logs;
1341 #define EXTENTS_EA "write_extents"
1342 #define EXTENTS_EA_SIZE 64
1344 int ext3_ext_in_ea_alloc_space(struct inode *, int, const char *, unsigned long, unsigned long);
1345 int ext3_ext_in_ea_remove_space(struct inode *, int, const char *, unsigned long, unsigned long);
1346 int ext3_ext_in_ea_get_extents(struct inode *, int, const char *, char **, int *);
1347 int ext3_ext_in_ea_get_extents_num(struct inode *, int, const char *, int *);
1349 static int fsfilt_ext3_insert_extents_ea(struct inode *inode,
1355 rc = ext3_ext_in_ea_alloc_space(inode, EXT3_XATTR_INDEX_TRUSTED,
1356 EXTENTS_EA, from, num);
1360 static int fsfilt_ext3_remove_extents_ea(struct inode *inode,
1366 rc = ext3_ext_in_ea_remove_space(inode, EXT3_XATTR_INDEX_TRUSTED,
1367 EXTENTS_EA, from, num);
1371 extern int ext3_init_tree_in_ea(struct inode *inode, int name_index,
1372 const char *eaname, int size);
1374 static int fsfilt_ext3_init_extents_ea(struct inode *inode)
1378 rc = ext3_init_tree_in_ea(inode, EXT3_XATTR_INDEX_TRUSTED,
1383 static int fsfilt_ext3_get_inode_write_extents(struct inode *inode,
1384 char **pbuf, int *size)
1388 rc = ext3_ext_in_ea_get_extents(inode, EXT3_XATTR_INDEX_TRUSTED,
1389 EXTENTS_EA, pbuf, size);
1393 static int fsfilt_ext3_get_write_extents_num(struct inode *inode, int *size)
1397 rc = ext3_ext_in_ea_get_extents_num(inode, EXT3_XATTR_INDEX_TRUSTED,
1402 static struct fsfilt_operations fsfilt_ext3_ops = {
1404 .fs_owner = THIS_MODULE,
1405 .fs_start = fsfilt_ext3_start,
1406 .fs_brw_start = fsfilt_ext3_brw_start,
1407 .fs_commit = fsfilt_ext3_commit,
1408 .fs_commit_async = fsfilt_ext3_commit_async,
1409 .fs_commit_wait = fsfilt_ext3_commit_wait,
1410 .fs_setattr = fsfilt_ext3_setattr,
1411 .fs_iocontrol = fsfilt_ext3_iocontrol,
1412 .fs_set_md = fsfilt_ext3_set_md,
1413 .fs_get_md = fsfilt_ext3_get_md,
1414 .fs_readpage = fsfilt_ext3_readpage,
1415 .fs_add_journal_cb = fsfilt_ext3_add_journal_cb,
1416 .fs_statfs = fsfilt_ext3_statfs,
1417 .fs_sync = fsfilt_ext3_sync,
1418 .fs_map_inode_pages = fsfilt_ext3_map_inode_pages,
1419 .fs_prep_san_write = fsfilt_ext3_prep_san_write,
1420 .fs_write_record = fsfilt_ext3_write_record,
1421 .fs_read_record = fsfilt_ext3_read_record,
1422 .fs_setup = fsfilt_ext3_setup,
1423 .fs_getpage = fsfilt_ext3_getpage,
1424 .fs_send_bio = fsfilt_ext3_send_bio,
1425 .fs_set_xattr = fsfilt_ext3_set_xattr,
1426 .fs_get_xattr = fsfilt_ext3_get_xattr,
1427 .fs_get_op_len = fsfilt_ext3_get_op_len,
1428 .fs_add_dir_entry = fsfilt_ext3_add_dir_entry,
1429 .fs_del_dir_entry = fsfilt_ext3_del_dir_entry,
1430 .fs_init_extents_ea = fsfilt_ext3_init_extents_ea,
1431 .fs_insert_extents_ea = fsfilt_ext3_insert_extents_ea,
1432 .fs_remove_extents_ea = fsfilt_ext3_remove_extents_ea,
1433 .fs_get_inode_write_extents = fsfilt_ext3_get_inode_write_extents,
1434 .fs_get_write_extents_num = fsfilt_ext3_get_write_extents_num,
1437 static int __init fsfilt_ext3_init(void)
1441 fcb_cache = kmem_cache_create("fsfilt_ext3_fcb",
1442 sizeof(struct fsfilt_cb_data), 0,
1445 CERROR("error allocating fsfilt journal callback cache\n");
1446 GOTO(out, rc = -ENOMEM);
1449 rc = fsfilt_register_ops(&fsfilt_ext3_ops);
1452 kmem_cache_destroy(fcb_cache);
1457 static void __exit fsfilt_ext3_exit(void)
1459 fsfilt_unregister_ops(&fsfilt_ext3_ops);
1460 LASSERTF(kmem_cache_destroy(fcb_cache) == 0,
1461 "can't free fsfilt callback cache: count %d\n",
1462 atomic_read(&fcb_cache_count));
1465 module_init(fsfilt_ext3_init);
1466 module_exit(fsfilt_ext3_exit);
1468 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1469 MODULE_DESCRIPTION("Lustre ext3 Filesystem Helper v0.1");
1470 MODULE_LICENSE("GPL");