Whamcloud - gitweb
LU-3336 lfsck: namespace visible lost+found directory
[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         des->lb_async_windows = le16_to_cpu(src->lb_async_windows);
52         fid_le_to_cpu(&des->lb_lpf_fid, &src->lb_lpf_fid);
53         fid_le_to_cpu(&des->lb_last_fid, &src->lb_last_fid);
54 }
55
56 void lfsck_bookmark_cpu_to_le(struct lfsck_bookmark *des,
57                               struct lfsck_bookmark *src)
58 {
59         des->lb_magic = cpu_to_le32(src->lb_magic);
60         des->lb_version = cpu_to_le16(src->lb_version);
61         des->lb_param = cpu_to_le16(src->lb_param);
62         des->lb_speed_limit = cpu_to_le32(src->lb_speed_limit);
63         des->lb_async_windows = cpu_to_le16(src->lb_async_windows);
64         fid_cpu_to_le(&des->lb_lpf_fid, &src->lb_lpf_fid);
65         fid_cpu_to_le(&des->lb_last_fid, &src->lb_last_fid);
66 }
67
68 static int lfsck_bookmark_load(const struct lu_env *env,
69                                struct lfsck_instance *lfsck)
70 {
71         loff_t pos = 0;
72         int    len = sizeof(struct lfsck_bookmark);
73         int    rc;
74
75         rc = dt_record_read(env, lfsck->li_bookmark_obj,
76                             lfsck_buf_get(env, &lfsck->li_bookmark_disk, len),
77                             &pos);
78         if (rc == 0) {
79                 struct lfsck_bookmark *bm = &lfsck->li_bookmark_ram;
80
81                 lfsck_bookmark_le_to_cpu(bm, &lfsck->li_bookmark_disk);
82                 if (bm->lb_magic != LFSCK_BOOKMARK_MAGIC) {
83                         CWARN("%s: invalid lfsck_bookmark magic %#x != %#x\n",
84                               lfsck_lfsck2name(lfsck), bm->lb_magic,
85                               LFSCK_BOOKMARK_MAGIC);
86                         /* Process it as new lfsck_bookmark. */
87                         rc = -ENODATA;
88                 }
89         } else {
90                 if (rc == -EFAULT && pos == 0)
91                         /* return -ENODATA for empty lfsck_bookmark. */
92                         rc = -ENODATA;
93                 else
94                         CERROR("%s: fail to load lfsck_bookmark: "
95                                "expected = %d, rc = %d\n",
96                                lfsck_lfsck2name(lfsck), len, rc);
97         }
98         return rc;
99 }
100
101 int lfsck_bookmark_store(const struct lu_env *env, struct lfsck_instance *lfsck)
102 {
103         struct thandle    *handle;
104         struct dt_object  *obj    = lfsck->li_bookmark_obj;
105         loff_t             pos    = 0;
106         int                len    = sizeof(struct lfsck_bookmark);
107         int                rc;
108         ENTRY;
109
110         lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk,
111                                  &lfsck->li_bookmark_ram);
112         handle = dt_trans_create(env, lfsck->li_bottom);
113         if (IS_ERR(handle)) {
114                 rc = PTR_ERR(handle);
115                 CERROR("%s: fail to create trans for storing lfsck_bookmark: "
116                        "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
117                 RETURN(rc);
118         }
119
120         rc = dt_declare_record_write(env, obj, len, 0, handle);
121         if (rc != 0) {
122                 CERROR("%s: fail to declare trans for storing lfsck_bookmark: "
123                        "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
124                 GOTO(out, rc);
125         }
126
127         rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
128         if (rc != 0) {
129                 CERROR("%s: fail to start trans for storing lfsck_bookmark: "
130                        "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
131                 GOTO(out, rc);
132         }
133
134         rc = dt_record_write(env, obj,
135                              lfsck_buf_get(env, &lfsck->li_bookmark_disk, len),
136                              &pos, handle);
137         if (rc != 0)
138                 CERROR("%s: fail to store lfsck_bookmark: expected = %d, "
139                        "rc = %d\n", lfsck_lfsck2name(lfsck), len, rc);
140
141         GOTO(out, rc);
142
143 out:
144         dt_trans_stop(env, lfsck->li_bottom, handle);
145         return rc;
146 }
147
148 static int lfsck_bookmark_init(const struct lu_env *env,
149                                struct lfsck_instance *lfsck)
150 {
151         struct lfsck_bookmark *mb = &lfsck->li_bookmark_ram;
152         int rc;
153
154         memset(mb, 0, sizeof(*mb));
155         mb->lb_magic = LFSCK_BOOKMARK_MAGIC;
156         mb->lb_version = LFSCK_VERSION_V2;
157         mb->lb_async_windows = LFSCK_ASYNC_WIN_DEFAULT;
158         mutex_lock(&lfsck->li_mutex);
159         rc = lfsck_bookmark_store(env, lfsck);
160         mutex_unlock(&lfsck->li_mutex);
161         return rc;
162 }
163
164 int lfsck_bookmark_setup(const struct lu_env *env,
165                          struct lfsck_instance *lfsck)
166 {
167         struct dt_object *root;
168         struct dt_object *obj;
169         int               rc;
170         ENTRY;
171
172         root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
173         if (IS_ERR(root))
174                 RETURN(PTR_ERR(root));
175
176         if (unlikely(!dt_try_as_dir(env, root))) {
177                 lu_object_put(env, &root->do_lu);
178
179                 RETURN(-ENOTDIR);
180         }
181
182         obj = local_file_find_or_create(env, lfsck->li_los, root,
183                                         lfsck_bookmark_name,
184                                         S_IFREG | S_IRUGO | S_IWUSR);
185         lu_object_put(env, &root->do_lu);
186         if (IS_ERR(obj))
187                 RETURN(PTR_ERR(obj));
188
189         lfsck->li_bookmark_obj = obj;
190         rc = lfsck_bookmark_load(env, lfsck);
191         if (rc == -ENODATA)
192                 rc = lfsck_bookmark_init(env, lfsck);
193
194         RETURN(rc);
195 }