Skip to content

Commit 63b6df1

Browse files
author
Al Viro
committed
give readdir(2)/getdents(2)/etc. uniform exclusion with lseek()
same as read() on regular files has, and for the same reason. Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 9902af7 commit 63b6df1

7 files changed

Lines changed: 33 additions & 27 deletions

File tree

‎arch/alpha/kernel/osf_sys.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ SYSCALL_DEFINE4(osf_getdirentries, unsigned int, fd,
147147
long __user *, basep)
148148
{
149149
int error;
150-
struct fd arg = fdget(fd);
150+
struct fd arg = fdget_pos(fd);
151151
struct osf_dirent_callback buf = {
152152
.ctx.actor = osf_filldir,
153153
.dirent = dirent,
@@ -164,7 +164,7 @@ SYSCALL_DEFINE4(osf_getdirentries, unsigned int, fd,
164164
if (count != buf.count)
165165
error = count - buf.count;
166166

167-
fdput(arg);
167+
fdput_pos(arg);
168168
return error;
169169
}
170170

‎fs/compat.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ COMPAT_SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
884884
struct compat_old_linux_dirent __user *, dirent, unsigned int, count)
885885
{
886886
int error;
887-
struct fd f = fdget(fd);
887+
struct fd f = fdget_pos(fd);
888888
struct compat_readdir_callback buf = {
889889
.ctx.actor = compat_fillonedir,
890890
.dirent = dirent
@@ -897,7 +897,7 @@ COMPAT_SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
897897
if (buf.result)
898898
error = buf.result;
899899

900-
fdput(f);
900+
fdput_pos(f);
901901
return error;
902902
}
903903

@@ -975,7 +975,7 @@ COMPAT_SYSCALL_DEFINE3(getdents, unsigned int, fd,
975975
if (!access_ok(VERIFY_WRITE, dirent, count))
976976
return -EFAULT;
977977

978-
f = fdget(fd);
978+
f = fdget_pos(fd);
979979
if (!f.file)
980980
return -EBADF;
981981

@@ -989,7 +989,7 @@ COMPAT_SYSCALL_DEFINE3(getdents, unsigned int, fd,
989989
else
990990
error = count - buf.count;
991991
}
992-
fdput(f);
992+
fdput_pos(f);
993993
return error;
994994
}
995995

@@ -1062,7 +1062,7 @@ COMPAT_SYSCALL_DEFINE3(getdents64, unsigned int, fd,
10621062
if (!access_ok(VERIFY_WRITE, dirent, count))
10631063
return -EFAULT;
10641064

1065-
f = fdget(fd);
1065+
f = fdget_pos(fd);
10661066
if (!f.file)
10671067
return -EBADF;
10681068

@@ -1077,7 +1077,7 @@ COMPAT_SYSCALL_DEFINE3(getdents64, unsigned int, fd,
10771077
else
10781078
error = count - buf.count;
10791079
}
1080-
fdput(f);
1080+
fdput_pos(f);
10811081
return error;
10821082
}
10831083
#endif /* __ARCH_WANT_COMPAT_SYS_GETDENTS64 */

‎fs/file.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,11 @@ unsigned long __fdget_pos(unsigned int fd)
784784
return v;
785785
}
786786

787+
void __f_unlock_pos(struct file *f)
788+
{
789+
mutex_unlock(&f->f_pos_lock);
790+
}
791+
787792
/*
788793
* We only lock f_pos if we have threads or if the file might be
789794
* shared with another process. In both cases we'll have an elevated

‎fs/open.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ static int do_dentry_open(struct file *f,
713713
}
714714

715715
/* POSIX.1-2008/SUSv4 Section XSI 2.9.7 */
716-
if (S_ISREG(inode->i_mode))
716+
if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))
717717
f->f_mode |= FMODE_ATOMIC_POS;
718718

719719
f->f_op = fops_get(inode->i_fop);

‎fs/read_write.c‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -302,18 +302,6 @@ loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
302302
}
303303
EXPORT_SYMBOL(vfs_llseek);
304304

305-
static inline struct fd fdget_pos(int fd)
306-
{
307-
return __to_fd(__fdget_pos(fd));
308-
}
309-
310-
static inline void fdput_pos(struct fd f)
311-
{
312-
if (f.flags & FDPUT_POS_UNLOCK)
313-
mutex_unlock(&f.file->f_pos_lock);
314-
fdput(f);
315-
}
316-
317305
SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
318306
{
319307
off_t retval;

‎fs/readdir.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
112112
struct old_linux_dirent __user *, dirent, unsigned int, count)
113113
{
114114
int error;
115-
struct fd f = fdget(fd);
115+
struct fd f = fdget_pos(fd);
116116
struct readdir_callback buf = {
117117
.ctx.actor = fillonedir,
118118
.dirent = dirent
@@ -125,7 +125,7 @@ SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
125125
if (buf.result)
126126
error = buf.result;
127127

128-
fdput(f);
128+
fdput_pos(f);
129129
return error;
130130
}
131131

@@ -209,7 +209,7 @@ SYSCALL_DEFINE3(getdents, unsigned int, fd,
209209
if (!access_ok(VERIFY_WRITE, dirent, count))
210210
return -EFAULT;
211211

212-
f = fdget(fd);
212+
f = fdget_pos(fd);
213213
if (!f.file)
214214
return -EBADF;
215215

@@ -223,7 +223,7 @@ SYSCALL_DEFINE3(getdents, unsigned int, fd,
223223
else
224224
error = count - buf.count;
225225
}
226-
fdput(f);
226+
fdput_pos(f);
227227
return error;
228228
}
229229

@@ -290,7 +290,7 @@ SYSCALL_DEFINE3(getdents64, unsigned int, fd,
290290
if (!access_ok(VERIFY_WRITE, dirent, count))
291291
return -EFAULT;
292292

293-
f = fdget(fd);
293+
f = fdget_pos(fd);
294294
if (!f.file)
295295
return -EBADF;
296296

@@ -305,6 +305,6 @@ SYSCALL_DEFINE3(getdents64, unsigned int, fd,
305305
else
306306
error = count - buf.count;
307307
}
308-
fdput(f);
308+
fdput_pos(f);
309309
return error;
310310
}

‎include/linux/file.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ extern struct file *fget_raw(unsigned int fd);
4444
extern unsigned long __fdget(unsigned int fd);
4545
extern unsigned long __fdget_raw(unsigned int fd);
4646
extern unsigned long __fdget_pos(unsigned int fd);
47+
extern void __f_unlock_pos(struct file *);
4748

4849
static inline struct fd __to_fd(unsigned long v)
4950
{
@@ -60,6 +61,18 @@ static inline struct fd fdget_raw(unsigned int fd)
6061
return __to_fd(__fdget_raw(fd));
6162
}
6263

64+
static inline struct fd fdget_pos(int fd)
65+
{
66+
return __to_fd(__fdget_pos(fd));
67+
}
68+
69+
static inline void fdput_pos(struct fd f)
70+
{
71+
if (f.flags & FDPUT_POS_UNLOCK)
72+
__f_unlock_pos(f.file);
73+
fdput(f);
74+
}
75+
6376
extern int f_dupfd(unsigned int from, struct file *file, unsigned flags);
6477
extern int replace_fd(unsigned fd, struct file *file, unsigned flags);
6578
extern void set_close_on_exec(unsigned int fd, int flag);

0 commit comments

Comments
 (0)