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>
25 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
26 kmem_cache_t *ll_file_data_slab;
27 extern struct address_space_operations ll_aops;
28 extern struct address_space_operations ll_dir_aops;
29 struct super_operations ll_super_operations;
31 extern int ll_recover(struct recovd_data *, int);
32 extern int ll_commitcbd_setup(struct ll_sb_info *);
33 extern int ll_commitcbd_cleanup(struct ll_sb_info *);
35 extern void ll_proc_namespace(struct super_block* sb, char* osc, char* mdc);
37 static char *ll_read_opt(const char *opt, char *data)
43 CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
44 if ( strncmp(opt, data, strlen(opt)) )
46 if ( (value = strchr(data, '=')) == NULL )
50 OBD_ALLOC(retval, strlen(value) + 1);
52 CERROR("out of memory!\n");
56 memcpy(retval, value, strlen(value)+1);
57 CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
61 static int ll_set_opt(const char *opt, char *data, int fl)
65 CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
66 if ( strncmp(opt, data, strlen(opt)) )
72 static void ll_options(char *options, char **ost, char **mds, int *flags)
82 for (this_char = strtok (options, ",");
84 this_char = strtok (NULL, ",")) {
85 CDEBUG(D_SUPER, "this_char %s\n", this_char);
86 if ( (!*ost && (*ost = ll_read_opt("osc", this_char)))||
87 (!*mds && (*mds = ll_read_opt("mdc", this_char)))||
88 (!(*flags & LL_SBI_NOLCK) && ((*flags) = (*flags) |
89 ll_set_opt("nolock", this_char, LL_SBI_NOLCK))) )
96 #define log2(n) ffz(~(n))
99 static struct super_block * ll_read_super(struct super_block *sb,
100 void *data, int silent)
102 struct inode *root = 0;
103 struct obd_device *obd;
104 struct ll_sb_info *sbi;
108 struct ll_fid rootfid;
109 struct obd_statfs osfs;
110 struct ptlrpc_request *request = NULL;
111 struct ptlrpc_connection *mdc_conn;
112 struct ll_read_inode2_cookie lic;
120 OBD_ALLOC(sbi, sizeof(*sbi));
126 INIT_LIST_HEAD(&sbi->ll_conn_chain);
127 INIT_LIST_HEAD(&sbi->ll_orphan_dentry_list);
128 generate_random_uuid(uuid);
129 class_uuid_unparse(uuid, sbi->ll_sb_uuid);
131 sb->u.generic_sbp = sbi;
133 ll_options(data, &osc, &mdc, &sbi->ll_flags);
137 GOTO(out_free, sb = NULL);
142 GOTO(out_free, sb = NULL);
145 obd = class_uuid2obd(mdc);
147 CERROR("MDC %s: not setup or attached\n", mdc);
148 GOTO(out_free, sb = NULL);
151 err = obd_connect(&sbi->ll_mdc_conn, obd, sbi->ll_sb_uuid,
152 ptlrpc_recovd, ll_recover);
154 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
155 GOTO(out_free, sb = NULL);
158 #warning Mike: is this the right place to raise the connection level?
159 mdc_conn = sbi2mdc(sbi)->cl_import.imp_connection;
160 mdc_conn->c_level = LUSTRE_CONN_FULL;
161 list_add(&mdc_conn->c_sb_chain, &sbi->ll_conn_chain);
163 obd = class_uuid2obd(osc);
165 CERROR("OSC %s: not setup or attached\n", osc);
166 GOTO(out_mdc, sb = NULL);
169 err = obd_connect(&sbi->ll_osc_conn, obd, sbi->ll_sb_uuid,
170 ptlrpc_recovd, ll_recover);
172 CERROR("cannot connect to %s: rc = %d\n", osc, err);
173 GOTO(out_mdc, sb = NULL);
176 err = mdc_getstatus(&sbi->ll_mdc_conn, &rootfid);
178 CERROR("cannot mds_connect: rc = %d\n", err);
179 GOTO(out_mdc, sb = NULL);
181 CDEBUG(D_SUPER, "rootfid "LPU64"\n", rootfid.id);
182 sbi->ll_rootino = rootfid.id;
184 memset(&osfs, 0, sizeof(osfs));
185 err = obd_statfs(&sbi->ll_mdc_conn, &osfs);
186 sb->s_blocksize = osfs.os_bsize;
187 sb->s_blocksize_bits = log2(osfs.os_bsize);
188 sb->s_magic = LL_SUPER_MAGIC;
189 sb->s_maxbytes = (1ULL << (32 + 9)) - osfs.os_bsize;
191 sb->s_op = &ll_super_operations;
193 /* make root inode */
194 err = mdc_getattr(&sbi->ll_mdc_conn, sbi->ll_rootino, S_IFDIR,
195 OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 0, &request);
197 CERROR("mdc_getattr failed for root: rc = %d\n", err);
198 GOTO(out_request, sb = NULL);
201 /* initialize committed transaction callback daemon */
202 spin_lock_init(&sbi->ll_commitcbd_lock);
203 init_waitqueue_head(&sbi->ll_commitcbd_waitq);
204 init_waitqueue_head(&sbi->ll_commitcbd_ctl_waitq);
205 sbi->ll_commitcbd_flags = 0;
206 err = ll_commitcbd_setup(sbi);
208 CERROR("failed to start commit callback daemon: rc = %d\n",err);
209 GOTO(out_request, sb = NULL);
212 lic.lic_body = lustre_msg_buf(request->rq_repmsg, 0);
214 LASSERT(sbi->ll_rootino != 0);
215 root = iget4(sb, sbi->ll_rootino, NULL, &lic);
218 sb->s_root = d_alloc_root(root);
220 CERROR("lustre_lite: bad iget4 for root\n");
221 GOTO(out_cdb, sb = NULL);
224 ptlrpc_req_finished(request);
226 ll_proc_namespace(sb, osc, mdc);
230 OBD_FREE(mdc, strlen(mdc) + 1);
232 OBD_FREE(osc, strlen(osc) + 1);
237 ll_commitcbd_cleanup(sbi);
239 ptlrpc_req_finished(request);
240 obd_disconnect(&sbi->ll_osc_conn);
242 obd_disconnect(&sbi->ll_mdc_conn);
244 OBD_FREE(sbi, sizeof(*sbi));
248 } /* ll_read_super */
250 static void ll_put_super(struct super_block *sb)
252 struct ll_sb_info *sbi = ll_s2sbi(sb);
253 struct list_head *tmp, *next;
254 struct ll_fid rootfid;
257 list_del(&sbi->ll_conn_chain);
258 ll_commitcbd_cleanup(sbi);
259 obd_disconnect(&sbi->ll_osc_conn);
261 /* NULL request to force sync on the MDS, and get the last_committed
262 * value to flush remaining RPCs from the sending queue on client.
264 * XXX This should be an mdc_sync() call to sync the whole MDS fs,
265 * which we can call for other reasons as well.
267 mdc_getstatus(&sbi->ll_mdc_conn, &rootfid);
269 lprocfs_dereg_mnt(sbi->ll_proc_root);
272 obd_disconnect(&sbi->ll_mdc_conn);
274 spin_lock(&dcache_lock);
275 list_for_each_safe(tmp, next, &sbi->ll_orphan_dentry_list) {
276 struct dentry *dentry = list_entry(tmp, struct dentry, d_hash);
277 shrink_dcache_parent(dentry);
279 spin_unlock(&dcache_lock);
281 OBD_FREE(sbi, sizeof(*sbi));
287 static void ll_clear_inode(struct inode *inode)
289 struct ll_sb_info *sbi = ll_i2sbi(inode);
290 struct ll_inode_info *lli = ll_i2info(inode);
294 rc = mdc_cancel_unused(&sbi->ll_mdc_conn, inode, LDLM_FL_NO_CALLBACK);
296 CERROR("mdc_cancel_unused: %d\n", rc);
297 /* XXX FIXME do something dramatic */
301 rc = obd_cancel_unused(&sbi->ll_osc_conn, lli->lli_smd,
302 LDLM_FL_NO_CALLBACK);
304 CERROR("obd_cancel_unused: %d\n", rc);
305 /* XXX FIXME do something dramatic */
309 if (atomic_read(&inode->i_count) == 0) {
310 struct lov_stripe_md *lsm = lli->lli_smd;
311 char *symlink_name = lli->lli_symlink_name;
314 OBD_FREE(lsm, ll_ost_easize(inode->i_sb));
318 OBD_FREE(symlink_name, strlen(symlink_name) + 1);
319 lli->lli_symlink_name = NULL;
326 static void ll_delete_inode(struct inode *inode)
329 if (S_ISREG(inode->i_mode)) {
332 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
337 if (lsm->lsm_object_id == 0) {
338 CERROR("This really happens\n");
339 /* No obdo was ever created */
347 oa->o_id = lsm->lsm_object_id;
348 oa->o_easize = ll_mds_easize(inode->i_sb);
349 oa->o_mode = inode->i_mode;
350 oa->o_valid = OBD_MD_FLID | OBD_MD_FLEASIZE | OBD_MD_FLTYPE;
352 err = obd_destroy(ll_i2obdconn(inode), oa, lsm);
354 CDEBUG(D_SUPER, "obd destroy of objid "LPX64" error %d\n",
355 lsm->lsm_object_id, err);
362 /* like inode_setattr, but doesn't mark the inode dirty */
363 static int ll_attr2inode(struct inode * inode, struct iattr * attr, int trunc)
365 unsigned int ia_valid = attr->ia_valid;
368 if ((ia_valid & ATTR_SIZE) && trunc) {
369 error = vmtruncate(inode, attr->ia_size);
372 } else if (ia_valid & ATTR_SIZE)
373 inode->i_size = attr->ia_size;
375 if (ia_valid & ATTR_UID)
376 inode->i_uid = attr->ia_uid;
377 if (ia_valid & ATTR_GID)
378 inode->i_gid = attr->ia_gid;
379 if (ia_valid & ATTR_ATIME)
380 inode->i_atime = attr->ia_atime;
381 if (ia_valid & ATTR_MTIME)
382 inode->i_mtime = attr->ia_mtime;
383 if (ia_valid & ATTR_CTIME)
384 inode->i_ctime = attr->ia_ctime;
385 if (ia_valid & ATTR_MODE) {
386 inode->i_mode = attr->ia_mode;
387 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
388 inode->i_mode &= ~S_ISGID;
394 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
396 struct ptlrpc_request *request = NULL;
397 struct ll_sb_info *sbi = ll_i2sbi(inode);
402 /* change incore inode */
403 ll_attr2inode(inode, attr, do_trunc);
405 err = mdc_setattr(&sbi->ll_mdc_conn, inode, attr, &request);
407 CERROR("mdc_setattr fails (%d)\n", err);
409 ptlrpc_req_finished(request);
414 int ll_setattr(struct dentry *de, struct iattr *attr)
416 int rc = inode_change_ok(de->d_inode, attr);
421 return ll_inode_setattr(de->d_inode, attr, 1);
424 static int ll_statfs(struct super_block *sb, struct statfs *sfs)
426 struct ll_sb_info *sbi = ll_s2sbi(sb);
427 struct obd_statfs osfs;
431 memset(sfs, 0, sizeof(*sfs));
432 rc = obd_statfs(&sbi->ll_mdc_conn, &osfs);
433 statfs_unpack(sfs, &osfs);
435 CERROR("mdc_statfs fails: rc = %d\n", rc);
437 CDEBUG(D_SUPER, "mdc_statfs shows blocks "LPU64"/"LPU64
438 " objects "LPU64"/"LPU64"\n",
439 osfs.os_bavail, osfs.os_blocks,
440 osfs.os_ffree, osfs.os_files);
442 /* temporary until mds_statfs returns statfs info for all OSTs */
444 rc = obd_statfs(&sbi->ll_osc_conn, &osfs);
446 CERROR("obd_statfs fails: rc = %d\n", rc);
449 CDEBUG(D_SUPER, "obd_statfs shows blocks "LPU64"/"LPU64
450 " objects "LPU64"/"LPU64"\n",
451 osfs.os_bavail, osfs.os_blocks,
452 osfs.os_ffree, osfs.os_files);
454 while (osfs.os_blocks > ~0UL) {
457 osfs.os_blocks >>= 1;
459 osfs.os_bavail >>= 1;
461 sfs->f_blocks = osfs.os_blocks;
462 sfs->f_bfree = osfs.os_bfree;
463 sfs->f_bavail = osfs.os_bavail;
464 if (osfs.os_ffree < (__u64)sfs->f_ffree)
465 sfs->f_ffree = osfs.os_ffree;
472 void ll_update_inode(struct inode *inode, struct mds_body *body)
474 if (body->valid & OBD_MD_FLID)
475 inode->i_ino = body->ino;
476 if (body->valid & OBD_MD_FLATIME)
477 inode->i_atime = body->atime;
478 if (body->valid & OBD_MD_FLMTIME)
479 inode->i_mtime = body->mtime;
480 if (body->valid & OBD_MD_FLCTIME)
481 inode->i_ctime = body->ctime;
482 if (body->valid & OBD_MD_FLMODE)
483 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
484 if (body->valid & OBD_MD_FLTYPE)
485 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
486 if (body->valid & OBD_MD_FLUID)
487 inode->i_uid = body->uid;
488 if (body->valid & OBD_MD_FLGID)
489 inode->i_gid = body->gid;
490 if (body->valid & OBD_MD_FLFLAGS)
491 inode->i_flags = body->flags;
492 if (body->valid & OBD_MD_FLNLINK)
493 inode->i_nlink = body->nlink;
494 if (body->valid & OBD_MD_FLGENER)
495 inode->i_generation = body->generation;
496 if (body->valid & OBD_MD_FLRDEV)
497 inode->i_rdev = body->rdev;
498 if (body->valid & OBD_MD_FLSIZE)
499 inode->i_size = body->size;
502 static void ll_read_inode2(struct inode *inode, void *opaque)
504 struct ll_read_inode2_cookie *lic = opaque;
505 struct mds_body *body = lic->lic_body;
506 struct ll_inode_info *lli = ll_i2info(inode);
509 sema_init(&lli->lli_open_sem, 1);
510 atomic_set(&lli->lli_open_count, 0);
512 /* core attributes first */
513 ll_update_inode(inode, body);
515 //if (body->valid & OBD_MD_FLEASIZE)
516 if (lic && lic->lic_lmm) {
517 struct lov_mds_md *lmm = lic->lic_lmm;
520 /* XXX This should probably not be an error in the future,
521 * when we allow LOV OSTs to be added.
523 if (lmm->lmm_easize != ll_mds_easize(inode->i_sb)) {
524 CERROR("Striping metadata size error %ld\n",
528 size = ll_ost_easize(inode->i_sb);
529 OBD_ALLOC(lli->lli_smd, size);
531 CERROR("No memory for %d\n", size);
534 lov_unpackmd(lli->lli_smd, lmm);
539 /* Get the authoritative file size */
540 if (lli->lli_smd && (inode->i_mode & S_IFREG)) {
543 rc = ll_file_size(inode, lli->lli_smd);
545 CERROR("ll_file_size: %d\n", rc);
546 /* FIXME: need to somehow prevent inode creation */
551 /* OIDEBUG(inode); */
553 if (S_ISREG(inode->i_mode)) {
554 inode->i_op = &ll_file_inode_operations;
555 inode->i_fop = &ll_file_operations;
556 inode->i_mapping->a_ops = &ll_aops;
558 } else if (S_ISDIR(inode->i_mode)) {
559 inode->i_op = &ll_dir_inode_operations;
560 inode->i_fop = &ll_dir_operations;
561 inode->i_mapping->a_ops = &ll_dir_aops;
563 } else if (S_ISLNK(inode->i_mode)) {
564 inode->i_op = &ll_fast_symlink_inode_operations;
567 init_special_inode(inode, inode->i_mode, inode->i_rdev);
572 static inline void invalidate_request_list(struct list_head *req_list)
574 struct list_head *tmp, *n;
575 list_for_each_safe(tmp, n, req_list) {
576 struct ptlrpc_request *req =
577 list_entry(tmp, struct ptlrpc_request, rq_list);
578 CERROR("invalidating req xid "LPD64" op %d to %s:%d\n",
579 (unsigned long long)req->rq_xid, req->rq_reqmsg->opc,
580 req->rq_connection->c_remote_uuid,
581 req->rq_import->imp_client->cli_request_portal);
582 req->rq_flags |= PTL_RPC_FL_ERR;
583 wake_up(&req->rq_wait_for_rep);
587 void ll_umount_begin(struct super_block *sb)
589 struct ll_sb_info *sbi = ll_s2sbi(sb);
590 struct list_head *ctmp;
594 list_for_each(ctmp, &sbi->ll_conn_chain) {
595 struct ptlrpc_connection *conn;
596 conn = list_entry(ctmp, struct ptlrpc_connection, c_sb_chain);
598 spin_lock(&conn->c_lock);
599 /* XXX should just be dealing with imports, probably through
600 * XXX iocontrol, need next-gen recovery! */
601 conn->c_flags |= CONN_INVALID;
602 invalidate_request_list(&conn->c_sending_head);
603 invalidate_request_list(&conn->c_delayed_head);
604 spin_unlock(&conn->c_lock);
610 /* exported operations */
611 struct super_operations ll_super_operations =
613 read_inode2: ll_read_inode2,
614 clear_inode: ll_clear_inode,
615 delete_inode: ll_delete_inode,
616 put_super: ll_put_super,
618 umount_begin: ll_umount_begin
621 struct file_system_type lustre_lite_fs_type = {
622 "lustre_lite", 0, ll_read_super, NULL
625 static int __init init_lustre_lite(void)
627 printk(KERN_INFO "Lustre Lite 0.5.14, info@clusterfs.com\n");
628 ll_file_data_slab = kmem_cache_create("ll_file_data",
629 sizeof(struct ll_file_data), 0,
630 SLAB_HWCACHE_ALIGN, NULL, NULL);
631 if (ll_file_data_slab == NULL)
633 return register_filesystem(&lustre_lite_fs_type);
636 static void __exit exit_lustre_lite(void)
638 unregister_filesystem(&lustre_lite_fs_type);
639 kmem_cache_destroy(ll_file_data_slab);
642 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
643 MODULE_DESCRIPTION("Lustre Lite Client File System v1.0");
644 MODULE_LICENSE("GPL");
646 module_init(init_lustre_lite);
647 module_exit(exit_lustre_lite);