Skip to content

Commit eb04c28

Browse files
jankaraAl Viro
authored andcommitted
fs: Add freezing handling to mnt_want_write() / mnt_drop_write()
Most of places where we want freeze protection coincides with the places where we also have remount-ro protection. So make mnt_want_write() and mnt_drop_write() (and their _file alternative) prevent freezing as well. For the few cases that are really interested only in remount-ro protection provide new function variants. BugLink: https://bugs.launchpad.net/bugs/897421 Tested-by: Kamal Mostafa <kamal@canonical.com> Tested-by: Peter M. Petrakis <peter.petrakis@canonical.com> Tested-by: Dann Frazier <dann.frazier@canonical.com> Tested-by: Massimo Morana <massimo.morana@canonical.com> Signed-off-by: Jan Kara <jack@suse.cz> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 5accdf8 commit eb04c28

5 files changed

Lines changed: 85 additions & 24 deletions

File tree

‎fs/file_table.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ static void drop_file_write_access(struct file *file)
217217
return;
218218
if (file_check_writeable(file) != 0)
219219
return;
220-
mnt_drop_write(mnt);
220+
__mnt_drop_write(mnt);
221221
file_release_write(file);
222222
}
223223

‎fs/inode.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,11 +1660,11 @@ int file_update_time(struct file *file)
16601660
return 0;
16611661

16621662
/* Finally allowed to write? Takes lock. */
1663-
if (mnt_want_write_file(file))
1663+
if (__mnt_want_write_file(file))
16641664
return 0;
16651665

16661666
ret = update_time(inode, &now, sync_it);
1667-
mnt_drop_write_file(file);
1667+
__mnt_drop_write_file(file);
16681668

16691669
return ret;
16701670
}

‎fs/internal.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ extern void __init mnt_init(void);
6161

6262
extern struct lglock vfsmount_lock;
6363

64+
extern int __mnt_want_write(struct vfsmount *);
65+
extern int __mnt_want_write_file(struct file *);
66+
extern void __mnt_drop_write(struct vfsmount *);
67+
extern void __mnt_drop_write_file(struct file *);
6468

6569
/*
6670
* fs_struct.c

‎fs/namespace.c‎

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -283,24 +283,22 @@ static int mnt_is_readonly(struct vfsmount *mnt)
283283
}
284284

285285
/*
286-
* Most r/o checks on a fs are for operations that take
287-
* discrete amounts of time, like a write() or unlink().
288-
* We must keep track of when those operations start
289-
* (for permission checks) and when they end, so that
290-
* we can determine when writes are able to occur to
291-
* a filesystem.
286+
* Most r/o & frozen checks on a fs are for operations that take discrete
287+
* amounts of time, like a write() or unlink(). We must keep track of when
288+
* those operations start (for permission checks) and when they end, so that we
289+
* can determine when writes are able to occur to a filesystem.
292290
*/
293291
/**
294-
* mnt_want_write - get write access to a mount
292+
* __mnt_want_write - get write access to a mount without freeze protection
295293
* @m: the mount on which to take a write
296294
*
297-
* This tells the low-level filesystem that a write is
298-
* about to be performed to it, and makes sure that
299-
* writes are allowed before returning success. When
300-
* the write operation is finished, mnt_drop_write()
301-
* must be called. This is effectively a refcount.
295+
* This tells the low-level filesystem that a write is about to be performed to
296+
* it, and makes sure that writes are allowed (mnt it read-write) before
297+
* returning success. This operation does not protect against filesystem being
298+
* frozen. When the write operation is finished, __mnt_drop_write() must be
299+
* called. This is effectively a refcount.
302300
*/
303-
int mnt_want_write(struct vfsmount *m)
301+
int __mnt_want_write(struct vfsmount *m)
304302
{
305303
struct mount *mnt = real_mount(m);
306304
int ret = 0;
@@ -326,6 +324,27 @@ int mnt_want_write(struct vfsmount *m)
326324
ret = -EROFS;
327325
}
328326
preempt_enable();
327+
328+
return ret;
329+
}
330+
331+
/**
332+
* mnt_want_write - get write access to a mount
333+
* @m: the mount on which to take a write
334+
*
335+
* This tells the low-level filesystem that a write is about to be performed to
336+
* it, and makes sure that writes are allowed (mount is read-write, filesystem
337+
* is not frozen) before returning success. When the write operation is
338+
* finished, mnt_drop_write() must be called. This is effectively a refcount.
339+
*/
340+
int mnt_want_write(struct vfsmount *m)
341+
{
342+
int ret;
343+
344+
sb_start_write(m->mnt_sb);
345+
ret = __mnt_want_write(m);
346+
if (ret)
347+
sb_end_write(m->mnt_sb);
329348
return ret;
330349
}
331350
EXPORT_SYMBOL_GPL(mnt_want_write);
@@ -355,38 +374,76 @@ int mnt_clone_write(struct vfsmount *mnt)
355374
EXPORT_SYMBOL_GPL(mnt_clone_write);
356375

