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/version.h>
17 #include <linux/lustre_lite.h>
18 #include <linux/lustre_ha.h>
19 #include <linux/obd_lov.h>
20 #include <linux/lustre_dlm.h>
21 #include <linux/init.h>
23 #include <linux/lprocfs_status.h>
26 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
27 kmem_cache_t *ll_file_data_slab;
28 extern struct address_space_operations ll_aops;
29 extern struct address_space_operations ll_dir_aops;
30 struct super_operations ll_super_operations;
32 extern int ll_recover(struct recovd_data *, int);
33 extern int ll_commitcbd_setup(struct ll_sb_info *);
34 extern int ll_commitcbd_cleanup(struct ll_sb_info *);
36 extern int rd_dev_uuid(char* page, char **start, off_t off,
37 int count, int *eof, void *data);
39 extern int rd_dev_name(char* page, char **start, off_t off,
40 int count, int *eof, void *data);
43 extern struct lprocfs_vars status_var_nm_1[];
45 static char *ll_read_opt(const char *opt, char *data)
51 CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
52 if ( strncmp(opt, data, strlen(opt)) )
54 if ( (value = strchr(data, '=')) == NULL )
58 OBD_ALLOC(retval, strlen(value) + 1);
60 CERROR("out of memory!\n");
64 memcpy(retval, value, strlen(value)+1);
65 CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
69 static int ll_set_opt(const char *opt, char *data, int fl)
73 CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
74 if ( strncmp(opt, data, strlen(opt)) )
80 static void ll_options(char *options, char **ost, char **mds, int *flags)
90 for (this_char = strtok (options, ",");
92 this_char = strtok (NULL, ",")) {
93 CDEBUG(D_SUPER, "this_char %s\n", this_char);
94 if ( (!*ost && (*ost = ll_read_opt("osc", this_char)))||
95 (!*mds && (*mds = ll_read_opt("mdc", this_char)))||
96 (!(*flags & LL_SBI_NOLCK) && ((*flags) = (*flags) |
97 ll_set_opt("nolock", this_char, LL_SBI_NOLCK))) )
104 #define log2(n) ffz(~(n))
107 static struct super_block * ll_read_super(struct super_block *sb,
108 void *data, int silent)
110 struct inode *root = 0;
111 struct obd_device *obd;
112 struct ll_sb_info *sbi;
116 struct ll_fid rootfid;
117 struct obd_statfs osfs;
118 struct ptlrpc_request *request = NULL;
119 struct ptlrpc_connection *mdc_conn;
120 struct ll_read_inode2_cookie lic;
124 /* Lprocfs variables */
127 struct lprocfs_vars d_vars[3];
134 OBD_ALLOC(sbi, sizeof(*sbi));
140 INIT_LIST_HEAD(&sbi->ll_conn_chain);
141 INIT_LIST_HEAD(&sbi->ll_orphan_dentry_list);
142 generate_random_uuid(uuid);
143 class_uuid_unparse(uuid, sbi->ll_sb_uuid);
145 sb->u.generic_sbp = sbi;
147 ll_options(data, &osc, &mdc, &sbi->ll_flags);
151 GOTO(out_free, sb = NULL);
156 GOTO(out_free, sb = NULL);
159 obd = class_uuid2obd(mdc);
161 CERROR("MDC %s: not setup or attached\n", mdc);
162 GOTO(out_free, sb = NULL);
165 err = obd_connect(&sbi->ll_mdc_conn, obd, sbi->ll_sb_uuid,
166 ptlrpc_recovd, ll_recover);
168 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
169 GOTO(out_free, sb = NULL);
172 #warning Mike: is this the right place to raise the connection level?
173 mdc_conn = sbi2mdc(sbi)->cl_import.imp_connection;
174 mdc_conn->c_level = LUSTRE_CONN_FULL;
175 list_add(&mdc_conn->c_sb_chain, &sbi->ll_conn_chain);
177 obd = class_uuid2obd(osc);
179 CERROR("OSC %s: not setup or attached\n", osc);
180 GOTO(out_mdc, sb = NULL);
183 err = obd_connect(&sbi->ll_osc_conn, obd, sbi->ll_sb_uuid,
184 ptlrpc_recovd, ll_recover);
186 CERROR("cannot connect to %s: rc = %d\n", osc, err);
187 GOTO(out_mdc, sb = NULL);
190 err = mdc_getstatus(&sbi->ll_mdc_conn, &rootfid);
192 CERROR("cannot mds_connect: rc = %d\n", err);
193 GOTO(out_mdc, sb = NULL);
195 CDEBUG(D_SUPER, "rootfid "LPU64"\n", rootfid.id);
196 sbi->ll_rootino = rootfid.id;
198 memset(&osfs, 0, sizeof(osfs));
199 err = obd_statfs(&sbi->ll_mdc_conn, &osfs);
200 sb->s_blocksize = osfs.os_bsize;
201 sb->s_blocksize_bits = log2(osfs.os_bsize);
202 sb->s_magic = LL_SUPER_MAGIC;
203 sb->s_maxbytes = (1ULL << (32 + 9)) - osfs.os_bsize;
205 sb->s_op = &ll_super_operations;
207 /* make root inode */
208 err = mdc_getattr(&sbi->ll_mdc_conn, sbi->ll_rootino, S_IFDIR,
209 OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 0, &request);
211 CERROR("mdc_getattr failed for root: rc = %d\n", err);
212 GOTO(out_request, sb = NULL);
215 /* initialize committed transaction callback daemon */
216 spin_lock_init(&sbi->ll_commitcbd_lock);
217 init_waitqueue_head(&sbi->ll_commitcbd_waitq);
218 init_waitqueue_head(&sbi->ll_commitcbd_ctl_waitq);
219 sbi->ll_commitcbd_flags = 0;
220 err = ll_commitcbd_setup(sbi);
222 CERROR("failed to start commit callback daemon: rc = %d\n",err);
223 GOTO(out_request, sb = NULL);
226 lic.lic_body = lustre_msg_buf(request->rq_repmsg, 0);
228 LASSERT(sbi->ll_rootino != 0);
229 root = iget4(sb, sbi->ll_rootino, NULL, &lic);
232 sb->s_root = d_alloc_root(root);
234 CERROR("lustre_lite: bad iget4 for root\n");
235 GOTO(out_cdb, sb = NULL);
238 ptlrpc_req_finished(request);
242 /* Register this mount instance with LProcFS */
244 snprintf(mnt_name, 100, "mount_%s", sbi->ll_sb_uuid);
245 sbi->ll_mnt_root=lprocfs_reg_mnt(mnt_name);
246 if(!sbi->ll_mnt_root)
249 /* Add the static configuration info */
250 lprocfs_add_vars(sbi->ll_mnt_root,
251 (struct lprocfs_vars*)status_var_nm_1,
254 /* Add the dynamic configuration stuff */
257 obd = class_uuid2obd(mdc);
261 sprintf(mnt_name, "status/%s/common_name", obd->obd_type->typ_name);
263 memset(d_vars, 0, sizeof(d_vars));
264 d_vars[0].read_fptr=rd_dev_name;
265 d_vars[0].write_fptr=0;
266 d_vars[0].name=(char*)mnt_name;
269 snprintf(uuid_name, strlen(uuid_name), "status/%s/uuid",
270 obd->obd_type->typ_name);
271 d_vars[1].read_fptr=rd_dev_uuid;
272 d_vars[1].write_fptr=0;
273 d_vars[1].name=(char*)uuid_name;
275 err=lprocfs_add_vars(sbi->ll_mnt_root, (struct lprocfs_vars* )d_vars,
278 CDEBUG(D_OTHER, "Unable to add fs proc dynamic variables");
283 obd = class_uuid2obd(osc);
286 snprintf(mnt_name, strlen(mnt_name), "status/%s/common_name",
287 obd->obd_type->typ_name);
289 memset(d_vars, 0, sizeof(d_vars));
290 d_vars[0].read_fptr=rd_dev_name;
291 d_vars[0].write_fptr=0;
292 d_vars[0].name=(char*)mnt_name;
295 snprintf(uuid_name, strlen(uuid_name), "status/%s/uuid",
296 obd->obd_type->typ_name);
297 d_vars[1].read_fptr=rd_dev_uuid;
298 d_vars[1].write_fptr=0;
299 d_vars[1].name=(char*)uuid_name;
301 err=lprocfs_add_vars(sbi->ll_mnt_root, (struct lprocfs_vars* )d_vars,
304 CDEBUG(D_OTHER, "Unable to add fs proc dynamic variables");
308 OBD_FREE(mdc, strlen(mdc) + 1);
310 OBD_FREE(osc, strlen(osc) + 1);
315 ll_commitcbd_cleanup(sbi);
317 ptlrpc_req_finished(request);
318 obd_disconnect(&sbi->ll_osc_conn);
320 obd_disconnect(&sbi->ll_mdc_conn);
322 OBD_FREE(sbi, sizeof(*sbi));
326 } /* ll_read_super */
328 static void ll_put_super(struct super_block *sb)
330 struct ll_sb_info *sbi = ll_s2sbi(sb);
331 struct list_head *tmp, *next;
332 struct ll_fid rootfid;
335 list_del(&sbi->ll_conn_chain);
336 ll_commitcbd_cleanup(sbi);
337 obd_disconnect(&sbi->ll_osc_conn);
339 /* NULL request to force sync on the MDS, and get the last_committed
340 * value to flush remaining RPCs from the sending queue on client.
342 * XXX This should be an mdc_sync() call to sync the whole MDS fs,
343 * which we can call for other reasons as well.
345 mdc_getstatus(&sbi->ll_mdc_conn, &rootfid);
347 lprocfs_dereg_mnt(sbi->ll_mnt_root);
348 obd_disconnect(&sbi->ll_mdc_conn);
350 spin_lock(&dcache_lock);
351 list_for_each_safe(tmp, next, &sbi->ll_orphan_dentry_list) {
352 struct dentry *dentry = list_entry(tmp, struct dentry, d_hash);
353 shrink_dcache_parent(dentry);
355 spin_unlock(&dcache_lock);
357 OBD_FREE(sbi, sizeof(*sbi));
363 static void ll_clear_inode(struct inode *inode)
365 struct ll_sb_info *sbi = ll_i2sbi(inode);
369 rc = mdc_cancel_unused(&sbi->ll_mdc_conn, inode, LDLM_FL_NO_CALLBACK);
371 CERROR("obd_cancel_unused: %d\n", rc);
372 /* XXX FIXME do something dramatic */
375 if (atomic_read(&inode->i_count) == 0) {
376 struct ll_inode_info *lli = ll_i2info(inode);
377 struct lov_stripe_md *lsm = lli->lli_smd;
378 char *symlink_name = lli->lli_symlink_name;
381 OBD_FREE(lsm, ll_ost_easize(inode->i_sb));
385 OBD_FREE(symlink_name, strlen(symlink_name) + 1);
386 lli->lli_symlink_name = NULL;
393 static void ll_delete_inode(struct inode *inode)
396 if (S_ISREG(inode->i_mode)) {
399 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
404 if (lsm->lsm_object_id == 0) {
405 CERROR("This really happens\n");
406 /* No obdo was ever created */
414 oa->o_id = lsm->lsm_object_id;
415 oa->o_easize = ll_mds_easize(inode->i_sb);
416 oa->o_mode = inode->i_mode;
417 oa->o_valid = OBD_MD_FLID | OBD_MD_FLEASIZE | OBD_MD_FLTYPE;
419 err = obd_destroy(ll_i2obdconn(inode), oa, lsm);
421 CDEBUG(D_SUPER, "obd destroy of objid "LPX64" error %d\n",
422 lsm->lsm_object_id, err);
429 /* like inode_setattr, but doesn't mark the inode dirty */
430 static int ll_attr2inode(struct inode * inode, struct iattr * attr, int trunc)
432 unsigned int ia_valid = attr->ia_valid;
435 if ((ia_valid & ATTR_SIZE) && trunc) {
436 error = vmtruncate(inode, attr->ia_size);
439 } else if (ia_valid & ATTR_SIZE)
440 inode->i_size = attr->ia_size;
442 if (ia_valid & ATTR_UID)
443 inode->i_uid = attr->ia_uid;
444 if (ia_valid & ATTR_GID)
445 inode->i_gid = attr->ia_gid;
446 if (ia_valid & ATTR_ATIME)
447 inode->i_atime = attr->ia_atime;
448 if (ia_valid & ATTR_MTIME)
449 inode->i_mtime = attr->ia_mtime;
450 if (ia_valid & ATTR_CTIME)
451 inode->i_ctime = attr->ia_ctime;
452 if (ia_valid & ATTR_MODE) {
453 inode->i_mode = attr->ia_mode;
454 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
455 inode->i_mode &= ~S_ISGID;
461 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
463 struct ptlrpc_request *request = NULL;
464 struct ll_sb_info *sbi = ll_i2sbi(inode);
469 /* change incore inode */
470 ll_attr2inode(inode, attr, do_trunc);
472 err = mdc_setattr(&sbi->ll_mdc_conn, inode, attr, &request);
474 CERROR("mdc_setattr fails (%d)\n", err);
476 ptlrpc_req_finished(request);
481 int ll_setattr(struct dentry *de, struct iattr *attr)
483 int rc = inode_change_ok(de->d_inode, attr);
488 return ll_inode_setattr(de->d_inode, attr, 1);
491 static int ll_statfs(struct super_block *sb, struct statfs *sfs)
493 struct ll_sb_info *sbi = ll_s2sbi(sb);
494 struct obd_statfs osfs;
498 memset(sfs, 0, sizeof(*sfs));
499 rc = obd_statfs(&sbi->ll_mdc_conn, &osfs);
500 statfs_unpack(sfs, &osfs);
502 CERROR("mdc_statfs fails: rc = %d\n", rc);
504 CDEBUG(D_SUPER, "mdc_statfs shows blocks "LPU64"/"LPU64
505 " objects "LPU64"/"LPU64"\n",
506 osfs.os_bavail, osfs.os_blocks,
507 osfs.os_ffree, osfs.os_files);
509 /* temporary until mds_statfs returns statfs info for all OSTs */
511 rc = obd_statfs(&sbi->ll_osc_conn, &osfs);
513 CERROR("obd_statfs fails: rc = %d\n", rc);
516 CDEBUG(D_SUPER, "obd_statfs shows blocks "LPU64"/"LPU64
517 " objects "LPU64"/"LPU64"\n",
518 osfs.os_bavail, osfs.os_blocks,
519 osfs.os_ffree, osfs.os_files);
521 while (osfs.os_blocks > ~0UL) {
524 osfs.os_blocks >>= 1;
526 osfs.os_bavail >>= 1;
528 sfs->f_blocks = osfs.os_blocks;
529 sfs->f_bfree = osfs.os_bfree;
530 sfs->f_bavail = osfs.os_bavail;
531 if (osfs.os_ffree < (__u64)sfs->f_ffree)
532 sfs->f_ffree = osfs.os_ffree;
539 void ll_update_inode(struct inode *inode, struct mds_body *body)
541 if (body->valid & OBD_MD_FLID)
542 inode->i_ino = body->ino;
543 if (body->valid & OBD_MD_FLATIME)
544 inode->i_atime = body->atime;
545 if (body->valid & OBD_MD_FLMTIME)
546 inode->i_mtime = body->mtime;
547 if (body->valid & OBD_MD_FLCTIME)
548 inode->i_ctime = body->ctime;
549 if (body->valid & OBD_MD_FLMODE)
550 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
551 if (body->valid & OBD_MD_FLTYPE)
552 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
553 if (body->valid & OBD_MD_FLUID)
554 inode->i_uid = body->uid;
555 if (body->valid & OBD_MD_FLGID)
556 inode->i_gid = body->gid;
557 if (body->valid & OBD_MD_FLFLAGS)
558 inode->i_flags = body->flags;
559 if (body->valid & OBD_MD_FLNLINK)
560 inode->i_nlink = body->nlink;
561 if (body->valid & OBD_MD_FLGENER)
562 inode->i_generation = body->generation;
563 if (body->valid & OBD_MD_FLRDEV)
564 inode->i_rdev = body->rdev;
565 if (body->valid & OBD_MD_FLSIZE)
566 inode->i_size = body->size;
569 static void ll_read_inode2(struct inode *inode, void *opaque)
571 struct ll_read_inode2_cookie *lic = opaque;
572 struct mds_body *body = lic->lic_body;
573 struct ll_inode_info *lli = ll_i2info(inode);
576 sema_init(&lli->lli_open_sem, 1);
578 /* core attributes first */
579 ll_update_inode(inode, body);
581 //if (body->valid & OBD_MD_FLEASIZE)
582 if (lic && lic->lic_lmm) {
583 struct lov_mds_md *lmm = lic->lic_lmm;
586 /* XXX This should probably not be an error in the future,
587 * when we allow LOV OSTs to be added.
589 if (lmm->lmm_easize != ll_mds_easize(inode->i_sb)) {
590 CERROR("Striping metadata size error %ld\n",
594 size = ll_ost_easize(inode->i_sb);
595 OBD_ALLOC(lli->lli_smd, size);
597 CERROR("No memory for %d\n", size);
600 lov_unpackmd(lli->lli_smd, lmm);
605 /* Get the authoritative file size */
606 if (lli->lli_smd && (inode->i_mode & S_IFREG)) {
609 rc = ll_file_size(inode, lli->lli_smd);
611 CERROR("ll_file_size: %d\n", rc);
612 /* FIXME: need to somehow prevent inode creation */
617 /* OIDEBUG(inode); */
619 if (S_ISREG(inode->i_mode)) {
620 inode->i_op = &ll_file_inode_operations;
621 inode->i_fop = &ll_file_operations;
622 inode->i_mapping->a_ops = &ll_aops;
624 } else if (S_ISDIR(inode->i_mode)) {
625 inode->i_op = &ll_dir_inode_operations;
626 inode->i_fop = &ll_dir_operations;
627 inode->i_mapping->a_ops = &ll_dir_aops;
629 } else if (S_ISLNK(inode->i_mode)) {
630 inode->i_op = &ll_fast_symlink_inode_operations;
633 init_special_inode(inode, inode->i_mode, inode->i_rdev);
638 static inline void invalidate_request_list(struct list_head *req_list)
640 struct list_head *tmp, *n;
641 list_for_each_safe(tmp, n, req_list) {
642 struct ptlrpc_request *req =
643 list_entry(tmp, struct ptlrpc_request, rq_list);
644 CERROR("invalidating req xid "LPD64" op %d to %s:%d\n",
645 (unsigned long long)req->rq_xid, req->rq_reqmsg->opc,
646 req->rq_connection->c_remote_uuid,
647 req->rq_import->imp_client->cli_request_portal);
648 req->rq_flags |= PTL_RPC_FL_ERR;
649 wake_up(&req->rq_wait_for_rep);
653 void ll_umount_begin(struct super_block *sb)
655 struct ll_sb_info *sbi = ll_s2sbi(sb);
656 struct list_head *ctmp;
660 list_for_each(ctmp, &sbi->ll_conn_chain) {
661 struct ptlrpc_connection *conn;
662 conn = list_entry(ctmp, struct ptlrpc_connection, c_sb_chain);
664 spin_lock(&conn->c_lock);
665 /* XXX should just be dealing with imports, probably through
666 * XXX iocontrol, need next-gen recovery! */
667 conn->c_flags |= CONN_INVALID;
668 invalidate_request_list(&conn->c_sending_head);
669 invalidate_request_list(&conn->c_delayed_head);
670 spin_unlock(&conn->c_lock);
676 /* exported operations */
677 struct super_operations ll_super_operations =
679 read_inode2: ll_read_inode2,
680 clear_inode: ll_clear_inode,
681 delete_inode: ll_delete_inode,
682 put_super: ll_put_super,
684 umount_begin: ll_umount_begin
687 struct file_system_type lustre_lite_fs_type = {
688 "lustre_lite", 0, ll_read_super, NULL
691 static int __init init_lustre_lite(void)
693 printk(KERN_INFO "Lustre Lite 0.5.14, info@clusterfs.com\n");
694 ll_file_data_slab = kmem_cache_create("ll_file_data",
695 sizeof(struct ll_file_data), 0,
696 SLAB_HWCACHE_ALIGN, NULL, NULL);
697 if (ll_file_data_slab == NULL)
699 return register_filesystem(&lustre_lite_fs_type);
702 static void __exit exit_lustre_lite(void)
704 unregister_filesystem(&lustre_lite_fs_type);
705 kmem_cache_destroy(ll_file_data_slab);
708 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
709 MODULE_DESCRIPTION("Lustre Lite Client File System v1.0");
710 MODULE_LICENSE("GPL");
712 module_init(init_lustre_lite);
713 module_exit(exit_lustre_lite);