summaryrefslogtreecommitdiff
path: root/filesystems/glusterfs/patches/patch-9084
blob: d179a946495f70a37bc388c511555c3469470e7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
$NetBSD: patch-9084,v 1.1 2014/11/18 14:38:15 manu Exp $
From 23b7ead5de0f21abdcf5440a4a43dcf3a42c3822 Mon Sep 17 00:00:00 2001
From: Emmanuel Dreyfus <manu@netbsd.org>
Date: Mon, 10 Nov 2014 10:01:00 +0100
Subject: [PATCH] telldir()/seekdir() portability fixes

POSIX says that an offset obtained from telldir() can only be used
on the same DIR *. Linux is abls to reuse the offset accross
closedir()/opendir() for a given directory, but this is not portable
and such a behavior should be fixed.

An incomplete fix for the posix xlator was merged in
http://review.gluster.org/8933
This change set completes it.

- Perform the same fix index xlator.
- Use appropriate casts and variable types so that 32 bit signed
  offsets obtained by telldir() do not get clobbered when copied into
  64 bit signed types.
- modify afr-self-heald.c so that it does not use anonymous fd,
  since this will cause closedir()/opendir() between each
  syncop_readdir(). On failure we fallback to anonymous fs
  only for Linux so that we can cope with updated client vs not
  updated brick.
- Avoid sending an EINVAL when the client request for the EOF offset.
  Here we fix an error in previous fix for posix xlator: since we
  fill each directory entry with the offset of the next entry, we
  must consider as EOF the offset of the last entry, and not the
  value of telldir() after we read it.

This is a backport of I59fb7f06a872c4f98987105792d648141c258c6a

BUG: 1138897
Change-Id: I1e9f3e4a7d780b98adf6d9f197ee2198d43ef94d
Signed-off-by: Emmanuel Dreyfus <manu@netbsd.org>
---
 xlators/cluster/afr/src/afr-self-heald.c | 46 +++++++++++++++++++++++++++-----
 xlators/features/index/src/index.c       | 42 ++++++++++++++++++++---------
 xlators/features/index/src/index.h       |  1 +
 xlators/storage/posix/src/posix.c        | 26 +++++++++++-------
 4 files changed, 87 insertions(+), 28 deletions(-)

diff --git xlators/cluster/afr/src/afr-self-heald.c xlators/cluster/afr/src/afr-self-heald.c
index 10e1996..6ce52aa 100644
--- xlators/cluster/afr/src/afr-self-heald.c
+++ xlators/cluster/afr/src/afr-self-heald.c
@@ -276,6 +276,8 @@ afr_shd_index_opendir (xlator_t *this, int child)
 #ifdef GF_LINUX_HOST_OS
 		fd_unref (fd);
 		fd = fd_anonymous (inode);
