Whamcloud - gitweb
LU-1347 style: removes obsolete EXPORT_SYMTAB macros v2
[fs/lustre-release.git] / lustre / lfsck / lfsck_bookmark.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.  A copy is
14  * included in the COPYING file that accompanied this code.
15
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2012, 2013, Intel Corporation.
24  */
25 /*
26  * lustre/lfsck/lfsck_bookmark.c
27  *
28  * Author: Fan, Yong <fan.yong@intel.com>
29  */
30
31 #define DEBUG_SUBSYSTEM S_LFSCK
32
33 #include <lu_object.h>
34 #include <dt_object.h>
35 #include <lustre_fid.h>
36 #include <lustre/lustre_user.h>
37
38 #include "lfsck_internal.h"
39
40 #define LFSCK_BOOKMARK_MAGIC    0x20130C1D
41
42 static const char lfsck_bookmark_name[] = "lfsck_bookmark";
43
44 static void lfsck_bookmark_le_to_cpu(struct lfsck_bookmark *des,
45                                      struct lfsck_bookmark *src)
46 {
47         des->lb_magic = le32_to_cpu(src->lb_magic);
48         des->lb_version = le16_to_cpu(src->lb_version);
49         des->lb_param = le16_to_cpu(src->lb_param);
50         des->lb_speed_limit = le32_to_cpu(src->lb_speed_limit);
51 }
52
53 static void lfsck_bookmark_cpu_to_le(struct lfsck_bookmark *des,
54                                      struct lfsck_bookmark *src)
55 {
56         des->lb_magic = cpu_to_le32(src->lb_magic);
57         des->lb_version = cpu_to_le16(src->lb_version);
58         des->lb_param = cpu_to_le16(src->lb_param);
59         des->lb_speed_limit = cpu_to_le32(src->lb_speed_limit);
60 }
61
62 static int lfsck_bookmark_load(const struct lu_env *env,
63                                struct lfsck_instance *lfsck)
64 {
65         loff_t pos = 0;
66         int    len = sizeof(struct lfsck_bookmark);
67         int    rc;
68
69         rc = dt_record_read(env, lfsck->li_bookmark_obj,
70                             lfsck_buf_get(env, &lfsck->li_bookmark_disk, len),
71                             &pos);
72         if (rc == 0) {
73                 struct lfsck_bookmark *bm = &lfsck->li_bookmark_ram;
74
75                 lfsck_bookmark_le_to_cpu(bm, &lfsck->li_bookmark_disk);
76                 if (bm->lb_magic != LFSCK_BOOKMARK_MAGIC) {
77                         CWARN("%.16s: invalid lfsck_bookmark magic "
78                               "0x%x != 0x%x\n", lfsck_lfsck2name(lfsck),
79                               bm->lb_magic, LFSCK_BOOKMARK_MAGIC);
80                         /* Process it as new lfsck_bookmark. */
81                         rc = -ENODATA;
82                 }
83         } else {
84                 if (rc == -EFAULT && pos == 0)
85                         /* return -ENODATA for empty lfsck_bookmark. */
86                         rc = -ENODATA;
87                 else
88                         CERROR("%.16s: fail to load lfsck_bookmark, "
89                                "expected = %d, rc = %d\n",
90                                lfsck_lfsck2name(lfsck), len, rc);
91         }
92         return rc;
93 }
94
95 int lfsck_bookmark_store(const struct lu_env *env, struct lfsck_instance *lfsck)
96 {
97         struct thandle    *handle;
98         struct dt_object  *obj    = lfsck->li_bookmark_obj;
99         loff_t             pos    = 0;
100         int                len    = sizeof(struct lfsck_bookmark);
101         int                rc;
102         ENTRY;
103
104         lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk,
105                                  &lfsck->li_bookmark_ram);
106         handle = dt_trans_create(env, lfsck->li_bottom);
107         if (IS_ERR(handle)) {
108                 rc = PTR_ERR(handle);
109                 CERROR("%.16s: fail to create trans for storing "
110                        "lfsck_bookmark: %d\n,", lfsck_lfsck2name(lfsck), rc);
111                 RETURN(rc);
112         }
113
114         rc = dt_declare_record_write(env, obj, len, 0, handle);
115         if (rc != 0) {
116                 CERROR("%.16s: fail to declare trans for storing "
117                        "lfsck_bookmark: %d\n,", lfsck_lfsck2name(lfsck), rc);
118                 GOTO(out, rc);
119         }
120
121         rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
122         if (rc != 0) {
123                 CERROR("%.16s: fail to start trans for storing "
124                        "lfsck_bookmark: %d\n,", lfsck_lfsck2name(lfsck), rc);
125                 GOTO(out, rc);
126         }
127
128         rc = dt_record_write(env, obj,
129                              lfsck_buf_get(env, &lfsck->li_bookmark_disk, len),
130                              &pos, handle);
131         if (rc != 0)
132                 CERROR("%.16s: fail to store lfsck_bookmark, expected = %d, "
133                        "rc = %d\n", lfsck_lfsck2name(lfsck), len, rc);
134
135         GOTO(out, rc);
136
137 out:
138         dt_trans_stop(env, lfsck->li_bottom, handle);
139         return rc;
140 }
141
142 static int lfsck_bookmark_init(const struct lu_env *env,
143                                struct lfsck_instance *lfsck)
144 {
145         struct lfsck_bookmark *mb = &lfsck->li_bookmark_ram;
146         int rc;
147
148         memset(mb, 0, sizeof(*mb));
149         mb->lb_magic = LFSCK_BOOKMARK_MAGIC;
150         mb->lb_version = LFSCK_VERSION_V2;
151         mutex_lock(&lfsck->li_mutex);
152         rc = lfsck_bookmark_store(env, lfsck);
153         mutex_unlock(&lfsck->li_mutex);
154         return rc;
155 }
156
157 int lfsck_bookmark_setup(const struct lu_env *env,
158                          struct lfsck_instance *lfsck)
159 {
160         struct dt_object *obj;
161         int               rc;
162         ENTRY;
163
164         obj = local_file_find_or_create(env, lfsck->li_los,
165                                         lfsck->li_local_root,
166                                         lfsck_bookmark_name,
167                                         S_IFREG | S_IRUGO | S_IWUSR);
168         if (IS_ERR(obj))
169                 RETURN(PTR_ERR(obj));
170
171         lfsck->li_bookmark_obj = obj;
172         rc = lfsck_bookmark_load(env, lfsck);
173         if (rc == -ENODATA)
174                 rc = lfsck_bookmark_init(env, lfsck);
175
176         RETURN(rc);
177 }