Whamcloud - gitweb
bb321596952fc74d357af2584e6cf880f9d68924
[fs/lustre-release.git] / lustre / llite / super.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Light Super operations
5  *
6  * This code is issued under the GNU General Public License.
7  * See the file COPYING in this distribution
8  *
9  * Copryright (C) 2002 Cluster File Systems, Inc.
10  */
11
12 #define DEBUG_SUBSYSTEM S_LLITE
13
14 #include <linux/module.h>
15 #include <linux/lustre_lite.h>
16 #include <linux/lustre_ha.h>
17 #include <linux/lustre_dlm.h>
18 #include <linux/init.h>
19
20 kmem_cache_t *ll_file_data_slab;
21 extern struct address_space_operations ll_aops;
22 extern struct address_space_operations ll_dir_aops;
23 struct super_operations ll_super_operations;
24
25 extern int ll_recover(struct ptlrpc_client *);
26 extern int ll_commitcbd_setup(struct ll_sb_info *);
27 extern int ll_commitcbd_cleanup(struct ll_sb_info *);
28
29 static char *ll_read_opt(const char *opt, char *data)
30 {
31         char *value;
32         char *retval;
33         ENTRY;
34
35         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
36         if ( strncmp(opt, data, strlen(opt)) )
37                 RETURN(NULL);
38         if ( (value = strchr(data, '=')) == NULL )
39                 RETURN(NULL);
40
41         value++;
42         OBD_ALLOC(retval, strlen(value) + 1);
43         if ( !retval ) {
44                 CERROR("out of memory!\n");
45                 RETURN(NULL);
46         }
47         
48         memcpy(retval, value, strlen(value)+1);
49         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
50         RETURN(retval);
51 }
52
53 static void ll_options(char *options, char **ost, char **mds)
54 {
55         char *this_char;
56         ENTRY;
57
58         if (!options) {
59                 EXIT;
60                 return;
61         }
62
63         for (this_char = strtok (options, ",");
64              this_char != NULL;
65              this_char = strtok (NULL, ",")) {
66                 CDEBUG(D_SUPER, "this_char %s\n", this_char);
67                 if ( (!*ost && (*ost = ll_read_opt("ost", this_char)))||
68                      (!*mds && (*mds = ll_read_opt("mds", this_char))) )
69                         continue;
70         }
71         EXIT;
72 }
73
74 #ifndef log2
75 #define log2(n) ffz(~(n))
76 #endif
77
78 static struct super_block * ll_read_super(struct super_block *sb,
79                                           void *data, int silent)
80 {
81         struct inode *root = 0;
82         struct obd_device *obd;
83         struct ll_sb_info *sbi;
84         char *ost = NULL;
85         char *mds = NULL;
86         int err;
87         struct ll_fid rootfid;
88         struct statfs sfs;
89         __u64 last_committed, last_rcvd;
90         __u32 last_xid;
91         struct ptlrpc_request *request = NULL;
92         struct ll_inode_md md;
93
94         ENTRY;
95         MOD_INC_USE_COUNT;
96
97         OBD_ALLOC(sbi, sizeof(*sbi));
98         if (!sbi) {
99                 MOD_DEC_USE_COUNT;
100                 RETURN(NULL);
101         }
102
103         sb->u.generic_sbp = sbi;
104
105         ll_options(data, &ost, &mds);
106
107         if (!ost) {
108                 CERROR("no ost\n");
109                 GOTO(out_free, sb = NULL);
110         }
111
112         if (!mds) {
113                 CERROR("no mds\n");
114                 GOTO(out_free, sb = NULL);
115         }
116
117         obd = class_uuid2obd(mds); 
118         if (!obd) {
119                 CERROR("MDS %s: not setup or attached\n", mds);
120                 GOTO(out_free, sb = NULL);
121         }
122
123 #if 0
124         err = connmgr_connect(ptlrpc_connmgr, sbi->ll_mds_conn);
125         if (err) {
126                 CERROR("cannot connect to MDS: rc = %d\n", err);
127                 GOTO(out_rpc, sb = NULL);
128         }
129 #endif 
130
131         err = obd_connect(&sbi->ll_mdc_conn, obd);
132         if (err) {
133                 CERROR("cannot connect to %s: rc = %d\n", mds, err);
134                 GOTO(out_free, sb = NULL);
135         }
136         sbi2mdc(sbi)->mdc_conn->c_level = LUSTRE_CONN_FULL;
137
138         obd = class_uuid2obd(ost);
139         if (!obd) {
140                 CERROR("OST %s: not setup or attached\n", ost);
141                 GOTO(out_mdc, sb = NULL);
142         }
143         err = obd_connect(&sbi->ll_osc_conn, obd);
144         if (err) {
145                 CERROR("cannot connect to %s: rc = %d\n", ost, err);
146                 GOTO(out_mdc, sb = NULL);
147         }
148
149         /* XXX: need to store the last_* values somewhere */
150         err = mdc_getstatus(&sbi->ll_mdc_conn, &rootfid, &last_committed,
151                             &last_rcvd, &last_xid, &request);
152         if (err) {
153                 CERROR("cannot mds_connect: rc = %d\n", err);
154                 GOTO(out_request, sb = NULL);
155         }
156         CDEBUG(D_SUPER, "rootfid %Ld\n", (unsigned long long)rootfid.id);
157         sbi->ll_rootino = rootfid.id;
158
159         memset(&sfs, 0, sizeof(sfs));
160         err = mdc_statfs(&sbi->ll_mdc_conn, &sfs, &request);
161         sb->s_blocksize = sfs.f_bsize;
162         sb->s_blocksize_bits = log2(sfs.f_bsize);
163         sb->s_magic = LL_SUPER_MAGIC;
164         sb->s_maxbytes = (1ULL << (32 + 9)) - sfs.f_bsize;
165         ptlrpc_req_finished(request);
166
167         sb->s_op = &ll_super_operations;
168
169         /* make root inode */
170         err = mdc_getattr(&sbi->ll_mdc_conn, sbi->ll_rootino, S_IFDIR,
171                           OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 0, &request);
172         if (err) {
173                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
174                 GOTO(out_request, sb = NULL);
175         }
176
177         /* initialize committed transaction callback daemon */
178         spin_lock_init(&sbi->ll_commitcbd_lock);
179         init_waitqueue_head(&sbi->ll_commitcbd_waitq);
180         init_waitqueue_head(&sbi->ll_commitcbd_ctl_waitq);
181         sbi->ll_commitcbd_flags = 0;
182         err = ll_commitcbd_setup(sbi);
183         if (err) {
184                 CERROR("failed to start commit callback daemon: rc = %d\n",err);
185                 GOTO(out_request, sb = NULL);
186         }
187
188         md.body = lustre_msg_buf(request->rq_repmsg, 0);
189         md.md = NULL;
190         root = iget4(sb, sbi->ll_rootino, NULL, &md);
191
192         if (root) {
193                 sb->s_root = d_alloc_root(root);
194         } else {
195                 CERROR("lustre_lite: bad iget4 for root\n");
196                 GOTO(out_cdb, sb = NULL);
197         }
198
199         ptlrpc_free_req(request);
200
201 out_dev:
202         if (mds)
203                 OBD_FREE(mds, strlen(mds) + 1);
204         if (ost)
205                 OBD_FREE(ost, strlen(ost) + 1);
206
207         RETURN(sb);
208
209 out_cdb:
210         ll_commitcbd_cleanup(sbi);
211 out_request:
212         ptlrpc_free_req(request);
213         obd_disconnect(&sbi->ll_osc_conn);
214 out_mdc:
215         obd_disconnect(&sbi->ll_mdc_conn);
216 out_free:
217         OBD_FREE(sbi, sizeof(*sbi));
218
219         MOD_DEC_USE_COUNT;
220         goto out_dev;
221 } /* ll_read_super */
222
223 static void ll_put_super(struct super_block *sb)
224 {
225         struct ll_sb_info *sbi = ll_s2sbi(sb);
226         ENTRY;
227         ll_commitcbd_cleanup(sbi);
228         obd_disconnect(&sbi->ll_osc_conn);
229         obd_disconnect(&sbi->ll_mdc_conn);
230         OBD_FREE(sbi, sizeof(*sbi));
231
232         MOD_DEC_USE_COUNT;
233         EXIT;
234 } /* ll_put_super */
235
236 static void ll_clear_inode(struct inode *inode)
237 {
238         if (atomic_read(&inode->i_count) == 0) {
239                 struct ll_inode_info *lli = ll_i2info(inode);
240                 struct lov_stripe_md *md = lli->lli_smd;
241
242                 if (md) {
243                         OBD_FREE(md, md->lmd_size); 
244                         lli->lli_smd = NULL;
245                 }
246                 if (lli->lli_symlink_name) {
247                         OBD_FREE(lli->lli_symlink_name,
248                                  strlen(lli->lli_symlink_name) + 1);
249                         lli->lli_symlink_name = NULL;
250                 }
251         }
252 }
253
254 static void ll_delete_inode(struct inode *inode)
255 {
256         if (S_ISREG(inode->i_mode)) {
257                 int err;
258                 struct obdo oa;
259                 struct lov_stripe_md *md = ll_i2info(inode)->lli_smd;
260
261                if (!md)
262                         GOTO(out, -EINVAL);
263
264                 oa.o_id = md->lmd_object_id;
265                 oa.o_easize = md->lmd_size;
266                 if (oa.o_id == 0) {
267                         CERROR("This really happens\n");
268                         /* No obdo was ever created */
269                         GOTO(out, 0);
270                 }
271
272                 err = obd_destroy(ll_i2obdconn(inode), &oa, md);
273                 CDEBUG(D_SUPER, "obd destroy of %Ld error %d\n",
274                        md->lmd_object_id, err);
275         }
276 out:
277         clear_inode(inode);
278 }
279
280 /* like inode_setattr, but doesn't mark the inode dirty */
281 static int ll_attr2inode(struct inode * inode, struct iattr * attr, int trunc)
282 {
283         unsigned int ia_valid = attr->ia_valid;
284         int error = 0;
285
286         if ((ia_valid & ATTR_SIZE) && trunc) {
287                 error = vmtruncate(inode, attr->ia_size);
288                 if (error)
289                         goto out;
290         } else if (ia_valid & ATTR_SIZE)
291                 inode->i_size = attr->ia_size;
292
293         if (ia_valid & ATTR_UID)
294                 inode->i_uid = attr->ia_uid;
295         if (ia_valid & ATTR_GID)
296                 inode->i_gid = attr->ia_gid;
297         if (ia_valid & ATTR_ATIME)
298                 inode->i_atime = attr->ia_atime;
299         if (ia_valid & ATTR_MTIME)
300                 inode->i_mtime = attr->ia_mtime;
301         if (ia_valid & ATTR_CTIME)
302                 inode->i_ctime = attr->ia_ctime;
303         if (ia_valid & ATTR_MODE) {
304                 inode->i_mode = attr->ia_mode;
305                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
306                         inode->i_mode &= ~S_ISGID;
307         }
308 out:
309         return error;
310 }
311
312 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
313 {
314         struct ptlrpc_request *request = NULL;
315         struct ll_sb_info *sbi = ll_i2sbi(inode);
316         int err;
317
318         ENTRY;
319
320         /* change incore inode */
321         ll_attr2inode(inode, attr, do_trunc);
322
323         err = mdc_setattr(&sbi->ll_mdc_conn, inode, attr, &request);
324         if (err)
325                 CERROR("mdc_setattr fails (%d)\n", err);
326
327         ptlrpc_req_finished(request);
328
329         RETURN(err);
330 }
331
332 int ll_setattr(struct dentry *de, struct iattr *attr)
333 {
334         int rc = inode_change_ok(de->d_inode, attr);
335
336         if (rc)
337                 return rc;
338
339         return ll_inode_setattr(de->d_inode, attr, 1);
340 }
341
342 static int ll_statfs(struct super_block *sb, struct statfs *sfs)
343 {
344         struct ptlrpc_request *request = NULL;
345         struct ll_sb_info *sbi = ll_s2sbi(sb);
346         int rc;
347         ENTRY;
348
349         memset(sfs, 0, sizeof(*sfs));
350         rc = mdc_statfs(&sbi->ll_mdc_conn, sfs, &request);
351         ptlrpc_req_finished(request);
352         if (rc)
353                 CERROR("obd_statfs fails: rc = %d\n", rc);
354         else
355                 CDEBUG(D_SUPER, "statfs shows blocks %ld/%ld objects %ld/%ld\n",
356                        sfs->f_bavail, sfs->f_blocks, sfs->f_files,sfs->f_ffree);
357
358         /* temporary until mds_statfs returns statfs info for all OSTs */
359         if (!rc) {
360                 struct statfs obd_sfs;
361
362                 rc = obd_statfs(&sbi->ll_osc_conn, &obd_sfs);
363                 if (rc) {
364                         CERROR("obd_statfs fails: rc = %d\n", rc);
365                         GOTO(out, rc);
366                 }
367                 CDEBUG(D_SUPER, "obd_statfs returns blocks %ld/%ld, "
368                        "objects %ld/%ld\n",
369                        obd_sfs.f_bavail, obd_sfs.f_blocks,
370                        obd_sfs.f_ffree, obd_sfs.f_files);
371
372                 sfs->f_bfree = obd_sfs.f_bfree;
373                 sfs->f_bavail = obd_sfs.f_bavail;
374                 sfs->f_blocks = obd_sfs.f_blocks;
375                 if (obd_sfs.f_ffree < sfs->f_ffree)
376                         sfs->f_ffree = obd_sfs.f_ffree;
377         }
378
379 out:
380         RETURN(rc);
381 }
382
383 inline int ll_stripe_md_size(struct super_block *sb)
384 {
385         struct mdc_obd *mdc = sbi2mdc(ll_s2sbi(sb));
386         return mdc->mdc_max_mdsize;
387 }
388
389 static void ll_to_inode(struct inode *dst, struct ll_inode_md *md)
390 {
391         struct mds_body *body = md->body;
392         struct ll_inode_info *ii = ll_i2info(dst);
393
394         /* core attributes first */
395         if (body->valid & OBD_MD_FLID)
396                 dst->i_ino = body->ino;
397         if (body->valid & OBD_MD_FLATIME)
398                 dst->i_atime = body->atime;
399         if (body->valid & OBD_MD_FLMTIME)
400                 dst->i_mtime = body->mtime;
401         if (body->valid & OBD_MD_FLCTIME)
402                 dst->i_ctime = body->ctime;
403         if (body->valid & OBD_MD_FLSIZE)
404                 dst->i_size = body->size;
405         if (body->valid & OBD_MD_FLMODE)
406                 dst->i_mode = body->mode;
407         if (body->valid & OBD_MD_FLUID)
408                 dst->i_uid = body->uid;
409         if (body->valid & OBD_MD_FLGID)
410                 dst->i_gid = body->gid;
411         if (body->valid & OBD_MD_FLFLAGS)
412                 dst->i_flags = body->flags;
413         if (body->valid & OBD_MD_FLNLINK)
414                 dst->i_nlink = body->nlink;
415         if (body->valid & OBD_MD_FLGENER)
416                 dst->i_generation = body->generation;
417         if (body->valid & OBD_MD_FLRDEV)
418                 dst->i_rdev = body->extra;
419         if (md && md->md && md->md->lmd_stripe_count) { 
420                 struct lov_stripe_md *smd = md->md;
421                 int size = ll_stripe_md_size(dst->i_sb);
422                 if (md->md->lmd_size != size) { 
423                         CERROR("Striping metadata size error %ld\n",
424                                dst->i_ino); 
425                         LBUG();
426                 }
427                 OBD_ALLOC(ii->lli_smd, size);
428                 if (!ii->lli_smd){ 
429                         CERROR("No memory for %d\n", size);
430                         LBUG();
431                 }
432                 memcpy(ii->lli_smd, smd, size);
433         }
434 } /* ll_to_inode */
435
436 static void ll_read_inode2(struct inode *inode, void *opaque)
437 {
438         struct ll_inode_md *md = opaque;
439
440         ENTRY;
441         ll_to_inode(inode, md);
442
443         /* OIDEBUG(inode); */
444
445         if (S_ISREG(inode->i_mode)) {
446                 inode->i_op = &ll_file_inode_operations;
447                 inode->i_fop = &ll_file_operations;
448                 inode->i_mapping->a_ops = &ll_aops;
449                 EXIT;
450         } else if (S_ISDIR(inode->i_mode)) {
451                 inode->i_op = &ll_dir_inode_operations;
452                 inode->i_fop = &ll_dir_operations;
453                 inode->i_mapping->a_ops = &ll_dir_aops;
454                 EXIT;
455         } else if (S_ISLNK(inode->i_mode)) {
456                 inode->i_op = &ll_fast_symlink_inode_operations;
457                 EXIT;
458         } else {
459                 init_special_inode(inode, inode->i_mode,
460                                    ((int *)ll_i2info(inode)->lli_inline)[0]);
461                 EXIT;
462         }
463
464         return;
465 }
466
467 /* exported operations */
468 struct super_operations ll_super_operations =
469 {
470         read_inode2: ll_read_inode2,
471         clear_inode: ll_clear_inode,
472         delete_inode: ll_delete_inode,
473         put_super: ll_put_super,
474         statfs: ll_statfs
475 };
476
477 struct file_system_type lustre_lite_fs_type = {
478         "lustre_lite", 0, ll_read_super, NULL
479 };
480
481 static int __init init_lustre_lite(void)
482 {
483         printk(KERN_INFO "Lustre Lite 0.0.1, info@clusterfs.com\n");
484         ll_file_data_slab = kmem_cache_create("ll_file_data",
485                                               sizeof(struct ll_file_data), 0,
486                                                SLAB_HWCACHE_ALIGN, NULL, NULL);
487         if (ll_file_data_slab == NULL)
488                 return -ENOMEM;
489         return register_filesystem(&lustre_lite_fs_type);
490 }
491
492 static void __exit exit_lustre_lite(void)
493 {
494         unregister_filesystem(&lustre_lite_fs_type);
495         kmem_cache_destroy(ll_file_data_slab);
496 }
497
498 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
499 MODULE_DESCRIPTION("Lustre Lite Client File System v1.0");
500 MODULE_LICENSE("GPL");
501
502 module_init(init_lustre_lite);
503 module_exit(exit_lustre_lite);