+		if (!fd)
+		        goto out;
 #else /* GF_LINUX_HOST_OS */
 		gf_log(this->name, GF_LOG_ERROR,
 		       "opendir of %s for %s failed: %s",
@@ -429,7 +431,7 @@ afr_shd_index_sweep (struct subvol_healer *healer)
 	fd_t *fd = NULL;
 	xlator_t *subvol = NULL;
 	afr_private_t *priv = NULL;
-	off_t offset = 0;
+	uint64_t offset = 0;
 	gf_dirent_t entries;
 	gf_dirent_t *entry = NULL;
 	uuid_t gfid;
@@ -503,11 +505,12 @@ afr_shd_index_sweep (struct subvol_healer *healer)
 int
 afr_shd_full_sweep (struct subvol_healer *healer, inode_t *inode)
 {
+	loc_t loc = {0, };
 	fd_t *fd = NULL;
 	xlator_t *this = NULL;
 	xlator_t *subvol = NULL;
 	afr_private_t *priv = NULL;
-	off_t offset = 0;
+	uint64_t offset = 0;
 	gf_dirent_t entries;
 	gf_dirent_t *entry = NULL;
 	int ret = 0;
@@ -516,9 +519,38 @@ afr_shd_full_sweep (struct subvol_healer *healer, inode_t *inode)
 	priv = this->private;
 	subvol = priv->children[healer->subvol];
 
-	fd = fd_anonymous (inode);
-	if (!fd)
-		return -errno;
+	uuid_copy (loc.gfid, inode->gfid);
+	loc.inode = inode_ref(inode);
+
+	fd = fd_create (inode, GF_CLIENT_PID_AFR_SELF_HEALD);
+	if (!fd) {
+		gf_log(this->name, GF_LOG_ERROR,
+		       "fd_create of %s failed: %s",
+		       uuid_utoa (loc.gfid), strerror(errno));
+		ret = -errno;
+		goto out;
+	}
+
+	ret = syncop_opendir (subvol, &loc, fd);
+	if (ret) {
+#ifdef GF_LINUX_HOST_OS /* See comment in afr_shd_index_opendir() */
+		fd_unref(fd);
+		fd = fd_anonymous (inode);
+		if (!fd) {
+			gf_log(this->name, GF_LOG_ERROR,
+			       "fd_anonymous of %s failed: %s",
+			       uuid_utoa (loc.gfid), strerror(errno));
+			ret = -errno;
+			goto out;
+		}
+#else /* GF_LINUX_HOST_OS */
+		gf_log(this->name, GF_LOG_ERROR,
+		       "opendir of %s failed: %s",
+		       uuid_utoa (loc.gfid), strerror(errno));
+		ret = -errno;
+		goto out;
+#endif /* GF_LINUX_HOST_OS */
+	}
 
 	INIT_LIST_HEAD (&entries.list);
 
@@ -560,6 +592,8 @@ afr_shd_full_sweep (struct subvol_healer *healer, inode_t *inode)
 			break;
 	}
 
+out:
+	loc_wipe (&loc);
 	if (fd)
 		fd_unref (fd);
 	return ret;
@@ -949,7 +983,7 @@ afr_shd_gather_index_entries (xlator_t *this, int child, dict_t *output)
 	fd_t *fd = NULL;
 	xlator_t *subvol = NULL;
 	afr_private_t *priv = NULL;
-	off_t offset = 0;
+	uint64_t offset = 0;
 	gf_dirent_t entries;
 	gf_dirent_t *entry = NULL;
 	uuid_t gfid;
diff --git xlators/features/index/src/index.c xlators/features/index/src/index.c
index c342da8..50e2e0a 100644
--- xlators/features/index/src/index.c
+++ xlators/features/index/src/index.c
@@ -268,10 +268,11 @@ check_delete_stale_index_file (xlator_t *this, char *filename)
 }
 
 static int
