Skip to content

Commit 88309aa

Browse files
committed
Orangefs: fix dir_emit code in pvfs2_readdir.
Al Viro glanced at readdir and surmised that getdents would misbehave the way it was written... and sure enough. Signed-off-by: Mike Marshall <hubcap@omnibond.com>
1 parent 4d1c440 commit 88309aa

2 files changed

Lines changed: 50 additions & 82 deletions

File tree

‎fs/orangefs/dir.c‎

Lines changed: 49 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -95,26 +95,16 @@ static void readdir_handle_dtor(struct pvfs2_bufmap *bufmap,
9595

9696
/*
9797
* Read directory entries from an instance of an open directory.
98-
*
99-
* \note This routine was converted for the readdir to iterate change
100-
* in "struct file_operations". "converted" mostly amounts to
101-
* changing occurrences of "readdir" and "filldir" in the
102-
* comments to "iterate" and "dir_emit". Also filldir calls
103-
* were changed to dir_emit calls.
104-
*
105-
* \param dir_emit callback function called for each entry read.
106-
*
107-
* \retval 0 when directory has been completely traversed
108-
* \retval >0 if we don't call dir_emit for all entries
109-
*
110-
* \note If the dir_emit call-back returns non-zero, then iterate should
111-
* assume that it has had enough, and should return as well.
11298
*/
11399
static int pvfs2_readdir(struct file *file, struct dir_context *ctx)
114100
{
115101
struct pvfs2_bufmap *bufmap = NULL;
116102
int ret = 0;
117103
int buffer_index;
104+
/*
105+
* ptoken supports Orangefs' distributed directory logic, added
106+
* in 2.9.2.
107+
*/
118108
__u64 *ptoken = file->private_data;
119109
__u64 pos = 0;
120110
ino_t ino = 0;
@@ -129,11 +119,11 @@ static int pvfs2_readdir(struct file *file, struct dir_context *ctx)
129119
char *current_entry = NULL;
130120
long bytes_decoded;
131121

132-
gossip_ldebug(GOSSIP_DIR_DEBUG,
133-
"%s: ctx->pos:%lld, token = %llu\n",
134-
__func__,
135-
lld(ctx->pos),
136-
llu(*ptoken));
122+
gossip_debug(GOSSIP_DIR_DEBUG,
123+
"%s: ctx->pos:%lld, ptoken = %llu\n",
124+
__func__,
125+
lld(ctx->pos),
126+
llu(*ptoken));
137127

138128
pos = (__u64) ctx->pos;
139129

@@ -165,16 +155,6 @@ static int pvfs2_readdir(struct file *file, struct dir_context *ctx)
165155
__func__,
166156
&new_op->upcall.req.readdir.refn.khandle);
167157

168-
/*
169-
* NOTE: the position we send to the readdir upcall is out of
170-
* sync with ctx->pos since:
171-
* 1. pvfs2 doesn't include the "." and ".." entries that are
172-
* added below.
173-
* 2. the introduction of distributed directory logic makes token no
174-
* longer be related to f_pos and pos. Instead an independent
175-
* variable is used inside the function and stored in the
176-
* private_data of the file structure.
177-
*/
178158
new_op->upcall.req.readdir.token = *ptoken;
179159

180160
get_new_buffer_index:
@@ -238,26 +218,26 @@ static int pvfs2_readdir(struct file *file, struct dir_context *ctx)
238218
}
239219

240220
if (bytes_decoded != new_op->downcall.trailer_size) {
241-
gossip_err("pvfs2_readdir: # bytes decoded (%ld) != trailer size (%ld)\n",
242-
bytes_decoded,
243-
(long)new_op->downcall.trailer_size);
221+
gossip_err("pvfs2_readdir: # bytes decoded (%ld) "
222+
"!= trailer size (%ld)\n",
223+
bytes_decoded,
224+
(long)new_op->downcall.trailer_size);
244225
ret = -EINVAL;
245226
goto out_destroy_handle;
246227
}
247228

229+
/*
230+
* pvfs2 doesn't actually store dot and dot-dot, but
231+
* we need to have them represented.
232+
*/
248233
if (pos == 0) {
249234
ino = get_ino_from_khandle(dentry->d_inode);
250235
gossip_debug(GOSSIP_DIR_DEBUG,
251236
"%s: calling dir_emit of \".\" with pos = %llu\n",
252237
__func__,
253238
llu(pos));
254239
ret = dir_emit(ctx, ".", 1, ino, DT_DIR);
255-
ctx->pos++;
256-
gossip_ldebug(GOSSIP_DIR_DEBUG,
257-
"%s: ctx->pos:%lld\n",
258-
__func__,
259-
lld(ctx->pos));
260-
pos++;
240+
pos += 1;
261241
}
262242

