1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * Lustre filesystem abstraction routines
7 * Copyright (C) 2004 Cluster File Systems, Inc.
9 * This file is part of Lustre, http://www.lustre.org.
11 * Lustre is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General Public
13 * License as published by the Free Software Foundation.
15 * Lustre is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Lustre; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 # define EXPORT_SYMTAB
28 #define DEBUG_SUBSYSTEM S_SM
30 #include <linux/kmod.h>
31 #include <linux/init.h>
33 #include <linux/slab.h>
34 #include <linux/obd_class.h>
35 #include <linux/obd_support.h>
36 #include <linux/lustre_lib.h>
37 #include <linux/lustre_idl.h>
38 #include <linux/lustre_fsfilt.h>
39 #include <linux/lustre_smfs.h>
40 #include "smfs_internal.h"
43 #define KML_BUF_REC_INIT(buffer, pbuf, len) \
45 pbuf = buffer + sizeof(int); \
49 #define KML_BUF_REC_END(buffer, length, pbuf) \
52 memcpy(buffer, &len, sizeof(len)); \
53 length += sizeof(int); \
54 pbuf = buffer + length; \
57 static int smfs_llog_process_rec_cb(struct llog_handle *handle,
58 struct llog_rec_hdr *rec, void *data)
61 struct smfs_proc_args *args = (struct smfs_proc_args *)data;
62 struct lvfs_run_ctxt saved;
65 if (!(le32_to_cpu(handle->lgh_hdr->llh_flags) & LLOG_F_IS_PLAIN)) {
66 CERROR("log is not plain\n");
70 if (le32_to_cpu(rec->lrh_type) == LLOG_GEN_REC) {
71 struct llog_cookie cookie;
73 cookie.lgc_lgl = handle->lgh_id;
74 cookie.lgc_index = le32_to_cpu(rec->lrh_index);
76 llog_cancel(handle->lgh_ctxt, 1, &cookie, 0, NULL);
77 RETURN(LLOG_PROC_BREAK);
80 if (le32_to_cpu(rec->lrh_type) != SMFS_UPDATE_REC)
83 rec_buf = (char*) (rec + 1);
85 if (!S2SMI(args->sr_sb)->smsi_ctxt)
86 GOTO(exit, rc = -ENODEV);
88 push_ctxt(&saved, S2SMI(args->sr_sb)->smsi_ctxt, NULL);
90 /*FIXME later should first unpack the rec,
91 * then call lvfs_reint or lvfs_undo
92 * kml rec format has changed lvfs_reint lvfs_undo should
93 * be rewrite FIXME later*/
94 if (SMFS_DO_REINT_REC(args->sr_flags))
95 rc = lvfs_reint(args->sr_sb, rec_buf);
97 rc = lvfs_undo(args->sr_sb, rec_buf);
99 if (!rc && !SMFS_DO_REC_ALL(args->sr_flags)) {
101 if (args->sr_count == 0)
102 rc = LLOG_PROC_BREAK;
104 pop_ctxt(&saved, S2SMI(args->sr_sb)->smsi_ctxt, NULL);
110 /* not used curently */
111 static smfs_pack_rec_func smfs_get_rec_pack_type(struct super_block *sb)
114 struct smfs_super_info *smsi = S2SMI(sb);
116 idx = GET_REC_PACK_TYPE_INDEX(smsi->smsi_flags);
117 return smsi->smsi_pack_rec[idx];
122 copy_inode_attr(struct iattr *iattr, struct inode *inode)
124 iattr->ia_mode = inode->i_mode;
125 iattr->ia_uid = inode->i_uid;
126 iattr->ia_gid = inode->i_gid;
127 iattr->ia_atime = inode->i_atime;
128 iattr->ia_ctime = inode->i_ctime;
129 iattr->ia_mtime = inode->i_mtime;
130 iattr->ia_size = inode->i_size;
134 static inline int unpack_rec_data(char **p_buffer, int *size,
135 char *in_data, char *args_data)
142 args_len = strlen(args_data);
144 *size = *((int*)(in_data));
145 rc = *size + sizeof(int);
147 OBD_ALLOC(*p_buffer, *size + args_len + 1);
151 /* first copy reint dir. */
153 memcpy(*p_buffer, args_data, args_len);
155 /* then copy the node name. */
156 memcpy(*p_buffer + args_len,
157 (in_data + sizeof(int)), *size);
165 int smfs_rec_unpack(struct smfs_proc_args *args, char *record,
166 char **pbuf, int *opcode)
168 //int offset = *(int *)(record);
169 //char *tmp = record + offset + sizeof(int);
171 *opcode = *(int *)record;
172 *pbuf = record + sizeof(*opcode);
175 EXPORT_SYMBOL(smfs_rec_unpack); /* cmobd/cm_reint.c */
177 int smfs_write_extents(struct inode *dir, struct dentry *dentry,
178 unsigned long from, unsigned long num)
180 //smfs_post_rec_write(dir, dentry, &from, &num);
184 int smfs_rec_precreate(struct dentry *dentry, int *num, struct obdo *oa)
186 return smfs_post_rec_create(dentry->d_inode, dentry, num, oa);
189 int smfs_process_rec(struct super_block *sb,
190 int count, char *dir, int flags)
192 struct llog_ctxt *ctxt;
193 struct llog_handle *loghandle;
194 struct smfs_proc_args args;
198 if (!SMFS_INIT_REC(S2SMI(sb))) {
199 CWARN("Did not start up rec server \n");
203 memset(&args, 0, sizeof(struct smfs_proc_args));
205 args.sr_count = count;
207 args.sr_flags = flags ;
208 ctxt = S2SMI(sb)->smsi_kml_log;
209 loghandle = ctxt->loc_handle;
212 if (SMFS_DO_REINT_REC(flags)) {
213 struct llog_gen_rec *lgr;
215 /* for reint rec, we need insert a gen rec to identify
216 * the end of the rec.*/
217 OBD_ALLOC(lgr, sizeof(*lgr));
220 lgr->lgr_hdr.lrh_len = lgr->lgr_tail.lrt_len = sizeof(*lgr);
221 lgr->lgr_hdr.lrh_type = LLOG_GEN_REC;
222 lgr->lgr_gen = ctxt->loc_gen;
223 rc = llog_add(ctxt, &lgr->lgr_hdr, NULL, NULL, 1,
225 OBD_FREE(lgr, sizeof(*lgr));
230 SET_REC_COUNT_FLAGS(args.sr_flags, SMFS_REC_ALL);
233 if (SMFS_DO_REINT_REC(flags))
234 rc = llog_cat_process(loghandle, ctxt->llog_proc_cb,
237 rc = llog_cat_reverse_process(loghandle,
240 if (rc == LLOG_PROC_BREAK)
246 /*smfs_path is gotten from intermezzo*/
247 static char* smfs_path(struct dentry *dentry, struct dentry *root, char *buffer,
250 char * end = buffer + buflen;
251 char * name = buffer;
252 char * buf_end = buffer + buflen;
262 struct dentry * parent;
267 parent = dentry->d_parent;
268 if (dentry == parent)
270 namelen = dentry->d_name.len;
271 buflen -= namelen + 1;
275 memcpy(end, dentry->d_name.name, namelen);
281 while (end != buf_end)
287 static int smfs_log_path(struct super_block *sb,
288 struct dentry *dentry,
292 struct dentry *root=sb->s_root;
293 char *p_name = buffer + sizeof(int);
299 name = smfs_path(dentry, root, p_name, buffer_len - sizeof(int));
300 namelen = cpu_to_le32(strlen(p_name));
301 memcpy(buffer, &namelen, sizeof(int));
303 namelen += sizeof(int);
307 static int smfs_pack_rec (char *buffer, struct dentry *dentry,
308 struct inode *dir, void *data1,
311 smfs_pack_rec_func pack_func;
313 pack_func = smfs_get_rec_pack_type(dir->i_sb);
316 return pack_func(buffer, dentry, dir, data1, data2, op);
319 static int smfs_insert_extents_ea(struct inode *inode, size_t from, loff_t num)
321 struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
325 if (SMFS_INODE_OVER_WRITE(inode))
328 rc = fsfilt->fs_insert_extents_ea(inode, OFF2BLKS(from, inode),
329 SIZE2BLKS(num, inode));
333 static int smfs_remove_extents_ea(struct inode *inode, size_t from, loff_t num)
335 struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
339 rc = fsfilt->fs_remove_extents_ea(inode, OFF2BLKS(from, inode),
340 SIZE2BLKS(num, inode));
345 static int smfs_remove_all_extents_ea(struct inode *inode)
347 struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
351 rc = fsfilt->fs_remove_extents_ea(inode, 0, 0xffffffff);
354 static int smfs_init_extents_ea(struct inode *inode)
356 struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
360 rc = fsfilt->fs_init_extents_ea(inode);
364 static int smfs_set_dirty_flags(struct inode *inode, int flags)
366 struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
371 if (SMFS_INODE_OVER_WRITE(inode))
373 /*FIXME later, the blocks needed in journal here will be recalculated*/
374 handle = smfs_trans_start(inode, FSFILT_OP_SETATTR);
375 if (IS_ERR(handle)) {
376 CERROR("smfs_set_dirty_flag:no space for transaction\n");
379 if ((!SMFS_INODE_DIRTY_WRITE(inode) && (!SMFS_INODE_OVER_WRITE(inode))) ||
380 ((flags == SMFS_OVER_WRITE) && (SMFS_INODE_DIRTY_WRITE(inode)))) {
381 rc = fsfilt->fs_set_xattr(inode, handle, REINT_EXTENTS_FLAGS,
382 &flags, sizeof(int));
386 if (flags == SMFS_OVER_WRITE)
387 SMFS_SET_INODE_OVER_WRITE(inode);
389 SMFS_SET_INODE_DIRTY_WRITE(inode);
391 smfs_trans_commit(inode, handle, 0);
395 static int all_blocks_present_ea(struct inode *inode)
405 static int kml_pack_path (char **buf, struct dentry * dentry)
408 int length = 0, rc = 0;
410 OBD_ALLOC(*buf, PAGE_SIZE);
415 KML_BUF_REC_INIT(*buf, pbuf, length);
416 rc = smfs_log_path(dentry->d_sb, dentry, pbuf, length);
422 KML_BUF_REC_END(*buf, length, pbuf);
427 static int kml_create(struct inode * inode, void *arg, struct kml_priv * priv)
429 struct hook_msg * msg = arg;
430 //return smfs_post_rec_create(inode, msg->dentry, NULL, NULL);
431 struct smfs_super_info *smb = S2SMI(inode->i_sb);
433 int rc = 0, length = 0;
436 OBD_ALLOC(buffer, PAGE_SIZE);
441 rc = kml_pack_path(&buffer, msg->dentry);
446 pbuf = buffer + length;
448 rc = priv->pack_fn(REINT_CREATE, buffer, msg->dentry, inode,
454 rc = smfs_llog_add_rec(smb, (void*)buffer, length);
457 OBD_FREE(buffer, PAGE_SIZE);
462 static int kml_link(struct inode *inode, void *arg, struct kml_priv *priv)
464 struct hook_link_msg *msg = arg;
465 int rc = 0, length = 0;
469 OBD_ALLOC(buffer, PAGE_SIZE);
471 GOTO(exit, rc = -ENOMEM);
473 rc = priv->pack_fn(REINT_LINK, buffer, msg->dentry, inode,
474 msg->dentry, msg->new_dentry);
479 rc = smfs_llog_add_rec(S2SMI(inode->i_sb), (void *)buffer, length);
482 OBD_FREE(buffer, PAGE_SIZE);
487 static int kml_unlink(struct inode * inode, void *arg, struct kml_priv * priv)
489 struct hook_unlink_msg * msg = arg;
491 int length = 0, rc = 0;
494 OBD_ALLOC(buffer, PAGE_SIZE);
496 GOTO(exit, rc = -ENOMEM);
498 rc = priv->pack_fn(REINT_UNLINK, buffer, msg->dentry, inode,
504 rc = smfs_llog_add_rec(S2SMI(inode->i_sb), (void*)buffer, length);
507 OBD_FREE(buffer, PAGE_SIZE);
512 static int kml_symlink(struct inode *inode, void *arg, struct kml_priv *priv)
514 struct smfs_super_info *smb = S2SMI(inode->i_sb);
515 struct hook_symlink_msg *msg = arg;
516 int rc = 0, length = 0;
520 OBD_ALLOC(buffer, PAGE_SIZE);
522 GOTO(exit, rc = -ENOMEM);
524 rc = priv->pack_fn(REINT_CREATE, buffer, msg->dentry, inode,
525 msg->symname, &msg->tgt_len);
530 rc = smfs_llog_add_rec(smb, (void*)buffer, length);
533 OBD_FREE(buffer, PAGE_SIZE);
538 static int kml_rename(struct inode *inode, void *arg, struct kml_priv *priv)
540 struct hook_rename_msg *msg = arg;
541 int rc = 0, length = 0;
545 OBD_ALLOC(buffer, PAGE_SIZE);
547 GOTO(exit, rc = -ENOMEM);
549 rc = priv->pack_fn(REINT_RENAME, buffer, msg->dentry, inode,
550 msg->new_dir, msg->new_dentry);
555 rc = smfs_llog_add_rec(S2SMI(inode->i_sb), (void*)buffer, length);
558 OBD_FREE(buffer, PAGE_SIZE);
562 static int kml_setattr(struct inode *inode, void *arg, struct kml_priv *priv)
564 struct hook_setattr_msg *msg = arg;
565 int rc = 0, length = 0;
569 OBD_ALLOC(buffer, PAGE_SIZE);
571 GOTO(exit, rc = -ENOMEM);
573 rc = priv->pack_fn(REINT_SETATTR, buffer, msg->dentry, inode,
579 rc = smfs_llog_add_rec(S2SMI(inode->i_sb), (void*)buffer, length);
582 if (attr && attr->ia_valid & ATTR_SIZE) {
583 smfs_remove_extents_ea(inode, attr->ia_size,
585 if (attr->ia_size == 0)
586 smfs_set_dirty_flags(inode, SMFS_OVER_WRITE);
588 smfs_set_dirty_flags(inode, SMFS_DIRTY_WRITE);
594 OBD_FREE(buffer, PAGE_SIZE);
598 static int kml_setxattr(struct inode *inode, void *arg, struct kml_priv *priv)
600 struct hook_setxattr_msg *msg = arg;
601 struct kml_buffer kbuf;
602 int rc = 0, length = 0;
606 OBD_ALLOC(buffer, PAGE_SIZE);
608 GOTO(exit, rc = -ENOMEM);
610 kbuf.buf = msg->buffer;
611 kbuf.buf_size = msg->buffer_size;
613 rc = priv->pack_fn(REINT_SETXATTR, buffer, NULL, msg->inode, msg->name,
619 rc = smfs_llog_add_rec(S2SMI(inode->i_sb), (void*)buffer, length);
622 if (attr && attr->ia_valid & ATTR_SIZE) {
623 smfs_remove_extents_ea(inode, attr->ia_size,
625 if (attr->ia_size == 0)
626 smfs_set_dirty_flags(inode, SMFS_OVER_WRITE);
628 smfs_set_dirty_flags(inode, SMFS_DIRTY_WRITE);
634 OBD_FREE(buffer, PAGE_SIZE);
639 static int kml_write(struct inode * inode, void *arg, struct kml_priv * priv)
641 struct hook_write_msg * msg = arg;
642 //return smfs_post_rec_write(inode, msg->dentry, &msg->count, &msg->pos);
643 struct smfs_super_info *sinfo;
644 char *buffer = NULL, *pbuf;
645 int rc = 0, length = 0, buf_len = 0;
648 if (!SMFS_INODE_OVER_WRITE(dentry->d_inode) &&
649 !SMFS_INODE_DIRTY_WRITE(dentry->d_inode)) {
650 sinfo = S2SMI(dentry->d_inode->i_sb);
654 OBD_ALLOC(buffer, PAGE_SIZE);
656 GOTO(exit, rc = -ENOMEM);
659 KML_BUF_REC_INIT(buffer, pbuf, buf_len);
660 rc = smfs_log_path(dir->i_sb, dentry, pbuf, buf_len);
665 memcpy(buffer, &rc, sizeof(int));
666 length = rc + sizeof(int);
668 rc = smfs_pack_rec(pbuf, dentry, dir,
669 data1, data2, REINT_WRITE);
675 rc = smfs_llog_add_rec(sinfo, (void*)buffer, length);
678 rc = smfs_init_extents_ea(dentry->d_inode);
682 if (dentry->d_inode->i_size == 0) {
683 smfs_set_dirty_flags(dentry->d_inode, SMFS_OVER_WRITE);
686 loff_t off = *((loff_t*)data1);
687 size_t count = *((size_t*)data2);
689 rc = smfs_insert_extents_ea(dentry->d_inode, off, count);
692 if (all_blocks_present_ea(dentry->d_inode)){
693 smfs_set_dirty_flags(dentry->d_inode, SMFS_OVER_WRITE);
694 smfs_remove_all_extents_ea(dentry->d_inode);
696 smfs_set_dirty_flags(dentry->d_inode, SMFS_DIRTY_WRITE);
701 OBD_FREE(buffer, PAGE_SIZE);
706 typedef int (*post_kml_op)(struct inode * inode, void *msg, struct kml_priv * priv);
707 static post_kml_op smfs_kml_post[HOOK_MAX] = {
708 [HOOK_CREATE] kml_create,
710 [HOOK_LINK] kml_link,
711 [HOOK_UNLINK] kml_unlink,
712 [HOOK_SYMLINK] kml_symlink,
713 [HOOK_MKDIR] kml_create,
714 [HOOK_RMDIR] kml_unlink,
715 [HOOK_MKNOD] kml_create,
716 [HOOK_RENAME] kml_rename,
717 [HOOK_SETATTR] kml_setattr,
720 [HOOK_F_SETXATTR] kml_setxattr,
723 static int smfs_kml_post_op(int code, struct inode * inode,
724 void * msg, int ret, void * priv)
730 //KML don't handle failed ops
734 if (smfs_kml_post[code]) {
735 CDEBUG(D_INODE,"KML: inode %lu, code: %u\n", inode->i_ino, code);
736 rc = smfs_kml_post[code](inode, msg, priv);
743 static int smfs_exit_kml(struct super_block *sb, void * arg, struct kml_priv * priv)
745 struct smfs_plugin * plg = NULL;
748 plg = smfs_deregister_plugin(sb, SMFS_PLG_KML);
750 OBD_FREE(plg, sizeof(*plg));
752 CERROR("Cannot find KLM plugin while unregistering\n");
755 OBD_FREE(priv, sizeof(*priv));
761 static int smfs_trans_kml (struct super_block *sb, void *arg,
762 struct kml_priv * priv)
766 //TODO: pass fs opcode and see if kml can participate or not
767 //one record in log per operation
773 extern int mds_rec_pack(int, char*, struct dentry*, struct inode*, void*, void*);
775 static int smfs_start_kml(struct super_block *sb, void *arg,
776 struct kml_priv * kml_p)
779 struct smfs_super_info * smb = S2SMI(sb);
780 struct llog_ctxt **ctxt = &smb->smsi_kml_log;
781 struct obd_device *obd = arg;
784 //is plugin already activated
785 if (SMFS_IS(smb->plg_flags, SMFS_PLG_KML))
788 if (obd && obd->obd_type && obd->obd_type->typ_name) {
789 if (strcmp(obd->obd_type->typ_name, "mds"))
793 kml_p->pack_fn = mds_rec_pack;
795 //this will do OBD_ALLOC() for ctxt
796 rc = llog_catalog_setup(ctxt, KML_LOG_NAME, smb->smsi_exp,
797 smb->smsi_ctxt, smb->sm_fsfilt,
799 smb->smsi_objects_dir);
802 CERROR("Failed to initialize kml log list catalog %d\n", rc);
806 (*ctxt)->llog_proc_cb = NULL;//smfs_llog_process_rec_cb;
808 SMFS_SET(smb->plg_flags, SMFS_PLG_KML);
813 int smfs_stop_kml(struct super_block *sb, void *arg,
814 struct kml_priv * kml_p)
816 struct smfs_super_info * smb = S2SMI(sb);
817 struct llog_ctxt *ctxt = smb->smsi_kml_log;
820 if (!SMFS_IS(smb->plg_flags, SMFS_PLG_KML))
823 SMFS_CLEAR(smb->plg_flags, SMFS_PLG_KML);
825 llog_catalog_cleanup(ctxt);
826 OBD_FREE(ctxt, sizeof(*ctxt));
831 typedef int (*kml_helper)(struct super_block * sb, void *msg, struct kml_priv *);
832 static kml_helper smfs_kml_helpers[PLG_HELPER_MAX] = {
833 [PLG_EXIT] smfs_exit_kml,
834 [PLG_START] smfs_start_kml,
835 [PLG_STOP] smfs_stop_kml,
836 [PLG_TRANS_SIZE] smfs_trans_kml,
837 [PLG_TEST_INODE] NULL,
838 [PLG_SET_INODE] NULL,
841 static int smfs_kml_help_op(int code, struct super_block * sb,
842 void * arg, void * priv)
846 if (smfs_kml_helpers[code])
847 rc = smfs_kml_helpers[code](sb, arg, (struct kml_priv *) priv);
851 int smfs_init_kml(struct super_block *sb)
854 struct kml_priv * priv = NULL;
855 struct smfs_plugin * plg = NULL;
858 OBD_ALLOC(plg, sizeof(*plg));
864 plg->plg_type = SMFS_PLG_KML;
865 plg->plg_pre_op = NULL;
866 plg->plg_post_op = &smfs_kml_post_op;
867 plg->plg_helper = &smfs_kml_help_op;
869 OBD_ALLOC(priv, sizeof(*priv));
875 plg->plg_private = priv;
877 rc = smfs_register_plugin(sb, plg);
882 OBD_FREE(priv, sizeof(*priv));
885 OBD_FREE(plg, sizeof(*plg));