1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Lustre Light Super operations
6 * Copyright (c) 2002, 2003 Cluster File Systems, Inc.
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define DEBUG_SUBSYSTEM S_LLITE
26 #include <linux/module.h>
27 #include <linux/random.h>
28 #include <linux/version.h>
29 #include <linux/lustre_lite.h>
30 #include <linux/lustre_ha.h>
31 #include <linux/lustre_dlm.h>
32 #include <linux/init.h>
34 #include <linux/lprocfs_status.h>
36 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
37 #include <asm/statfs.h>
38 kmem_cache_t *ll_file_data_slab;
39 extern struct address_space_operations ll_aops;
40 extern struct address_space_operations ll_dir_aops;
41 struct super_operations ll_super_operations;
43 /* /proc/lustre/llite root that tracks llite mount points */
44 struct proc_dir_entry *proc_lustre_fs_root = NULL;
46 extern int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
47 struct super_block *sb,
48 char *osc, char *mdc);
50 extern int ll_init_inodecache(void);
51 extern void ll_destroy_inodecache(void);
52 extern int ll_recover(struct recovd_data *, int);
53 extern int ll_commitcbd_setup(struct ll_sb_info *);
54 extern int ll_commitcbd_cleanup(struct ll_sb_info *);
55 int ll_read_inode2(struct inode *inode, void *opaque);
57 extern int ll_proc_namespace(struct super_block* sb, char* osc, char* mdc);
59 static char *ll_read_opt(const char *opt, char *data)
65 CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
66 if (strncmp(opt, data, strlen(opt)))
68 if ((value = strchr(data, '=')) == NULL)
72 OBD_ALLOC(retval, strlen(value) + 1);
74 CERROR("out of memory!\n");
78 memcpy(retval, value, strlen(value)+1);
79 CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
83 static int ll_set_opt(const char *opt, char *data, int fl)
87 CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
88 if (strncmp(opt, data, strlen(opt)))
94 static void ll_options(char *options, char **ost, char **mds, int *flags)
96 char *opt_ptr = options;
105 while ((this_char = strsep (&opt_ptr, ",")) != NULL) {
106 CDEBUG(D_SUPER, "this_char %s\n", this_char);
107 if ((!*ost && (*ost = ll_read_opt("osc", this_char)))||
108 (!*mds && (*mds = ll_read_opt("mdc", this_char)))||
109 (!(*flags & LL_SBI_NOLCK) &&
110 ((*flags) = (*flags) |
111 ll_set_opt("nolock", this_char, LL_SBI_NOLCK))))
118 #define log2(n) ffz(~(n))
122 static int ll_fill_super(struct super_block *sb, void *data, int silent)
124 struct inode *root = 0;
125 struct obd_device *obd;
126 struct ll_sb_info *sbi;
130 struct ll_fid rootfid;
131 struct obd_statfs osfs;
132 struct ptlrpc_request *request = NULL;
133 struct ptlrpc_connection *mdc_conn;
134 struct ll_read_inode2_cookie lic;
136 struct obd_uuid param_uuid;
140 OBD_ALLOC(sbi, sizeof(*sbi));
144 INIT_LIST_HEAD(&sbi->ll_conn_chain);
145 INIT_LIST_HEAD(&sbi->ll_orphan_dentry_list);
146 generate_random_uuid(uuid);
147 class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
151 ll_options(data, &osc, &mdc, &sbi->ll_flags);
155 GOTO(out_free, sb = NULL);
160 GOTO(out_free, sb = NULL);
163 strncpy(param_uuid.uuid, mdc, sizeof(param_uuid.uuid));
164 obd = class_uuid2obd(¶m_uuid);
166 CERROR("MDC %s: not setup or attached\n", mdc);
167 GOTO(out_free, sb = NULL);
170 err = obd_connect(&sbi->ll_mdc_conn, obd, &sbi->ll_sb_uuid,
171 ptlrpc_recovd, ll_recover);
173 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
174 GOTO(out_free, sb = NULL);
177 mdc_conn = sbi2mdc(sbi)->cl_import.imp_connection;
178 list_add(&mdc_conn->c_sb_chain, &sbi->ll_conn_chain);
180 obd = class_uuid2obd(osc);
182 CERROR("OSC %s: not setup or attached\n", osc);
183 GOTO(out_mdc, sb = NULL);
186 err = obd_connect(&sbi->ll_osc_conn, obd, &sbi->ll_sb_uuid,
187 ptlrpc_recovd, ll_recover);
189 CERROR("cannot connect to %s: rc = %d\n", osc, err);
190 GOTO(out_mdc, sb = NULL);
193 err = mdc_getstatus(&sbi->ll_mdc_conn, &rootfid);
195 CERROR("cannot mds_connect: rc = %d\n", err);
196 GOTO(out_mdc, sb = NULL);
198 CDEBUG(D_SUPER, "rootfid "LPU64"\n", rootfid.id);
199 sbi->ll_rootino = rootfid.id;
201 memset(&osfs, 0, sizeof(osfs));
202 err = obd_statfs(&sbi->ll_mdc_conn, &osfs);
203 sb->s_blocksize = osfs.os_bsize;
204 sb->s_blocksize_bits = log2(osfs.os_bsize);
205 sb->s_magic = LL_SUPER_MAGIC;
206 sb->s_maxbytes = (1ULL << (32 + 9)) - osfs.os_bsize;
208 sb->s_op = &ll_super_operations;
210 /* make root inode */
211 err = mdc_getattr(&sbi->ll_mdc_conn, sbi->ll_rootino, S_IFDIR,
212 OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 0, &request);
214 CERROR("mdc_getattr failed for root: rc = %d\n", err);
215 GOTO(out_request, sb = NULL);
218 /* initialize committed transaction callback daemon */
219 spin_lock_init(&sbi->ll_commitcbd_lock);
220 init_waitqueue_head(&sbi->ll_commitcbd_waitq);
221 init_waitqueue_head(&sbi->ll_commitcbd_ctl_waitq);
222 sbi->ll_commitcbd_flags = 0;
223 err = ll_commitcbd_setup(sbi);
225 CERROR("failed to start commit callback daemon: rc = %d\n",err);
226 GOTO(out_request, sb = NULL);
229 lic.lic_body = lustre_msg_buf(request->rq_repmsg, 0);
231 root = iget5_locked(sb, sbi->ll_rootino, NULL,
232 ll_read_inode2, &lic);
235 sb->s_root = d_alloc_root(root);
236 root->i_state &= ~(I_LOCK | I_NEW);
238 CERROR("lustre_lite: bad iget4 for root\n");
239 GOTO(out_cdb, sb = NULL);
242 ptlrpc_req_finished(request);
245 if (proc_lustre_fs_root) {
246 err = lprocfs_register_mountpoint(proc_lustre_fs_root, sb,
249 CERROR("could not register mount in /proc/lustre");
254 OBD_FREE(mdc, strlen(mdc) + 1);
256 OBD_FREE(osc, strlen(osc) + 1);
261 ll_commitcbd_cleanup(sbi);
263 ptlrpc_req_finished(request);
264 obd_disconnect(&sbi->ll_osc_conn);
266 obd_disconnect(&sbi->ll_mdc_conn);
268 OBD_FREE(sbi, sizeof(*sbi));
271 } /* ll_fill_super */
273 struct super_block * ll_get_sb(struct file_system_type *fs_type,
274 int flags, char *devname, void * data)
276 return get_sb_nodev(fs_type, flags, data, ll_fill_super);
279 static void ll_put_super(struct super_block *sb)
281 struct ll_sb_info *sbi = ll_s2sbi(sb);
282 struct list_head *tmp, *next;
283 struct ll_fid rootfid;
286 list_del(&sbi->ll_conn_chain);
287 ll_commitcbd_cleanup(sbi);
288 obd_disconnect(&sbi->ll_osc_conn);
290 /* NULL request to force sync on the MDS, and get the last_committed
291 * value to flush remaining RPCs from the pending queue on client.
293 * XXX This should be an mdc_sync() call to sync the whole MDS fs,
294 * which we can call for other reasons as well.
296 mdc_getstatus(&sbi->ll_mdc_conn, &rootfid);
298 if (sbi->ll_proc_root) {
299 lprocfs_remove(sbi->ll_proc_root);
300 sbi->ll_proc_root = NULL;
303 obd_disconnect(&sbi->ll_mdc_conn);
305 spin_lock(&dcache_lock);
306 list_for_each_safe(tmp, next, &sbi->ll_orphan_dentry_list){
307 struct dentry *dentry = list_entry(tmp, struct dentry, d_hash);
308 shrink_dcache_parent(dentry);
310 spin_unlock(&dcache_lock);
312 OBD_FREE(sbi, sizeof(*sbi));
317 static void ll_clear_inode(struct inode *inode)
319 struct ll_sb_info *sbi = ll_i2sbi(inode);
320 struct ll_inode_info *lli = ll_i2info(inode);
324 #warning "Is there a reason we don't do this in 2.5, but we do in 2.4?"
326 rc = mdc_cancel_unused(&sbi->ll_mdc_conn, inode, LDLM_FL_NO_CALLBACK);
328 CERROR("mdc_cancel_unused: %d\n", rc);
329 /* XXX FIXME do something dramatic */
333 rc = obd_cancel_unused(&sbi->ll_osc_conn, lli->lli_smd, 0);
335 CERROR("obd_cancel_unused: %d\n", rc);
336 /* XXX FIXME do something dramatic */
341 if (atomic_read(&inode->i_count) != 0)
342 CERROR("clearing in-use inode %lu: count = %d\n",
343 inode->i_ino, atomic_read(&inode->i_count));
346 obd_free_memmd(&sbi->ll_osc_conn, &lli->lli_smd);
348 if (lli->lli_symlink_name) {
349 OBD_FREE(lli->lli_symlink_name,strlen(lli->lli_symlink_name)+1);
350 lli->lli_symlink_name = NULL;
357 static void ll_delete_inode(struct inode *inode)
360 if (S_ISREG(inode->i_mode)) {
363 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
365 /* mcreate with no open */
369 if (lsm->lsm_object_id == 0) {
370 CERROR("This really happens\n");
371 /* No obdo was ever created */
379 oa->o_id = lsm->lsm_object_id;
380 oa->o_mode = inode->i_mode;
381 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
383 err = obd_destroy(ll_i2obdconn(inode), oa, lsm);
386 CDEBUG(D_SUPER, "obd destroy objid "LPX64" error %d\n",
387 lsm->lsm_object_id, err);
395 /* like inode_setattr, but doesn't mark the inode dirty */
396 static int ll_attr2inode(struct inode * inode, struct iattr * attr, int trunc)
398 unsigned int ia_valid = attr->ia_valid;
401 if ((ia_valid & ATTR_SIZE) && trunc) {
402 error = vmtruncate(inode, attr->ia_size);
405 } else if (ia_valid & ATTR_SIZE)
406 inode->i_size = attr->ia_size;
408 if (ia_valid & ATTR_UID)
409 inode->i_uid = attr->ia_uid;
410 if (ia_valid & ATTR_GID)
411 inode->i_gid = attr->ia_gid;
412 if (ia_valid & ATTR_ATIME)
413 inode->i_atime = attr->ia_atime;
414 if (ia_valid & ATTR_MTIME)
415 inode->i_mtime = attr->ia_mtime;
416 if (ia_valid & ATTR_CTIME)
417 inode->i_ctime = attr->ia_ctime;
418 if (ia_valid & ATTR_MODE) {
419 inode->i_mode = attr->ia_mode;
420 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
421 inode->i_mode &= ~S_ISGID;
427 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
429 struct ptlrpc_request *request = NULL;
430 struct ll_sb_info *sbi = ll_i2sbi(inode);
435 /* change incore inode */
436 ll_attr2inode(inode, attr, do_trunc);
438 /* Don't send size changes to MDS to avoid "fast EA" problems, and
439 * also avoid a pointless RPC (we get file size from OST anyways).
441 attr->ia_valid &= ~ATTR_SIZE;
442 if (attr->ia_valid) {
443 err = mdc_setattr(&sbi->ll_mdc_conn, inode, attr, NULL, 0,
446 CERROR("mdc_setattr fails: err = %d\n", err);
448 ptlrpc_req_finished(request);
449 if (S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_MTIME_SET) {
450 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
454 CDEBUG(D_ERROR, "setting mtime on OST\n");
455 oa.o_id = lsm->lsm_object_id;
457 oa.o_valid = OBD_MD_FLID |OBD_MD_FLTYPE |OBD_MD_FLMTIME;
458 oa.o_mtime = attr->ia_mtime.tv_sec;
459 err2 = obd_setattr(&sbi->ll_osc_conn, &oa, lsm, NULL);
461 CERROR("obd_setattr fails: rc=%d\n", err);
471 int ll_setattr(struct dentry *de, struct iattr *attr)
473 int rc = inode_change_ok(de->d_inode, attr);
478 return ll_inode_setattr(de->d_inode, attr, 1);
481 static int ll_statfs(struct super_block *sb, struct statfs *sfs)
483 struct ll_sb_info *sbi = ll_s2sbi(sb);
484 struct obd_statfs osfs;
488 memset(sfs, 0, sizeof(*sfs));
489 rc = obd_statfs(&sbi->ll_mdc_conn, &osfs);
490 statfs_unpack(sfs, &osfs);
492 CERROR("mdc_statfs fails: rc = %d\n", rc);
494 CDEBUG(D_SUPER, "mdc_statfs shows blocks "LPU64"/"LPU64
495 " objects "LPU64"/"LPU64"\n",
496 osfs.os_bavail, osfs.os_blocks,
497 osfs.os_ffree, osfs.os_files);
499 /* temporary until mds_statfs returns statfs info for all OSTs */
501 rc = obd_statfs(&sbi->ll_osc_conn, &osfs);
503 CERROR("obd_statfs fails: rc = %d\n", rc);
506 CDEBUG(D_SUPER, "obd_statfs shows blocks "LPU64"/"LPU64
507 " objects "LPU64"/"LPU64"\n",
508 osfs.os_bavail, osfs.os_blocks,
509 osfs.os_ffree, osfs.os_files);
511 while (osfs.os_blocks > ~0UL) {
514 osfs.os_blocks >>= 1;
516 osfs.os_bavail >>= 1;
518 sfs->f_blocks = osfs.os_blocks;
519 sfs->f_bfree = osfs.os_bfree;
520 sfs->f_bavail = osfs.os_bavail;
521 if (osfs.os_ffree < (__u64)sfs->f_ffree)
522 sfs->f_ffree = osfs.os_ffree;
529 void ll_update_inode(struct inode *inode, struct mds_body *body,
530 struct lov_mds_md *lmm)
532 struct ll_inode_info *lli = ll_i2info(inode);
535 obd_unpackmd(ll_i2obdconn(inode), &lli->lli_smd, lmm);
537 if (body->valid & OBD_MD_FLID)
538 inode->i_ino = body->ino;
539 if (body->valid & OBD_MD_FLATIME)
540 inode->i_atime.tv_sec = body->atime;
541 if (body->valid & OBD_MD_FLMTIME)
542 inode->i_mtime.tv_sec = body->mtime;
543 if (body->valid & OBD_MD_FLCTIME)
544 inode->i_ctime.tv_sec = body->ctime;
545 if (body->valid & OBD_MD_FLMODE)
546 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
547 if (body->valid & OBD_MD_FLTYPE)
548 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
549 if (body->valid & OBD_MD_FLUID)
550 inode->i_uid = body->uid;
551 if (body->valid & OBD_MD_FLGID)
552 inode->i_gid = body->gid;
553 if (body->valid & OBD_MD_FLFLAGS)
554 inode->i_flags = body->flags;
555 if (body->valid & OBD_MD_FLNLINK)
556 inode->i_nlink = body->nlink;
557 if (body->valid & OBD_MD_FLGENER)
558 inode->i_generation = body->generation;
559 if (body->valid & OBD_MD_FLRDEV)
560 inode->i_rdev = to_kdev_t(body->rdev);
561 if (body->valid & OBD_MD_FLSIZE)
562 inode->i_size = body->size;
563 if (body->valid & OBD_MD_FLBLOCKS)
564 inode->i_blocks = body->blocks;
567 int ll_read_inode2(struct inode *inode, void *opaque)
569 struct ll_read_inode2_cookie *lic = opaque;
570 struct mds_body *body = lic->lic_body;
571 struct ll_inode_info *lli = ll_i2info(inode);
575 sema_init(&lli->lli_open_sem, 1);
577 init_MUTEX(&lli->lli_getattr_sem);
578 /* these are 2.4 only, but putting them here for consistency.. */
579 spin_lock_init(&lli->lli_read_extent_lock);
580 INIT_LIST_HEAD(&lli->lli_read_extents);
582 LASSERT(!lli->lli_smd);
584 /* core attributes first */
585 ll_update_inode(inode, body, lic ? lic->lic_lmm : NULL);
587 /* Get the authoritative file size */
588 if (lli->lli_smd && S_ISREG(inode->i_mode)) {
589 struct ll_file_data *fd = file->private_data;
590 struct ldlm_extent extent = {0, OBD_OBJECT_EOF};
591 struct lustre_handle lockh = {0, 0};
593 LASSERT(lli->lli_smd->lsm_object_id != 0);
595 rc = ll_extent_lock(fd, inode, lsm, LCK_PR, &extent, &lockh);
596 if (err != ELDLM_OK && err != ELDLM_MATCHED) {
597 ll_clear_inode(inode);
598 make_bad_inode(inode);
600 l_extent_unlock(fd, inode, lsm, LCK_PR, &extent,
605 /* OIDEBUG(inode); */
607 if (S_ISREG(inode->i_mode)) {
608 inode->i_op = &ll_file_inode_operations;
609 inode->i_fop = &ll_file_operations;
610 inode->i_mapping->a_ops = &ll_aops;
612 } else if (S_ISDIR(inode->i_mode)) {
613 inode->i_op = &ll_dir_inode_operations;
614 inode->i_fop = &ll_dir_operations;
615 inode->i_mapping->a_ops = &ll_dir_aops;
617 } else if (S_ISLNK(inode->i_mode)) {
618 inode->i_op = &ll_fast_symlink_inode_operations;
621 init_special_inode(inode, inode->i_mode,
622 kdev_t_to_nr(inode->i_rdev));
629 static inline void invalidate_request_list(struct list_head *req_list)
631 struct list_head *tmp, *n;
632 list_for_each_safe(tmp, n, req_list) {
633 struct ptlrpc_request *req =
634 list_entry(tmp, struct ptlrpc_request, rq_list);
635 CERROR("invalidating req xid %d op %d to %s:%d\n",
636 (unsigned long long)req->rq_xid, req->rq_reqmsg->opc,
637 req->rq_connection->c_remote_uuid,
638 req->rq_import->imp_client->cli_request_portal);
639 req->rq_flags |= PTL_RPC_FL_ERR;
640 wake_up(&req->rq_wait_for_rep);
644 void ll_umount_begin(struct super_block *sb)
646 struct ll_sb_info *sbi = ll_s2sbi(sb);
647 struct list_head *ctmp;
651 list_for_each(ctmp, &sbi->ll_conn_chain) {
652 struct ptlrpc_connection *conn;
653 conn = list_entry(ctmp, struct ptlrpc_connection, c_sb_chain);
655 spin_lock(&conn->c_lock);
656 conn->c_flags |= CONN_INVALID;
657 /*invalidate_request_list(&conn->c_sending_head);*/
658 invalidate_request_list(&conn->c_delayed_head);
659 spin_unlock(&conn->c_lock);
666 static kmem_cache_t *ll_inode_cachep;
668 static struct inode *ll_alloc_inode(struct super_block *sb)
670 struct ll_inode_info *lli;
671 lli = kmem_cache_alloc(ll_inode_cachep, SLAB_KERNEL);
675 memset(lli, 0, (char *)&lli->lli_vfs_inode - (char *)lli);
676 sema_init(&lli->lli_open_sem, 1);
677 init_MUTEX(&lli->lli_size_valid_sem);
679 return &lli->lli_vfs_inode;
682 static void ll_destroy_inode(struct inode *inode)
684 kmem_cache_free(ll_inode_cachep, ll_i2info(inode));
687 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
689 struct ll_inode_info *lli = foo;
691 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
692 SLAB_CTOR_CONSTRUCTOR)
693 inode_init_once(&lli->lli_vfs_inode);
696 int ll_init_inodecache(void)
698 ll_inode_cachep = kmem_cache_create("lustre_inode_cache",
699 sizeof(struct ll_inode_info),
700 0, SLAB_HWCACHE_ALIGN,
702 if (ll_inode_cachep == NULL)
707 void ll_destroy_inodecache(void)
709 if (kmem_cache_destroy(ll_inode_cachep))
710 CERROR("ll_inode_cache: not all structures were freed\n");
715 /* exported operations */
716 struct super_operations ll_super_operations =
718 alloc_inode: ll_alloc_inode,
719 destroy_inode: ll_destroy_inode,
720 clear_inode: ll_clear_inode,
721 // delete_inode: ll_delete_inode,
722 put_super: ll_put_super,
724 umount_begin: ll_umount_begin
728 struct file_system_type lustre_lite_fs_type = {
729 .owner = THIS_MODULE,
730 .name = "lustre_lite",
732 .kill_sb = kill_anon_super,
735 static int __init init_lustre_lite(void)
738 printk(KERN_INFO "Lustre Lite Client File System; "
739 "info@clusterfs.com\n");
740 rc = ll_init_inodecache();
743 ll_file_data_slab = kmem_cache_create("ll_file_data",
744 sizeof(struct ll_file_data), 0,
745 SLAB_HWCACHE_ALIGN, NULL, NULL);
746 if (ll_file_data_slab == NULL) {
747 ll_destroy_inodecache();
751 proc_lustre_fs_root = proc_lustre_root ?
752 proc_mkdir("llite", proc_lustre_root) : NULL;
754 return register_filesystem(&lustre_lite_fs_type);
757 static void __exit exit_lustre_lite(void)
759 unregister_filesystem(&lustre_lite_fs_type);
760 ll_destroy_inodecache();
761 kmem_cache_destroy(ll_file_data_slab);
762 if (proc_lustre_fs_root) {
763 lprocfs_remove(proc_lustre_fs_root);
764 proc_lustre_fs_root = NULL;
768 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
769 MODULE_DESCRIPTION("Lustre Lite Client File System");
770 MODULE_LICENSE("GPL");
772 module_init(init_lustre_lite);
773 module_exit(exit_lustre_lite);