-index_fill_readdir (fd_t *fd, DIR *dir, off_t off,
+index_fill_readdir (fd_t *fd, index_fd_ctx_t *fctx, DIR *dir, off_t off,
                     size_t size, gf_dirent_t *entries)
 {
         off_t     in_case = -1;
+        off_t     last_off = 0;
         size_t    filled = 0;
         int       count = 0;
         char      entrybuf[sizeof(struct dirent) + 256 + 8];
@@ -286,11 +287,11 @@ index_fill_readdir (fd_t *fd, DIR *dir, off_t off,
         } else {
                 seekdir (dir, off);
 #ifndef GF_LINUX_HOST_OS
-                if (telldir(dir) != off) {
+                if ((u_long)telldir(dir) != off && off != fctx->dir_eof) {
                         gf_log (THIS->name, GF_LOG_ERROR,
-                                "seekdir(%ld) failed on dir=%p: "
+                                "seekdir(0x%llx) failed on dir=%p: "
 				"Invalid argument (offset reused from "
-				"another DIR * structure?)", (long)off, dir);
+				"another DIR * structure?)", off, dir);
                         errno = EINVAL;
                         count = -1;
                         goto out;
@@ -299,7 +300,7 @@ index_fill_readdir (fd_t *fd, DIR *dir, off_t off,
         }
 
         while (filled <= size) {
-                in_case = telldir (dir);
+                in_case = (u_long)telldir (dir);
 
                 if (in_case == -1) {
                         gf_log (THIS->name, GF_LOG_ERROR,
@@ -335,12 +336,13 @@ index_fill_readdir (fd_t *fd, DIR *dir, off_t off,
                 if (this_size + filled > size) {
                         seekdir (dir, in_case);
 #ifndef GF_LINUX_HOST_OS
-                        if (telldir(dir) != in_case) {
+                        if ((u_long)telldir(dir) != in_case &&
+                            in_case != fctx->dir_eof) {
 				gf_log (THIS->name, GF_LOG_ERROR,
-					"seekdir(%ld) failed on dir=%p: "
+					"seekdir(0x%llx) failed on dir=%p: "
 					"Invalid argument (offset reused from "
 					"another DIR * structure?)",
-					(long)in_case, dir);
+					in_case, dir);
 				errno = EINVAL;
 				count = -1;
 				goto out;
@@ -357,7 +359,14 @@ index_fill_readdir (fd_t *fd, DIR *dir, off_t off,
                                 entry->d_name, strerror (errno));
                         goto out;
                 }
-                this_entry->d_off = telldir (dir);
+                /*
+                 * we store the offset of next entry here, which is
+                 * probably not intended, but code using syncop_readdir()
+                 * (glfs-heal.c, afr-self-heald.c, pump.c) rely on it
+                 * for directory read resumption.
+                 */
+                last_off = (u_long)telldir(dir);
+                this_entry->d_off = last_off;
                 this_entry->d_ino = entry->d_ino;
 
                 list_add_tail (&this_entry->list, &entries->list);
@@ -366,9 +375,12 @@ index_fill_readdir (fd_t *fd, DIR *dir, off_t off,
                 count ++;
         }
 
-        if ((!readdir (dir) && (errno == 0)))
+        if ((!readdir (dir) && (errno == 0))) {
                 /* Indicate EOF */
                 errno = ENOENT;
+                /* Remember EOF offset for later detection */
+                fctx->dir_eof = last_off;
+        }
 out:
         return count;
 }
@@ -581,6 +593,7 @@ __index_fd_ctx_get (fd_t *fd, xlator_t *this, index_fd_ctx_t **ctx)
                 fctx = NULL;
                 goto out;
         }
+        fctx->dir_eof = -1;
 
         ret = __fd_ctx_set (fd, this, (uint64_t)(long)fctx);
         if (ret) {
@@ -949,7 +962,7 @@ index_readdir_wrapper (call_frame_t *frame, xlator_t *this,
                 goto done;
         }
 
-        count = index_fill_readdir (fd, dir, off, size, &entries);
+        count = index_fill_readdir (fd, fctx, dir, off, size, &entries);
 
         /* pick ENOENT to indicate EOF */
         op_errno = errno;
@@ -1270,8 +1283,11 @@ index_releasedir (xlator_t *this, fd_t *fd)
                 goto out;
 
         fctx = (index_fd_ctx_t*) (long) ctx;
-        if (fctx->dir)
-                closedir (fctx->dir);
+        if (fctx->dir) {
+                ret = closedir (fctx->dir);
+                if (ret)
+                        gf_log (this->name, GF_LOG_ERROR, "closedir error: %s", strerror (errno));
+        }
 
         GF_FREE (fctx);
 out:
diff --git xlators/features/index/src/index.h xlators/features/index/src/index.h
index 661dcdb..206d280 100644
--- xlators/features/index/src/index.h
+++ xlators/features/index/src/index.h
@@ -34,6 +34,7 @@ typedef struct index_inode_ctx {
 
 typedef struct index_fd_ctx {
         DIR *dir;
+        off_t dir_eof;
 } index_fd_ctx_t;
 
 typedef struct index_priv {
diff --git xlators/storage/posix/src/posix.c xlators/storage/posix/src/posix.c
index 3e3d14f..5d37c9f 100644
--- xlators/storage/posix/src/posix.c
+++ xlators/storage/posix/src/posix.c
@@ -4839,6 +4839,7 @@ posix_fill_readdir (fd_t *fd, DIR *dir, off_t off, size_t size,
                     gf_dirent_t *entries, xlator_t *this, int32_t skip_dirs)
 {
         off_t     in_case = -1;
+        off_t     last_off = 0;
         size_t    filled = 0;
         int             count = 0;
         char      entrybuf[sizeof(struct dirent) + 256 + 8];
@@ -4874,11 +4875,11 @@ posix_fill_readdir (fd_t *fd, DIR *dir, off_t off, size_t size,
         } else {
                 seekdir (dir, off);
 #ifndef GF_LINUX_HOST_OS
-                if (telldir(dir) != (long)off && off != pfd->dir_eof) {
+                if ((u_long)telldir(dir) != off && off != pfd->dir_eof) {
                         gf_log (THIS->name, GF_LOG_ERROR,
-                                "seekdir(%ld) failed on dir=%p: "
+                                "seekdir(0x%llx) failed on dir=%p: "
                                 "Invalid argument (offset reused from "
-                                "another DIR * structure?)", (long)off, dir);
+                                "another DIR * structure?)", off, dir);
                         errno = EINVAL;
                         count = -1;
                         goto out;
@@ -4887,7 +4888,7 @@ posix_fill_readdir (fd_t *fd, DIR *dir, off_t off, size_t size,
         }
 
         while (filled <= size) {
-                in_case = telldir (dir);
+                in_case = (u_long)telldir (dir);
 
                 if (in_case == -1) {
                         gf_log (THIS->name, GF_LOG_ERROR,
@@ -4947,13 +4948,13 @@ posix_fill_readdir (fd_t *fd, DIR *dir, off_t off, size_t size,
                 if (this_size + filled > size) {
                         seekdir (dir, in_case);
 #ifndef GF_LINUX_HOST_OS
-                        if (telldir(dir) != (long)in_case &&
+                        if ((u_long)telldir(dir) != in_case &&
                             in_case != pfd->dir_eof) {
                                 gf_log (THIS->name, GF_LOG_ERROR,
-                                        "seekdir(%ld) failed on dir=%p: "
+                                        "seekdir(0x%llx) failed on dir=%p: "
                                         "Invalid argument (offset reused from "
                                         "another DIR * structure?)",
-                                        (long)in_case, dir);
+                                        in_case, dir);
                                 errno = EINVAL;
                                 count = -1;
                                 goto out;
@@ -4970,7 +4971,14 @@ posix_fill_readdir (fd_t *fd, DIR *dir, off_t off, size_t size,
                                 entry->d_name, strerror (errno));
                         goto out;
                 }
-                this_entry->d_off = telldir (dir);
+                /*
+                 * we store the offset of next entry here, which is
+                 * probably not intended, but code using syncop_readdir()
+                 * (glfs-heal.c, afr-self-heald.c, pump.c) rely on it
+                 * for directory read resumption.
+                 */
+                last_off = (u_long)telldir(dir);
+                this_entry->d_off = last_off;
                 this_entry->d_ino = entry->d_ino;
                 this_entry->d_type = entry->d_type;
 
@@ -4984,7 +4992,7 @@ posix_fill_readdir (fd_t *fd, DIR *dir, off_t off, size_t size,
                 /* Indicate EOF */
                 errno = ENOENT;
                 /* Remember EOF offset for later detection */
-                pfd->dir_eof = telldir (dir);
+                pfd->dir_eof = (u_long)last_off;
         }
 out:
         return count;
-- 
1.8.2.3