Whamcloud - gitweb
merge b_devel into HEAD (20030626 merge tag) for 0.7.1
[fs/lustre-release.git] / lustre / obdclass / fsfilt_ext3.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/lib/fsfilt_ext3.c
5  *  Lustre filesystem abstraction routines
6  *
7  *  Copyright (C) 2002, 2003 Cluster File Systems, Inc.
8  *   Author: Andreas Dilger <adilger@clusterfs.com>
9  *
10  *   This file is part of Lustre, http://www.lustre.org.
11  *
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.
15  *
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.
20  *
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.
24  */
25
26 #define DEBUG_SUBSYSTEM S_FILTER
27
28 #include <linux/fs.h>
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/ext3_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>
41
42 static kmem_cache_t *fcb_cache;
43 static atomic_t fcb_cache_count = ATOMIC_INIT(0);
44
45 struct fsfilt_cb_data {
46         struct journal_callback cb_jcb; /* data private to jbd */
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 };
51
52 #define EXT3_XATTR_INDEX_LUSTRE         5
53 #define XATTR_LUSTRE_MDS_OBJID          "system.lustre_mds_objid"
54
55 /*
56  * We don't currently need any additional blocks for rmdir and
57  * unlink transactions because we are storing the OST oa_id inside
58  * the inode (which we will be changing anyways as part of this
59  * transaction).
60  */
61 static void *fsfilt_ext3_start(struct inode *inode, int op)
62 {
63         /* For updates to the last recieved file */
64         int nblocks = EXT3_DATA_TRANS_BLOCKS;
65         void *handle;
66
67         switch(op) {
68         case FSFILT_OP_RMDIR:
69         case FSFILT_OP_UNLINK:
70                 nblocks += EXT3_DELETE_TRANS_BLOCKS;
71                 break;
72         case FSFILT_OP_RENAME:
73                 /* modify additional directory */
74                 nblocks += EXT3_DATA_TRANS_BLOCKS;
75                 /* no break */
76         case FSFILT_OP_SYMLINK:
77                 /* additional block + block bitmap + GDT for long symlink */
78                 nblocks += 3;
79                 /* no break */
80         case FSFILT_OP_CREATE:
81         case FSFILT_OP_MKDIR:
82         case FSFILT_OP_MKNOD:
83                 /* modify one inode + block bitmap + GDT */
84                 nblocks += 3;
85                 /* no break */
86         case FSFILT_OP_LINK:
87                 /* modify parent directory */
88                 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
89                 break;
90         case FSFILT_OP_SETATTR:
91                 /* Setattr on inode */
92                 nblocks += 1;
93                 break;
94         default: CERROR("unknown transaction start op %d\n", op);
95                  LBUG();
96         }
97
98         LASSERT(!current->journal_info);
99         lock_kernel();
100         handle = journal_start(EXT3_JOURNAL(inode), nblocks);
101         unlock_kernel();
102
103         return handle;
104 }
105
106 /*
107  * Calculate the number of buffer credits needed to write multiple pages in
108  * a single ext3 transaction.  No, this shouldn't be here, but as yet ext3
109  * doesn't have a nice API for calculating this sort of thing in advance.
110  *
111  * See comment above ext3_writepage_trans_blocks for details.  We assume
112  * no data journaling is being done, but it does allow for all of the pages
113  * being non-contiguous.  If we are guaranteed contiguous pages we could
114  * reduce the number of (d)indirect blocks a lot.
115  *
116  * With N blocks per page and P pages, for each inode we have at most:
117  * N*P indirect
118  * min(N*P, blocksize/4 + 1) dindirect blocks
119  * niocount tindirect
120  *
121  * For the entire filesystem, we have at most:
122  * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
123  * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
124  * objcount inode blocks
125  * 1 superblock
126  * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
127  *
128  * 1 EXT3_DATA_TRANS_BLOCKS for the last_rcvd update.
129  */
130 static int fsfilt_ext3_credits_needed(int objcount, struct fsfilt_objinfo *fso)
131 {
132         struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
133         int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
134         int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
135         int nbitmaps = 0;
136         int ngdblocks = 0;
137         int needed = objcount + 1;
138         int i;
139
140         for (i = 0; i < objcount; i++, fso++) {
141                 int nblocks = fso->fso_bufcnt * blockpp;
142                 int ndindirect = min(nblocks, addrpp + 1);
143                 int nindir = nblocks + ndindirect + 1;
144
145                 nbitmaps += nindir + nblocks;
146                 ngdblocks += nindir + nblocks;
147
148                 needed += nindir;
149         }
150
151         /* Assumes ext3 and ext3 have same sb_info layout at the start. */
152         if (nbitmaps > EXT3_SB(sb)->s_groups_count)
153                 nbitmaps = EXT3_SB(sb)->s_groups_count;
154         if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
155                 ngdblocks = EXT3_SB(sb)->s_gdb_count;
156
157         needed += nbitmaps + ngdblocks;
158
159         /* last_rcvd update */
160         needed += EXT3_DATA_TRANS_BLOCKS;
161
162 #ifdef CONFIG_QUOTA
163         /* We assume that there will be 1 bit set in s_dquot.flags for each
164          * quota file that is active.  This is at least true for now.
165          */
166         needed += hweight32(sb_any_quota_enabled(sb)) *
167                 EXT3_SINGLEDATA_TRANS_BLOCKS;
168 #endif
169
170         return needed;
171 }
172
173 /* We have to start a huge journal transaction here to hold all of the
174  * metadata for the pages being written here.  This is necessitated by
175  * the fact that we do lots of prepare_write operations before we do
176  * any of the matching commit_write operations, so even if we split
177  * up to use "smaller" transactions none of them could complete until
178  * all of them were opened.  By having a single journal transaction,
179  * we eliminate duplicate reservations for common blocks like the
180  * superblock and group descriptors or bitmaps.
181  *
182  * We will start the transaction here, but each prepare_write will
183  * add a refcount to the transaction, and each commit_write will
184  * remove a refcount.  The transaction will be closed when all of
185  * the pages have been written.
186  */
187 static void *fsfilt_ext3_brw_start(int objcount, struct fsfilt_objinfo *fso,
188                                    int niocount, struct niobuf_remote *nb)
189 {
190         journal_t *journal;
191         handle_t *handle;
192         int needed;
193         ENTRY;
194
195         LASSERT(!current->journal_info);
196         journal = EXT3_SB(fso->fso_dentry->d_inode->i_sb)->s_journal;
197         needed = fsfilt_ext3_credits_needed(objcount, fso);
198
199         /* The number of blocks we could _possibly_ dirty can very large.
200          * We reduce our request if it is absurd (and we couldn't get that
201          * many credits for a single handle anyways).
202          *
203          * At some point we have to limit the size of I/Os sent at one time,
204          * increase the size of the journal, or we have to calculate the
205          * actual journal requirements more carefully by checking all of
206          * the blocks instead of being maximally pessimistic.  It remains to
207          * be seen if this is a real problem or not.
208          */
209         if (needed > journal->j_max_transaction_buffers) {
210                 CERROR("want too many journal credits (%d) using %d instead\n",
211                        needed, journal->j_max_transaction_buffers);
212                 needed = journal->j_max_transaction_buffers;
213         }
214
215         lock_kernel();
216         handle = journal_start(journal, needed);
217         unlock_kernel();
218         if (IS_ERR(handle))
219                 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
220                        PTR_ERR(handle));
221
222         RETURN(handle);
223 }
224
225 static int fsfilt_ext3_commit(struct inode *inode, void *h, int force_sync)
226 {
227         int rc;
228         handle_t *handle = h;
229
230         if (force_sync)
231                 handle->h_sync = 1; /* recovery likes this */
232
233         lock_kernel();
234         rc = journal_stop(handle);
235         unlock_kernel();
236
237         return rc;
238 }
239
240 static int fsfilt_ext3_setattr(struct dentry *dentry, void *handle,
241                                struct iattr *iattr, int do_trunc)
242 {
243         struct inode *inode = dentry->d_inode;
244         int rc;
245
246         lock_kernel();
247
248         /* A _really_ horrible hack to avoid removing the data stored
249          * in the block pointers; this is really the "small" stripe MD data.
250          * We can avoid further hackery by virtue of the MDS file size being
251          * zero all the time (which doesn't invoke block truncate at unlink
252          * time), so we assert we never change the MDS file size from zero.
253          */
254         if (iattr->ia_valid & ATTR_SIZE && !do_trunc) {
255                 /* ATTR_SIZE would invoke truncate: clear it */
256                 iattr->ia_valid &= ~ATTR_SIZE;
257                 inode->i_size = iattr->ia_size;
258
259                 /* make sure _something_ gets set - so new inode
260                  * goes to disk (probably won't work over XFS
261                  */
262                 if (!iattr->ia_valid & ATTR_MODE) {
263                         iattr->ia_valid |= ATTR_MODE;
264                         iattr->ia_mode = inode->i_mode;
265                 }
266         }
267         if (inode->i_op->setattr)
268                 rc = inode->i_op->setattr(dentry, iattr);
269         else{
270                 rc = inode_change_ok(inode, iattr);
271                 if (!rc)
272                         rc = inode_setattr(inode, iattr);
273         }
274
275         unlock_kernel();
276
277         return rc;
278 }
279
280 static int fsfilt_ext3_set_md(struct inode *inode, void *handle,
281                               void *lmm, int lmm_size)
282 {
283         int rc;
284
285         /* Nasty hack city - store stripe MD data in the block pointers if
286          * it will fit, because putting it in an EA currently kills the MDS
287          * performance.  We'll fix this with "fast EAs" in the future.
288          */
289         if (lmm_size <= sizeof(EXT3_I(inode)->i_data) -
290                         sizeof(EXT3_I(inode)->i_data[0])) {
291                 /* XXX old_size is debugging only */
292                 int old_size = EXT3_I(inode)->i_data[0];
293                 if (old_size != 0) {
294                         LASSERT(old_size < sizeof(EXT3_I(inode)->i_data));
295                         CERROR("setting EA on %lu again... interesting\n",
296                                inode->i_ino);
297                 }
298
299                 EXT3_I(inode)->i_data[0] = cpu_to_le32(lmm_size);
300                 memcpy(&EXT3_I(inode)->i_data[1], lmm, lmm_size);
301                 mark_inode_dirty(inode);
302                 return 0;
303         } else {
304                 down(&inode->i_sem);
305                 lock_kernel();
306                 rc = ext3_xattr_set(handle, inode, EXT3_XATTR_INDEX_LUSTRE,
307                                     XATTR_LUSTRE_MDS_OBJID, lmm, lmm_size, 0);
308                 unlock_kernel();
309                 up(&inode->i_sem);
310         }
311
312         if (rc)
313                 CERROR("error adding MD data to inode %lu: rc = %d\n",
314                        inode->i_ino, rc);
315         return rc;
316 }
317
318 static int fsfilt_ext3_get_md(struct inode *inode, void *lmm, int lmm_size)
319 {
320         int rc;
321
322         if (EXT3_I(inode)->i_data[0]) {
323                 int size = le32_to_cpu(EXT3_I(inode)->i_data[0]);
324                 LASSERT(size < sizeof(EXT3_I(inode)->i_data));
325                 if (lmm) {
326                         if (size > lmm_size)
327                                 return -ERANGE;
328                         memcpy(lmm, &EXT3_I(inode)->i_data[1], size);
329                 }
330                 return size;
331         }
332
333         down(&inode->i_sem);
334         lock_kernel();
335         rc = ext3_xattr_get(inode, EXT3_XATTR_INDEX_LUSTRE,
336                             XATTR_LUSTRE_MDS_OBJID, lmm, lmm_size);
337         unlock_kernel();
338         up(&inode->i_sem);
339
340         /* This gives us the MD size */
341         if (lmm == NULL)
342                 return (rc == -ENODATA) ? 0 : rc;
343
344         if (rc < 0) {
345                 CDEBUG(D_INFO, "error getting EA %s from inode %lu: "
346                        "rc = %d\n", XATTR_LUSTRE_MDS_OBJID, inode->i_ino, rc);
347                 memset(lmm, 0, lmm_size);
348                 return (rc == -ENODATA) ? 0 : rc;
349         }
350
351         return rc;
352 }
353
354 static ssize_t fsfilt_ext3_readpage(struct file *file, char *buf, size_t count,
355                                     loff_t *off)
356 {
357         struct inode *inode = file->f_dentry->d_inode;
358         int rc = 0;
359
360         if (S_ISREG(inode->i_mode))
361                 rc = file->f_op->read(file, buf, count, off);
362         else {
363                 const int blkbits = inode->i_sb->s_blocksize_bits;
364                 const int blksize = inode->i_sb->s_blocksize;
365
366                 CDEBUG(D_EXT2, "reading "LPSZ" at dir %lu+%llu\n",
367                        count, inode->i_ino, *off);
368                 while (count > 0) {
369                         struct buffer_head *bh;
370
371                         bh = NULL;
372                         if (*off < inode->i_size) {
373                                 int err = 0;
374
375                                 bh = ext3_bread(NULL, inode, *off >> blkbits,
376                                                 0, &err);
377
378                                 CDEBUG(D_EXT2, "read %u@%llu\n", blksize, *off);
379
380                                 if (bh) {
381                                         memcpy(buf, bh->b_data, blksize);
382                                         brelse(bh);
383                                 } else if (err) {
384                                         /* XXX in theory we should just fake
385                                          * this buffer and continue like ext3,
386                                          * especially if this is a partial read
387                                          */
388                                         CERROR("error read dir %lu+%llu: %d\n",
389                                                inode->i_ino, *off, err);
390                                         RETURN(err);
391                                 }
392                         }
393                         if (!bh) {
394                                 struct ext3_dir_entry_2 *fake = (void *)buf;
395
396                                 CDEBUG(D_EXT2, "fake %u@%llu\n", blksize, *off);
397                                 memset(fake, 0, sizeof(*fake));
398                                 fake->rec_len = cpu_to_le32(blksize);
399                         }
400                         count -= blksize;
401                         buf += blksize;
402                         *off += blksize;
403                         rc += blksize;
404                 }
405         }
406
407         return rc;
408 }
409
410 static void fsfilt_ext3_cb_func(struct journal_callback *jcb, int error)
411 {
412         struct fsfilt_cb_data *fcb = (struct fsfilt_cb_data *)jcb;
413
414         fcb->cb_func(fcb->cb_obd, fcb->cb_last_rcvd, error);
415
416         OBD_SLAB_FREE(fcb, fcb_cache, sizeof *fcb);
417         atomic_dec(&fcb_cache_count);
418 }
419
420 static int fsfilt_ext3_set_last_rcvd(struct obd_device *obd, __u64 last_rcvd,
421                                      void *handle, fsfilt_cb_t cb_func)
422 {
423         struct fsfilt_cb_data *fcb;
424
425         OBD_SLAB_ALLOC(fcb, fcb_cache, GFP_NOFS, sizeof *fcb);
426         if (fcb == NULL)
427                 RETURN(-ENOMEM);
428
429         atomic_inc(&fcb_cache_count);
430         fcb->cb_func = cb_func;
431         fcb->cb_obd = obd;
432         fcb->cb_last_rcvd = last_rcvd;
433
434         CDEBUG(D_EXT2, "set callback for last_rcvd: "LPD64"\n", last_rcvd);
435         lock_kernel();
436         /* Note that an "incompatible pointer" warning here is OK for now */
437         journal_callback_set(handle, fsfilt_ext3_cb_func,
438                              (struct journal_callback *)fcb);
439         unlock_kernel();
440
441         return 0;
442 }
443
444 static int fsfilt_ext3_journal_data(struct file *filp)
445 {
446         struct inode *inode = filp->f_dentry->d_inode;
447
448         EXT3_I(inode)->i_flags |= EXT3_JOURNAL_DATA_FL;
449
450         return 0;
451 }
452
453 /*
454  * We need to hack the return value for the free inode counts because
455  * the current EA code requires one filesystem block per inode with EAs,
456  * so it is possible to run out of blocks before we run out of inodes.
457  *
458  * This can be removed when the ext3 EA code is fixed.
459  */
460 static int fsfilt_ext3_statfs(struct super_block *sb, struct obd_statfs *osfs)
461 {
462         struct statfs sfs;
463         int rc = vfs_statfs(sb, &sfs);
464
465         if (!rc && sfs.f_bfree < sfs.f_ffree) {
466                 sfs.f_files = (sfs.f_files - sfs.f_ffree) + sfs.f_bfree;
467                 sfs.f_ffree = sfs.f_bfree;
468         }
469
470         statfs_pack(osfs, &sfs);
471         return rc;
472 }
473
474 static int fsfilt_ext3_sync(struct super_block *sb)
475 {
476         return ext3_force_commit(sb);
477 }
478
479 extern int ext3_prep_san_write(struct inode *inode, long *blocks,
480                                int nblocks, loff_t newsize);
481 static int fsfilt_ext3_prep_san_write(struct inode *inode, long *blocks,
482                                       int nblocks, loff_t newsize)
483 {
484         return ext3_prep_san_write(inode, blocks, nblocks, newsize);
485 }
486
487 static struct fsfilt_operations fsfilt_ext3_ops = {
488         fs_type:                "ext3",
489         fs_owner:               THIS_MODULE,
490         fs_start:               fsfilt_ext3_start,
491         fs_brw_start:           fsfilt_ext3_brw_start,
492         fs_commit:              fsfilt_ext3_commit,
493         fs_setattr:             fsfilt_ext3_setattr,
494         fs_set_md:              fsfilt_ext3_set_md,
495         fs_get_md:              fsfilt_ext3_get_md,
496         fs_readpage:            fsfilt_ext3_readpage,
497         fs_journal_data:        fsfilt_ext3_journal_data,
498         fs_set_last_rcvd:       fsfilt_ext3_set_last_rcvd,
499         fs_statfs:              fsfilt_ext3_statfs,
500         fs_sync:                fsfilt_ext3_sync,
501         fs_prep_san_write:      fsfilt_ext3_prep_san_write,
502 };
503
504 static int __init fsfilt_ext3_init(void)
505 {
506         int rc;
507
508         //rc = ext3_xattr_register();
509         fcb_cache = kmem_cache_create("fsfilt_ext3_fcb",
510                                       sizeof(struct fsfilt_cb_data), 0,
511                                       0, NULL, NULL);
512         if (!fcb_cache) {
513                 CERROR("error allocating fsfilt journal callback cache\n");
514                 GOTO(out, rc = -ENOMEM);
515         }
516
517         rc = fsfilt_register_ops(&fsfilt_ext3_ops);
518
519         if (rc)
520                 kmem_cache_destroy(fcb_cache);
521 out:
522         return rc;
523 }
524
525 static void __exit fsfilt_ext3_exit(void)
526 {
527         int rc;
528
529         fsfilt_unregister_ops(&fsfilt_ext3_ops);
530         rc = kmem_cache_destroy(fcb_cache);
531
532         if (rc || atomic_read(&fcb_cache_count)) {
533                 CERROR("can't free fsfilt callback cache: count %d, rc = %d\n",
534                        atomic_read(&fcb_cache_count), rc);
535         }
536
537         //rc = ext3_xattr_unregister();
538 }
539
540 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
541 MODULE_DESCRIPTION("Lustre ext3 Filesystem Helper v0.1");
542 MODULE_LICENSE("GPL");
543
544 module_init(fsfilt_ext3_init);
545 module_exit(fsfilt_ext3_exit);