Whamcloud - gitweb
- landing b_fid.
[fs/lustre-release.git] / lustre / smfs / smfs_llog.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/smfs/smfs_llog.c
5  *  Lustre filesystem abstraction routines
6  *
7  *  Copyright (C) 2004 Cluster File Systems, Inc.
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
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.
14  *
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.
19  *
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.
23  */
24
25 #define DEBUG_SUBSYSTEM S_SM
26
27 #include <linux/lustre_log.h>
28 #include <linux/lustre_fsfilt.h>
29 #include <linux/lustre_smfs.h>
30 #include <linux/lvfs.h>
31
32 #include "smfs_internal.h"
33
34 static int smfs_llog_process_rec_cb(struct llog_handle *handle,
35                                     struct llog_rec_hdr *rec, void *data)
36 {
37         char   *rec_buf ;
38         struct smfs_proc_args *args = (struct smfs_proc_args *)data;
39         struct lvfs_run_ctxt saved;
40         int    rc = 0;
41
42         if (!(le32_to_cpu(handle->lgh_hdr->llh_flags) & LLOG_F_IS_PLAIN)) {
43                 CERROR("log is not plain\n");
44                 RETURN(-EINVAL);
45         }
46
47         if (le32_to_cpu(rec->lrh_type) == LLOG_GEN_REC) {
48                 struct llog_cookie cookie;
49
50                 cookie.lgc_lgl = handle->lgh_id;
51                 cookie.lgc_index = le32_to_cpu(rec->lrh_index);
52
53                 llog_cancel(handle->lgh_ctxt, 1, &cookie, 0, NULL);
54                 RETURN(LLOG_PROC_BREAK);
55         }
56
57         if (le32_to_cpu(rec->lrh_type) != SMFS_UPDATE_REC)
58                 RETURN(-EINVAL);
59
60         rec_buf = (char*) (rec + 1);
61
62         if (!S2SMI(args->sr_sb)->smsi_ctxt)
63                 GOTO(exit, rc = -ENODEV);
64
65         push_ctxt(&saved, S2SMI(args->sr_sb)->smsi_ctxt, NULL);
66 #if 0
67         /*FIXME later should first unpack the rec,
68          * then call lvfs_reint or lvfs_undo
69          * kml rec format has changed lvfs_reint lvfs_undo should
70          * be rewrite FIXME later*/
71         if (SMFS_DO_REINT_REC(args->sr_flags))
72                 rc = lvfs_reint(args->sr_sb, rec_buf);
73         else
74                 rc = lvfs_undo(args->sr_sb, rec_buf);
75 #endif
76         if (!rc && !SMFS_DO_REC_ALL(args->sr_flags)) {
77                 args->sr_count --;
78                 if (args->sr_count == 0)
79                         rc = LLOG_PROC_BREAK;
80         }
81         pop_ctxt(&saved, S2SMI(args->sr_sb)->smsi_ctxt, NULL);
82 exit:
83         RETURN(rc);
84 }
85
86 int smfs_llog_setup(struct super_block *sb, struct vfsmount *mnt)
87 {
88         struct llog_ctxt **ctxt = &(S2SMI(sb)->smsi_rec_log);
89         struct lvfs_run_ctxt saved;
90         struct dentry *dentry;
91         int rc = 0, rc2;
92
93         /* create OBJECTS and LOGS for writing logs */
94         ENTRY;
95
96         LASSERT(mnt);
97
98         push_ctxt(&saved, S2SMI(sb)->smsi_ctxt, NULL);
99         dentry = simple_mkdir(current->fs->pwd, "LOGS", 0777, 1);
100         if (IS_ERR(dentry)) {
101                 rc = PTR_ERR(dentry);
102                 CERROR("cannot create LOGS directory: rc = %d\n", rc);
103                 GOTO(exit, rc = -EINVAL);
104         }
105
106         S2SMI(sb)->smsi_logs_dir = dentry;
107         dentry = simple_mkdir(current->fs->pwd, "OBJECTS", 0777, 1);
108         if (IS_ERR(dentry)) {
109                 rc = PTR_ERR(dentry);
110                 CERROR("cannot create OBJECTS directory: rc = %d\n", rc);
111                 GOTO(exit, rc = -EINVAL);
112         }
113
114         S2SMI(sb)->smsi_objects_dir = dentry;
115
116         /* write log will not write to KML, cleanup kml flags */
117         SMFS_CLEAN_INODE_REC(S2SMI(sb)->smsi_objects_dir->d_inode);
118         SMFS_CLEAN_INODE_REC(S2SMI(sb)->smsi_logs_dir->d_inode);
119
120         /* log create does not call cache hooks, cleanup hook flags */
121         SMFS_CLEAN_INODE_CACHE_HOOK(S2SMI(sb)->smsi_objects_dir->d_inode);
122         SMFS_CLEAN_INODE_CACHE_HOOK(S2SMI(sb)->smsi_logs_dir->d_inode);
123
124         if (SMFS_DO_REC(S2SMI(sb))) {
125                 rc = llog_catalog_setup(ctxt, KML_LOG_NAME, S2SMI(sb)->smsi_exp,
126                                         S2SMI(sb)->smsi_ctxt, S2SMI(sb)->sm_fsfilt,
127                                         S2SMI(sb)->smsi_logs_dir,
128                                         S2SMI(sb)->smsi_objects_dir);
129                 (*ctxt)->llog_proc_cb = smfs_llog_process_rec_cb;
130         }
131
132         if (SMFS_CACHE_HOOK(S2SMI(sb))) {
133                 rc2 = cache_space_hook_setup(sb);
134                 if (!rc && rc2)
135                         rc = rc2;
136         }
137 exit:
138         pop_ctxt(&saved, S2SMI(sb)->smsi_ctxt, NULL);
139         RETURN(rc);
140 }
141
142 int smfs_llog_cleanup(struct super_block *sb)
143 {
144         struct llog_ctxt *ctxt = S2SMI(sb)->smsi_rec_log;
145         int rc = 0, rc2;
146         ENTRY;
147
148         if (SMFS_CACHE_HOOK(S2SMI(sb)))
149                 rc = cache_space_hook_cleanup();
150
151         if (SMFS_DO_REC(S2SMI(sb))) {
152                 rc2 = llog_catalog_cleanup(ctxt);
153                 OBD_FREE(ctxt, sizeof(*ctxt));
154                 if (!rc)
155                         rc = rc2;
156         }
157
158         if (S2SMI(sb)->smsi_logs_dir) {
159                 l_dput(S2SMI(sb)->smsi_logs_dir);
160                 S2SMI(sb)->smsi_logs_dir = NULL;
161         }
162         if (S2SMI(sb)->smsi_objects_dir) {
163                 l_dput(S2SMI(sb)->smsi_objects_dir);
164                 S2SMI(sb)->smsi_objects_dir = NULL;
165         }
166         RETURN(rc);
167 }
168
169 int smfs_llog_add_rec(struct smfs_super_info *sinfo, void *data, int data_size)
170 {
171         struct llog_rec_hdr rec;
172         int rc = 0;
173
174         rec.lrh_len = size_round(data_size);
175         rec.lrh_type = SMFS_UPDATE_REC;
176
177         rc = llog_add(sinfo->smsi_rec_log, &rec, data, NULL, 0, NULL, NULL, NULL);
178         if (rc != 1) {
179                 CERROR("error adding kml rec: %d\n", rc);
180                 RETURN(-EINVAL);
181         }
182         RETURN(0);
183 }