Whamcloud - gitweb
a45560ae11b54154f6871dc0007530c38c30b1f5
[fs/lustre-release.git] / lustre / lvfs / 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/version.h>
36 /* XXX ugh */
37 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
38  #include <linux/ext3_xattr.h>
39 #else
40  #include <linux/../../fs/ext3/xattr.h>
41 #endif
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>
47
48 static kmem_cache_t *fcb_cache;
49 static atomic_t fcb_cache_count = ATOMIC_INIT(0);
50
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 */
57 };
58
59 #ifndef EXT3_XATTR_INDEX_TRUSTED        /* temporary until we hit l28 kernel */
60 #define EXT3_XATTR_INDEX_TRUSTED        4
61 #endif
62 #define XATTR_LUSTRE_MDS_LOV_EA         "lov"
63
64 #define EXT3_XATTR_INDEX_LUSTRE         5                         /* old */
65 #define XATTR_LUSTRE_MDS_OBJID          "system.lustre_mds_objid" /* old */
66
67 /*
68  * We don't currently need any additional blocks for rmdir and
69  * unlink transactions because we are storing the OST oa_id inside
70  * the inode (which we will be changing anyways as part of this
71  * transaction).
72  */
73 static void *fsfilt_ext3_start(struct inode *inode, int op, void *desc_private)
74 {
75         /* For updates to the last recieved file */
76         int nblocks = EXT3_DATA_TRANS_BLOCKS;
77         int blocksize, block_count = 0;
78         void *handle;
79
80         if (current->journal_info) {
81                 CDEBUG(D_INODE, "increasing refcount on %p\n", current->journal_info);
82                 goto journal_start;
83         }
84
85         switch(op) {
86         case FSFILT_OP_CREATE_LOG:
87                 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
88                 op = FSFILT_OP_CREATE;
89                 break;
90         case FSFILT_OP_UNLINK_LOG:
91                 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
92                 op = FSFILT_OP_UNLINK;
93                 break;
94         }
95
96         switch(op) {
97         case FSFILT_OP_RMDIR:
98         case FSFILT_OP_UNLINK:
99                 nblocks += EXT3_DELETE_TRANS_BLOCKS;
100                 break;
101         case FSFILT_OP_RENAME:
102                 /* modify additional directory */
103                 nblocks += EXT3_DATA_TRANS_BLOCKS;
104                 /* no break */
105         case FSFILT_OP_SYMLINK:
106                 /* additional block + block bitmap + GDT for long symlink */
107                 nblocks += 3;
108                 /* no break */
109         case FSFILT_OP_CREATE:
110         case FSFILT_OP_MKDIR:
111         case FSFILT_OP_MKNOD:
112                 /* modify one inode + block bitmap + GDT */
113                 nblocks += 3;
114                 /* no break */
115         case FSFILT_OP_LINK:
116                 /* modify parent directory */
117                 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
118                 break;
119         case FSFILT_OP_SETATTR:
120                 /* Setattr on inode */
121                 nblocks += 1;
122                 break;
123         case FSFILT_OP_CANCEL_UNLINK_LOG:
124                 blocksize = 1 << inode->i_blkbits;
125                 block_count = (blocksize - 1) + LLOG_CHUNK_SIZE;
126                 block_count = (block_count + blocksize - 1) >> inode->i_blkbits;
127                 block_count = block_count * EXT3_DATA_TRANS_BLOCKS + 2;
128                 nblocks = 2 * 2 * block_count;
129                 break;
130         default: CERROR("unknown transaction start op %d\n", op);
131                  LBUG();
132         }
133
134         LASSERT(current->journal_info == desc_private);
135
136  journal_start:
137         lock_kernel();
138         handle = journal_start(EXT3_JOURNAL(inode), nblocks);
139         unlock_kernel();
140
141         if (!IS_ERR(handle))
142                 LASSERT(current->journal_info == handle);
143         return handle;
144 }
145
146 /*
147  * Calculate the number of buffer credits needed to write multiple pages in
148  * a single ext3 transaction.  No, this shouldn't be here, but as yet ext3
149  * doesn't have a nice API for calculating this sort of thing in advance.
150  *
151  * See comment above ext3_writepage_trans_blocks for details.  We assume
152  * no data journaling is being done, but it does allow for all of the pages
153  * being non-contiguous.  If we are guaranteed contiguous pages we could
154  * reduce the number of (d)indirect blocks a lot.
155  *
156  * With N blocks per page and P pages, for each inode we have at most:
157  * N*P indirect
158  * min(N*P, blocksize/4 + 1) dindirect blocks
159  * niocount tindirect
160  *
161  * For the entire filesystem, we have at most:
162  * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
163  * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
164  * objcount inode blocks
165  * 1 superblock
166  * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
167  *
168  * 1 EXT3_DATA_TRANS_BLOCKS for the last_rcvd update.
169  */
170 static int fsfilt_ext3_credits_needed(int objcount, struct fsfilt_objinfo *fso,
171                                       int niocount, struct niobuf_local *nb)
172 {
173         struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
174         __u64 next_indir;
175         const int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
176         int nbitmaps = 0, ngdblocks;
177         int needed = objcount + 1; /* inodes + superblock */
178         int i, j;
179
180         for (i = 0, j = 0; i < objcount; i++, fso++) {
181                 /* two or more dindirect blocks in case we cross boundary */
182                 int ndind = (long)((nb[j + fso->fso_bufcnt - 1].offset -
183                                     nb[j].offset) >>
184                                    sb->s_blocksize_bits) /
185                         (EXT3_ADDR_PER_BLOCK(sb) * EXT3_ADDR_PER_BLOCK(sb));
186                 nbitmaps += min(fso->fso_bufcnt, ndind > 0 ? ndind : 2);
187
188                 /* leaf, indirect, tindirect blocks for first block */
189                 nbitmaps += blockpp + 2;
190
191                 j += fso->fso_bufcnt;
192         }
193
194         next_indir = nb[0].offset +
195                 (EXT3_ADDR_PER_BLOCK(sb) << sb->s_blocksize_bits);
196         for (i = 1; i < niocount; i++) {
197                 if (nb[i].offset >= next_indir) {
198                         nbitmaps++;     /* additional indirect */
199                         next_indir = nb[i].offset +
200                                 (EXT3_ADDR_PER_BLOCK(sb)<<sb->s_blocksize_bits);
201                 } else if (nb[i].offset != nb[i - 1].offset + sb->s_blocksize) {
202                         nbitmaps++;     /* additional indirect */
203                 }
204                 nbitmaps += blockpp;    /* each leaf in different group? */
205         }
206
207         ngdblocks = nbitmaps;
208         if (nbitmaps > EXT3_SB(sb)->s_groups_count)
209                 nbitmaps = EXT3_SB(sb)->s_groups_count;
210         if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
211                 ngdblocks = EXT3_SB(sb)->s_gdb_count;
212
213         needed += nbitmaps + ngdblocks;
214
215         /* last_rcvd update */
216         needed += EXT3_DATA_TRANS_BLOCKS;
217
218 #if defined(CONFIG_QUOTA) && !defined(__x86_64__) /* XXX */
219         /* We assume that there will be 1 bit set in s_dquot.flags for each
220          * quota file that is active.  This is at least true for now.
221          */
222         needed += hweight32(sb_any_quota_enabled(sb)) *
223                 EXT3_SINGLEDATA_TRANS_BLOCKS;
224 #endif
225
226         return needed;
227 }
228
229 /* We have to start a huge journal transaction here to hold all of the
230  * metadata for the pages being written here.  This is necessitated by
231  * the fact that we do lots of prepare_write operations before we do
232  * any of the matching commit_write operations, so even if we split
233  * up to use "smaller" transactions none of them could complete until
234  * all of them were opened.  By having a single journal transaction,
235  * we eliminate duplicate reservations for common blocks like the
236  * superblock and group descriptors or bitmaps.
237  *
238  * We will start the transaction here, but each prepare_write will
239  * add a refcount to the transaction, and each commit_write will
240  * remove a refcount.  The transaction will be closed when all of
241  * the pages have been written.
242  */
243 static void *fsfilt_ext3_brw_start(int objcount, struct fsfilt_objinfo *fso,
244                                    int niocount, struct niobuf_local *nb,
245                                    void *desc_private)
246 {
247         journal_t *journal;
248         handle_t *handle;
249         int needed;
250         ENTRY;
251
252         LASSERT(current->journal_info == desc_private);
253         journal = EXT3_SB(fso->fso_dentry->d_inode->i_sb)->s_journal;
254         needed = fsfilt_ext3_credits_needed(objcount, fso, niocount, nb);
255
256         /* The number of blocks we could _possibly_ dirty can very large.
257          * We reduce our request if it is absurd (and we couldn't get that
258          * many credits for a single handle anyways).
259          *
260          * At some point we have to limit the size of I/Os sent at one time,
261          * increase the size of the journal, or we have to calculate the
262          * actual journal requirements more carefully by checking all of
263          * the blocks instead of being maximally pessimistic.  It remains to
264          * be seen if this is a real problem or not.
265          */
266         if (needed > journal->j_max_transaction_buffers) {
267                 CERROR("want too many journal credits (%d) using %d instead\n",
268                        needed, journal->j_max_transaction_buffers);
269                 needed = journal->j_max_transaction_buffers;
270         }
271
272         lock_kernel();
273         handle = journal_start(journal, needed);
274         unlock_kernel();
275         if (IS_ERR(handle)) {
276                 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
277                        PTR_ERR(handle));
278         } else {
279                 LASSERT(handle->h_buffer_credits >= needed);
280                 LASSERT(current->journal_info == handle);
281         }
282
283         RETURN(handle);
284 }
285
286 static int fsfilt_ext3_commit(struct inode *inode, void *h, int force_sync)
287 {
288         int rc;
289         handle_t *handle = h;
290
291         LASSERT(current->journal_info == handle);
292         if (force_sync)
293                 handle->h_sync = 1; /* recovery likes this */
294
295         lock_kernel();
296         rc = journal_stop(handle);
297         unlock_kernel();
298
299         // LASSERT(current->journal_info == NULL);
300         return rc;
301 }
302
303 static int fsfilt_ext3_commit_async(struct inode *inode, void *h,
304                                         void **wait_handle)
305 {
306         transaction_t *transaction;
307         unsigned long tid, rtid;
308         handle_t *handle = h;
309         journal_t *journal;
310         int rc;
311
312         LASSERT(current->journal_info == handle);
313
314         lock_kernel();
315         transaction = handle->h_transaction;
316         journal = transaction->t_journal;
317         tid = transaction->t_tid;
318         /* we don't want to be blocked */
319         handle->h_sync = 0;
320         rc = journal_stop(handle);
321         if (rc) {
322                 CERROR("error while stopping transaction: %d\n", rc);
323                 unlock_kernel();
324                 return rc;
325         }
326 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
327         rtid = log_start_commit(journal, transaction);
328         if (rtid != tid)
329                 CERROR("strange race: %lu != %lu\n",
330                        (unsigned long) tid, (unsigned long) rtid);
331 #else
332         log_start_commit(journal, transaction->t_tid);
333 #endif
334         unlock_kernel();
335
336         *wait_handle = (void *) tid;
337         CDEBUG(D_INODE, "commit async: %lu\n", (unsigned long) tid);
338         return 0;
339 }
340
341 static int fsfilt_ext3_commit_wait(struct inode *inode, void *h)
342 {
343         tid_t tid = (tid_t)(long)h;
344
345         CDEBUG(D_INODE, "commit wait: %lu\n", (unsigned long) tid);
346         if (is_journal_aborted(EXT3_JOURNAL(inode)))
347                 return -EIO;
348
349         log_wait_commit(EXT3_JOURNAL(inode), tid);
350
351         return 0;
352 }
353
354 static int fsfilt_ext3_setattr(struct dentry *dentry, void *handle,
355                                struct iattr *iattr, int do_trunc)
356 {
357         struct inode *inode = dentry->d_inode;
358         int rc;
359
360         lock_kernel();
361
362         /* A _really_ horrible hack to avoid removing the data stored
363          * in the block pointers; this is really the "small" stripe MD data.
364          * We can avoid further hackery by virtue of the MDS file size being
365          * zero all the time (which doesn't invoke block truncate at unlink
366          * time), so we assert we never change the MDS file size from zero. */
367         if (iattr->ia_valid & ATTR_SIZE && !do_trunc) {
368                 /* ATTR_SIZE would invoke truncate: clear it */
369                 iattr->ia_valid &= ~ATTR_SIZE;
370                 EXT3_I(inode)->i_disksize = inode->i_size = iattr->ia_size;
371
372                 /* make sure _something_ gets set - so new inode
373                  * goes to disk (probably won't work over XFS */
374                 if (!(iattr->ia_valid & (ATTR_MODE | ATTR_MTIME | ATTR_CTIME))){
375                         iattr->ia_valid |= ATTR_MODE;
376                         iattr->ia_mode = inode->i_mode;
377                 }
378         }
379
380         /* Don't allow setattr to change file type */
381         iattr->ia_mode = (inode->i_mode & S_IFMT)|(iattr->ia_mode & ~S_IFMT);
382
383         /* We set these flags on the client, but have already checked perms
384          * so don't confuse inode_change_ok. */
385         iattr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET);
386
387         if (inode->i_op->setattr) {
388                 rc = inode->i_op->setattr(dentry, iattr);
389         } else {
390                 rc = inode_change_ok(inode, iattr);
391                 if (!rc)
392                         rc = inode_setattr(inode, iattr);
393         }
394
395         unlock_kernel();
396
397         return rc;
398 }
399
400 static int fsfilt_ext3_iocontrol(struct inode * inode, struct file *file,
401                                  unsigned int cmd, unsigned long arg)
402 {
403         int rc = 0;
404         ENTRY;
405
406         if (inode->i_fop->ioctl)
407                 rc = inode->i_fop->ioctl(inode, file, cmd, arg);
408         else
409                 RETURN(-ENOTTY);
410
411         RETURN(rc);
412 }
413
414 #undef INLINE_EA
415 #undef OLD_EA
416 static int fsfilt_ext3_set_md(struct inode *inode, void *handle,
417                               void *lmm, int lmm_size)
418 {
419         int rc, old_ea = 0;
420
421 #ifdef INLINE_EA  /* can go away before 1.0 - just for testing bug 2097 now */
422         /* Nasty hack city - store stripe MD data in the block pointers if
423          * it will fit, because putting it in an EA currently kills the MDS
424          * performance.  We'll fix this with "fast EAs" in the future.
425          */
426         if (inode->i_blocks == 0 && lmm_size <= sizeof(EXT3_I(inode)->i_data) -
427                                             sizeof(EXT3_I(inode)->i_data[0])) {
428                 unsigned old_size = EXT3_I(inode)->i_data[0];
429                 if (old_size != 0) {
430                         LASSERT(old_size < sizeof(EXT3_I(inode)->i_data));
431                         CERROR("setting EA on %lu/%u again... interesting\n",
432                                inode->i_ino, inode->i_generation);
433                 }
434
435                 EXT3_I(inode)->i_data[0] = cpu_to_le32(lmm_size);
436                 memcpy(&EXT3_I(inode)->i_data[1], lmm, lmm_size);
437                 mark_inode_dirty(inode);
438                 return 0;
439         }
440 #endif
441 #ifdef OLD_EA
442         /* keep this when we get rid of OLD_EA (too noisy during conversion) */
443         if (EXT3_I(inode)->i_file_acl /* || large inode EA flag */) {
444                 CWARN("setting EA on %lu/%u again... interesting\n",
445                        inode->i_ino, inode->i_generation);
446                 old_ea = 1;
447         }
448
449         lock_kernel();
450         /* this can go away before 1.0.  For bug 2097 testing only. */
451         rc = ext3_xattr_set_handle(handle, inode, EXT3_XATTR_INDEX_LUSTRE,
452                                    XATTR_LUSTRE_MDS_OBJID, lmm, lmm_size, 0);
453 #else
454         lock_kernel();
455         rc = ext3_xattr_set_handle(handle, inode, EXT3_XATTR_INDEX_TRUSTED,
456                                    XATTR_LUSTRE_MDS_LOV_EA, lmm, lmm_size, 0);
457
458         /* This tries to delete the old-format LOV EA, but only as long as we
459          * have successfully saved the new-format LOV EA (we can always try
460          * the conversion again the next time the file is accessed).  It is
461          * possible (although unlikely) that the new-format LOV EA couldn't be
462          * saved because it ran out of space but we would need a file striped
463          * over least 123 OSTs before the two EAs filled a 4kB block.
464          *
465          * This can be removed when all filesystems have converted to the
466          * new EA format, but otherwise adds little if any overhead.  If we
467          * wanted backward compatibility for existing files, we could keep
468          * the old EA around for a while but we'd have to clean it up later. */
469         if (rc >= 0 && old_ea) {
470                 int err = ext3_xattr_set_handle(handle, inode,
471                                                 EXT3_XATTR_INDEX_LUSTRE,
472                                                 XATTR_LUSTRE_MDS_OBJID,
473                                                 NULL, 0, 0);
474                 if (err)
475                         CERROR("error deleting old LOV EA on %lu/%u: rc %d\n",
476                                inode->i_ino, inode->i_generation, err);
477         }
478 #endif
479         unlock_kernel();
480
481         if (rc)
482                 CERROR("error adding MD data to inode %lu: rc = %d\n",
483                        inode->i_ino, rc);
484         return rc;
485 }
486
487 /* Must be called with i_sem held */
488 static int fsfilt_ext3_get_md(struct inode *inode, void *lmm, int lmm_size)
489 {
490         int rc;
491
492         LASSERT(down_trylock(&inode->i_sem) != 0);
493         lock_kernel();
494         /* Keep support for reading "inline EAs" until we convert
495          * users over to new format entirely.  See bug 841/2097. */
496         if (inode->i_blocks == 0 && EXT3_I(inode)->i_data[0]) {
497                 unsigned size = le32_to_cpu(EXT3_I(inode)->i_data[0]);
498                 void *handle;
499
500                 LASSERT(size < sizeof(EXT3_I(inode)->i_data));
501                 if (lmm) {
502                         if (size > lmm_size) {
503                                 CERROR("inline EA on %lu/%u bad size %u > %u\n",
504                                        inode->i_ino, inode->i_generation,
505                                        size, lmm_size);
506                                 return -ERANGE;
507                         }
508                         memcpy(lmm, &EXT3_I(inode)->i_data[1], size);
509                 }
510
511 #ifndef INLINE_EA
512                 /* migrate LOV EA data to external block - keep same format */
513                 CWARN("DEBUG: migrate inline EA for inode %lu/%u to block\n",
514                       inode->i_ino, inode->i_generation);
515
516                 handle = journal_start(EXT3_JOURNAL(inode),
517                                        EXT3_XATTR_TRANS_BLOCKS);
518                 if (!IS_ERR(handle)) {
519                         int err;
520                         rc = fsfilt_ext3_set_md(inode, handle,
521                                                 &EXT3_I(inode)->i_data[1],size);
522                         if (rc == 0) {
523                                 memset(EXT3_I(inode)->i_data, 0,
524                                        sizeof(EXT3_I(inode)->i_data));
525                                 mark_inode_dirty(inode);
526                         }
527                         err = journal_stop(handle);
528                         if (err && rc == 0)
529                                 rc = err;
530                 } else {
531                         rc = PTR_ERR(handle);
532                 }
533 #endif
534                 unlock_kernel();
535                 return size;
536         }
537
538         rc = ext3_xattr_get(inode, EXT3_XATTR_INDEX_TRUSTED,
539                             XATTR_LUSTRE_MDS_LOV_EA, lmm, lmm_size);
540         /* try old EA type if new one failed - MDS will convert it for us */
541         if (rc == -ENODATA) {
542                 CDEBUG(D_INFO,"failed new LOV EA %d/%s from inode %lu: rc %d\n",
543                        EXT3_XATTR_INDEX_TRUSTED, XATTR_LUSTRE_MDS_LOV_EA,
544                        inode->i_ino, rc);
545
546                 rc = ext3_xattr_get(inode, EXT3_XATTR_INDEX_LUSTRE,
547                                     XATTR_LUSTRE_MDS_OBJID, lmm, lmm_size);
548         }
549         unlock_kernel();
550
551         /* This gives us the MD size */
552         if (lmm == NULL)
553                 return (rc == -ENODATA) ? 0 : rc;
554
555         if (rc < 0) {
556                 CDEBUG(D_INFO, "error getting EA %d/%s from inode %lu: rc %d\n",
557                        EXT3_XATTR_INDEX_LUSTRE, XATTR_LUSTRE_MDS_OBJID,
558                        inode->i_ino, rc);
559                 memset(lmm, 0, lmm_size);
560                 return (rc == -ENODATA) ? 0 : rc;
561         }
562
563         return rc;
564 }
565
566 static ssize_t fsfilt_ext3_readpage(struct file *file, char *buf, size_t count,
567                                     loff_t *off)
568 {
569         struct inode *inode = file->f_dentry->d_inode;
570         int rc = 0;
571
572         if (S_ISREG(inode->i_mode))
573                 rc = file->f_op->read(file, buf, count, off);
574         else {
575                 const int blkbits = inode->i_sb->s_blocksize_bits;
576                 const int blksize = inode->i_sb->s_blocksize;
577
578                 CDEBUG(D_EXT2, "reading "LPSZ" at dir %lu+%llu\n",
579                        count, inode->i_ino, *off);
580                 while (count > 0) {
581                         struct buffer_head *bh;
582
583                         bh = NULL;
584                         if (*off < inode->i_size) {
585                                 int err = 0;
586
587                                 bh = ext3_bread(NULL, inode, *off >> blkbits,
588                                                 0, &err);
589
590                                 CDEBUG(D_EXT2, "read %u@%llu\n", blksize, *off);
591
592                                 if (bh) {
593                                         memcpy(buf, bh->b_data, blksize);
594                                         brelse(bh);
595                                 } else if (err) {
596                                         /* XXX in theory we should just fake
597                                          * this buffer and continue like ext3,
598                                          * especially if this is a partial read
599                                          */
600                                         CERROR("error read dir %lu+%llu: %d\n",
601                                                inode->i_ino, *off, err);
602                                         RETURN(err);
603                                 }
604                         }
605                         if (!bh) {
606                                 struct ext3_dir_entry_2 *fake = (void *)buf;
607
608                                 CDEBUG(D_EXT2, "fake %u@%llu\n", blksize, *off);
609                                 memset(fake, 0, sizeof(*fake));
610                                 fake->rec_len = cpu_to_le32(blksize);
611                         }
612                         count -= blksize;
613                         buf += blksize;
614                         *off += blksize;
615                         rc += blksize;
616                 }
617         }
618
619         return rc;
620 }
621
622 static void fsfilt_ext3_cb_func(struct journal_callback *jcb, int error)
623 {
624         struct fsfilt_cb_data *fcb = (struct fsfilt_cb_data *)jcb;
625
626         fcb->cb_func(fcb->cb_obd, fcb->cb_last_rcvd, fcb->cb_data, error);
627
628         OBD_SLAB_FREE(fcb, fcb_cache, sizeof *fcb);
629         atomic_dec(&fcb_cache_count);
630 }
631
632 static int fsfilt_ext3_add_journal_cb(struct obd_device *obd, __u64 last_rcvd,
633                                       void *handle, fsfilt_cb_t cb_func,
634                                       void *cb_data)
635 {
636         struct fsfilt_cb_data *fcb;
637
638         OBD_SLAB_ALLOC(fcb, fcb_cache, GFP_NOFS, sizeof *fcb);
639         if (fcb == NULL)
640                 RETURN(-ENOMEM);
641
642         atomic_inc(&fcb_cache_count);
643         fcb->cb_func = cb_func;
644         fcb->cb_obd = obd;
645         fcb->cb_last_rcvd = last_rcvd;
646         fcb->cb_data = cb_data;
647
648         CDEBUG(D_EXT2, "set callback for last_rcvd: "LPD64"\n", last_rcvd);
649         lock_kernel();
650         journal_callback_set(handle, fsfilt_ext3_cb_func,
651                              (struct journal_callback *)fcb);
652         unlock_kernel();
653
654         return 0;
655 }
656
657 /*
658  * We need to hack the return value for the free inode counts because
659  * the current EA code requires one filesystem block per inode with EAs,
660  * so it is possible to run out of blocks before we run out of inodes.
661  *
662  * This can be removed when the ext3 EA code is fixed.
663  */
664 static int fsfilt_ext3_statfs(struct super_block *sb, struct obd_statfs *osfs)
665 {
666         struct kstatfs sfs;
667         int rc = vfs_statfs(sb, &sfs);
668
669         if (!rc && sfs.f_bfree < sfs.f_ffree) {
670                 sfs.f_files = (sfs.f_files - sfs.f_ffree) + sfs.f_bfree;
671                 sfs.f_ffree = sfs.f_bfree;
672         }
673
674         statfs_pack(osfs, &sfs);
675         return rc;
676 }
677
678 static int fsfilt_ext3_sync(struct super_block *sb)
679 {
680         return ext3_force_commit(sb);
681 }
682
683 extern int ext3_map_inode_page(struct inode *inode, struct page *page,
684                                unsigned long *blocks, int *created, int create);
685 int fsfilt_ext3_map_inode_page(struct inode *inode, struct page *page,
686                                unsigned long *blocks, int *created, int create)
687 {
688         return ext3_map_inode_page(inode, page, blocks, created, create);
689 }
690
691 extern int ext3_prep_san_write(struct inode *inode, long *blocks,
692                                int nblocks, loff_t newsize);
693 static int fsfilt_ext3_prep_san_write(struct inode *inode, long *blocks,
694                                       int nblocks, loff_t newsize)
695 {
696         return ext3_prep_san_write(inode, blocks, nblocks, newsize);
697 }
698
699 static int fsfilt_ext3_read_record(struct file * file, void *buf,
700                                    int size, loff_t *offs)
701 {
702         struct inode *inode = file->f_dentry->d_inode;
703         unsigned long block;
704         struct buffer_head *bh;
705         int err, blocksize, csize, boffs;
706
707         /* prevent reading after eof */
708         lock_kernel();
709         if (inode->i_size < *offs + size) {
710                 size = inode->i_size - *offs;
711                 unlock_kernel();
712                 if (size < 0) {
713                         CERROR("size %llu is too short for read %u@%llu\n",
714                                inode->i_size, size, *offs);
715                         return -EIO;
716                 } else if (size == 0) {
717                         return 0;
718                 }
719         } else {
720                 unlock_kernel();
721         }
722
723         blocksize = 1 << inode->i_blkbits;
724
725         while (size > 0) {
726                 block = *offs >> inode->i_blkbits;
727                 boffs = *offs & (blocksize - 1);
728                 csize = min(blocksize - boffs, size);
729                 bh = ext3_bread(NULL, inode, block, 0, &err);
730                 if (!bh) {
731                         CERROR("can't read block: %d\n", err);
732                         return err;
733                 }
734
735                 memcpy(buf, bh->b_data + boffs, csize);
736                 brelse(bh);
737
738                 *offs += csize;
739                 buf += csize;
740                 size -= csize;
741         }
742         return 0;
743 }
744
745 static int fsfilt_ext3_write_record(struct file *file, void *buf, int bufsize,
746                                     loff_t *offs, int force_sync)
747 {
748         struct buffer_head *bh = NULL;
749         unsigned long block;
750         struct inode *inode = file->f_dentry->d_inode;
751         loff_t old_size = inode->i_size, offset = *offs;
752         loff_t new_size = inode->i_size;
753         journal_t *journal;
754         handle_t *handle;
755         int err, block_count = 0, blocksize, size, boffs;
756
757         /* Determine how many transaction credits are needed */
758         blocksize = 1 << inode->i_blkbits;
759         block_count = (*offs & (blocksize - 1)) + bufsize;
760         block_count = (block_count + blocksize - 1) >> inode->i_blkbits;
761
762         journal = EXT3_SB(inode->i_sb)->s_journal;
763         lock_kernel();
764         handle = journal_start(journal,
765                                block_count * EXT3_DATA_TRANS_BLOCKS + 2);
766         unlock_kernel();
767         if (IS_ERR(handle)) {
768                 CERROR("can't start transaction\n");
769                 return PTR_ERR(handle);
770         }
771
772         while (bufsize > 0) {
773                 if (bh != NULL)
774                         brelse(bh);
775
776                 block = offset >> inode->i_blkbits;
777                 boffs = offset & (blocksize - 1);
778                 size = min(blocksize - boffs, bufsize);
779                 bh = ext3_bread(handle, inode, block, 1, &err);
780                 if (!bh) {
781                         CERROR("can't read/create block: %d\n", err);
782                         goto out;
783                 }
784
785                 err = ext3_journal_get_write_access(handle, bh);
786                 if (err) {
787                         CERROR("journal_get_write_access() returned error %d\n",
788                                err);
789                         goto out;
790                 }
791                 LASSERT(bh->b_data + boffs + size <= bh->b_data + bh->b_size);
792                 memcpy(bh->b_data + boffs, buf, size);
793                 err = ext3_journal_dirty_metadata(handle, bh);
794                 if (err) {
795                         CERROR("journal_dirty_metadata() returned error %d\n",
796                                err);
797                         goto out;
798                 }
799                 if (offset + size > new_size)
800                         new_size = offset + size;
801                 offset += size;
802                 bufsize -= size;
803                 buf += size;
804         }
805
806         if (force_sync)
807                 handle->h_sync = 1; /* recovery likes this */
808 out:
809         if (bh)
810                 brelse(bh);
811
812         /* correct in-core and on-disk sizes */
813         if (new_size > inode->i_size) {
814                 lock_kernel();
815                 if (new_size > inode->i_size)
816                         inode->i_size = new_size;
817                 if (inode->i_size > EXT3_I(inode)->i_disksize)
818                         EXT3_I(inode)->i_disksize = inode->i_size;
819                 if (inode->i_size > old_size)
820                         mark_inode_dirty(inode);
821                 unlock_kernel();
822         }
823
824         lock_kernel();
825         journal_stop(handle);
826         unlock_kernel();
827
828         if (err == 0)
829                 *offs = offset;
830         return err;
831 }
832
833 static int fsfilt_ext3_setup(struct super_block *sb)
834 {
835 #if 0
836         EXT3_SB(sb)->dx_lock = fsfilt_ext3_dx_lock;
837         EXT3_SB(sb)->dx_unlock = fsfilt_ext3_dx_unlock;
838 #endif
839 #ifdef S_PDIROPS
840         CWARN("Enabling PDIROPS\n");
841         set_opt(EXT3_SB(sb)->s_mount_opt, PDIROPS);
842         sb->s_flags |= S_PDIROPS;
843 #endif
844         return 0;
845 }
846
847 static struct fsfilt_operations fsfilt_ext3_ops = {
848         fs_type:                "ext3",
849         fs_owner:               THIS_MODULE,
850         fs_start:               fsfilt_ext3_start,
851         fs_brw_start:           fsfilt_ext3_brw_start,
852         fs_commit:              fsfilt_ext3_commit,
853         fs_commit_async:        fsfilt_ext3_commit_async,
854         fs_commit_wait:         fsfilt_ext3_commit_wait,
855         fs_setattr:             fsfilt_ext3_setattr,
856         fs_iocontrol:           fsfilt_ext3_iocontrol,
857         fs_set_md:              fsfilt_ext3_set_md,
858         fs_get_md:              fsfilt_ext3_get_md,
859         fs_readpage:            fsfilt_ext3_readpage,
860         fs_add_journal_cb:      fsfilt_ext3_add_journal_cb,
861         fs_statfs:              fsfilt_ext3_statfs,
862         fs_sync:                fsfilt_ext3_sync,
863         fs_map_inode_page:      fsfilt_ext3_map_inode_page,
864         fs_prep_san_write:      fsfilt_ext3_prep_san_write,
865         fs_write_record:        fsfilt_ext3_write_record,
866         fs_read_record:         fsfilt_ext3_read_record,
867         fs_setup:               fsfilt_ext3_setup,
868 };
869
870 static int __init fsfilt_ext3_init(void)
871 {
872         int rc;
873
874         //rc = ext3_xattr_register();
875         fcb_cache = kmem_cache_create("fsfilt_ext3_fcb",
876                                       sizeof(struct fsfilt_cb_data), 0,
877                                       0, NULL, NULL);
878         if (!fcb_cache) {
879                 CERROR("error allocating fsfilt journal callback cache\n");
880                 GOTO(out, rc = -ENOMEM);
881         }
882
883         rc = fsfilt_register_ops(&fsfilt_ext3_ops);
884
885         if (rc)
886                 kmem_cache_destroy(fcb_cache);
887 out:
888         return rc;
889 }
890
891 static void __exit fsfilt_ext3_exit(void)
892 {
893         int rc;
894
895         fsfilt_unregister_ops(&fsfilt_ext3_ops);
896         rc = kmem_cache_destroy(fcb_cache);
897
898         if (rc || atomic_read(&fcb_cache_count)) {
899                 CERROR("can't free fsfilt callback cache: count %d, rc = %d\n",
900                        atomic_read(&fcb_cache_count), rc);
901         }
902
903         //rc = ext3_xattr_unregister();
904 }
905
906 module_init(fsfilt_ext3_init);
907 module_exit(fsfilt_ext3_exit);
908
909 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
910 MODULE_DESCRIPTION("Lustre ext3 Filesystem Helper v0.1");
911 MODULE_LICENSE("GPL");