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;
252 OBD_FREE(md, md->lmd_easize);
255 if (lli->lli_symlink_name) {
256 OBD_FREE(lli->lli_symlink_name,
257 strlen(lli->lli_symlink_name) + 1);
258 lli->lli_symlink_name = NULL;
263 static void ll_delete_inode(struct inode *inode)
265 if (S_ISREG(inode->i_mode)) {
268 struct lov_stripe_md *md = ll_i2info(inode)->lli_smd;
273 if (md->lmd_object_id == 0) {
274 CERROR("This really happens\n");
275 /* No obdo was ever created */
283 oa->o_id = md->lmd_object_id;
284 oa->o_easize = md->lmd_easize;
285 oa->o_mode = inode->i_mode;
286 oa->o_valid = OBD_MD_FLID | OBD_MD_FLEASIZE | OBD_MD_FLMODE;
288 err = obd_destroy(ll_i2obdconn(inode), oa, md);
290 CDEBUG(D_SUPER, "obd destroy of %Ld error %d\n",
291 md->lmd_object_id, err);
297 /* like inode_setattr, but doesn't mark the inode dirty */
298 static int ll_attr2inode(struct inode * inode, struct iattr * attr, int trunc)
300 unsigned int ia_valid = attr->ia_valid;
303 if ((ia_valid & ATTR_SIZE) && trunc) {
304 error = vmtruncate(inode, attr->ia_size);
307 } else if (ia_valid & ATTR_SIZE)
308 inode->i_size = attr->ia_size;
310 if (ia_valid & ATTR_UID)
311 inode->i_uid = attr->ia_uid;
312 if (ia_valid & ATTR_GID)
313 inode->i_gid = attr->ia_gid;
314 if (ia_valid & ATTR_ATIME)
315 inode->i_atime = attr->ia_atime;
316 if (ia_valid & ATTR_MTIME)
317 inode->i_mtime = attr->ia_mtime;
318 if (ia_valid & ATTR_CTIME)
319 inode->i_ctime = attr->ia_ctime;
320 if (ia_valid & ATTR_MODE) {
321 inode->i_mode = attr->ia_mode;
322 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
323 inode->i_mode &= ~S_ISGID;
329 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
331 struct ptlrpc_request *request = NULL;
332 struct ll_sb_info *sbi = ll_i2sbi(inode);
337 /* change incore inode */
338 ll_attr2inode(inode, attr, do_trunc);
340 err = mdc_setattr(&sbi->ll_mdc_conn, inode, attr, &request);
342 CERROR("mdc_setattr fails (%d)\n", err);
344 ptlrpc_req_finished(request);
349 int ll_setattr(struct dentry *de, struct iattr *attr)
351 int rc = inode_change_ok(de->d_inode, attr);
356 return ll_inode_setattr(de->d_inode, attr, 1);
359 static int ll_statfs(struct super_block *sb, struct statfs *sfs)
361 struct ptlrpc_request *request = NULL;
362 struct ll_sb_info *sbi = ll_s2sbi(sb);
366 memset(sfs, 0, sizeof(*sfs));
367 rc = mdc_statfs(&sbi->ll_mdc_conn, sfs, &request);
368 ptlrpc_req_finished(request);
370 CERROR("obd_statfs fails: rc = %d\n", rc);
372 CDEBUG(D_SUPER, "statfs shows blocks %ld/%ld objects %ld/%ld\n",
373 sfs->f_bavail, sfs->f_blocks, sfs->f_files,sfs->f_ffree);
375 /* temporary until mds_statfs returns statfs info for all OSTs */
377 struct statfs obd_sfs;
379 rc = obd_statfs(&sbi->ll_osc_conn, &obd_sfs);
381 CERROR("obd_statfs fails: rc = %d\n", rc);
384 CDEBUG(D_SUPER, "obd_statfs returns blocks %ld/%ld, "
386 obd_sfs.f_bavail, obd_sfs.f_blocks,
387 obd_sfs.f_ffree, obd_sfs.f_files);
389 sfs->f_bfree = obd_sfs.f_bfree;
390 sfs->f_bavail = obd_sfs.f_bavail;
391 sfs->f_blocks = obd_sfs.f_blocks;
392 if (obd_sfs.f_ffree < sfs->f_ffree)
393 sfs->f_ffree = obd_sfs.f_ffree;
400 inline int ll_stripe_mds_md_size(struct super_block *sb)
402 struct client_obd *mdc = sbi2mdc(ll_s2sbi(sb));
403 return mdc->cl_max_mdsize;
406 static int ll_file_size(struct inode *inode, struct lov_stripe_md *md,
409 struct ll_sb_info *sbi = ll_i2sbi(inode);
410 struct lustre_handle *lockhs;
414 rc = ll_size_lock(inode, md, 0, LCK_PR, &lockhs);
415 if (rc != ELDLM_OK) {
416 CERROR("lock enqueue: %d\n", rc);
420 /* FIXME: I don't like this; why doesn't osc_getattr get o_id from md
421 * like lov_getattr? --phil */
422 oa.o_id = md->lmd_object_id;
424 oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLSIZE;
425 rc = obd_getattr(&sbi->ll_osc_conn, &oa, md);
429 err = ll_size_unlock(inode, md, LCK_PR, lockhs);
430 if (err != ELDLM_OK) {
431 CERROR("lock cancel: %d\n", err);
437 static void ll_read_inode2(struct inode *inode, void *opaque)
439 struct ll_inode_md *md = opaque;
440 struct mds_body *body = md->body;
441 struct ll_inode_info *ii = ll_i2info(inode);
444 sema_init(&ii->lli_open_sem, 1);
445 INIT_LIST_HEAD(&ii->lli_osc_locks);
447 /* core attributes first */
448 if (body->valid & OBD_MD_FLID)
449 inode->i_ino = body->ino;
450 if (body->valid & OBD_MD_FLATIME)
451 inode->i_atime = body->atime;
452 if (body->valid & OBD_MD_FLMTIME)
453 inode->i_mtime = body->mtime;
454 if (body->valid & OBD_MD_FLCTIME)
455 inode->i_ctime = body->ctime;
456 if (body->valid & OBD_MD_FLMODE)
457 inode->i_mode = body->mode;
458 if (body->valid & OBD_MD_FLUID)
459 inode->i_uid = body->uid;
460 if (body->valid & OBD_MD_FLGID)
461 inode->i_gid = body->gid;
462 if (body->valid & OBD_MD_FLFLAGS)
463 inode->i_flags = body->flags;
464 if (body->valid & OBD_MD_FLNLINK)
465 inode->i_nlink = body->nlink;
466 if (body->valid & OBD_MD_FLGENER)
467 inode->i_generation = body->generation;
468 if (body->valid & OBD_MD_FLRDEV)
469 inode->i_rdev = body->extra;
470 //if (body->valid & OBD_MD_FLSIZE)
471 // inode->i_size = body->size;
473 /* Get the authoritative file size */
474 if (md && md->md && inode->i_mode & S_IFREG) {
476 rc = ll_file_size(inode, md->md, &inode->i_size);
478 CERROR("ll_file_size: %d\n", rc);
479 /* FIXME: need to somehow prevent inode creation */
484 //if (body->valid & OBD_MD_FLEASIZE)
485 if (md && md->md && md->md->lmd_stripe_count) {
486 struct lov_mds_md *smd = md->md;
488 if (md->md->lmd_easize != ll_stripe_mds_md_size(inode->i_sb)) {
489 CERROR("Striping metadata size error %ld\n",
493 size = sizeof(*ii->lli_smd) +
494 md->md->lmd_stripe_count * sizeof(struct lov_oinfo);
495 OBD_ALLOC(ii->lli_smd, size);
497 CERROR("No memory for %d\n", size);
500 lov_unpackmd(ii->lli_smd, smd);
503 /* OIDEBUG(inode); */
505 if (S_ISREG(inode->i_mode)) {
506 inode->i_op = &ll_file_inode_operations;
507 inode->i_fop = &ll_file_operations;
508 inode->i_mapping->a_ops = &ll_aops;
510 } else if (S_ISDIR(inode->i_mode)) {
511 inode->i_op = &ll_dir_inode_operations;
512 inode->i_fop = &ll_dir_operations;
513 inode->i_mapping->a_ops = &ll_dir_aops;
515 } else if (S_ISLNK(inode->i_mode)) {
516 inode->i_op = &ll_fast_symlink_inode_operations;
519 init_special_inode(inode, inode->i_mode, inode->i_rdev);
526 /* exported operations */
527 struct super_operations ll_super_operations =
529 read_inode2: ll_read_inode2,
530 clear_inode: ll_clear_inode,
531 delete_inode: ll_delete_inode,
532 put_super: ll_put_super,
536 struct file_system_type lustre_lite_fs_type = {
537 "lustre_lite", 0, ll_read_super, NULL
540 static int __init init_lustre_lite(void)
542 printk(KERN_INFO "Lustre Lite 0.0.1, info@clusterfs.com\n");
543 ll_file_data_slab = kmem_cache_create("ll_file_data",
544 sizeof(struct ll_file_data), 0,
545 SLAB_HWCACHE_ALIGN, NULL, NULL);
546 if (ll_file_data_slab == NULL)
548 return register_filesystem(&lustre_lite_fs_type);
551 static void __exit exit_lustre_lite(void)
553 unregister_filesystem(&lustre_lite_fs_type);
554 kmem_cache_destroy(ll_file_data_slab);
557 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
558 MODULE_DESCRIPTION("Lustre Lite Client File System v1.0");
559 MODULE_LICENSE("GPL");
561 module_init(init_lustre_lite);
562 module_exit(exit_lustre_lite);