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 kmem_cache_t *ll_file_data_slab;
38 extern struct address_space_operations ll_aops;
39 extern struct address_space_operations ll_dir_aops;
40 struct super_operations ll_super_operations;
42 /* /proc/lustre/llite root that tracks llite mount points */
43 struct proc_dir_entry *proc_lustre_fs_root;
45 extern int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
46 struct super_block *sb,
47 char *osc, char *mdc);
49 extern int ll_recover(struct recovd_data *, int);
50 extern int ll_commitcbd_setup(struct ll_sb_info *);
51 extern int ll_commitcbd_cleanup(struct ll_sb_info *);
53 static char *ll_read_opt(const char *opt, char *data)
59 CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
60 if ( strncmp(opt, data, strlen(opt)) )
62 if ( (value = strchr(data, '=')) == NULL )
66 OBD_ALLOC(retval, strlen(value) + 1);
68 CERROR("out of memory!\n");
72 memcpy(retval, value, strlen(value)+1);
73 CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
77 static int ll_set_opt(const char *opt, char *data, int fl)
81 CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
82 if ( strncmp(opt, data, strlen(opt)) )
88 static void ll_options(char *options, char **ost, char **mds, int *flags)
98 for (this_char = strtok (options, ",");
100 this_char = strtok (NULL, ",")) {
101 CDEBUG(D_SUPER, "this_char %s\n", this_char);
102 if ( (!*ost && (*ost = ll_read_opt("osc", this_char)))||
103 (!*mds && (*mds = ll_read_opt("mdc", this_char)))||
104 (!(*flags & LL_SBI_NOLCK) && ((*flags) = (*flags) |
105 ll_set_opt("nolock", this_char, LL_SBI_NOLCK))) )
112 #define log2(n) ffz(~(n))
115 static struct super_block *ll_read_super(struct super_block *sb,
116 void *data, int silent)
118 struct inode *root = 0;
119 struct obd_device *obd;
120 struct ll_sb_info *sbi;
124 struct ll_fid rootfid;
125 struct obd_statfs osfs;
126 struct ptlrpc_request *request = NULL;
127 struct ptlrpc_connection *mdc_conn;
128 struct ll_read_inode2_cookie lic;
130 struct obd_uuid param_uuid;
134 OBD_ALLOC(sbi, sizeof(*sbi));
138 INIT_LIST_HEAD(&sbi->ll_conn_chain);
139 INIT_LIST_HEAD(&sbi->ll_orphan_dentry_list);
140 generate_random_uuid(uuid);
141 class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
143 sb->u.generic_sbp = sbi;
145 ll_options(data, &osc, &mdc, &sbi->ll_flags);
149 GOTO(out_free, sb = NULL);
154 GOTO(out_free, sb = NULL);
157 strncpy(param_uuid.uuid, mdc, sizeof(param_uuid.uuid));
158 obd = class_uuid2obd(¶m_uuid);
160 CERROR("MDC %s: not setup or attached\n", mdc);
161 GOTO(out_free, sb = NULL);
164 err = obd_connect(&sbi->ll_mdc_conn, obd, &sbi->ll_sb_uuid,
165 ptlrpc_recovd, ll_recover);
167 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
168 GOTO(out_free, sb = NULL);
171 mdc_conn = sbi2mdc(sbi)->cl_import.imp_connection;
172 list_add(&mdc_conn->c_sb_chain, &sbi->ll_conn_chain);
174 strncpy(param_uuid.uuid, osc, sizeof(param_uuid.uuid));
175 obd = class_uuid2obd(¶m_uuid);
177 CERROR("OSC %s: not setup or attached\n", osc);
178 GOTO(out_mdc, sb = NULL);
181 err = obd_connect(&sbi->ll_osc_conn, obd, &sbi->ll_sb_uuid,
182 ptlrpc_recovd, ll_recover);
184 CERROR("cannot connect to %s: rc = %d\n", osc, err);
185 GOTO(out_mdc, sb = NULL);
188 err = mdc_getstatus(&sbi->ll_mdc_conn, &rootfid);
190 CERROR("cannot mds_connect: rc = %d\n", err);
191 GOTO(out_mdc, sb = NULL);
193 CDEBUG(D_SUPER, "rootfid "LPU64"\n", rootfid.id);
194 sbi->ll_rootino = rootfid.id;
196 memset(&osfs, 0, sizeof(osfs));
197 err = obd_statfs(&sbi->ll_mdc_conn, &osfs);
198 sb->s_blocksize = osfs.os_bsize;
199 sb->s_blocksize_bits = log2(osfs.os_bsize);
200 sb->s_magic = LL_SUPER_MAGIC;
201 sb->s_maxbytes = (1ULL << (32 + 9)) - osfs.os_bsize;
203 sb->s_op = &ll_super_operations;
205 /* make root inode */
206 err = mdc_getattr(&sbi->ll_mdc_conn, sbi->ll_rootino, S_IFDIR,
207 OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 0, &request);
209 CERROR("mdc_getattr failed for root: rc = %d\n", err);
210 GOTO(out_request, sb = NULL);
213 /* initialize committed transaction callback daemon */
214 spin_lock_init(&sbi->ll_commitcbd_lock);
215 init_waitqueue_head(&sbi->ll_commitcbd_waitq);
216 init_waitqueue_head(&sbi->ll_commitcbd_ctl_waitq);
217 sbi->ll_commitcbd_flags = 0;
218 err = ll_commitcbd_setup(sbi);
220 CERROR("failed to start commit callback daemon: rc = %d\n",err);
221 GOTO(out_request, sb = NULL);
224 lic.lic_body = lustre_msg_buf(request->rq_repmsg, 0);
226 LASSERT(sbi->ll_rootino != 0);
227 root = iget4(sb, sbi->ll_rootino, NULL, &lic);
230 sb->s_root = d_alloc_root(root);
232 CERROR("lustre_lite: bad iget4 for root\n");
233 GOTO(out_cdb, sb = NULL);
236 ptlrpc_req_finished(request);
239 if (proc_lustre_fs_root) {
240 err = lprocfs_register_mountpoint(proc_lustre_fs_root, sb,
243 CERROR("could not register mount in /proc/lustre");
248 OBD_FREE(mdc, strlen(mdc) + 1);
250 OBD_FREE(osc, strlen(osc) + 1);
255 ll_commitcbd_cleanup(sbi);
257 ptlrpc_req_finished(request);
258 obd_disconnect(&sbi->ll_osc_conn);
260 obd_disconnect(&sbi->ll_mdc_conn);
262 OBD_FREE(sbi, sizeof(*sbi));
265 } /* ll_read_super */
267 static void ll_put_super(struct super_block *sb)
269 struct ll_sb_info *sbi = ll_s2sbi(sb);
270 struct list_head *tmp, *next;
271 struct ll_fid rootfid;
274 list_del(&sbi->ll_conn_chain);
275 ll_commitcbd_cleanup(sbi);
276 obd_disconnect(&sbi->ll_osc_conn);
278 /* NULL request to force sync on the MDS, and get the last_committed
279 * value to flush remaining RPCs from the sending queue on client.
281 * XXX This should be an mdc_sync() call to sync the whole MDS fs,
282 * which we can call for other reasons as well.
284 mdc_getstatus(&sbi->ll_mdc_conn, &rootfid);
286 if (sbi->ll_proc_root) {
287 lprocfs_remove(sbi->ll_proc_root);
288 sbi->ll_proc_root = NULL;
291 obd_disconnect(&sbi->ll_mdc_conn);
293 spin_lock(&dcache_lock);
294 list_for_each_safe(tmp, next, &sbi->ll_orphan_dentry_list) {
295 struct dentry *dentry = list_entry(tmp, struct dentry, d_hash);
296 shrink_dcache_parent(dentry);
298 spin_unlock(&dcache_lock);
300 OBD_FREE(sbi, sizeof(*sbi));
305 static void ll_clear_inode(struct inode *inode)
307 struct ll_sb_info *sbi = ll_i2sbi(inode);
308 struct ll_inode_info *lli = ll_i2info(inode);
312 rc = mdc_cancel_unused(&sbi->ll_mdc_conn, inode, LDLM_FL_NO_CALLBACK);
314 CERROR("mdc_cancel_unused: %d\n", rc);
315 /* XXX FIXME do something dramatic */
319 rc = obd_cancel_unused(&sbi->ll_osc_conn, lli->lli_smd, 0);
321 CERROR("obd_cancel_unused: %d\n", rc);
322 /* XXX FIXME do something dramatic */
326 if (atomic_read(&inode->i_count) != 0)
327 CERROR("clearing in-use inode %lu: count = %d\n",
328 inode->i_ino, atomic_read(&inode->i_count));
331 obd_free_memmd(&sbi->ll_osc_conn, &lli->lli_smd);
333 if (lli->lli_symlink_name) {
334 OBD_FREE(lli->lli_symlink_name,
335 strlen(lli->lli_symlink_name) + 1);
336 lli->lli_symlink_name = NULL;
343 static void ll_delete_inode(struct inode *inode)
346 if (S_ISREG(inode->i_mode)) {
349 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
351 /* mcreate with no open */
355 if (lsm->lsm_object_id == 0) {
356 CERROR("This really happens\n");
357 /* No obdo was ever created */
365 oa->o_id = lsm->lsm_object_id;
366 obdo_from_inode(oa, inode, OBD_MD_FLID | OBD_MD_FLTYPE);
368 err = obd_destroy(ll_i2obdconn(inode), oa, lsm, NULL);
372 "inode %lu obd_destroy objid "LPX64" error %d\n",
373 inode->i_ino, lsm->lsm_object_id, err);
381 /* like inode_setattr, but doesn't mark the inode dirty */
382 static int ll_attr2inode(struct inode *inode, struct iattr *attr, int trunc)
384 unsigned int ia_valid = attr->ia_valid;
387 if ((ia_valid & ATTR_SIZE) && trunc) {
388 error = vmtruncate(inode, attr->ia_size);
391 } else if (ia_valid & ATTR_SIZE)
392 inode->i_size = attr->ia_size;
394 if (ia_valid & ATTR_UID)
395 inode->i_uid = attr->ia_uid;
396 if (ia_valid & ATTR_GID)
397 inode->i_gid = attr->ia_gid;
398 if (ia_valid & ATTR_ATIME)
399 inode->i_atime = attr->ia_atime;
400 if (ia_valid & ATTR_MTIME)
401 inode->i_mtime = attr->ia_mtime;
402 if (ia_valid & ATTR_CTIME)
403 inode->i_ctime = attr->ia_ctime;
404 if (ia_valid & ATTR_MODE) {
405 inode->i_mode = attr->ia_mode;
406 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
407 inode->i_mode &= ~S_ISGID;
413 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
415 struct ptlrpc_request *request = NULL;
416 struct ll_sb_info *sbi = ll_i2sbi(inode);
420 /* change incore inode */
421 ll_attr2inode(inode, attr, do_trunc);
423 /* Don't send size changes to MDS to avoid "fast EA" problems, and
424 * also avoid a pointless RPC (we get file size from OST anyways).
426 attr->ia_valid &= ~ATTR_SIZE;
427 if (attr->ia_valid) {
428 err = mdc_setattr(&sbi->ll_mdc_conn, inode, attr, NULL, 0,
431 CERROR("mdc_setattr fails: err = %d\n", err);
433 ptlrpc_req_finished(request);
434 if (S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_MTIME_SET) {
435 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
439 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
440 inode->i_ino, attr->ia_mtime);
441 oa.o_id = lsm->lsm_object_id;
443 oa.o_valid = OBD_MD_FLID |OBD_MD_FLTYPE |OBD_MD_FLMTIME;
444 oa.o_mtime = attr->ia_mtime;
445 err2 = obd_setattr(&sbi->ll_osc_conn, &oa, lsm, NULL);
447 CERROR("obd_setattr fails: rc=%d\n", err);
457 int ll_setattr(struct dentry *de, struct iattr *attr)
459 int rc = inode_change_ok(de->d_inode, attr);
464 return ll_inode_setattr(de->d_inode, attr, 1);
467 static int ll_statfs(struct super_block *sb, struct statfs *sfs)
469 struct ll_sb_info *sbi = ll_s2sbi(sb);
470 struct obd_statfs osfs;
474 memset(sfs, 0, sizeof(*sfs));
475 rc = obd_statfs(&sbi->ll_mdc_conn, &osfs);
476 statfs_unpack(sfs, &osfs);
478 CERROR("mdc_statfs fails: rc = %d\n", rc);
480 CDEBUG(D_SUPER, "mdc_statfs shows blocks "LPU64"/"LPU64
481 " objects "LPU64"/"LPU64"\n",
482 osfs.os_bavail, osfs.os_blocks,
483 osfs.os_ffree, osfs.os_files);
485 /* temporary until mds_statfs returns statfs info for all OSTs */
487 rc = obd_statfs(&sbi->ll_osc_conn, &osfs);
489 CERROR("obd_statfs fails: rc = %d\n", rc);
492 CDEBUG(D_SUPER, "obd_statfs shows blocks "LPU64"/"LPU64
493 " objects "LPU64"/"LPU64"\n",
494 osfs.os_bavail, osfs.os_blocks,
495 osfs.os_ffree, osfs.os_files);
497 while (osfs.os_blocks > ~0UL) {
500 osfs.os_blocks >>= 1;
502 osfs.os_bavail >>= 1;
504 sfs->f_blocks = osfs.os_blocks;
505 sfs->f_bfree = osfs.os_bfree;
506 sfs->f_bavail = osfs.os_bavail;
507 if (osfs.os_ffree < (__u64)sfs->f_ffree)
508 sfs->f_ffree = osfs.os_ffree;
515 void ll_update_inode(struct inode *inode, struct mds_body *body,
516 struct lov_mds_md *lmm)
518 struct ll_inode_info *lli = ll_i2info(inode);
521 obd_unpackmd(ll_i2obdconn(inode), &lli->lli_smd, lmm);
523 if (body->valid & OBD_MD_FLID)
524 inode->i_ino = body->ino;
525 if (body->valid & OBD_MD_FLATIME)
526 inode->i_atime = body->atime;
527 if (body->valid & OBD_MD_FLMTIME)
528 inode->i_mtime = body->mtime;
529 if (body->valid & OBD_MD_FLCTIME)
530 inode->i_ctime = body->ctime;
531 if (body->valid & OBD_MD_FLMODE)
532 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
533 if (body->valid & OBD_MD_FLTYPE)
534 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
535 if (body->valid & OBD_MD_FLUID)
536 inode->i_uid = body->uid;
537 if (body->valid & OBD_MD_FLGID)
538 inode->i_gid = body->gid;
539 if (body->valid & OBD_MD_FLFLAGS)
540 inode->i_flags = body->flags;
541 if (body->valid & OBD_MD_FLNLINK)
542 inode->i_nlink = body->nlink;
543 if (body->valid & OBD_MD_FLGENER)
544 inode->i_generation = body->generation;
545 if (body->valid & OBD_MD_FLRDEV)
546 inode->i_rdev = body->rdev;
547 if (body->valid & OBD_MD_FLSIZE)
548 inode->i_size = body->size;
549 if (body->valid & OBD_MD_FLBLOCKS)
550 inode->i_blocks = body->blocks;
553 static void ll_read_inode2(struct inode *inode, void *opaque)
555 struct ll_read_inode2_cookie *lic = opaque;
556 struct mds_body *body = lic->lic_body;
557 struct ll_inode_info *lli = ll_i2info(inode);
560 sema_init(&lli->lli_open_sem, 1);
561 atomic_set(&lli->lli_open_count, 0);
563 LASSERT(!lli->lli_smd);
565 /* core attributes first */
566 ll_update_inode(inode, body, lic ? lic->lic_lmm : NULL);
568 /* Get the authoritative file size */
569 if (lli->lli_smd && (inode->i_mode & S_IFREG)) {
571 LASSERT(lli->lli_smd->lsm_object_id != 0);
572 rc = ll_file_size(inode, lli->lli_smd, NULL);
574 CERROR("ll_file_size: %d\n", rc);
575 ll_clear_inode(inode);
576 make_bad_inode(inode);
580 /* OIDEBUG(inode); */
582 if (S_ISREG(inode->i_mode)) {
583 inode->i_op = &ll_file_inode_operations;
584 inode->i_fop = &ll_file_operations;
585 inode->i_mapping->a_ops = &ll_aops;
587 } else if (S_ISDIR(inode->i_mode)) {
588 inode->i_op = &ll_dir_inode_operations;
589 inode->i_fop = &ll_dir_operations;
590 inode->i_mapping->a_ops = &ll_dir_aops;
592 } else if (S_ISLNK(inode->i_mode)) {
593 inode->i_op = &ll_fast_symlink_inode_operations;
596 inode->i_op = &ll_special_inode_operations;
597 init_special_inode(inode, inode->i_mode, inode->i_rdev);
602 static inline void invalidate_request_list(struct list_head *req_list)
604 struct list_head *tmp, *n;
605 list_for_each_safe(tmp, n, req_list) {
606 struct ptlrpc_request *req =
607 list_entry(tmp, struct ptlrpc_request, rq_list);
608 CERROR("invalidating req xid "LPU64" op %d to %s:%d\n",
609 req->rq_xid, req->rq_reqmsg->opc,
610 req->rq_connection->c_remote_uuid.uuid,
611 req->rq_import->imp_client->cli_request_portal);
612 req->rq_flags |= PTL_RPC_FL_ERR;
613 wake_up(&req->rq_wait_for_rep);
617 void ll_umount_begin(struct super_block *sb)
619 struct ll_sb_info *sbi = ll_s2sbi(sb);
620 struct list_head *ctmp;
624 list_for_each(ctmp, &sbi->ll_conn_chain) {
625 struct ptlrpc_connection *conn;
626 conn = list_entry(ctmp, struct ptlrpc_connection, c_sb_chain);
628 spin_lock(&conn->c_lock);
629 /* XXX should just be dealing with imports, probably through
630 * XXX iocontrol, need next-gen recovery! */
631 conn->c_flags |= CONN_INVALID;
632 /* invalidate_request_list(&conn->c_sending_head); */
633 invalidate_request_list(&conn->c_delayed_head);
634 spin_unlock(&conn->c_lock);
640 /* exported operations */
641 struct super_operations ll_super_operations =
643 read_inode2: ll_read_inode2,
644 clear_inode: ll_clear_inode,
645 // delete_inode: ll_delete_inode,
646 put_super: ll_put_super,
648 umount_begin: ll_umount_begin
651 static struct file_system_type lustre_lite_fs_type = {
654 read_super: ll_read_super,
658 static int __init init_lustre_lite(void)
660 printk(KERN_INFO "Lustre Lite Client File System; "
661 "info@clusterfs.com\n");
662 ll_file_data_slab = kmem_cache_create("ll_file_data",
663 sizeof(struct ll_file_data), 0,
664 SLAB_HWCACHE_ALIGN, NULL, NULL);
665 if (ll_file_data_slab == NULL)
668 proc_lustre_fs_root = proc_lustre_root ? proc_mkdir("llite", proc_lustre_root) : NULL;
670 return register_filesystem(&lustre_lite_fs_type);
673 static void __exit exit_lustre_lite(void)
675 unregister_filesystem(&lustre_lite_fs_type);
676 kmem_cache_destroy(ll_file_data_slab);
678 if (proc_lustre_fs_root) {
679 lprocfs_remove(proc_lustre_fs_root);
680 proc_lustre_fs_root = NULL;
684 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
685 MODULE_DESCRIPTION("Lustre Lite Client File System");
686 MODULE_LICENSE("GPL");
688 module_init(init_lustre_lite);
689 module_exit(exit_lustre_lite);