Whamcloud - gitweb
Large commit which implements more "intelligent" offsets for stripe
[fs/lustre-release.git] / lustre / mds / mds_extN.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/mds/mds_extN.c
5  *  Lustre Metadata Server (mds) journal 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_MDS
27
28 #include <linux/fs.h>
29 #include <linux/jbd.h>
30 #include <linux/slab.h>
31 #include <linux/extN_fs.h>
32 #include <linux/extN_jbd.h>
33 #include <linux/extN_xattr.h>
34 #include <linux/kp30.h>
35 #include <linux/lustre_mds.h>
36 #include <linux/obd.h>
37 #include <linux/module.h>
38 #include <linux/obd_lov.h>
39
40 static struct mds_fs_operations mds_extN_fs_ops;
41 static kmem_cache_t *jcb_cache;
42 static int jcb_cache_count;
43
44 struct mds_cb_data {
45         struct journal_callback cb_jcb;
46         struct mds_obd *cb_mds;
47         __u64 cb_last_rcvd;
48 };
49
50 #define EXTN_XATTR_INDEX_LUSTRE         5
51 #define XATTR_LUSTRE_MDS_OBJID          "system.lustre_mds_objid"
52
53 #define XATTR_MDS_MO_MAGIC              0xEA0BD047
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 *mds_extN_start(struct inode *inode, int op)
62 {
63         /* For updates to the last recieved file */
64         int nblocks = EXTN_DATA_TRANS_BLOCKS;
65
66         switch(op) {
67         case MDS_FSOP_RMDIR:
68         case MDS_FSOP_UNLINK:
69                 nblocks += EXTN_DELETE_TRANS_BLOCKS;
70                 break;
71         case MDS_FSOP_RENAME:
72                 /* We may be modifying two directories */
73                 nblocks += EXTN_DATA_TRANS_BLOCKS;
74         case MDS_FSOP_SYMLINK:
75                 /* Possible new block + block bitmap + GDT for long symlink */
76                 nblocks += 3;
77         case MDS_FSOP_CREATE:
78         case MDS_FSOP_MKDIR:
79         case MDS_FSOP_MKNOD:
80                 /* New inode + block bitmap + GDT for new file */
81                 nblocks += 3;
82         case MDS_FSOP_LINK:
83                 /* Change parent directory */
84                 nblocks += EXTN_INDEX_EXTRA_TRANS_BLOCKS+EXTN_DATA_TRANS_BLOCKS;
85                 break;
86         case MDS_FSOP_SETATTR:
87                 /* Setattr on inode */
88                 nblocks += 1;
89                 break;
90         default: CERROR("unknown transaction start op %d\n", op);
91                  LBUG();
92         }
93
94         return journal_start(EXTN_JOURNAL(inode), nblocks);
95 }
96
97 static int mds_extN_commit(struct inode *inode, void *handle)
98 {
99         return journal_stop((handle_t *)handle);
100 }
101
102 /* Assumes BKL is held */
103 static int mds_extN_setattr(struct dentry *dentry, void *handle,
104                             struct iattr *iattr)
105 {
106         struct inode *inode = dentry->d_inode;
107
108         if (inode->i_op->setattr)
109                 return inode->i_op->setattr(dentry, iattr);
110         else
111                 return inode_setattr(inode, iattr);
112 }
113
114 static int mds_extN_set_md(struct inode *inode, void *handle,
115                            struct lov_mds_md *lmm)
116 {
117         int rc;
118
119         down(&inode->i_sem);
120         lock_kernel();
121         if (lmm == NULL)
122                 rc = extN_xattr_set(handle, inode, EXTN_XATTR_INDEX_LUSTRE,
123                                     XATTR_LUSTRE_MDS_OBJID, NULL, 0, 0);
124         else {
125                 lmm->lmm_magic = cpu_to_le32(XATTR_MDS_MO_MAGIC);
126                 rc = extN_xattr_set(handle, inode, EXTN_XATTR_INDEX_LUSTRE,
127                                     XATTR_LUSTRE_MDS_OBJID, lmm,
128                                     lmm->lmm_easize, XATTR_CREATE);
129                 if (rc == -EEXIST)
130                         rc = extN_xattr_set(handle, inode,
131                                             EXTN_XATTR_INDEX_LUSTRE,
132                                             XATTR_LUSTRE_MDS_OBJID, lmm,
133                                             lmm->lmm_easize, 0);
134         }
135         unlock_kernel();
136         up(&inode->i_sem);
137
138         if (rc) {
139                 CERROR("error adding objectid "LPX64" to inode %ld: %d\n",
140                        lmm->lmm_object_id, inode->i_ino, rc);
141                 LBUG();
142         }
143         return rc;
144 }
145
146 static int mds_extN_get_md(struct inode *inode, struct lov_mds_md *lmm)
147 {
148         int rc;
149         int size = lmm->lmm_easize;
150
151         down(&inode->i_sem);
152         lock_kernel();
153         rc = extN_xattr_get(inode, EXTN_XATTR_INDEX_LUSTRE,
154                             XATTR_LUSTRE_MDS_OBJID, lmm, size);
155         unlock_kernel();
156         up(&inode->i_sem);
157
158         /* This gives us the MD size */
159         if (lmm == NULL)
160                 return rc;
161
162         if (rc < 0) {
163                 CDEBUG(D_INFO, "error getting EA %s from MDS inode %ld: "
164                        "rc = %d\n", XATTR_LUSTRE_MDS_OBJID, inode->i_ino, rc);
165                 memset(lmm, 0, size);
166                 return rc;
167         }
168
169         if (lmm->lmm_magic != cpu_to_le32(XATTR_MDS_MO_MAGIC)) {
170                 CERROR("MDS striping md for ino %ld has bad magic\n",
171                        inode->i_ino);
172                 rc = -EINVAL;
173         } else {
174                 /* This field is byteswapped because it appears in the
175                  * catalogue.  All others are opaque to the MDS */
176                 lmm->lmm_object_id = le64_to_cpu(lmm->lmm_object_id);
177         }
178
179         return rc;
180 }
181
182 static ssize_t mds_extN_readpage(struct file *file, char *buf, size_t count,
183                                  loff_t *offset)
184 {
185         struct inode *inode = file->f_dentry->d_inode;
186         int rc = 0;
187
188         if (S_ISREG(inode->i_mode))
189                 rc = file->f_op->read(file, buf, count, offset);
190         else {
191                 struct buffer_head *bh;
192
193                 /* FIXME: this assumes the blocksize == count, but the calling
194                  *        function will detect this as an error for now */
195                 bh = extN_bread(NULL, inode,
196                                 *offset >> inode->i_sb->s_blocksize_bits,
197                                 0, &rc);
198
199                 if (bh) {
200                         memcpy(buf, bh->b_data, inode->i_blksize);
201                         brelse(bh);
202                         rc = inode->i_blksize;
203                 }
204         }
205
206         return rc;
207 }
208
209 static void mds_extN_delete_inode(struct inode *inode)
210 {
211         if (S_ISREG(inode->i_mode)) {
212                 void *handle = mds_extN_start(inode, MDS_FSOP_UNLINK);
213
214                 if (IS_ERR(handle)) {
215                         CERROR("unable to start transaction");
216                         EXIT;
217                         return;
218                 }
219                 if (mds_extN_set_md(inode, handle, NULL))
220                         CERROR("error clearing obdo on %ld\n", inode->i_ino);
221
222                 if (mds_extN_fs_ops.cl_delete_inode)
223                         mds_extN_fs_ops.cl_delete_inode(inode);
224
225                 if (mds_extN_commit(inode, handle))
226                         CERROR("error closing handle on %ld\n", inode->i_ino);
227         } else
228                 mds_extN_fs_ops.cl_delete_inode(inode);
229 }
230
231 static void mds_extN_callback_status(void *jcb, int error)
232 {
233         struct mds_cb_data *mcb = (struct mds_cb_data *)jcb;
234
235         CDEBUG(D_EXT2, "got callback for last_rcvd "LPD64": rc = %d\n",
236                mcb->cb_last_rcvd, error);
237         if (!error && mcb->cb_last_rcvd > mcb->cb_mds->mds_last_committed)
238                 mcb->cb_mds->mds_last_committed = mcb->cb_last_rcvd;
239
240         kmem_cache_free(jcb_cache, jcb);
241         --jcb_cache_count;
242 }
243
244 static int mds_extN_set_last_rcvd(struct mds_obd *mds, void *handle)
245 {
246         struct mds_cb_data *mcb;
247
248         mcb = kmem_cache_alloc(jcb_cache, GFP_NOFS);
249         if (!mcb)
250                 RETURN(-ENOMEM);
251
252         ++jcb_cache_count;
253         mcb->cb_mds = mds;
254         mcb->cb_last_rcvd = mds->mds_last_rcvd;
255
256 #ifdef HAVE_JOURNAL_CALLBACK_STATUS
257         CDEBUG(D_EXT2, "set callback for last_rcvd: %Ld\n",
258                (unsigned long long)mcb->cb_last_rcvd);
259         journal_callback_set(handle, mds_extN_callback_status,
260                              (void *)mcb);
261 #else
262 #warning "no journal callback kernel patch, faking it..."
263         {
264         static long next = 0;
265
266         if (time_after(jiffies, next)) {
267                 CERROR("no journal callback kernel patch, faking it...\n");
268                 next = jiffies + 300 * HZ;
269         }
270         }
271         mds_extN_callback_status((struct journal_callback *)mcb, 0);
272 #endif
273
274         return 0;
275 }
276
277 static int mds_extN_journal_data(struct file *filp)
278 {
279         struct inode *inode = filp->f_dentry->d_inode;
280
281         EXTN_I(inode)->i_flags |= EXTN_JOURNAL_DATA_FL;
282
283         return 0;
284 }
285
286 /*
287  * We need to hack the return value for the free inode counts because
288  * the current EA code requires one filesystem block per inode with EAs,
289  * so it is possible to run out of blocks before we run out of inodes.
290  *
291  * This can be removed when the extN EA code is fixed.
292  */
293 static int mds_extN_statfs(struct super_block *sb, struct statfs *sfs)
294 {
295         int rc = vfs_statfs(sb, sfs);
296
297         if (!rc && sfs->f_bfree < sfs->f_ffree)
298                 sfs->f_ffree = sfs->f_bfree;
299
300         return rc;
301 }
302
303 static struct mds_fs_operations mds_extN_fs_ops = {
304         fs_owner:               THIS_MODULE,
305         fs_start:               mds_extN_start,
306         fs_commit:              mds_extN_commit,
307         fs_setattr:             mds_extN_setattr,
308         fs_set_md:              mds_extN_set_md,
309         fs_get_md:              mds_extN_get_md,
310         fs_readpage:            mds_extN_readpage,
311         fs_delete_inode:        mds_extN_delete_inode,
312         cl_delete_inode:        clear_inode,
313         fs_journal_data:        mds_extN_journal_data,
314         fs_set_last_rcvd:       mds_extN_set_last_rcvd,
315         fs_statfs:              mds_extN_statfs,
316 };
317
318 static int __init mds_extN_init(void)
319 {
320         int rc;
321
322         //rc = extN_xattr_register();
323         jcb_cache = kmem_cache_create("mds_extN_jcb",
324                                       sizeof(struct mds_cb_data), 0,
325                                       0, NULL, NULL);
326         if (!jcb_cache) {
327                 CERROR("error allocating MDS journal callback cache\n");
328                 GOTO(out, rc = -ENOMEM);
329         }
330         rc = mds_register_fs_type(&mds_extN_fs_ops, "extN");
331
332         if (rc)
333                 kmem_cache_destroy(jcb_cache);
334 out:
335         return rc;
336 }
337
338 static void __exit mds_extN_exit(void)
339 {
340         int rc;
341
342         mds_unregister_fs_type("extN");
343         rc = kmem_cache_destroy(jcb_cache);
344
345         if (rc || jcb_cache_count) {
346                 CERROR("can't free MDS callback cache: count %d, rc = %d\n",
347                        jcb_cache_count, rc);
348         }
349
350         //rc = extN_xattr_unregister();
351 }
352
353 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
354 MODULE_DESCRIPTION("Lustre MDS extN Filesystem Helper v0.1");
355 MODULE_LICENSE("GPL");
356
357 module_init(mds_extN_init);
358 module_exit(mds_extN_exit);