Whamcloud - gitweb
Merge b_md to HEAD for 0.5.19 release.
[fs/lustre-release.git] / lustre / obdclass / fsfilt_extN.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/lib/fsfilt_extN.c
5  *  Lustre filesystem abstraction routines
6  *
7  *  Copyright (C) 2002 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/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>
41
42 static kmem_cache_t *fcb_cache;
43 static int fcb_cache_count;
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 EXTN_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_extN_start(struct inode *inode, int op)
62 {
63         /* For updates to the last recieved file */
64         int nblocks = EXTN_DATA_TRANS_BLOCKS;
65         void *handle;
66
67         switch(op) {
68         case FSFILT_OP_RMDIR:
69         case FSFILT_OP_UNLINK:
70                 nblocks += EXTN_DELETE_TRANS_BLOCKS;
71                 break;
72         case FSFILT_OP_RENAME:
73                 /* modify additional directory */
74                 nblocks += EXTN_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 += EXTN_INDEX_EXTRA_TRANS_BLOCKS+EXTN_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(EXTN_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 extN transaction.  No, this shouldn't be here, but as yet extN
109  * doesn't have a nice API for calculating this sort of thing in advance.
110  *
111  * See comment above extN_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 * EXTN_SINGLEDATA_TRANS_BLOCKS for the quota files
127  */
128 static int fsfilt_extN_credits_needed(int objcount, struct fsfilt_objinfo *fso)
129 {
130         struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
131         int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
132         int addrpp = EXTN_ADDR_PER_BLOCK(sb) * blockpp;
133         int nbitmaps = 0;
134         int ngdblocks = 0;
135         int needed = objcount + 1;
136         int i;
137
138         for (i = 0; i < objcount; i++, fso++) {
139                 int nblocks = fso->fso_bufcnt * blockpp;
140                 int ndindirect = min(nblocks, addrpp + 1);
141                 int nindir = nblocks + ndindirect + 1;
142
143                 nbitmaps += nindir + nblocks;
144                 ngdblocks += nindir + nblocks;
145
146                 needed += nindir;
147         }
148
149         /* Assumes extN and extN have same sb_info layout at the start. */
150         if (nbitmaps > EXTN_SB(sb)->s_groups_count)
151                 nbitmaps = EXTN_SB(sb)->s_groups_count;
152         if (ngdblocks > EXTN_SB(sb)->s_gdb_count)
153                 ngdblocks = EXTN_SB(sb)->s_gdb_count;
154
155         needed += nbitmaps + ngdblocks;
156
157 #ifdef CONFIG_QUOTA
158         /* We assume that there will be 1 bit set in s_dquot.flags for each
159          * quota file that is active.  This is at least true for now.
160          */
161         needed += hweight32(sb_any_quota_enabled(sb)) *
162                 EXTN_SINGLEDATA_TRANS_BLOCKS;
163 #endif
164
165         return needed;
166 }
167
168 /* We have to start a huge journal transaction here to hold all of the
169  * metadata for the pages being written here.  This is necessitated by
170  * the fact that we do lots of prepare_write operations before we do
171  * any of the matching commit_write operations, so even if we split
172  * up to use "smaller" transactions none of them could complete until
173  * all of them were opened.  By having a single journal transaction,
174  * we eliminate duplicate reservations for common blocks like the
175  * superblock and group descriptors or bitmaps.
176  *
177  * We will start the transaction here, but each prepare_write will
178  * add a refcount to the transaction, and each commit_write will
179  * remove a refcount.  The transaction will be closed when all of
180  * the pages have been written.
181  */
182 static void *fsfilt_extN_brw_start(int objcount, struct fsfilt_objinfo *fso,
183                                    int niocount, struct niobuf_remote *nb)
184 {
185         journal_t *journal;
186         handle_t *handle;
187         int needed;
188         ENTRY;
189
190         LASSERT(!current->journal_info);
191         journal = EXTN_SB(fso->fso_dentry->d_inode->i_sb)->s_journal;
192         needed = fsfilt_extN_credits_needed(objcount, fso);
193
194         /* The number of blocks we could _possibly_ dirty can very large.
195          * We reduce our request if it is absurd (and we couldn't get that
196          * many credits for a single handle anyways).
197          *
198          * At some point we have to limit the size of I/Os sent at one time,
199          * increase the size of the journal, or we have to calculate the
200          * actual journal requirements more carefully by checking all of
201          * the blocks instead of being maximally pessimistic.  It remains to
202          * be seen if this is a real problem or not.
203          */
204         if (needed > journal->j_max_transaction_buffers) {
205                 CERROR("want too many journal credits (%d) using %d instead\n",
206                        needed, journal->j_max_transaction_buffers);
207                 needed = journal->j_max_transaction_buffers;
208         }
209
210         lock_kernel();
211         handle = journal_start(journal, needed);
212         unlock_kernel();
213         if (IS_ERR(handle))
214                 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
215                        PTR_ERR(handle));
216
217         RETURN(handle);
218 }
219
220 static int fsfilt_extN_commit(struct inode *inode, void *h /*, force_sync */)
221 {
222         int rc;
223         handle_t *handle = h;
224
225 #if 0
226         if (force_sync)
227                 handle->h_sync = 1; /* recovery likes this */
228 #endif
229
230         lock_kernel();
231         rc = journal_stop(handle);
232         unlock_kernel();
233
234         return rc;
235 }
236
237 static int fsfilt_extN_setattr(struct dentry *dentry, void *handle,
238                                struct iattr *iattr)
239 {
240         struct inode *inode = dentry->d_inode;
241         int rc;
242
243         lock_kernel();
244
245         /* A _really_ horrible hack to avoid removing the data stored
246          * in the block pointers; this is really the "small" stripe MD data.
247          * We can avoid further hackery by virtue of the MDS file size being
248          * zero all the time (which doesn't invoke block truncate at unlink
249          * time), so we assert we never change the MDS file size from zero.
250          */
251         if (iattr->ia_valid & ATTR_SIZE) {
252                 CERROR("hmm, setting %*s file size to %lld\n",
253                        dentry->d_name.len, dentry->d_name.name, iattr->ia_size);
254                 LASSERT(iattr->ia_size == 0);
255 #if 0
256                 /* ATTR_SIZE would invoke truncate: clear it */
257                 iattr->ia_valid &= ~ATTR_SIZE;
258                 inode->i_size = iattr->ia_size;
259
260                 /* make sure _something_ gets set - so new inode
261                  * goes to disk (probably won't work over XFS
262                  */
263                 if (!iattr->ia_valid & ATTR_MODE) {
264                         iattr->ia_valid |= ATTR_MODE;
265                         iattr->ia_mode = inode->i_mode;
266                 }
267 #endif
268         }
269         if (inode->i_op->setattr)
270                 rc = inode->i_op->setattr(dentry, iattr);
271         else
272                 rc = inode_setattr(inode, iattr);
273
274         unlock_kernel();
275
276         return rc;
277 }
278
279 static int fsfilt_extN_set_md(struct inode *inode, void *handle,
280                               void *lmm, int lmm_size)
281 {
282         int rc;
283
284         /* Nasty hack city - store stripe MD data in the block pointers if
285          * it will fit, because putting it in an EA currently kills the MDS
286          * performance.  We'll fix this with "fast EAs" in the future.
287          */
288         if (lmm_size <= sizeof(EXTN_I(inode)->i_data) -
289                         sizeof(EXTN_I(inode)->i_data[0])) {
290                 /* XXX old_size is debugging only */
291                 int old_size = EXTN_I(inode)->i_data[0];
292                 if (old_size != 0) {
293                         LASSERT(old_size < sizeof(EXTN_I(inode)->i_data));
294                         CERROR("setting EA on %lu again... interesting\n",
295                                inode->i_ino);
296                 }
297
298                 EXTN_I(inode)->i_data[0] = cpu_to_le32(lmm_size);
299                 memcpy(&EXTN_I(inode)->i_data[1], lmm, lmm_size);
300                 mark_inode_dirty(inode);
301                 return 0;
302         } else {
303                 down(&inode->i_sem);
304                 lock_kernel();
305                 rc = extN_xattr_set(handle, inode, EXTN_XATTR_INDEX_LUSTRE,
306                                     XATTR_LUSTRE_MDS_OBJID, lmm, lmm_size, 0);
307                 unlock_kernel();
308                 up(&inode->i_sem);
309         }
310
311         if (rc)
312                 CERROR("error adding MD data to inode %lu: rc = %d\n",
313                        inode->i_ino, rc);
314         return rc;
315 }
316
317 static int fsfilt_extN_get_md(struct inode *inode, void *lmm, int lmm_size)
318 {
319         int rc;
320
321         if (EXTN_I(inode)->i_data[0]) {
322                 int size = le32_to_cpu(EXTN_I(inode)->i_data[0]);
323                 LASSERT(size < sizeof(EXTN_I(inode)->i_data));
324                 if (lmm) {
325                         if (size > lmm_size)
326                                 return -ERANGE;
327                         memcpy(lmm, &EXTN_I(inode)->i_data[1], size);
328                 }
329                 return size;
330         }
331
332         down(&inode->i_sem);
333         lock_kernel();
334         rc = extN_xattr_get(inode, EXTN_XATTR_INDEX_LUSTRE,
335                             XATTR_LUSTRE_MDS_OBJID, lmm, lmm_size);
336         unlock_kernel();
337         up(&inode->i_sem);
338
339         /* This gives us the MD size */
340         if (lmm == NULL)
341                 return (rc == -ENODATA) ? 0 : rc;
342
343         if (rc < 0) {
344                 CDEBUG(D_INFO, "error getting EA %s from inode %lu: "
345                        "rc = %d\n", XATTR_LUSTRE_MDS_OBJID, inode->i_ino, rc);
346                 memset(lmm, 0, lmm_size);
347                 return (rc == -ENODATA) ? 0 : rc;
348         }
349
350         return rc;
351 }
352
353 static ssize_t fsfilt_extN_readpage(struct file *file, char *buf, size_t count,
354                                     loff_t *offset)
355 {
356         struct inode *inode = file->f_dentry->d_inode;
357         int rc = 0;
358
359         if (S_ISREG(inode->i_mode))
360                 rc = file->f_op->read(file, buf, count, offset);
361         else {
362                 struct buffer_head *bh;
363
364                 /* FIXME: this assumes the blocksize == count, but the calling
365                  *        function will detect this as an error for now */
366                 bh = extN_bread(NULL, inode,
367                                 *offset >> inode->i_sb->s_blocksize_bits,
368                                 0, &rc);
369
370                 if (bh) {
371                         memcpy(buf, bh->b_data, inode->i_blksize);
372                         brelse(bh);
373                         rc = inode->i_blksize;
374                 }
375         }
376
377         return rc;
378 }
379
380 static void fsfilt_extN_cb_func(struct journal_callback *jcb, int error)
381 {
382         struct fsfilt_cb_data *fcb = (struct fsfilt_cb_data *)jcb;
383
384         fcb->cb_func(fcb->cb_obd, fcb->cb_last_rcvd, error);
385
386         kmem_cache_free(fcb_cache, fcb);
387         --fcb_cache_count;
388 }
389
390 static int fsfilt_extN_set_last_rcvd(struct obd_device *obd, __u64 last_rcvd,
391                                      void *handle, fsfilt_cb_t cb_func)
392 {
393 #ifdef HAVE_JOURNAL_CALLBACK_STATUS
394         struct fsfilt_cb_data *fcb;
395
396         fcb = kmem_cache_alloc(fcb_cache, GFP_NOFS);
397         if (!fcb)
398                 RETURN(-ENOMEM);
399
400         ++fcb_cache_count;
401         fcb->cb_func = cb_func;
402         fcb->cb_obd = obd;
403         fcb->cb_last_rcvd = last_rcvd;
404
405         CDEBUG(D_EXT2, "set callback for last_rcvd: "LPD64"\n", last_rcvd);
406         lock_kernel();
407         /* Note that an "incompatible pointer" warning here is OK for now */
408         journal_callback_set(handle, fsfilt_extN_cb_func,
409                              (struct journal_callback *)fcb);
410         unlock_kernel();
411 #else
412 #warning "no journal callback kernel patch, faking it..."
413         static long next = 0;
414
415         if (time_after(jiffies, next)) {
416                 CERROR("no journal callback kernel patch, faking it...\n");
417                 next = jiffies + 300 * HZ;
418         }
419
420         cb_func(obd, last_rcvd, 0);
421 #endif
422
423         return 0;
424 }
425
426 static int fsfilt_extN_journal_data(struct file *filp)
427 {
428         struct inode *inode = filp->f_dentry->d_inode;
429
430         EXTN_I(inode)->i_flags |= EXTN_JOURNAL_DATA_FL;
431
432         return 0;
433 }
434
435 /*
436  * We need to hack the return value for the free inode counts because
437  * the current EA code requires one filesystem block per inode with EAs,
438  * so it is possible to run out of blocks before we run out of inodes.
439  *
440  * This can be removed when the extN EA code is fixed.
441  */
442 static int fsfilt_extN_statfs(struct super_block *sb, struct obd_statfs *osfs)
443 {
444         struct statfs sfs;
445         int rc = vfs_statfs(sb, &sfs);
446
447         if (!rc && sfs.f_bfree < sfs.f_ffree)
448                 sfs.f_ffree = sfs.f_bfree;
449
450         statfs_pack(osfs, &sfs);
451         return rc;
452 }
453
454 static struct fsfilt_operations fsfilt_extN_ops = {
455         fs_type:                "extN",
456         fs_owner:               THIS_MODULE,
457         fs_start:               fsfilt_extN_start,
458         fs_brw_start:           fsfilt_extN_brw_start,
459         fs_commit:              fsfilt_extN_commit,
460         fs_setattr:             fsfilt_extN_setattr,
461         fs_set_md:              fsfilt_extN_set_md,
462         fs_get_md:              fsfilt_extN_get_md,
463         fs_readpage:            fsfilt_extN_readpage,
464         fs_journal_data:        fsfilt_extN_journal_data,
465         fs_set_last_rcvd:       fsfilt_extN_set_last_rcvd,
466         fs_statfs:              fsfilt_extN_statfs,
467 };
468
469 static int __init fsfilt_extN_init(void)
470 {
471         int rc;
472
473         //rc = extN_xattr_register();
474         fcb_cache = kmem_cache_create("fsfilt_extN_fcb",
475                                       sizeof(struct fsfilt_cb_data), 0,
476                                       0, NULL, NULL);
477         if (!fcb_cache) {
478                 CERROR("error allocating fsfilt journal callback cache\n");
479                 GOTO(out, rc = -ENOMEM);
480         }
481
482         rc = fsfilt_register_ops(&fsfilt_extN_ops);
483
484         if (rc)
485                 kmem_cache_destroy(fcb_cache);
486 out:
487         return rc;
488 }
489
490 static void __exit fsfilt_extN_exit(void)
491 {
492         int rc;
493
494         fsfilt_unregister_ops(&fsfilt_extN_ops);
495         rc = kmem_cache_destroy(fcb_cache);
496
497         if (rc || fcb_cache_count) {
498                 CERROR("can't free fsfilt callback cache: count %d, rc = %d\n",
499                        fcb_cache_count, rc);
500         }
501
502         //rc = extN_xattr_unregister();
503 }
504
505 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
506 MODULE_DESCRIPTION("Lustre extN Filesystem Helper v0.1");
507 MODULE_LICENSE("GPL");
508
509 module_init(fsfilt_extN_init);
510 module_exit(fsfilt_extN_exit);