1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * lustre/lib/fsfilt_extN.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/extN_fs.h>
34 #include <linux/extN_jbd.h>
35 #include <linux/extN_xattr.h>
36 #include <linux/kp30.h>
37 #include <linux/lustre_fsfilt.h>
38 #include <linux/obd.h>
39 #include <linux/obd_class.h>
40 #include <linux/module.h>
42 static kmem_cache_t *fcb_cache;
43 static atomic_t fcb_cache_count = ATOMIC_INIT(0);
45 struct fsfilt_cb_data {
46 struct journal_callback cb_jcb; /* jbd private data - MUST BE FIRST */
47 fsfilt_cb_t cb_func; /* MDS/OBD completion function */
48 struct obd_device *cb_obd; /* MDS/OBD completion device */
49 __u64 cb_last_rcvd; /* MDS/OST last committed operation */
50 void *cb_data; /* MDS/OST completion function data */
53 #define EXTN_XATTR_INDEX_LUSTRE 5
54 #define XATTR_LUSTRE_MDS_OBJID "system.lustre_mds_objid"
57 * We don't currently need any additional blocks for rmdir and
58 * unlink transactions because we are storing the OST oa_id inside
59 * the inode (which we will be changing anyways as part of this
62 static void *fsfilt_extN_start(struct inode *inode, int op, void *desc_private)
64 /* For updates to the last recieved file */
65 int nblocks = EXTN_DATA_TRANS_BLOCKS;
68 LASSERT(current->journal_info == NULL);
71 case FSFILT_OP_CREATE_LOG:
72 nblocks += EXTN_INDEX_EXTRA_TRANS_BLOCKS+EXTN_DATA_TRANS_BLOCKS;
73 op = FSFILT_OP_CREATE;
75 case FSFILT_OP_UNLINK_LOG:
76 nblocks += EXTN_INDEX_EXTRA_TRANS_BLOCKS+EXTN_DATA_TRANS_BLOCKS;
77 op = FSFILT_OP_UNLINK;
83 case FSFILT_OP_UNLINK:
84 nblocks += EXTN_DELETE_TRANS_BLOCKS;
86 case FSFILT_OP_RENAME:
87 /* modify additional directory */
88 nblocks += EXTN_DATA_TRANS_BLOCKS;
90 case FSFILT_OP_SYMLINK:
91 /* additional block + block bitmap + GDT for long symlink */
94 case FSFILT_OP_CREATE:
97 /* modify one inode + block bitmap + GDT */
101 /* modify parent directory */
102 nblocks += EXTN_INDEX_EXTRA_TRANS_BLOCKS+EXTN_DATA_TRANS_BLOCKS;
104 case FSFILT_OP_SETATTR:
105 /* Setattr on inode */
108 default: CERROR("unknown transaction start op %d\n", op);
112 LASSERT(current->journal_info == desc_private);
114 handle = journal_start(EXTN_JOURNAL(inode), nblocks);
118 LASSERT(current->journal_info == handle);
123 * Calculate the number of buffer credits needed to write multiple pages in
124 * a single extN transaction. No, this shouldn't be here, but as yet extN
125 * doesn't have a nice API for calculating this sort of thing in advance.
127 * See comment above extN_writepage_trans_blocks for details. We assume
128 * no data journaling is being done, but it does allow for all of the pages
129 * being non-contiguous. If we are guaranteed contiguous pages we could
130 * reduce the number of (d)indirect blocks a lot.
132 * With N blocks per page and P pages, for each inode we have at most:
134 * min(N*P, blocksize/4 + 1) dindirect blocks
137 * For the entire filesystem, we have at most:
138 * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
139 * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
140 * objcount inode blocks
142 * 2 * EXTN_SINGLEDATA_TRANS_BLOCKS for the quota files
144 * 1 EXTN_DATA_TRANS_BLOCKS for the last_rcvd update.
146 static int fsfilt_extN_credits_needed(int objcount, struct fsfilt_objinfo *fso)
148 struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
149 int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
150 int addrpp = EXTN_ADDR_PER_BLOCK(sb) * blockpp;
153 int needed = objcount + 1;
156 for (i = 0; i < objcount; i++, fso++) {
157 int nblocks = fso->fso_bufcnt * blockpp;
158 int ndindirect = min(nblocks, addrpp + 1);
159 int nindir = nblocks + ndindirect + 1;
161 nbitmaps += nindir + nblocks;
162 ngdblocks += nindir + nblocks;
167 /* Assumes extN and extN have same sb_info layout at the start. */
168 if (nbitmaps > EXTN_SB(sb)->s_groups_count)
169 nbitmaps = EXTN_SB(sb)->s_groups_count;
170 if (ngdblocks > EXTN_SB(sb)->s_gdb_count)
171 ngdblocks = EXTN_SB(sb)->s_gdb_count;
173 needed += nbitmaps + ngdblocks;
175 /* last_rcvd update */
176 needed += EXTN_DATA_TRANS_BLOCKS;
179 /* We assume that there will be 1 bit set in s_dquot.flags for each
180 * quota file that is active. This is at least true for now.
182 needed += hweight32(sb_any_quota_enabled(sb)) *
183 EXTN_SINGLEDATA_TRANS_BLOCKS;
189 /* We have to start a huge journal transaction here to hold all of the
190 * metadata for the pages being written here. This is necessitated by
191 * the fact that we do lots of prepare_write operations before we do
192 * any of the matching commit_write operations, so even if we split
193 * up to use "smaller" transactions none of them could complete until
194 * all of them were opened. By having a single journal transaction,
195 * we eliminate duplicate reservations for common blocks like the
196 * superblock and group descriptors or bitmaps.
198 * We will start the transaction here, but each prepare_write will
199 * add a refcount to the transaction, and each commit_write will
200 * remove a refcount. The transaction will be closed when all of
201 * the pages have been written.
203 static void *fsfilt_extN_brw_start(int objcount, struct fsfilt_objinfo *fso,
204 int niocount, void *desc_private)
211 LASSERT(current->journal_info == desc_private);
212 journal = EXTN_SB(fso->fso_dentry->d_inode->i_sb)->s_journal;
213 needed = fsfilt_extN_credits_needed(objcount, fso);
215 /* The number of blocks we could _possibly_ dirty can very large.
216 * We reduce our request if it is absurd (and we couldn't get that
217 * many credits for a single handle anyways).
219 * At some point we have to limit the size of I/Os sent at one time,
220 * increase the size of the journal, or we have to calculate the
221 * actual journal requirements more carefully by checking all of
222 * the blocks instead of being maximally pessimistic. It remains to
223 * be seen if this is a real problem or not.
225 if (needed > journal->j_max_transaction_buffers) {
226 CERROR("want too many journal credits (%d) using %d instead\n",
227 needed, journal->j_max_transaction_buffers);
228 needed = journal->j_max_transaction_buffers;
232 handle = journal_start(journal, needed);
234 if (IS_ERR(handle)) {
235 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
238 LASSERT(handle->h_buffer_credits >= needed);
239 LASSERT(current->journal_info == handle);
245 static int fsfilt_extN_commit(struct inode *inode, void *h, int force_sync)
248 handle_t *handle = h;
250 LASSERT(current->journal_info == handle);
252 handle->h_sync = 1; /* recovery likes this */
255 rc = journal_stop(handle);
258 LASSERT(current->journal_info == NULL);
262 static int fsfilt_extN_setattr(struct dentry *dentry, void *handle,
263 struct iattr *iattr, int do_trunc)
265 struct inode *inode = dentry->d_inode;
270 /* A _really_ horrible hack to avoid removing the data stored
271 * in the block pointers; this is really the "small" stripe MD data.
272 * We can avoid further hackery by virtue of the MDS file size being
273 * zero all the time (which doesn't invoke block truncate at unlink
274 * time), so we assert we never change the MDS file size from zero. */
275 if (iattr->ia_valid & ATTR_SIZE && !do_trunc) {
276 /* ATTR_SIZE would invoke truncate: clear it */
277 iattr->ia_valid &= ~ATTR_SIZE;
278 EXTN_I(inode)->i_disksize = inode->i_size = iattr->ia_size;
280 /* make sure _something_ gets set - so new inode
281 * goes to disk (probably won't work over XFS */
282 if (!(iattr->ia_valid & (ATTR_MODE | ATTR_MTIME | ATTR_CTIME))){
283 iattr->ia_valid |= ATTR_MODE;
284 iattr->ia_mode = inode->i_mode;
288 /* Don't allow setattr to change file type */
289 iattr->ia_mode = (inode->i_mode & S_IFMT)|(iattr->ia_mode & ~S_IFMT);
291 /* We set these flags on the client, but have already checked perms
292 * so don't confuse inode_change_ok. */
293 iattr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET);
295 if (inode->i_op->setattr) {
296 rc = inode->i_op->setattr(dentry, iattr);
298 rc = inode_change_ok(inode, iattr);
300 rc = inode_setattr(inode, iattr);
308 static int fsfilt_extN_set_md(struct inode *inode, void *handle,
309 void *lmm, int lmm_size)
313 /* Nasty hack city - store stripe MD data in the block pointers if
314 * it will fit, because putting it in an EA currently kills the MDS
315 * performance. We'll fix this with "fast EAs" in the future.
317 if (inode->i_blocks == 0 && lmm_size <= sizeof(EXTN_I(inode)->i_data) -
318 sizeof(EXTN_I(inode)->i_data[0])) {
319 /* XXX old_size is debugging only */
320 int old_size = EXTN_I(inode)->i_data[0];
322 LASSERT(old_size < sizeof(EXTN_I(inode)->i_data));
323 CERROR("setting EA on %lu again... interesting\n",
327 EXTN_I(inode)->i_data[0] = cpu_to_le32(lmm_size);
328 memcpy(&EXTN_I(inode)->i_data[1], lmm, lmm_size);
329 mark_inode_dirty(inode);
334 rc = extN_xattr_set(handle, inode, EXTN_XATTR_INDEX_LUSTRE,
335 XATTR_LUSTRE_MDS_OBJID, lmm, lmm_size, 0);
341 CERROR("error adding MD data to inode %lu: rc = %d\n",
346 static int fsfilt_extN_get_md(struct inode *inode, void *lmm, int lmm_size)
350 if (inode->i_blocks == 0 && EXTN_I(inode)->i_data[0]) {
351 int size = le32_to_cpu(EXTN_I(inode)->i_data[0]);
352 LASSERT(size < sizeof(EXTN_I(inode)->i_data));
356 memcpy(lmm, &EXTN_I(inode)->i_data[1], size);
363 rc = extN_xattr_get(inode, EXTN_XATTR_INDEX_LUSTRE,
364 XATTR_LUSTRE_MDS_OBJID, lmm, lmm_size);
368 /* This gives us the MD size */
370 return (rc == -ENODATA) ? 0 : rc;
373 CDEBUG(D_INFO, "error getting EA %s from inode %lu: "
374 "rc = %d\n", XATTR_LUSTRE_MDS_OBJID, inode->i_ino, rc);
375 memset(lmm, 0, lmm_size);
376 return (rc == -ENODATA) ? 0 : rc;
382 static ssize_t fsfilt_extN_readpage(struct file *file, char *buf, size_t count,
385 struct inode *inode = file->f_dentry->d_inode;
388 if (S_ISREG(inode->i_mode))
389 rc = file->f_op->read(file, buf, count, off);
391 const int blkbits = inode->i_sb->s_blocksize_bits;
392 const int blksize = inode->i_sb->s_blocksize;
394 CDEBUG(D_EXT2, "reading "LPSZ" at dir %lu+%llu\n",
395 count, inode->i_ino, *off);
397 struct buffer_head *bh;
400 if (*off < inode->i_size) {
403 bh = extN_bread(NULL, inode, *off >> blkbits,
406 CDEBUG(D_EXT2, "read %u@%llu\n", blksize, *off);
409 memcpy(buf, bh->b_data, blksize);
412 /* XXX in theory we should just fake
413 * this buffer and continue like extN,
414 * especially if this is a partial read
416 CERROR("error read dir %lu+%llu: %d\n",
417 inode->i_ino, *off, err);
422 struct extN_dir_entry_2 *fake = (void *)buf;
424 CDEBUG(D_EXT2, "fake %u@%llu\n", blksize, *off);
425 memset(fake, 0, sizeof(*fake));
426 fake->rec_len = cpu_to_le32(blksize);
438 static void fsfilt_extN_cb_func(struct journal_callback *jcb, int error)
440 struct fsfilt_cb_data *fcb = (struct fsfilt_cb_data *)jcb;
442 fcb->cb_func(fcb->cb_obd, fcb->cb_last_rcvd, fcb->cb_data, error);
444 OBD_SLAB_FREE(fcb, fcb_cache, sizeof *fcb);
445 atomic_dec(&fcb_cache_count);
448 static int fsfilt_extN_set_last_rcvd(struct obd_device *obd, __u64 last_rcvd,
449 void *handle, fsfilt_cb_t cb_func,
452 struct fsfilt_cb_data *fcb;
454 OBD_SLAB_ALLOC(fcb, fcb_cache, GFP_NOFS, sizeof *fcb);
458 atomic_inc(&fcb_cache_count);
459 fcb->cb_func = cb_func;
461 fcb->cb_last_rcvd = last_rcvd;
462 fcb->cb_data = cb_data;
464 CDEBUG(D_EXT2, "set callback for last_rcvd: "LPD64"\n", last_rcvd);
466 journal_callback_set(handle, fsfilt_extN_cb_func,
467 (struct journal_callback *)fcb);
473 static int fsfilt_extN_journal_data(struct file *filp)
475 struct inode *inode = filp->f_dentry->d_inode;
477 EXTN_I(inode)->i_flags |= EXTN_JOURNAL_DATA_FL;
483 * We need to hack the return value for the free inode counts because
484 * the current EA code requires one filesystem block per inode with EAs,
485 * so it is possible to run out of blocks before we run out of inodes.
487 * This can be removed when the extN EA code is fixed.
489 static int fsfilt_extN_statfs(struct super_block *sb, struct obd_statfs *osfs)
492 int rc = vfs_statfs(sb, &sfs);
494 if (!rc && sfs.f_bfree < sfs.f_ffree) {
495 sfs.f_files = (sfs.f_files - sfs.f_ffree) + sfs.f_bfree;
496 sfs.f_ffree = sfs.f_bfree;
499 statfs_pack(osfs, &sfs);
503 static int fsfilt_extN_sync(struct super_block *sb)
505 return extN_force_commit(sb);
508 extern int extN_prep_san_write(struct inode *inode, long *blocks,
509 int nblocks, loff_t newsize);
510 static int fsfilt_extN_prep_san_write(struct inode *inode, long *blocks,
511 int nblocks, loff_t newsize)
513 return extN_prep_san_write(inode, blocks, nblocks, newsize);
516 static int fsfilt_extN_read_record(struct file * file, void *buf,
517 int size, loff_t *offs)
519 struct buffer_head *bh;
520 unsigned long block, boffs;
521 struct inode *inode = file->f_dentry->d_inode;
524 if (inode->i_size < *offs + size) {
525 CERROR("file size %llu is too short for read %u@%llu\n",
526 inode->i_size, size, *offs);
530 block = *offs >> inode->i_blkbits;
531 bh = extN_bread(NULL, inode, block, 0, &err);
533 CERROR("can't read block: %d\n", err);
537 boffs = (unsigned)*offs % bh->b_size;
538 if (boffs + size > bh->b_size) {
539 CERROR("request crosses block's border. offset %llu, size %u\n",
545 memcpy(buf, bh->b_data + boffs, size);
551 static int fsfilt_extN_write_record(struct file * file, void *buf,
552 int size, loff_t *offs)
554 struct buffer_head *bh;
555 unsigned long block, boffs;
556 struct inode *inode = file->f_dentry->d_inode;
557 loff_t old_size = inode->i_size;
562 journal = EXTN_SB(inode->i_sb)->s_journal;
563 handle = journal_start(journal, EXTN_DATA_TRANS_BLOCKS + 2);
564 if (handle == NULL) {
565 CERROR("can't start transaction\n");
569 block = *offs >> inode->i_blkbits;
570 if (*offs + size > inode->i_size) {
572 if (*offs + size > inode->i_size)
573 inode->i_size = ((loff_t)block + 1) << inode->i_blkbits;
577 bh = extN_bread(handle, inode, block, 1, &err);
579 CERROR("can't read/create block: %d\n", err);
583 /* This is a hack only needed because extN_get_block_handle() updates
584 * i_disksize after marking the inode dirty in extN_splice_branch().
585 * We will fix that when we get a chance, as extN_mark_inode_dirty()
586 * is not without cost, nor is it even exported.
588 if (inode->i_size > old_size)
589 mark_inode_dirty(inode);
591 boffs = (unsigned)*offs % bh->b_size;
592 if (boffs + size > bh->b_size) {
593 CERROR("request crosses block's border. offset %llu, size %u\n",
599 err = extN_journal_get_write_access(handle, bh);
601 CERROR("journal_get_write_access() returned error %d\n", err);
604 memcpy(bh->b_data + boffs, buf, size);
605 err = extN_journal_dirty_metadata(handle, bh);
607 CERROR("journal_dirty_metadata() returned error %d\n", err);
614 journal_stop(handle);
620 static struct fsfilt_operations fsfilt_extN_ops = {
622 fs_owner: THIS_MODULE,
623 fs_start: fsfilt_extN_start,
624 fs_brw_start: fsfilt_extN_brw_start,
625 fs_commit: fsfilt_extN_commit,
626 fs_setattr: fsfilt_extN_setattr,
627 fs_set_md: fsfilt_extN_set_md,
628 fs_get_md: fsfilt_extN_get_md,
629 fs_readpage: fsfilt_extN_readpage,
630 fs_journal_data: fsfilt_extN_journal_data,
631 fs_set_last_rcvd: fsfilt_extN_set_last_rcvd,
632 fs_statfs: fsfilt_extN_statfs,
633 fs_sync: fsfilt_extN_sync,
634 fs_prep_san_write: fsfilt_extN_prep_san_write,
635 fs_write_record: fsfilt_extN_write_record,
636 fs_read_record: fsfilt_extN_read_record,
639 static int __init fsfilt_extN_init(void)
643 //rc = extN_xattr_register();
644 fcb_cache = kmem_cache_create("fsfilt_extN_fcb",
645 sizeof(struct fsfilt_cb_data), 0,
648 CERROR("error allocating fsfilt journal callback cache\n");
649 GOTO(out, rc = -ENOMEM);
652 rc = fsfilt_register_ops(&fsfilt_extN_ops);
655 kmem_cache_destroy(fcb_cache);
660 static void __exit fsfilt_extN_exit(void)
664 fsfilt_unregister_ops(&fsfilt_extN_ops);
665 rc = kmem_cache_destroy(fcb_cache);
667 if (rc || atomic_read(&fcb_cache_count)) {
668 CERROR("can't free fsfilt callback cache: count %d, rc = %d\n",
669 atomic_read(&fcb_cache_count), rc);
672 //rc = extN_xattr_unregister();
675 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
676 MODULE_DESCRIPTION("Lustre extN Filesystem Helper v0.1");
677 MODULE_LICENSE("GPL");
679 module_init(fsfilt_extN_init);
680 module_exit(fsfilt_extN_exit);