357376
/**
358-
* mnt_want_write_file - get write access to a file's mount
377+
* __mnt_want_write_file - get write access to a file's mount
359378
* @file: the file who's mount on which to take a write
360379
*
361-
* This is like mnt_want_write, but it takes a file and can
380+
* This is like __mnt_want_write, but it takes a file and can
362381
* do some optimisations if the file is open for write already
363382
*/
364-
int mnt_want_write_file(struct file *file)
383+
int __mnt_want_write_file(struct file *file)
365384
{
366385
struct inode *inode = file->f_dentry->d_inode;
386+
367387
if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode))
368-
return mnt_want_write(file->f_path.mnt);
388+
return __mnt_want_write(file->f_path.mnt);
369389
else
370390
return mnt_clone_write(file->f_path.mnt);
371391
}
392+
393+
/**
394+
* mnt_want_write_file - get write access to a file's mount
395+
* @file: the file who's mount on which to take a write
396+
*
397+
* This is like mnt_want_write, but it takes a file and can
398+
* do some optimisations if the file is open for write already
399+
*/
400+
int mnt_want_write_file(struct file *file)
401+
{
402+
int ret;
403+
404+
sb_start_write(file->f_path.mnt->mnt_sb);
405+
ret = __mnt_want_write_file(file);
406+
if (ret)
407+
sb_end_write(file->f_path.mnt->mnt_sb);
408+
return ret;
409+
}
372410
EXPORT_SYMBOL_GPL(mnt_want_write_file);
373411

374412
/**
375-
* mnt_drop_write - give up write access to a mount
413+
* __mnt_drop_write - give up write access to a mount
376414
* @mnt: the mount on which to give up write access
377415
*
378416
* Tells the low-level filesystem that we are done
379417
* performing writes to it. Must be matched with
380-
* mnt_want_write() call above.
418+
* __mnt_want_write() call above.
381419
*/
382-
void mnt_drop_write(struct vfsmount *mnt)
420+
void __mnt_drop_write(struct vfsmount *mnt)
383421
{
384422
preempt_disable();
385423
mnt_dec_writers(real_mount(mnt));
386424
preempt_enable();
387425
}
426+
427+
/**
428+
* mnt_drop_write - give up write access to a mount
429+
* @mnt: the mount on which to give up write access
430+
*
431+
* Tells the low-level filesystem that we are done performing writes to it and
432+
* also allows filesystem to be frozen again. Must be matched with
433+
* mnt_want_write() call above.
434+
*/
435+
void mnt_drop_write(struct vfsmount *mnt)
436+
{
437+
__mnt_drop_write(mnt);
438+
sb_end_write(mnt->mnt_sb);
439+
}
388440
EXPORT_SYMBOL_GPL(mnt_drop_write);
389441

442+
void __mnt_drop_write_file(struct file *file)
443+
{
444+
__mnt_drop_write(file->f_path.mnt);
445+
}
446+
390447
void mnt_drop_write_file(struct file *file)
391448
{
392449
mnt_drop_write(file->f_path.mnt);

‎fs/open.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ static inline int __get_file_write_access(struct inode *inode,
620620
/*
621621
* Balanced in __fput()
622622
*/
623-
error = mnt_want_write(mnt);
623+
error = __mnt_want_write(mnt);
624624
if (error)
625625
put_write_access(inode);
626626
}

0 commit comments

Comments
 (0)