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"
42 #define KML_BUF_REC_INIT(buffer, pbuf, len) \
44 pbuf = buffer + sizeof(int); \
48 #define KML_BUF_REC_END(buffer, length, pbuf) \
51 memcpy(buffer, &len, sizeof(len)); \
52 length += sizeof(int); \
53 pbuf = buffer + length; \
56 static smfs_pack_rec_func smfs_get_rec_pack_type(struct super_block *sb)
59 struct smfs_super_info *smsi = S2SMI(sb);
61 idx = GET_REC_PACK_TYPE_INDEX(smsi->smsi_flags);
62 return smsi->smsi_pack_rec[idx];
65 static int smfs_post_kml_rec(struct inode *dir, void *de, void *data1,
68 static int smfs_rec_post_hook(struct inode *inode, void *dentry,
69 void *data1, void *data2, int op, void *handle)
74 if (smfs_do_rec(inode))
75 rc = smfs_post_kml_rec(inode, dentry, data1, data2, op);
80 #define KML_HOOK "kml_hook"
82 int smfs_rec_init(struct super_block *sb)
85 struct smfs_super_info *smfs_info = S2SMI(sb);
86 struct smfs_hook_ops *rec_hops = NULL;
88 SMFS_SET_REC(smfs_info);
90 rc = ost_rec_pack_init(smfs_info);
94 rc = mds_rec_pack_init(smfs_info);
98 rec_hops = smfs_alloc_hook_ops(KML_HOOK, NULL, smfs_rec_post_hook);
102 rc = smfs_register_hook_ops(smfs_info, rec_hops);
103 if (rc && rec_hops) {
104 smfs_unregister_hook_ops(smfs_info, rec_hops->smh_name);
105 smfs_free_hook_ops(rec_hops);
110 int smfs_rec_cleanup(struct smfs_super_info *smfs_info)
112 struct smfs_hook_ops *rec_hops;
115 rec_hops = smfs_unregister_hook_ops(smfs_info, KML_HOOK);
116 smfs_free_hook_ops(rec_hops);
117 SMFS_CLEAN_REC(smfs_info);
122 static inline void copy_inode_attr(struct iattr *iattr,
125 iattr->ia_mode = inode->i_mode;
126 iattr->ia_uid = inode->i_uid;
127 iattr->ia_gid = inode->i_gid;
128 iattr->ia_atime = inode->i_atime;
129 iattr->ia_ctime = inode->i_ctime;
130 iattr->ia_mtime = inode->i_mtime;
131 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)
141 args_len = strlen(args_data);
143 *size = *((int*)(in_data));
144 rc = *size + sizeof(int);
146 OBD_ALLOC(*p_buffer, *size + args_len + 1);
150 /* first copy reint dir. */
152 memcpy(*p_buffer, args_data, args_len);
154 /* then copy the node name. */
155 memcpy(*p_buffer + args_len,
156 (in_data + sizeof(int)), *size);
163 int smfs_rec_unpack(struct smfs_proc_args *args, char *record,
164 char **pbuf, int *opcode)
166 int offset = *(int *)(record);
167 char *tmp = record + offset + sizeof(int);
169 *opcode = *(int *)tmp;
170 *pbuf = tmp + sizeof(*opcode);
174 EXPORT_SYMBOL(smfs_rec_unpack);
176 int smfs_start_rec(struct super_block *sb, struct vfsmount *mnt)
178 struct dentry *dentry;
179 struct lvfs_run_ctxt saved;
183 if (SMFS_INIT_REC(S2SMI(sb)) ||
184 (!SMFS_DO_REC(S2SMI(sb)) && !SMFS_CACHE_HOOK(S2SMI(sb))))
187 rc = smfs_llog_setup(sb, mnt);
190 push_ctxt(&saved, S2SMI(sb)->smsi_ctxt, NULL);
191 dentry = simple_mkdir(current->fs->pwd, "DELETE", 0777, 1);
192 if (IS_ERR(dentry)) {
193 rc = PTR_ERR(dentry);
194 CERROR("cannot create DELETE directory: rc = %d\n", rc);
195 GOTO(err_exit, rc = -EINVAL);
197 S2SMI(sb)->smsi_delete_dir = dentry;
200 SMFS_SET_INIT_REC(S2SMI(sb));
202 pop_ctxt(&saved, S2SMI(sb)->smsi_ctxt, NULL);
205 if (S2SMI(sb)->smsi_ctxt)
206 OBD_FREE(S2SMI(sb)->smsi_ctxt, sizeof(struct lvfs_run_ctxt));
209 EXPORT_SYMBOL(smfs_start_rec);
211 int smfs_post_setup(struct super_block *sb, struct vfsmount *mnt)
213 struct lvfs_run_ctxt *current_ctxt = NULL;
214 struct smfs_super_info *smb = S2SMI(sb);
216 OBD_ALLOC(current_ctxt, sizeof(*current_ctxt));
219 OBD_SET_CTXT_MAGIC(current_ctxt);
221 current_ctxt->pwdmnt = mnt;
222 current_ctxt->pwd = mnt->mnt_root;
223 current_ctxt->fs = get_ds();
224 smb->smsi_ctxt = current_ctxt;
228 EXPORT_SYMBOL(smfs_post_setup);
230 int smfs_post_cleanup(struct super_block *sb)
232 struct smfs_super_info *smb = S2SMI(sb);
237 OBD_FREE(S2SMI(sb)->smsi_ctxt, sizeof(struct lvfs_run_ctxt));
240 EXPORT_SYMBOL(smfs_post_cleanup);
242 int smfs_stop_rec(struct super_block *sb)
246 if (!SMFS_INIT_REC(S2SMI(sb)) ||
247 (!SMFS_DO_REC(S2SMI(sb)) && !SMFS_CACHE_HOOK(S2SMI(sb))))
250 rc = smfs_llog_cleanup(sb);
252 SMFS_CLEAN_INIT_REC(S2SMI(sb));
254 if (S2SMI(sb)->smsi_delete_dir) {
255 l_dput(S2SMI(sb)->smsi_delete_dir);
256 S2SMI(sb)->smsi_delete_dir = NULL;
260 EXPORT_SYMBOL(smfs_stop_rec);
262 int smfs_write_extents(struct inode *dir, struct dentry *dentry,
263 unsigned long from, unsigned long num)
265 return smfs_post_rec_write(dir, dentry, &from, &num);
267 EXPORT_SYMBOL(smfs_write_extents);
269 int smfs_rec_setattr(struct inode *dir, struct dentry *dentry,
272 return smfs_post_rec_setattr(dir, dentry, attr, NULL);
274 EXPORT_SYMBOL(smfs_rec_setattr);
276 int smfs_rec_md(struct inode *inode, void *lmm, int lmm_size)
278 char *set_lmm = NULL;
282 if (!SMFS_DO_REC(S2SMI(inode->i_sb)))
286 OBD_ALLOC(set_lmm, lmm_size + sizeof(lmm_size));
289 memcpy(set_lmm, &lmm_size, sizeof(lmm_size));
290 memcpy(set_lmm + sizeof(lmm_size), lmm, lmm_size);
291 rc = smfs_post_rec_setattr(inode, NULL, NULL, set_lmm);
293 CERROR("Error: Record md for inode %lu rc=%d\n",
298 OBD_FREE(set_lmm, lmm_size + sizeof(lmm_size));
301 EXPORT_SYMBOL(smfs_rec_md);
303 int smfs_rec_precreate(struct dentry *dentry, int *num, struct obdo *oa)
305 return smfs_post_rec_create(dentry->d_inode, dentry, num, oa);
307 EXPORT_SYMBOL(smfs_rec_precreate);
309 int smfs_process_rec(struct super_block *sb,
310 int count, char *dir, int flags)
312 struct llog_ctxt *ctxt;
313 struct llog_handle *loghandle;
314 struct smfs_proc_args args;
317 if (!SMFS_INIT_REC(S2SMI(sb))) {
318 CWARN("Did not start up rec server \n");
322 memset(&args, 0, sizeof(struct smfs_proc_args));
324 args.sr_count = count;
326 args.sr_flags = flags ;
327 ctxt = S2SMI(sb)->smsi_rec_log;
328 loghandle = ctxt->loc_handle;
331 if (SMFS_DO_REINT_REC(flags)) {
332 struct llog_gen_rec *lgr;
334 /* for reint rec, we need insert a gen rec to identify
335 * the end of the rec.*/
336 OBD_ALLOC(lgr, sizeof(*lgr));
339 lgr->lgr_hdr.lrh_len = lgr->lgr_tail.lrt_len = sizeof(*lgr);
340 lgr->lgr_hdr.lrh_type = LLOG_GEN_REC;
341 lgr->lgr_gen = ctxt->loc_gen;
342 rc = llog_add(ctxt, &lgr->lgr_hdr, NULL, NULL, 1,
344 OBD_FREE(lgr, sizeof(*lgr));
349 SET_REC_COUNT_FLAGS(args.sr_flags, SMFS_REC_ALL);
352 if (SMFS_DO_REINT_REC(flags))
353 rc = llog_cat_process(loghandle, ctxt->llog_proc_cb,
356 rc = llog_cat_reverse_process(loghandle,
359 if (rc == LLOG_PROC_BREAK)
365 /*smfs_path is gotten from intermezzo*/
366 static char* smfs_path(struct dentry *dentry, struct dentry *root, char *buffer,
369 char * end = buffer + buflen;
370 char * name = buffer;
371 char * buf_end = buffer + buflen;
381 struct dentry * parent;
386 parent = dentry->d_parent;
387 if (dentry == parent)
389 namelen = dentry->d_name.len;
390 buflen -= namelen + 1;
394 memcpy(end, dentry->d_name.name, namelen);
400 while (end != buf_end)
406 static int smfs_log_path(struct super_block *sb,
407 struct dentry *dentry,
411 struct dentry *root=sb->s_root;
412 char *p_name = buffer + sizeof(int);
416 name = smfs_path(dentry, root, p_name, buffer_len - sizeof(int));
417 namelen = cpu_to_le32(strlen(p_name));
418 memcpy(buffer, &namelen, sizeof(int));
420 namelen += sizeof(int);
424 static inline int log_it(char *buffer, void *data, int length)
426 memcpy(buffer, &length, sizeof(int));
427 memcpy(buffer + sizeof(int), data, length);
428 return (sizeof(int) + length);
431 static int smfs_pack_rec (char *buffer, struct dentry *dentry,
432 struct inode *dir, void *data1,
435 smfs_pack_rec_func pack_func;
438 pack_func = smfs_get_rec_pack_type(dir->i_sb);
442 rc = pack_func(buffer, dentry, dir, data1, data2, op);
446 int smfs_post_rec_create(struct inode *dir, struct dentry *dentry,
447 void *data1, void *data2)
449 struct smfs_super_info *sinfo;
450 char *buffer = NULL, *pbuf;
451 int rc = 0, length = 0, buf_len = 0;
453 sinfo = S2SMI(dentry->d_inode->i_sb);
457 OBD_ALLOC(buffer, PAGE_SIZE);
459 GOTO(exit, rc = -ENOMEM);
462 KML_BUF_REC_INIT(buffer, pbuf, buf_len);
463 rc = smfs_log_path(dir->i_sb, dentry, pbuf, buf_len);
467 KML_BUF_REC_END(buffer, length, pbuf);
469 rc = smfs_pack_rec(pbuf, dentry, dir,
470 data1, data2, REINT_CREATE);
475 rc = smfs_llog_add_rec(sinfo, (void*)buffer, length);
478 OBD_FREE(buffer, PAGE_SIZE);
483 static int smfs_post_rec_link(struct inode *dir, struct dentry *dentry,
484 void *data1, void *data2)
486 struct dentry *new_dentry = (struct dentry *)data1;
487 int rc = 0, length = 0, buf_len = 0;
488 char *buffer = NULL, *pbuf = NULL;
489 struct smfs_super_info *sinfo;
491 sinfo = S2SMI(dir->i_sb);
494 OBD_ALLOC(buffer, PAGE_SIZE);
496 GOTO(exit, rc = -ENOMEM);
499 KML_BUF_REC_INIT(buffer, pbuf, buf_len);
501 rc = smfs_log_path(dir->i_sb, dentry, pbuf, buf_len);
506 KML_BUF_REC_END(buffer, length, pbuf);
508 rc = smfs_pack_rec(pbuf, dentry, dir, dentry,
509 new_dentry, REINT_LINK);
514 rc = smfs_llog_add_rec(sinfo, (void *)buffer, length);
517 OBD_FREE(buffer, PAGE_SIZE);
522 static int smfs_post_rec_unlink(struct inode *dir, struct dentry *dentry,
523 void *data1, void *data2)
525 struct smfs_super_info *sinfo;
526 int mode = *((int*)data1);
527 char *buffer = NULL, *pbuf = NULL;
528 int length = 0, rc = 0, buf_len = 0;
530 sinfo = S2SMI(dentry->d_inode->i_sb);
534 OBD_ALLOC(buffer, PAGE_SIZE);
536 GOTO(exit, rc = -ENOMEM);
539 KML_BUF_REC_INIT(buffer, pbuf, buf_len);
540 rc = smfs_log_path(dir->i_sb, dentry, pbuf, buf_len);
545 KML_BUF_REC_END(buffer, length, pbuf);
546 rc = smfs_pack_rec(pbuf, dentry, dir,
547 &mode, NULL, REINT_UNLINK);
553 rc = smfs_llog_add_rec(sinfo, (void*)buffer, length);
556 OBD_FREE(buffer, PAGE_SIZE);
561 static int smfs_post_rec_rename(struct inode *dir, struct dentry *dentry,
562 void *data1, void *data2)
564 struct smfs_super_info *sinfo;
565 struct inode *new_dir = (struct inode *)data1;
566 struct dentry *new_dentry = (struct dentry *)data2;
567 char *buffer = NULL, *pbuf = NULL;
568 int rc = 0, length = 0, buf_len = 0;
570 sinfo = S2SMI(dir->i_sb);
574 OBD_ALLOC(buffer, PAGE_SIZE);
576 GOTO(exit, rc = -ENOMEM);
579 KML_BUF_REC_INIT(buffer, pbuf, buf_len);
580 rc = smfs_log_path(dir->i_sb, dentry, pbuf, buf_len);
588 /* record new_dentry path. */
589 rc = smfs_log_path(dir->i_sb, new_dentry, pbuf, buf_len);
594 KML_BUF_REC_END(buffer, length, pbuf);
596 rc = smfs_pack_rec(pbuf, dentry, dir,
597 new_dir, new_dentry, REINT_RENAME);
602 rc = smfs_llog_add_rec(sinfo, (void*)buffer, length);
605 OBD_FREE(buffer, PAGE_SIZE);
609 static int smfs_insert_extents_ea(struct inode *inode, size_t from, loff_t num)
611 struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
614 if (SMFS_INODE_OVER_WRITE(inode))
617 rc = fsfilt->fs_insert_extents_ea(inode, OFF2BLKS(from, inode),
618 SIZE2BLKS(num, inode));
622 static int smfs_remove_extents_ea(struct inode *inode, size_t from, loff_t num)
624 struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
627 rc = fsfilt->fs_remove_extents_ea(inode, OFF2BLKS(from, inode),
628 SIZE2BLKS(num, inode));
633 static int smfs_remove_all_extents_ea(struct inode *inode)
635 struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
638 rc = fsfilt->fs_remove_extents_ea(inode, 0, 0xffffffff);
641 static int smfs_init_extents_ea(struct inode *inode)
643 struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
646 rc = fsfilt->fs_init_extents_ea(inode);
650 static int smfs_set_dirty_flags(struct inode *inode, int flags)
652 struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
656 if (SMFS_INODE_OVER_WRITE(inode))
658 /*FIXME later, the blocks needed in journal here will be recalculated*/
659 handle = smfs_trans_start(inode, FSFILT_OP_SETATTR, NULL);
660 if (IS_ERR(handle)) {
661 CERROR("smfs_set_dirty_flag:no space for transaction\n");
664 if ((!SMFS_INODE_DIRTY_WRITE(inode) && (!SMFS_INODE_OVER_WRITE(inode))) ||
665 ((flags == SMFS_OVER_WRITE) && (SMFS_INODE_DIRTY_WRITE(inode)))) {
666 rc = fsfilt->fs_set_xattr(inode, handle, REINT_EXTENTS_FLAGS,
667 &flags, sizeof(int));
671 if (flags == SMFS_OVER_WRITE)
672 SMFS_SET_INODE_OVER_WRITE(inode);
674 SMFS_SET_INODE_DIRTY_WRITE(inode);
676 smfs_trans_commit(inode, handle, 0);
680 int smfs_post_rec_setattr(struct inode *inode, struct dentry *dentry,
681 void *data1, void *data2)
683 struct smfs_super_info *sinfo;
684 struct iattr *attr = (struct iattr *)data1;
685 char *buffer = NULL, *pbuf;
686 int rc = 0, length = 0, buf_len = 0;
688 sinfo = S2SMI(inode->i_sb);
692 OBD_ALLOC(buffer, PAGE_SIZE);
694 GOTO(exit, rc = -ENOMEM);
697 KML_BUF_REC_INIT(buffer, pbuf, buf_len);
698 rc = smfs_log_path(inode->i_sb, dentry, pbuf, buf_len);
703 KML_BUF_REC_END(buffer, length, pbuf);
705 rc = smfs_pack_rec(pbuf, dentry, inode,
706 data1, data2, REINT_SETATTR);
712 rc = smfs_llog_add_rec(sinfo, (void*)buffer, length);
714 if (attr && attr->ia_valid & ATTR_SIZE) {
715 smfs_remove_extents_ea(inode, attr->ia_size,
717 if (attr->ia_size == 0)
718 smfs_set_dirty_flags(inode, SMFS_OVER_WRITE);
720 smfs_set_dirty_flags(inode, SMFS_DIRTY_WRITE);
725 OBD_FREE(buffer, PAGE_SIZE);
729 static int all_blocks_present_ea(struct inode *inode)
735 int smfs_post_rec_write(struct inode *dir, struct dentry *dentry, void *data1,
738 struct smfs_super_info *sinfo;
739 char *buffer = NULL, *pbuf;
740 int rc = 0, length = 0, buf_len = 0;
742 if (!SMFS_INODE_OVER_WRITE(dentry->d_inode) &&
743 !SMFS_INODE_DIRTY_WRITE(dentry->d_inode)) {
744 sinfo = S2SMI(dentry->d_inode->i_sb);
748 OBD_ALLOC(buffer, PAGE_SIZE);
750 GOTO(exit, rc = -ENOMEM);
753 KML_BUF_REC_INIT(buffer, pbuf, buf_len);
754 rc = smfs_log_path(dir->i_sb, dentry, pbuf, buf_len);
759 memcpy(buffer, &rc, sizeof(int));
760 length = rc + sizeof(int);
762 rc = smfs_pack_rec(pbuf, dentry, dir,
763 data1, data2, REINT_WRITE);
769 rc = smfs_llog_add_rec(sinfo, (void*)buffer, length);
772 rc = smfs_init_extents_ea(dentry->d_inode);
776 if (dentry->d_inode->i_size == 0) {
777 smfs_set_dirty_flags(dentry->d_inode, SMFS_OVER_WRITE);
780 loff_t off = *((loff_t*)data1);
781 size_t count = *((size_t*)data2);
783 rc = smfs_insert_extents_ea(dentry->d_inode, off, count);
786 if (all_blocks_present_ea(dentry->d_inode)){
787 smfs_set_dirty_flags(dentry->d_inode, SMFS_OVER_WRITE);
788 smfs_remove_all_extents_ea(dentry->d_inode);
790 smfs_set_dirty_flags(dentry->d_inode, SMFS_DIRTY_WRITE);
795 OBD_FREE(buffer, PAGE_SIZE);
799 typedef int (*post_kml_rec)(struct inode *dir, struct dentry *dentry,
800 void *data1, void *data2);
802 static post_kml_rec smfs_kml_post[HOOK_MAX + 1] = {
803 [HOOK_CREATE] smfs_post_rec_create,
805 [HOOK_LINK] smfs_post_rec_link,
806 [HOOK_UNLINK] smfs_post_rec_unlink,
807 [HOOK_SYMLINK] smfs_post_rec_create,
808 [HOOK_MKDIR] smfs_post_rec_create,
809 [HOOK_RMDIR] smfs_post_rec_unlink,
810 [HOOK_MKNOD] smfs_post_rec_create,
811 [HOOK_RENAME] smfs_post_rec_rename,
812 [HOOK_SETATTR] smfs_post_rec_setattr,
813 [HOOK_WRITE] smfs_post_rec_write,
816 static int smfs_post_kml_rec(struct inode *dir, void *de, void *data1,
819 if (smfs_kml_post[op]) {
820 struct dentry *dentry = (struct dentry *)de;
822 return smfs_kml_post[op](dir, dentry, data1, data2);