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
29 #include <linux/jbd.h>
30 #include <linux/slab.h>
31 #include <linux/pagemap.h>
32 #include <linux/quotaops.h>
33 #include <linux/ext3_fs.h>
34 #include <linux/ext3_jbd.h>
35 #include <linux/version.h>
37 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
38 #include <linux/ext3_xattr.h>
40 #include <linux/../../fs/ext3/xattr.h>
42 #include <linux/kp30.h>
43 #include <linux/lustre_fsfilt.h>
44 #include <linux/obd.h>
45 #include <linux/obd_class.h>
46 #include <linux/module.h>
48 static kmem_cache_t *fcb_cache;
49 static atomic_t fcb_cache_count = ATOMIC_INIT(0);
51 struct fsfilt_cb_data {
52 struct journal_callback cb_jcb; /* jbd private data - MUST BE FIRST */
53 fsfilt_cb_t cb_func; /* MDS/OBD completion function */
54 struct obd_device *cb_obd; /* MDS/OBD completion device */
55 __u64 cb_last_rcvd; /* MDS/OST last committed operation */
56 void *cb_data; /* MDS/OST completion function data */
59 #define EXT3_XATTR_INDEX_LUSTRE 5
60 #define XATTR_LUSTRE_MDS_OBJID "system.lustre_mds_objid"
63 * We don't currently need any additional blocks for rmdir and
64 * unlink transactions because we are storing the OST oa_id inside
65 * the inode (which we will be changing anyways as part of this
68 static void *fsfilt_ext3_start(struct inode *inode, int op, void *desc_private)
70 /* For updates to the last recieved file */
71 int nblocks = EXT3_DATA_TRANS_BLOCKS;
74 LASSERT(current->journal_info == NULL);
77 case FSFILT_OP_CREATE_LOG:
78 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
79 op = FSFILT_OP_CREATE;
81 case FSFILT_OP_UNLINK_LOG:
82 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
83 op = FSFILT_OP_UNLINK;
89 case FSFILT_OP_UNLINK:
90 nblocks += EXT3_DELETE_TRANS_BLOCKS;
92 case FSFILT_OP_RENAME:
93 /* modify additional directory */
94 nblocks += EXT3_DATA_TRANS_BLOCKS;
96 case FSFILT_OP_SYMLINK:
97 /* additional block + block bitmap + GDT for long symlink */
100 case FSFILT_OP_CREATE:
101 case FSFILT_OP_MKDIR:
102 case FSFILT_OP_MKNOD:
103 /* modify one inode + block bitmap + GDT */
107 /* modify parent directory */
108 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
110 case FSFILT_OP_SETATTR:
111 /* Setattr on inode */
114 default: CERROR("unknown transaction start op %d\n", op);
118 LASSERT(current->journal_info == desc_private);
120 handle = journal_start(EXT3_JOURNAL(inode), nblocks);
124 LASSERT(current->journal_info == handle);
129 * Calculate the number of buffer credits needed to write multiple pages in
130 * a single ext3 transaction. No, this shouldn't be here, but as yet ext3
131 * doesn't have a nice API for calculating this sort of thing in advance.
133 * See comment above ext3_writepage_trans_blocks for details. We assume
134 * no data journaling is being done, but it does allow for all of the pages
135 * being non-contiguous. If we are guaranteed contiguous pages we could
136 * reduce the number of (d)indirect blocks a lot.
138 * With N blocks per page and P pages, for each inode we have at most:
140 * min(N*P, blocksize/4 + 1) dindirect blocks
143 * For the entire filesystem, we have at most:
144 * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
145 * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
146 * objcount inode blocks
148 * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
150 * 1 EXT3_DATA_TRANS_BLOCKS for the last_rcvd update.
152 static int fsfilt_ext3_credits_needed(int objcount, struct fsfilt_objinfo *fso)
154 struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
155 int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
156 int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
159 int needed = objcount + 1;
162 for (i = 0; i < objcount; i++, fso++) {
163 int nblocks = fso->fso_bufcnt * blockpp;
164 int ndindirect = min(nblocks, addrpp + 1);
165 int nindir = nblocks + ndindirect + 1;
167 nbitmaps += nindir + nblocks;
168 ngdblocks += nindir + nblocks;
173 /* Assumes ext3 and ext3 have same sb_info layout at the start. */
174 if (nbitmaps > EXT3_SB(sb)->s_groups_count)
175 nbitmaps = EXT3_SB(sb)->s_groups_count;
176 if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
177 ngdblocks = EXT3_SB(sb)->s_gdb_count;
179 needed += nbitmaps + ngdblocks;
181 /* last_rcvd update */
182 needed += EXT3_DATA_TRANS_BLOCKS;
185 /* We assume that there will be 1 bit set in s_dquot.flags for each
186 * quota file that is active. This is at least true for now.
188 needed += hweight32(sb_any_quota_enabled(sb)) *
189 EXT3_SINGLEDATA_TRANS_BLOCKS;
195 /* We have to start a huge journal transaction here to hold all of the
196 * metadata for the pages being written here. This is necessitated by
197 * the fact that we do lots of prepare_write operations before we do
198 * any of the matching commit_write operations, so even if we split
199 * up to use "smaller" transactions none of them could complete until
200 * all of them were opened. By having a single journal transaction,
201 * we eliminate duplicate reservations for common blocks like the
202 * superblock and group descriptors or bitmaps.
204 * We will start the transaction here, but each prepare_write will
205 * add a refcount to the transaction, and each commit_write will
206 * remove a refcount. The transaction will be closed when all of
207 * the pages have been written.
209 static void *fsfilt_ext3_brw_start(int objcount, struct fsfilt_objinfo *fso,
210 int niocount, void *desc_private)
217 LASSERT(current->journal_info == desc_private);
218 journal = EXT3_SB(fso->fso_dentry->d_inode->i_sb)->s_journal;
219 needed = fsfilt_ext3_credits_needed(objcount, fso);
221 /* The number of blocks we could _possibly_ dirty can very large.
222 * We reduce our request if it is absurd (and we couldn't get that
223 * many credits for a single handle anyways).
225 * At some point we have to limit the size of I/Os sent at one time,
226 * increase the size of the journal, or we have to calculate the
227 * actual journal requirements more carefully by checking all of
228 * the blocks instead of being maximally pessimistic. It remains to
229 * be seen if this is a real problem or not.
231 if (needed > journal->j_max_transaction_buffers) {
232 CERROR("want too many journal credits (%d) using %d instead\n",
233 needed, journal->j_max_transaction_buffers);
234 needed = journal->j_max_transaction_buffers;
238 handle = journal_start(journal, needed);
240 if (IS_ERR(handle)) {
241 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
244 LASSERT(handle->h_buffer_credits >= needed);
245 LASSERT(current->journal_info == handle);
251 static int fsfilt_ext3_commit(struct inode *inode, void *h, int force_sync)
254 handle_t *handle = h;
256 LASSERT(current->journal_info == handle);
258 handle->h_sync = 1; /* recovery likes this */
261 rc = journal_stop(handle);
264 LASSERT(current->journal_info == NULL);
268 static int fsfilt_ext3_setattr(struct dentry *dentry, void *handle,
269 struct iattr *iattr, int do_trunc)
271 struct inode *inode = dentry->d_inode;
276 /* A _really_ horrible hack to avoid removing the data stored
277 * in the block pointers; this is really the "small" stripe MD data.
278 * We can avoid further hackery by virtue of the MDS file size being
279 * zero all the time (which doesn't invoke block truncate at unlink
280 * time), so we assert we never change the MDS file size from zero. */
281 if (iattr->ia_valid & ATTR_SIZE && !do_trunc) {
282 /* ATTR_SIZE would invoke truncate: clear it */
283 iattr->ia_valid &= ~ATTR_SIZE;
284 EXT3_I(inode)->i_disksize = inode->i_size = iattr->ia_size;
286 /* make sure _something_ gets set - so new inode
287 * goes to disk (probably won't work over XFS */
288 if (!(iattr->ia_valid & (ATTR_MODE | ATTR_MTIME | ATTR_CTIME))){
289 iattr->ia_valid |= ATTR_MODE;
290 iattr->ia_mode = inode->i_mode;
294 /* Don't allow setattr to change file type */
295 iattr->ia_mode = (inode->i_mode & S_IFMT)|(iattr->ia_mode & ~S_IFMT);
297 /* We set these flags on the client, but have already checked perms
298 * so don't confuse inode_change_ok. */
299 iattr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET);
301 if (inode->i_op->setattr) {
302 rc = inode->i_op->setattr(dentry, iattr);
304 rc = inode_change_ok(inode, iattr);
306 rc = inode_setattr(inode, iattr);
314 static int fsfilt_ext3_set_md(struct inode *inode, void *handle,
315 void *lmm, int lmm_size)
319 /* Nasty hack city - store stripe MD data in the block pointers if
320 * it will fit, because putting it in an EA currently kills the MDS
321 * performance. We'll fix this with "fast EAs" in the future.
323 if (inode->i_blocks == 0 && lmm_size <= sizeof(EXT3_I(inode)->i_data) -
324 sizeof(EXT3_I(inode)->i_data[0])) {
325 /* XXX old_size is debugging only */
326 int old_size = EXT3_I(inode)->i_data[0];
328 LASSERT(old_size < sizeof(EXT3_I(inode)->i_data));
329 CERROR("setting EA on %lu again... interesting\n",
333 EXT3_I(inode)->i_data[0] = cpu_to_le32(lmm_size);
334 memcpy(&EXT3_I(inode)->i_data[1], lmm, lmm_size);
335 mark_inode_dirty(inode);
340 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
341 rc = ext3_xattr_set(handle, inode, EXT3_XATTR_INDEX_LUSTRE,
342 XATTR_LUSTRE_MDS_OBJID, lmm, lmm_size, 0);
344 rc = ext3_xattr_set_handle(handle, inode,
345 EXT3_XATTR_INDEX_LUSTRE,
346 XATTR_LUSTRE_MDS_OBJID, lmm,
354 CERROR("error adding MD data to inode %lu: rc = %d\n",
359 static int fsfilt_ext3_get_md(struct inode *inode, void *lmm, int lmm_size)
363 if (inode->i_blocks == 0 && EXT3_I(inode)->i_data[0]) {
364 int size = le32_to_cpu(EXT3_I(inode)->i_data[0]);
365 LASSERT(size < sizeof(EXT3_I(inode)->i_data));
369 memcpy(lmm, &EXT3_I(inode)->i_data[1], size);
376 rc = ext3_xattr_get(inode, EXT3_XATTR_INDEX_LUSTRE,
377 XATTR_LUSTRE_MDS_OBJID, lmm, lmm_size);
381 /* This gives us the MD size */
383 return (rc == -ENODATA) ? 0 : rc;
386 CDEBUG(D_INFO, "error getting EA %s from inode %lu: "
387 "rc = %d\n", XATTR_LUSTRE_MDS_OBJID, inode->i_ino, rc);
388 memset(lmm, 0, lmm_size);
389 return (rc == -ENODATA) ? 0 : rc;
395 static ssize_t fsfilt_ext3_readpage(struct file *file, char *buf, size_t count,
398 struct inode *inode = file->f_dentry->d_inode;
401 if (S_ISREG(inode->i_mode))
402 rc = file->f_op->read(file, buf, count, off);
404 const int blkbits = inode->i_sb->s_blocksize_bits;
405 const int blksize = inode->i_sb->s_blocksize;
407 CDEBUG(D_EXT2, "reading "LPSZ" at dir %lu+%llu\n",
408 count, inode->i_ino, *off);
410 struct buffer_head *bh;
413 if (*off < inode->i_size) {
416 bh = ext3_bread(NULL, inode, *off >> blkbits,
419 CDEBUG(D_EXT2, "read %u@%llu\n", blksize, *off);
422 memcpy(buf, bh->b_data, blksize);
425 /* XXX in theory we should just fake
426 * this buffer and continue like ext3,
427 * especially if this is a partial read
429 CERROR("error read dir %lu+%llu: %d\n",
430 inode->i_ino, *off, err);
435 struct ext3_dir_entry_2 *fake = (void *)buf;
437 CDEBUG(D_EXT2, "fake %u@%llu\n", blksize, *off);
438 memset(fake, 0, sizeof(*fake));
439 fake->rec_len = cpu_to_le32(blksize);
451 static void fsfilt_ext3_cb_func(struct journal_callback *jcb, int error)
453 struct fsfilt_cb_data *fcb = (struct fsfilt_cb_data *)jcb;
455 fcb->cb_func(fcb->cb_obd, fcb->cb_last_rcvd, fcb->cb_data, error);
457 OBD_SLAB_FREE(fcb, fcb_cache, sizeof *fcb);
458 atomic_dec(&fcb_cache_count);
461 static int fsfilt_ext3_set_last_rcvd(struct obd_device *obd, __u64 last_rcvd,
462 void *handle, fsfilt_cb_t cb_func,
465 struct fsfilt_cb_data *fcb;
467 OBD_SLAB_ALLOC(fcb, fcb_cache, GFP_NOFS, sizeof *fcb);
471 atomic_inc(&fcb_cache_count);
472 fcb->cb_func = cb_func;
474 fcb->cb_last_rcvd = last_rcvd;
475 fcb->cb_data = cb_data;
477 CDEBUG(D_EXT2, "set callback for last_rcvd: "LPD64"\n", last_rcvd);
479 journal_callback_set(handle, fsfilt_ext3_cb_func,
480 (struct journal_callback *)fcb);
486 static int fsfilt_ext3_journal_data(struct file *filp)
488 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
489 /* bug 1576: enable data journaling on 2.5 when appropriate */
490 struct inode *inode = filp->f_dentry->d_inode;
491 EXT3_I(inode)->i_flags |= EXT3_JOURNAL_DATA_FL;
497 * We need to hack the return value for the free inode counts because
498 * the current EA code requires one filesystem block per inode with EAs,
499 * so it is possible to run out of blocks before we run out of inodes.
501 * This can be removed when the ext3 EA code is fixed.
503 static int fsfilt_ext3_statfs(struct super_block *sb, struct obd_statfs *osfs)
506 int rc = vfs_statfs(sb, &sfs);
508 if (!rc && sfs.f_bfree < sfs.f_ffree) {
509 sfs.f_files = (sfs.f_files - sfs.f_ffree) + sfs.f_bfree;
510 sfs.f_ffree = sfs.f_bfree;
513 statfs_pack(osfs, &sfs);
517 static int fsfilt_ext3_sync(struct super_block *sb)
519 return ext3_force_commit(sb);
522 extern int ext3_prep_san_write(struct inode *inode, long *blocks,
523 int nblocks, loff_t newsize);
524 static int fsfilt_ext3_prep_san_write(struct inode *inode, long *blocks,
525 int nblocks, loff_t newsize)
527 return ext3_prep_san_write(inode, blocks, nblocks, newsize);
530 static int fsfilt_ext3_read_record(struct file * file, void *buf,
531 int size, loff_t *offs)
533 struct buffer_head *bh;
534 unsigned long block, boffs;
535 struct inode *inode = file->f_dentry->d_inode;
538 if (inode->i_size < *offs + size) {
539 CERROR("file size %llu is too short for read %u@%llu\n",
540 inode->i_size, size, *offs);
544 block = *offs >> inode->i_blkbits;
545 bh = ext3_bread(NULL, inode, block, 0, &err);
547 CERROR("can't read block: %d\n", err);
551 boffs = (unsigned)*offs % bh->b_size;
552 if (boffs + size > bh->b_size) {
553 CERROR("request crosses block's border. offset %llu, size %u\n",
559 memcpy(buf, bh->b_data + boffs, size);
565 static int fsfilt_ext3_write_record(struct file * file, void *buf,
566 int size, loff_t *offs)
568 struct buffer_head *bh;
569 unsigned long block, boffs;
570 struct inode *inode = file->f_dentry->d_inode;
571 loff_t old_size = inode->i_size;
576 journal = EXT3_SB(inode->i_sb)->s_journal;
577 handle = journal_start(journal, EXT3_DATA_TRANS_BLOCKS + 2);
578 if (handle == NULL) {
579 CERROR("can't start transaction\n");
583 block = *offs >> inode->i_blkbits;
584 if (*offs + size > inode->i_size) {
586 if (*offs + size > inode->i_size)
587 inode->i_size = ((loff_t)block + 1) << inode->i_blkbits;
591 bh = ext3_bread(handle, inode, block, 1, &err);
593 CERROR("can't read/create block: %d\n", err);
597 /* This is a hack only needed because ext3_get_block_handle() updates
598 * i_disksize after marking the inode dirty in ext3_splice_branch().
599 * We will fix that when we get a chance, as ext3_mark_inode_dirty()
600 * is not without cost, nor is it even exported.
602 if (inode->i_size > old_size)
603 mark_inode_dirty(inode);
605 boffs = (unsigned)*offs % bh->b_size;
606 if (boffs + size > bh->b_size) {
607 CERROR("request crosses block's border. offset %llu, size %u\n",
613 err = ext3_journal_get_write_access(handle, bh);
615 CERROR("journal_get_write_access() returned error %d\n", err);
618 memcpy(bh->b_data + boffs, buf, size);
619 err = ext3_journal_dirty_metadata(handle, bh);
621 CERROR("journal_dirty_metadata() returned error %d\n", err);
628 journal_stop(handle);
634 static struct fsfilt_operations fsfilt_ext3_ops = {
636 fs_owner: THIS_MODULE,
637 fs_start: fsfilt_ext3_start,
638 fs_brw_start: fsfilt_ext3_brw_start,
639 fs_commit: fsfilt_ext3_commit,
640 fs_setattr: fsfilt_ext3_setattr,
641 fs_set_md: fsfilt_ext3_set_md,
642 fs_get_md: fsfilt_ext3_get_md,
643 fs_readpage: fsfilt_ext3_readpage,
644 fs_journal_data: fsfilt_ext3_journal_data,
645 fs_set_last_rcvd: fsfilt_ext3_set_last_rcvd,
646 fs_statfs: fsfilt_ext3_statfs,
647 fs_sync: fsfilt_ext3_sync,
648 fs_prep_san_write: fsfilt_ext3_prep_san_write,
649 fs_write_record: fsfilt_ext3_write_record,
650 fs_read_record: fsfilt_ext3_read_record,
653 static int __init fsfilt_ext3_init(void)
657 //rc = ext3_xattr_register();
658 fcb_cache = kmem_cache_create("fsfilt_ext3_fcb",
659 sizeof(struct fsfilt_cb_data), 0,
662 CERROR("error allocating fsfilt journal callback cache\n");
663 GOTO(out, rc = -ENOMEM);
666 rc = fsfilt_register_ops(&fsfilt_ext3_ops);
669 kmem_cache_destroy(fcb_cache);
674 static void __exit fsfilt_ext3_exit(void)
678 fsfilt_unregister_ops(&fsfilt_ext3_ops);
679 rc = kmem_cache_destroy(fcb_cache);
681 if (rc || atomic_read(&fcb_cache_count)) {
682 CERROR("can't free fsfilt callback cache: count %d, rc = %d\n",
683 atomic_read(&fcb_cache_count), rc);
686 //rc = ext3_xattr_unregister();
689 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
690 MODULE_DESCRIPTION("Lustre ext3 Filesystem Helper v0.1");
691 MODULE_LICENSE("GPL");
693 module_init(fsfilt_ext3_init);
694 module_exit(fsfilt_ext3_exit);