263243
if (pos == 1) {
@@ -267,80 +247,67 @@ static int pvfs2_readdir(struct file *file, struct dir_context *ctx)
267247
__func__,
268248
llu(pos));
269249
ret = dir_emit(ctx, "..", 2, ino, DT_DIR);
270-
ctx->pos++;
271-
gossip_ldebug(GOSSIP_DIR_DEBUG,
272-
"%s: ctx->pos:%lld\n",
273-
__func__,
274-
lld(ctx->pos));
275-
pos++;
250+
pos += 1;
276251
}
277252

278-
for (i = 0; i < rhandle.readdir_response.pvfs_dirent_outcount; i++) {
253+
/*
254+
* we stored PVFS_ITERATE_NEXT in ctx->pos last time around
255+
* to prevent "finding" dot and dot-dot on any iteration
256+
* other than the first.
257+
*/
258+
if (ctx->pos == PVFS_ITERATE_NEXT)
259+
ctx->pos = 0;
260+
261+
for (i = ctx->pos;
262+
i < rhandle.readdir_response.pvfs_dirent_outcount;
263+
i++) {
279264
len = rhandle.readdir_response.dirent_array[i].d_length;
280265
current_entry = rhandle.readdir_response.dirent_array[i].d_name;
281266
current_ino = pvfs2_khandle_to_ino(
282267
&(rhandle.readdir_response.dirent_array[i].khandle));
283268

284269
gossip_debug(GOSSIP_DIR_DEBUG,
285-
"calling dir_emit for %s with len %d, pos %ld\n",
270+
"calling dir_emit for %s with len %d"
271+
", ctx->pos %ld\n",
286272
current_entry,
287273
len,
288-
(unsigned long)pos);
274+
(unsigned long)ctx->pos);
275+
/*
276+
* type is unknown. We don't return object type
277+
* in the dirent_array. This leaves getdents
278+
* clueless about type.
279+
*/
289280
ret =
290281
dir_emit(ctx, current_entry, len, current_ino, DT_UNKNOWN);
282+
if (!ret)
283+
break;
291284
ctx->pos++;
292-
gossip_ldebug(GOSSIP_DIR_DEBUG,
285+
gossip_debug(GOSSIP_DIR_DEBUG,
293286
"%s: ctx->pos:%lld\n",
294287
__func__,
295288
lld(ctx->pos));
296289

297-
pos++;
298290
}
299291

300-
/* this means that all of the dir_emit calls succeeded */
301-
if (i == rhandle.readdir_response.pvfs_dirent_outcount) {
302-
/* update token */
292+
/*
293+
* we ran all the way through the last batch, set up for
294+
* getting another batch...
295+
*/
296+
if (ret) {
303297
*ptoken = rhandle.readdir_response.token;
304-
} else {
305-
/* this means a dir_emit call failed */
306-
if (rhandle.readdir_response.token == PVFS_READDIR_END) {
307-
/*
308-
* If PVFS hit end of directory, then there
309-
* is no way to do math on the token that it
310-
* returned. Instead we go by ctx->pos but
311-
* back up to account for the artificial .
312-
* and .. entries.
313-
*/
314-
ctx->pos -= 3;
315-
} else {
316-
/*
317-
* this means a dir_emit call failed. !!! need to set
318-
* back to previous ctx->pos, no middle value allowed
319-
*/
320-
pos -= (i - 1);
321-
ctx->pos -= (i - 1);
322-
}
323-
gossip_debug(GOSSIP_DIR_DEBUG,
324-
"at least one dir_emit call failed. Setting ctx->pos to: %lld\n",
325-
lld(ctx->pos));
298+
ctx->pos = PVFS_ITERATE_NEXT;
326299
}
327300

328301
/*
329302
* Did we hit the end of the directory?
330303
*/
331304
if (rhandle.readdir_response.token == PVFS_READDIR_END &&
332305
!buffer_full) {
333-
gossip_debug(GOSSIP_DIR_DEBUG, "End of dir detected; setting ctx->pos to PVFS_READDIR_END.\n");
306+
gossip_debug(GOSSIP_DIR_DEBUG,
307+
"End of dir detected; setting ctx->pos to PVFS_READDIR_END.\n");
334308
ctx->pos = PVFS_READDIR_END;
335309
}
336310

337-
gossip_debug(GOSSIP_DIR_DEBUG,
338-
"pos = %llu, token = %llu"
339-
", ctx->pos should have been %lld\n",
340-
llu(pos),
341-
llu(*ptoken),
342-
lld(ctx->pos));
343-
344311
out_destroy_handle:
345312
readdir_handle_dtor(bufmap, &rhandle);
346313
out_free_op:

‎fs/orangefs/protocol.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ DECLARE_ERRNO_MAPPING()
384384
#define INT32_MAX (2147483647)
385385
#define PVFS_ITERATE_START (INT32_MAX - 1)
386386
#define PVFS_ITERATE_END (INT32_MAX - 2)
387+
#define PVFS_ITERATE_NEXT (INT32_MAX - 3)
387388
#define PVFS_READDIR_START PVFS_ITERATE_START
388389
#define PVFS_READDIR_END PVFS_ITERATE_END
389390
#define PVFS_IMMUTABLE_FL FS_IMMUTABLE_FL

0 commit comments

Comments
 (0)