1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Lustre Light Super operations
6 * This code is issued under the GNU General Public License.
7 * See the file COPYING in this distribution
9 * Copryright (C) 2002 Cluster File Systems, Inc.
12 #define DEBUG_SUBSYSTEM S_LLITE
14 #include <linux/module.h>
15 #include <linux/random.h>
16 #include <linux/lustre_lite.h>
17 #include <linux/lustre_ha.h>
18 #include <linux/obd_lov.h>
19 #include <linux/lustre_dlm.h>
20 #include <linux/init.h>
22 kmem_cache_t *ll_file_data_slab;
23 extern struct address_space_operations ll_aops;
24 extern struct address_space_operations ll_dir_aops;
25 struct super_operations ll_super_operations;
27 extern int ll_recover(struct ptlrpc_client *);
28 extern int ll_commitcbd_setup(struct ll_sb_info *);
29 extern int ll_commitcbd_cleanup(struct ll_sb_info *);
31 static char *ll_read_opt(const char *opt, char *data)
37 CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
38 if ( strncmp(opt, data, strlen(opt)) )
40 if ( (value = strchr(data, '=')) == NULL )
44 OBD_ALLOC(retval, strlen(value) + 1);
46 CERROR("out of memory!\n");
50 memcpy(retval, value, strlen(value)+1);
51 CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
55 static void ll_options(char *options, char **ost, char **mds)
65 for (this_char = strtok (options, ",");
67 this_char = strtok (NULL, ",")) {
68 CDEBUG(D_SUPER, "this_char %s\n", this_char);
69 if ( (!*ost && (*ost = ll_read_opt("osc", this_char)))||
70 (!*mds && (*mds = ll_read_opt("mdc", this_char))) )
77 #define log2(n) ffz(~(n))
80 static struct super_block * ll_read_super(struct super_block *sb,
81 void *data, int silent)
83 struct inode *root = 0;
84 struct obd_device *obd;
85 struct ll_sb_info *sbi;
89 struct ll_fid rootfid;
93 struct ptlrpc_request *request = NULL;
94 struct ll_inode_md md;
100 OBD_ALLOC(sbi, sizeof(*sbi));
106 generate_random_uuid(uuid);
107 class_uuid_unparse(uuid, sbi->ll_sb_uuid);
109 sb->u.generic_sbp = sbi;
111 ll_options(data, &osc, &mdc);
115 GOTO(out_free, sb = NULL);
120 GOTO(out_free, sb = NULL);
123 obd = class_uuid2obd(mdc);
125 CERROR("MDC %s: not setup or attached\n", mdc);
126 GOTO(out_free, sb = NULL);
130 err = connmgr_connect(ptlrpc_connmgr, sbi->ll_mdc_conn);
132 CERROR("cannot connect to MDC: rc = %d\n", err);
133 GOTO(out_rpc, sb = NULL);
137 err = obd_connect(&sbi->ll_mdc_conn, obd, sbi->ll_sb_uuid);
139 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
140 GOTO(out_free, sb = NULL);
142 sbi2mdc(sbi)->cl_conn->c_level = LUSTRE_CONN_FULL;
144 obd = class_uuid2obd(osc);
146 CERROR("OSC %s: not setup or attached\n", osc);
147 GOTO(out_mdc, sb = NULL);
149 err = obd_connect(&sbi->ll_osc_conn, obd, sbi->ll_sb_uuid);
151 CERROR("cannot connect to %s: rc = %d\n", osc, err);
152 GOTO(out_mdc, sb = NULL);
155 /* XXX: need to store the last_* values somewhere */
156 err = mdc_getstatus(&sbi->ll_mdc_conn, &rootfid, &last_committed,
157 &last_xid, &request);
158 ptlrpc_req_finished(request);
160 CERROR("cannot mds_connect: rc = %d\n", err);
161 GOTO(out_mdc, sb = NULL);
163 CDEBUG(D_SUPER, "rootfid %Ld\n", (unsigned long long)rootfid.id);
164 sbi->ll_rootino = rootfid.id;
166 memset(&sfs, 0, sizeof(sfs));
168 err = mdc_statfs(&sbi->ll_mdc_conn, &sfs, &request);
169 ptlrpc_req_finished(request);
170 sb->s_blocksize = sfs.f_bsize;
171 sb->s_blocksize_bits = log2(sfs.f_bsize);
172 sb->s_magic = LL_SUPER_MAGIC;
173 sb->s_maxbytes = (1ULL << (32 + 9)) - sfs.f_bsize;
175 sb->s_op = &ll_super_operations;
177 /* make root inode */
179 err = mdc_getattr(&sbi->ll_mdc_conn, sbi->ll_rootino, S_IFDIR,
180 OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 0, &request);
182 CERROR("mdc_getattr failed for root: rc = %d\n", err);
183 GOTO(out_request, sb = NULL);
186 /* initialize committed transaction callback daemon */
187 spin_lock_init(&sbi->ll_commitcbd_lock);
188 init_waitqueue_head(&sbi->ll_commitcbd_waitq);
189 init_waitqueue_head(&sbi->ll_commitcbd_ctl_waitq);
190 sbi->ll_commitcbd_flags = 0;
191 err = ll_commitcbd_setup(sbi);
193 CERROR("failed to start commit callback daemon: rc = %d\n",err);
194 GOTO(out_request, sb = NULL);
197 md.body = lustre_msg_buf(request->rq_repmsg, 0);
199 root = iget4(sb, sbi->ll_rootino, NULL, &md);
202 sb->s_root = d_alloc_root(root);
204 CERROR("lustre_lite: bad iget4 for root\n");
205 GOTO(out_cdb, sb = NULL);
208 ptlrpc_req_finished(request);
212 OBD_FREE(mdc, strlen(mdc) + 1);
214 OBD_FREE(osc, strlen(osc) + 1);
219 ll_commitcbd_cleanup(sbi);
221 ptlrpc_req_finished(request);
222 obd_disconnect(&sbi->ll_osc_conn);
224 obd_disconnect(&sbi->ll_mdc_conn);
226 OBD_FREE(sbi, sizeof(*sbi));
230 } /* ll_read_super */
232 static void ll_put_super(struct super_block *sb)
234 struct ll_sb_info *sbi = ll_s2sbi(sb);
236 ll_commitcbd_cleanup(sbi);
237 obd_disconnect(&sbi->ll_osc_conn);
238 obd_disconnect(&sbi->ll_mdc_conn);
239 OBD_FREE(sbi, sizeof(*sbi));
245 static void ll_clear_inode(struct inode *inode)
247 if (atomic_read(&inode->i_count) == 0) {
248 struct ll_inode_info *lli = ll_i2info(inode);
249 struct lov_stripe_md *md = lli->lli_smd;
250 char *symlink_name = lli->lli_symlink_name;
253 int size = sizeof(*md) +
254 md->lmd_stripe_count * sizeof(struct lov_oinfo);
259 OBD_FREE(symlink_name, strlen(symlink_name) + 1);
260 lli->lli_symlink_name = NULL;
265 static void ll_delete_inode(struct inode *inode)
267 if (S_ISREG(inode->i_mode)) {
270 struct lov_stripe_md *md = ll_i2info(inode)->lli_smd;
275 if (md->lmd_object_id == 0) {
276 CERROR("This really happens\n");
277 /* No obdo was ever created */
285 oa->o_id = md->lmd_object_id;
286 oa->o_easize = md->lmd_easize;
287 oa->o_mode = inode->i_mode;
288 oa->o_valid = OBD_MD_FLID | OBD_MD_FLEASIZE | OBD_MD_FLMODE;
290 err = obd_destroy(ll_i2obdconn(inode), oa, md);
292 CDEBUG(D_SUPER, "obd destroy of %Ld error %d\n",
293 md->lmd_object_id, err);
299 /* like inode_setattr, but doesn't mark the inode dirty */
300 static int ll_attr2inode(struct inode * inode, struct iattr * attr, int trunc)
302 unsigned int ia_valid = attr->ia_valid;
305 if ((ia_valid & ATTR_SIZE) && trunc) {
306 error = vmtruncate(inode, attr->ia_size);
309 } else if (ia_valid & ATTR_SIZE)
310 inode->i_size = attr->ia_size;
312 if (ia_valid & ATTR_UID)
313 inode->i_uid = attr->ia_uid;
314 if (ia_valid & ATTR_GID)
315 inode->i_gid = attr->ia_gid;
316 if (ia_valid & ATTR_ATIME)
317 inode->i_atime = attr->ia_atime;
318 if (ia_valid & ATTR_MTIME)
319 inode->i_mtime = attr->ia_mtime;
320 if (ia_valid & ATTR_CTIME)
321 inode->i_ctime = attr->ia_ctime;
322 if (ia_valid & ATTR_MODE) {
323 inode->i_mode = attr->ia_mode;
324 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
325 inode->i_mode &= ~S_ISGID;
331 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
333 struct ptlrpc_request *request = NULL;
334 struct ll_sb_info *sbi = ll_i2sbi(inode);
339 /* change incore inode */
340 ll_attr2inode(inode, attr, do_trunc);
342 err = mdc_setattr(&sbi->ll_mdc_conn, inode, attr, &request);
344 CERROR("mdc_setattr fails (%d)\n", err);
346 ptlrpc_req_finished(request);
351 int ll_setattr(struct dentry *de, struct iattr *attr)
353 int rc = inode_change_ok(de->d_inode, attr);
358 return ll_inode_setattr(de->d_inode, attr, 1);
361 static int ll_statfs(struct super_block *sb, struct statfs *sfs)
363 struct ptlrpc_request *request = NULL;
364 struct ll_sb_info *sbi = ll_s2sbi(sb);
368 memset(sfs, 0, sizeof(*sfs));
369 rc = mdc_statfs(&sbi->ll_mdc_conn, sfs, &request);
370 ptlrpc_req_finished(request);
372 CERROR("obd_statfs fails: rc = %d\n", rc);
374 CDEBUG(D_SUPER, "statfs shows blocks %ld/%ld objects %ld/%ld\n",
375 sfs->f_bavail, sfs->f_blocks, sfs->f_files,sfs->f_ffree);
377 /* temporary until mds_statfs returns statfs info for all OSTs */
379 struct statfs obd_sfs;
381 rc = obd_statfs(&sbi->ll_osc_conn, &obd_sfs);
383 CERROR("obd_statfs fails: rc = %d\n", rc);
386 CDEBUG(D_SUPER, "obd_statfs returns blocks %ld/%ld, "
388 obd_sfs.f_bavail, obd_sfs.f_blocks,
389 obd_sfs.f_ffree, obd_sfs.f_files);
391 sfs->f_bfree = obd_sfs.f_bfree;
392 sfs->f_bavail = obd_sfs.f_bavail;
393 sfs->f_blocks = obd_sfs.f_blocks;
394 if (obd_sfs.f_ffree < sfs->f_ffree)
395 sfs->f_ffree = obd_sfs.f_ffree;
402 inline int ll_stripe_mds_md_size(struct super_block *sb)
404 struct client_obd *mdc = sbi2mdc(ll_s2sbi(sb));
405 return mdc->cl_max_mdsize;
408 static int ll_file_size(struct inode *inode, struct lov_stripe_md *md)
410 struct ll_sb_info *sbi = ll_i2sbi(inode);
411 struct lustre_handle *lockhs;
415 rc = ll_size_lock(inode, md, 0, LCK_PR, &lockhs);
416 if (rc != ELDLM_OK) {
417 CERROR("lock enqueue: %d\n", rc);
421 /* FIXME: I don't like this; why doesn't osc_getattr get o_id from md
422 * like lov_getattr? --phil */
423 oa.o_id = md->lmd_object_id;
425 oa.o_valid = OBD_MD_FLID|OBD_MD_FLMODE|OBD_MD_FLSIZE|OBD_MD_FLBLOCKS;
426 rc = obd_getattr(&sbi->ll_osc_conn, &oa, md);
428 inode->i_size = oa.o_size;
429 inode->i_blocks = oa.o_blocks;
432 err = ll_size_unlock(inode, md, LCK_PR, lockhs);
433 if (err != ELDLM_OK) {
434 CERROR("lock cancel: %d\n", err);
440 static void ll_read_inode2(struct inode *inode, void *opaque)
442 struct ll_inode_md *md = opaque;
443 struct mds_body *body = md->body;
444 struct ll_inode_info *lli = ll_i2info(inode);
447 sema_init(&lli->lli_open_sem, 1);
449 /* core attributes first */
450 if (body->valid & OBD_MD_FLID)
451 inode->i_ino = body->ino;
452 if (body->valid & OBD_MD_FLATIME)
453 inode->i_atime = body->atime;
454 if (body->valid & OBD_MD_FLMTIME)
455 inode->i_mtime = body->mtime;
456 if (body->valid & OBD_MD_FLCTIME)
457 inode->i_ctime = body->ctime;
458 if (body->valid & OBD_MD_FLMODE)
459 inode->i_mode = body->mode;
460 if (body->valid & OBD_MD_FLUID)
461 inode->i_uid = body->uid;
462 if (body->valid & OBD_MD_FLGID)
463 inode->i_gid = body->gid;
464 if (body->valid & OBD_MD_FLFLAGS)
465 inode->i_flags = body->flags;
466 if (body->valid & OBD_MD_FLNLINK)
467 inode->i_nlink = body->nlink;
468 if (body->valid & OBD_MD_FLGENER)
469 inode->i_generation = body->generation;
470 if (body->valid & OBD_MD_FLRDEV)
471 inode->i_rdev = body->extra;
472 if (body->valid & OBD_MD_FLSIZE)
473 inode->i_size = body->size;
476 //if (body->valid & OBD_MD_FLEASIZE)
477 if (md && md->md && md->md->lmd_stripe_count) {
478 struct lov_mds_md *smd = md->md;
480 if (md->md->lmd_easize != ll_stripe_mds_md_size(inode->i_sb)) {
481 CERROR("Striping metadata size error %ld\n",
485 size = sizeof(*lli->lli_smd) +
486 md->md->lmd_stripe_count * sizeof(struct lov_oinfo);
487 OBD_ALLOC(lli->lli_smd, size);
489 CERROR("No memory for %d\n", size);
492 lov_unpackmd(lli->lli_smd, smd);
497 /* Get the authoritative file size */
498 if (lli->lli_smd && (inode->i_mode & S_IFREG)) {
501 rc = ll_file_size(inode, lli->lli_smd);
503 CERROR("ll_file_size: %d\n", rc);
504 /* FIXME: need to somehow prevent inode creation */
509 /* OIDEBUG(inode); */
511 if (S_ISREG(inode->i_mode)) {
512 inode->i_op = &ll_file_inode_operations;
513 inode->i_fop = &ll_file_operations;
514 inode->i_mapping->a_ops = &ll_aops;
516 } else if (S_ISDIR(inode->i_mode)) {
517 inode->i_op = &ll_dir_inode_operations;
518 inode->i_fop = &ll_dir_operations;
519 inode->i_mapping->a_ops = &ll_dir_aops;
521 } else if (S_ISLNK(inode->i_mode)) {
522 inode->i_op = &ll_fast_symlink_inode_operations;
525 init_special_inode(inode, inode->i_mode, inode->i_rdev);
532 /* exported operations */
533 struct super_operations ll_super_operations =
535 read_inode2: ll_read_inode2,
536 clear_inode: ll_clear_inode,
537 delete_inode: ll_delete_inode,
538 put_super: ll_put_super,
542 struct file_system_type lustre_lite_fs_type = {
543 "lustre_lite", 0, ll_read_super, NULL
546 static int __init init_lustre_lite(void)
548 printk(KERN_INFO "Lustre Lite 0.0.1, info@clusterfs.com\n");
549 ll_file_data_slab = kmem_cache_create("ll_file_data",
550 sizeof(struct ll_file_data), 0,
551 SLAB_HWCACHE_ALIGN, NULL, NULL);
552 if (ll_file_data_slab == NULL)
554 return register_filesystem(&lustre_lite_fs_type);
557 static void __exit exit_lustre_lite(void)
559 unregister_filesystem(&lustre_lite_fs_type);
560 kmem_cache_destroy(ll_file_data_slab);
563 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
564 MODULE_DESCRIPTION("Lustre Lite Client File System v1.0");
565 MODULE_LICENSE("GPL");
567 module_init(init_lustre_lite);
568 module_exit(exit_lustre_lite);