2019-05-19 15:08:55 +03:00
// SPDX-License-Identifier: GPL-2.0-only
2005-04-17 02:20:36 +04:00
/*
* linux / fs / fat / file . c
*
* Written 1992 , 1993 by Werner Almesberger
*
* regular file handling primitives for fat - based filesystems
*/
2006-01-11 23:17:46 +03:00
# include <linux/capability.h>
2005-04-17 02:20:36 +04:00
# include <linux/module.h>
2010-05-17 03:13:47 +04:00
# include <linux/compat.h>
2008-02-16 01:37:46 +03:00
# include <linux/mount.h>
2006-09-29 13:00:03 +04:00
# include <linux/blkdev.h>
2015-05-23 00:13:32 +03:00
# include <linux/backing-dev.h>
2008-07-01 17:01:28 +04:00
# include <linux/fsnotify.h>
# include <linux/security.h>
2016-01-21 01:59:41 +03:00
# include <linux/falloc.h>
2008-11-06 23:53:46 +03:00
# include "fat.h"
2005-04-17 02:20:36 +04:00
2016-01-21 01:59:41 +03:00
static long fat_fallocate ( struct file * file , int mode ,
loff_t offset , loff_t len ) ;
2009-06-08 16:38:31 +04:00
static int fat_ioctl_get_attributes ( struct inode * inode , u32 __user * user_attr )
{
u32 attr ;
2020-08-12 04:35:56 +03:00
inode_lock_shared ( inode ) ;
2009-06-08 16:38:31 +04:00
attr = fat_make_attrs ( inode ) ;
2020-08-12 04:35:56 +03:00
inode_unlock_shared ( inode ) ;
2009-06-08 16:38:31 +04:00
return put_user ( attr , user_attr ) ;
}
static int fat_ioctl_set_attributes ( struct file * file , u32 __user * user_attr )
2005-04-17 02:20:36 +04:00
{
2013-01-24 02:07:38 +04:00
struct inode * inode = file_inode ( file ) ;
2005-04-17 02:20:36 +04:00
struct msdos_sb_info * sbi = MSDOS_SB ( inode - > i_sb ) ;
2009-06-08 16:38:31 +04:00
int is_dir = S_ISDIR ( inode - > i_mode ) ;
u32 attr , oldattr ;
struct iattr ia ;
int err ;
2005-04-17 02:20:36 +04:00
2009-06-08 16:38:31 +04:00
err = get_user ( attr , user_attr ) ;
if ( err )
goto out ;
2005-04-17 02:20:36 +04:00
2011-11-23 20:57:51 +04:00
err = mnt_want_write_file ( file ) ;
2009-06-08 16:38:31 +04:00
if ( err )
2012-06-12 18:20:31 +04:00
goto out ;
2016-01-22 23:40:57 +03:00
inode_lock ( inode ) ;
2009-06-08 16:38:31 +04:00
/*
* ATTR_VOLUME and ATTR_DIR cannot be changed ; this also
* prevents the user from turning us into a VFAT
* longname entry . Also , we obviously can ' t set
* any of the NTFS attributes in the high 24 bits .
*/
attr & = 0xff & ~ ( ATTR_VOLUME | ATTR_DIR ) ;
/* Merge in ATTR_VOLUME and ATTR_DIR */
attr | = ( MSDOS_I ( inode ) - > i_attrs & ATTR_VOLUME ) |
( is_dir ? ATTR_DIR : 0 ) ;
oldattr = fat_make_attrs ( inode ) ;
/* Equivalent to a chmod() */
ia . ia_valid = ATTR_MODE | ATTR_CTIME ;
2016-09-14 17:48:06 +03:00
ia . ia_ctime = current_time ( inode ) ;
2009-06-08 16:38:31 +04:00
if ( is_dir )
ia . ia_mode = fat_make_mode ( sbi , attr , S_IRWXUGO ) ;
else {
ia . ia_mode = fat_make_mode ( sbi , attr ,
S_IRUGO | S_IWUGO | ( inode - > i_mode & S_IXUGO ) ) ;
}
2005-04-17 02:20:36 +04:00
2009-06-08 16:38:31 +04:00
/* The root directory has no attributes */
if ( inode - > i_ino = = MSDOS_ROOT_INO & & attr ! = ATTR_DIR ) {
err = - EINVAL ;
2012-06-12 18:20:31 +04:00
goto out_unlock_inode ;
2005-04-17 02:20:36 +04:00
}
2009-06-08 16:38:31 +04:00
if ( sbi - > options . sys_immutable & &
( ( attr | oldattr ) & ATTR_SYS ) & &
! capable ( CAP_LINUX_IMMUTABLE ) ) {
err = - EPERM ;
2012-06-12 18:20:31 +04:00
goto out_unlock_inode ;
2009-06-08 16:38:31 +04:00
}
2005-04-17 02:20:36 +04:00
2009-06-08 16:38:31 +04:00
/*
* The security check is questionable . . . We single
* out the RO attribute for checking by the security
* module , just because it maps to a file mode .
*/
2023-01-13 14:49:11 +03:00
err = security_inode_setattr ( file_mnt_idmap ( file ) ,
2022-06-21 17:14:53 +03:00
file - > f_path . dentry , & ia ) ;
2009-06-08 16:38:31 +04:00
if ( err )
2012-06-12 18:20:31 +04:00
goto out_unlock_inode ;
2005-04-17 02:20:36 +04:00
2009-06-08 16:38:31 +04:00
/* This MUST be done before doing anything irreversible... */
2023-01-13 14:49:11 +03:00
err = fat_setattr ( file_mnt_idmap ( file ) , file - > f_path . dentry , & ia ) ;
2009-06-08 16:38:31 +04:00
if ( err )
2012-06-12 18:20:31 +04:00
goto out_unlock_inode ;
2009-06-08 16:38:31 +04:00
fsnotify_change ( file - > f_path . dentry , ia . ia_valid ) ;
if ( sbi - > options . sys_immutable ) {
if ( attr & ATTR_SYS )
inode - > i_flags | = S_IMMUTABLE ;
else
2011-05-31 14:38:07 +04:00
inode - > i_flags & = ~ S_IMMUTABLE ;
2009-06-08 16:38:31 +04:00
}
2005-04-17 02:20:36 +04:00
2009-06-08 16:38:31 +04:00
fat_save_attrs ( inode , attr ) ;
mark_inode_dirty ( inode ) ;
out_unlock_inode :
2016-01-22 23:40:57 +03:00
inode_unlock ( inode ) ;
2012-06-12 18:20:31 +04:00
mnt_drop_write_file ( file ) ;
2009-06-08 16:38:31 +04:00
out :
return err ;
}
2005-04-17 02:20:36 +04:00
fatfs: add FAT_IOCTL_GET_VOLUME_ID
This patch, originally from Android kernel, adds vfat ioctl command
FAT_IOCTL_GET_VOLUME_ID, with this command we can get the vfat volume ID
using following code:
ioctl(fd, FAT_IOCTL_GET_VOLUME_ID, &volume_ID)
This patch is a modified version of the patch by Mike Lockwood, with
changes from Dmitry Pervushin, who noticed the original patch makes some
volume IDs abiguous with error returns: for example, if volume id is
0xFFFFFDAD, that matches -ENOIOCTLCMD, we get "FFFFFFFF" from the user
space.
So add a parameter to ioctl to get the correct volume ID.
Android uses vfat volume ID to identify different sd card, when a new sd
card is inserted to device, android can scan the media on it and pop up
new contents.
Signed-off-by: Bintian Wang <bintian.wang@linaro.org>
Cc: dmitry pervushin <dpervushin@gmail.com>
Cc: Mike Lockwood <lockwood@android.com>
Cc: Colin Cross <ccross@android.com>
Acked-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Cc: John Stultz <john.stultz@linaro.org>
Cc: Sean McNeil <sean@mcneil.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-07-09 03:00:46 +04:00
static int fat_ioctl_get_volume_id ( struct inode * inode , u32 __user * user_attr )
{
struct msdos_sb_info * sbi = MSDOS_SB ( inode - > i_sb ) ;
return put_user ( sbi - > vol_id , user_attr ) ;
}
2018-08-22 07:59:41 +03:00
static int fat_ioctl_fitrim ( struct inode * inode , unsigned long arg )
{
struct super_block * sb = inode - > i_sb ;
struct fstrim_range __user * user_range ;
struct fstrim_range range ;
int err ;
if ( ! capable ( CAP_SYS_ADMIN ) )
return - EPERM ;
2022-04-15 07:52:55 +03:00
if ( ! bdev_max_discard_sectors ( sb - > s_bdev ) )
2018-08-22 07:59:41 +03:00
return - EOPNOTSUPP ;
user_range = ( struct fstrim_range __user * ) arg ;
if ( copy_from_user ( & range , user_range , sizeof ( range ) ) )
return - EFAULT ;
range . minlen = max_t ( unsigned int , range . minlen ,
2022-04-15 07:52:56 +03:00
bdev_discard_granularity ( sb - > s_bdev ) ) ;
2018-08-22 07:59:41 +03:00
err = fat_trim_fs ( inode , & range ) ;
if ( err < 0 )
return err ;
if ( copy_to_user ( user_range , & range , sizeof ( range ) ) )
return - EFAULT ;
return 0 ;
}
2010-05-17 03:13:47 +04:00
long fat_generic_ioctl ( struct file * filp , unsigned int cmd , unsigned long arg )
2005-04-17 02:20:36 +04:00
{
2013-01-24 02:07:38 +04:00
struct inode * inode = file_inode ( filp ) ;
2005-04-17 02:20:36 +04:00
u32 __user * user_attr = ( u32 __user * ) arg ;
switch ( cmd ) {
case FAT_IOCTL_GET_ATTRIBUTES :
2009-06-08 16:38:31 +04:00
return fat_ioctl_get_attributes ( inode , user_attr ) ;
2005-04-17 02:20:36 +04:00
case FAT_IOCTL_SET_ATTRIBUTES :
2009-06-08 16:38:31 +04:00
return fat_ioctl_set_attributes ( filp , user_attr ) ;
fatfs: add FAT_IOCTL_GET_VOLUME_ID
This patch, originally from Android kernel, adds vfat ioctl command
FAT_IOCTL_GET_VOLUME_ID, with this command we can get the vfat volume ID
using following code:
ioctl(fd, FAT_IOCTL_GET_VOLUME_ID, &volume_ID)
This patch is a modified version of the patch by Mike Lockwood, with
changes from Dmitry Pervushin, who noticed the original patch makes some
volume IDs abiguous with error returns: for example, if volume id is
0xFFFFFDAD, that matches -ENOIOCTLCMD, we get "FFFFFFFF" from the user
space.
So add a parameter to ioctl to get the correct volume ID.
Android uses vfat volume ID to identify different sd card, when a new sd
card is inserted to device, android can scan the media on it and pop up
new contents.
Signed-off-by: Bintian Wang <bintian.wang@linaro.org>
Cc: dmitry pervushin <dpervushin@gmail.com>
Cc: Mike Lockwood <lockwood@android.com>
Cc: Colin Cross <ccross@android.com>
Acked-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Cc: John Stultz <john.stultz@linaro.org>
Cc: Sean McNeil <sean@mcneil.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-07-09 03:00:46 +04:00
case FAT_IOCTL_GET_VOLUME_ID :
return fat_ioctl_get_volume_id ( inode , user_attr ) ;
2018-08-22 07:59:41 +03:00
case FITRIM :
return fat_ioctl_fitrim ( inode , arg ) ;
2005-04-17 02:20:36 +04:00
default :
return - ENOTTY ; /* Inappropriate ioctl for device */
}
}
2006-09-29 13:00:03 +04:00
static int fat_file_release ( struct inode * inode , struct file * filp )
{
if ( ( filp - > f_mode & FMODE_WRITE ) & &
2022-01-20 05:09:50 +03:00
MSDOS_SB ( inode - > i_sb ) - > options . flush ) {
2006-09-29 13:00:03 +04:00
fat_flush_inodes ( inode - > i_sb , inode , NULL ) ;
2022-01-20 05:09:50 +03:00
set_current_state ( TASK_UNINTERRUPTIBLE ) ;
io_schedule_timeout ( HZ / 10 ) ;
2006-09-29 13:00:03 +04:00
}
return 0 ;
}
2011-07-17 04:44:56 +04:00
int fat_file_fsync ( struct file * filp , loff_t start , loff_t end , int datasync )
2009-06-07 21:44:36 +04:00
{
2010-05-26 19:53:25 +04:00
struct inode * inode = filp - > f_mapping - > host ;
2019-05-15 01:44:32 +03:00
int err ;
err = __generic_file_fsync ( filp , start , end , datasync ) ;
if ( err )
return err ;
2009-06-07 21:44:36 +04:00
err = sync_mapping_buffers ( MSDOS_SB ( inode - > i_sb ) - > fat_inode - > i_mapping ) ;
2019-05-15 01:44:32 +03:00
if ( err )
return err ;
2009-06-07 21:44:36 +04:00
2021-01-26 17:52:35 +03:00
return blkdev_issue_flush ( inode - > i_sb - > s_bdev ) ;
2009-06-07 21:44:36 +04:00
}
2006-03-28 13:56:42 +04:00
const struct file_operations fat_file_operations = {
2005-04-17 02:20:36 +04:00
. llseek = generic_file_llseek ,
2014-04-02 22:33:16 +04:00
. read_iter = generic_file_read_iter ,
2014-04-03 11:17:43 +04:00
. write_iter = generic_file_write_iter ,
2005-04-17 02:20:36 +04:00
. mmap = generic_file_mmap ,
2006-09-29 13:00:03 +04:00
. release = fat_file_release ,
2010-05-17 03:13:47 +04:00
. unlocked_ioctl = fat_generic_ioctl ,
2018-09-11 18:23:00 +03:00
. compat_ioctl = compat_ptr_ioctl ,
2009-06-07 21:44:36 +04:00
. fsync = fat_file_fsync ,
2023-05-22 16:50:15 +03:00
. splice_read = filemap_splice_read ,
2019-03-08 03:29:19 +03:00
. splice_write = iter_file_splice_write ,
2016-01-21 01:59:41 +03:00
. fallocate = fat_fallocate ,
2005-04-17 02:20:36 +04:00
} ;
2006-01-08 12:02:13 +03:00
static int fat_cont_expand ( struct inode * inode , loff_t size )
{
struct address_space * mapping = inode - > i_mapping ;
loff_t start = inode - > i_size , count = size - inode - > i_size ;
int err ;
err = generic_cont_expand_simple ( inode , size ) ;
if ( err )
goto out ;
2018-10-31 01:06:57 +03:00
fat_truncate_time ( inode , NULL , S_CTIME | S_MTIME ) ;
2006-01-08 12:02:13 +03:00
mark_inode_dirty ( inode ) ;
2009-08-17 19:00:02 +04:00
if ( IS_SYNC ( inode ) ) {
int err2 ;
/*
* Opencode syncing since we don ' t have a file open to use
* standard fsync path .
*/
err = filemap_fdatawrite_range ( mapping , start ,
start + count - 1 ) ;
err2 = sync_mapping_buffers ( mapping ) ;
if ( ! err )
err = err2 ;
err2 = write_inode_now ( inode , 1 ) ;
if ( ! err )
err = err2 ;
if ( ! err ) {
err = filemap_fdatawait_range ( mapping , start ,
start + count - 1 ) ;
}
}
2006-01-08 12:02:13 +03:00
out :
return err ;
}
2016-01-21 01:59:41 +03:00
/*
* Preallocate space for a file . This implements fat ' s fallocate file
* operation , which gets called from sys_fallocate system call . User
* space requests len bytes at offset . If FALLOC_FL_KEEP_SIZE is set
* we just allocate clusters without zeroing them out . Otherwise we
* allocate and zero out clusters via an expanding truncate .
*/
static long fat_fallocate ( struct file * file , int mode ,
loff_t offset , loff_t len )
{
int nr_cluster ; /* Number of clusters to be allocated */
loff_t mm_bytes ; /* Number of bytes to be allocated for file */
loff_t ondisksize ; /* block aligned on-disk size in bytes*/
struct inode * inode = file - > f_mapping - > host ;
struct super_block * sb = inode - > i_sb ;
struct msdos_sb_info * sbi = MSDOS_SB ( sb ) ;
int err = 0 ;
/* No support for hole punch or other fallocate flags. */
if ( mode & ~ FALLOC_FL_KEEP_SIZE )
return - EOPNOTSUPP ;
/* No support for dir */
if ( ! S_ISREG ( inode - > i_mode ) )
return - EOPNOTSUPP ;
2016-01-22 23:40:57 +03:00
inode_lock ( inode ) ;
2016-01-21 01:59:41 +03:00
if ( mode & FALLOC_FL_KEEP_SIZE ) {
ondisksize = inode - > i_blocks < < 9 ;
if ( ( offset + len ) < = ondisksize )
goto error ;
/* First compute the number of clusters to be allocated */
mm_bytes = offset + len - ondisksize ;
nr_cluster = ( mm_bytes + ( sbi - > cluster_size - 1 ) ) > >
sbi - > cluster_bits ;
/* Start the allocation.We are not zeroing out the clusters */
while ( nr_cluster - - > 0 ) {
err = fat_add_cluster ( inode ) ;
if ( err )
goto error ;
}
} else {
if ( ( offset + len ) < = i_size_read ( inode ) )
goto error ;
/* This is just an expanding truncate */
err = fat_cont_expand ( inode , ( offset + len ) ) ;
}
error :
2016-01-22 23:40:57 +03:00
inode_unlock ( inode ) ;
2016-01-21 01:59:41 +03:00
return err ;
}
2005-04-17 02:20:36 +04:00
/* Free all clusters after the skip'th cluster. */
static int fat_free ( struct inode * inode , int skip )
{
struct super_block * sb = inode - > i_sb ;
int err , wait , free_start , i_start , i_logstart ;
if ( MSDOS_I ( inode ) - > i_start = = 0 )
return 0 ;
2006-02-03 14:04:44 +03:00
fat_cache_inval_inode ( inode ) ;
2005-04-17 02:20:36 +04:00
wait = IS_DIRSYNC ( inode ) ;
2006-02-03 14:04:44 +03:00
i_start = free_start = MSDOS_I ( inode ) - > i_start ;
i_logstart = MSDOS_I ( inode ) - > i_logstart ;
/* First, we write the new file size. */
if ( ! skip ) {
MSDOS_I ( inode ) - > i_start = 0 ;
MSDOS_I ( inode ) - > i_logstart = 0 ;
}
MSDOS_I ( inode ) - > i_attrs | = ATTR_ARCH ;
2018-10-31 01:06:57 +03:00
fat_truncate_time ( inode , NULL , S_CTIME | S_MTIME ) ;
2006-02-03 14:04:44 +03:00
if ( wait ) {
err = fat_sync_inode ( inode ) ;
if ( err ) {
MSDOS_I ( inode ) - > i_start = i_start ;
MSDOS_I ( inode ) - > i_logstart = i_logstart ;
return err ;
}
} else
mark_inode_dirty ( inode ) ;
/* Write a new EOF, and get the remaining cluster chain for freeing. */
2005-04-17 02:20:36 +04:00
if ( skip ) {
struct fat_entry fatent ;
int ret , fclus , dclus ;
ret = fat_get_cluster ( inode , skip - 1 , & fclus , & dclus ) ;
if ( ret < 0 )
return ret ;
else if ( ret = = FAT_ENT_EOF )
return 0 ;
fatent_init ( & fatent ) ;
ret = fat_ent_read ( inode , & fatent , dclus ) ;
if ( ret = = FAT_ENT_EOF ) {
fatent_brelse ( & fatent ) ;
return 0 ;
} else if ( ret = = FAT_ENT_FREE ) {
2009-06-03 21:34:22 +04:00
fat_fs_error ( sb ,
2005-04-17 02:20:36 +04:00
" %s: invalid cluster chain (i_pos %lld) " ,
2008-04-30 11:55:09 +04:00
__func__ , MSDOS_I ( inode ) - > i_pos ) ;
2005-04-17 02:20:36 +04:00
ret = - EIO ;
} else if ( ret > 0 ) {
err = fat_ent_write ( inode , & fatent , FAT_ENT_EOF , wait ) ;
if ( err )
ret = err ;
}
fatent_brelse ( & fatent ) ;
if ( ret < 0 )
return ret ;
free_start = ret ;
}
inode - > i_blocks = skip < < ( MSDOS_SB ( sb ) - > cluster_bits - 9 ) ;
/* Freeing the remained cluster chain */
return fat_free_clusters ( inode , free_start ) ;
}
2010-05-26 19:05:38 +04:00
void fat_truncate_blocks ( struct inode * inode , loff_t offset )
2005-04-17 02:20:36 +04:00
{
2008-05-30 03:14:05 +04:00
struct msdos_sb_info * sbi = MSDOS_SB ( inode - > i_sb ) ;
2005-04-17 02:20:36 +04:00
const unsigned int cluster_size = sbi - > cluster_size ;
int nr_clusters ;
/*
* This protects against truncating a file bigger than it was then
* trying to write into the hole .
*/
2010-05-26 19:05:38 +04:00
if ( MSDOS_I ( inode ) - > mmu_private > offset )
MSDOS_I ( inode ) - > mmu_private = offset ;
2005-04-17 02:20:36 +04:00
2010-05-26 19:05:38 +04:00
nr_clusters = ( offset + ( cluster_size - 1 ) ) > > sbi - > cluster_bits ;
2005-04-17 02:20:36 +04:00
fat_free ( inode , nr_clusters ) ;
2006-09-29 13:00:03 +04:00
fat_flush_inodes ( inode - > i_sb , inode , NULL ) ;
2005-04-17 02:20:36 +04:00
}
2023-01-13 14:49:12 +03:00
int fat_getattr ( struct mnt_idmap * idmap , const struct path * path ,
2021-01-21 16:19:43 +03:00
struct kstat * stat , u32 request_mask , unsigned int flags )
2006-11-16 12:19:28 +03:00
{
statx: Add a system call to make enhanced file info available
Add a system call to make extended file information available, including
file creation and some attribute flags where available through the
underlying filesystem.
The getattr inode operation is altered to take two additional arguments: a
u32 request_mask and an unsigned int flags that indicate the
synchronisation mode. This change is propagated to the vfs_getattr*()
function.
Functions like vfs_stat() are now inline wrappers around new functions
vfs_statx() and vfs_statx_fd() to reduce stack usage.
========
OVERVIEW
========
The idea was initially proposed as a set of xattrs that could be retrieved
with getxattr(), but the general preference proved to be for a new syscall
with an extended stat structure.
A number of requests were gathered for features to be included. The
following have been included:
(1) Make the fields a consistent size on all arches and make them large.
(2) Spare space, request flags and information flags are provided for
future expansion.
(3) Better support for the y2038 problem [Arnd Bergmann] (tv_sec is an
__s64).
(4) Creation time: The SMB protocol carries the creation time, which could
be exported by Samba, which will in turn help CIFS make use of
FS-Cache as that can be used for coherency data (stx_btime).
This is also specified in NFSv4 as a recommended attribute and could
be exported by NFSD [Steve French].
(5) Lightweight stat: Ask for just those details of interest, and allow a
netfs (such as NFS) to approximate anything not of interest, possibly
without going to the server [Trond Myklebust, Ulrich Drepper, Andreas
Dilger] (AT_STATX_DONT_SYNC).
(6) Heavyweight stat: Force a netfs to go to the server, even if it thinks
its cached attributes are up to date [Trond Myklebust]
(AT_STATX_FORCE_SYNC).
And the following have been left out for future extension:
(7) Data version number: Could be used by userspace NFS servers [Aneesh
Kumar].
Can also be used to modify fill_post_wcc() in NFSD which retrieves
i_version directly, but has just called vfs_getattr(). It could get
it from the kstat struct if it used vfs_xgetattr() instead.
(There's disagreement on the exact semantics of a single field, since
not all filesystems do this the same way).
(8) BSD stat compatibility: Including more fields from the BSD stat such
as creation time (st_btime) and inode generation number (st_gen)
[Jeremy Allison, Bernd Schubert].
(9) Inode generation number: Useful for FUSE and userspace NFS servers
[Bernd Schubert].
(This was asked for but later deemed unnecessary with the
open-by-handle capability available and caused disagreement as to
whether it's a security hole or not).
(10) Extra coherency data may be useful in making backups [Andreas Dilger].
(No particular data were offered, but things like last backup
timestamp, the data version number and the DOS archive bit would come
into this category).
(11) Allow the filesystem to indicate what it can/cannot provide: A
filesystem can now say it doesn't support a standard stat feature if
that isn't available, so if, for instance, inode numbers or UIDs don't
exist or are fabricated locally...
(This requires a separate system call - I have an fsinfo() call idea
for this).
(12) Store a 16-byte volume ID in the superblock that can be returned in
struct xstat [Steve French].
(Deferred to fsinfo).
(13) Include granularity fields in the time data to indicate the
granularity of each of the times (NFSv4 time_delta) [Steve French].
(Deferred to fsinfo).
(14) FS_IOC_GETFLAGS value. These could be translated to BSD's st_flags.
Note that the Linux IOC flags are a mess and filesystems such as Ext4
define flags that aren't in linux/fs.h, so translation in the kernel
may be a necessity (or, possibly, we provide the filesystem type too).
(Some attributes are made available in stx_attributes, but the general
feeling was that the IOC flags were to ext[234]-specific and shouldn't
be exposed through statx this way).
(15) Mask of features available on file (eg: ACLs, seclabel) [Brad Boyer,
Michael Kerrisk].
(Deferred, probably to fsinfo. Finding out if there's an ACL or
seclabal might require extra filesystem operations).
(16) Femtosecond-resolution timestamps [Dave Chinner].
(A __reserved field has been left in the statx_timestamp struct for
this - if there proves to be a need).
(17) A set multiple attributes syscall to go with this.
===============
NEW SYSTEM CALL
===============
The new system call is:
int ret = statx(int dfd,
const char *filename,
unsigned int flags,
unsigned int mask,
struct statx *buffer);
The dfd, filename and flags parameters indicate the file to query, in a
similar way to fstatat(). There is no equivalent of lstat() as that can be
emulated with statx() by passing AT_SYMLINK_NOFOLLOW in flags. There is
also no equivalent of fstat() as that can be emulated by passing a NULL
filename to statx() with the fd of interest in dfd.
Whether or not statx() synchronises the attributes with the backing store
can be controlled by OR'ing a value into the flags argument (this typically
only affects network filesystems):
(1) AT_STATX_SYNC_AS_STAT tells statx() to behave as stat() does in this
respect.
(2) AT_STATX_FORCE_SYNC will require a network filesystem to synchronise
its attributes with the server - which might require data writeback to
occur to get the timestamps correct.
(3) AT_STATX_DONT_SYNC will suppress synchronisation with the server in a
network filesystem. The resulting values should be considered
approximate.
mask is a bitmask indicating the fields in struct statx that are of
interest to the caller. The user should set this to STATX_BASIC_STATS to
get the basic set returned by stat(). It should be noted that asking for
more information may entail extra I/O operations.
buffer points to the destination for the data. This must be 256 bytes in
size.
======================
MAIN ATTRIBUTES RECORD
======================
The following structures are defined in which to return the main attribute
set:
struct statx_timestamp {
__s64 tv_sec;
__s32 tv_nsec;
__s32 __reserved;
};
struct statx {
__u32 stx_mask;
__u32 stx_blksize;
__u64 stx_attributes;
__u32 stx_nlink;
__u32 stx_uid;
__u32 stx_gid;
__u16 stx_mode;
__u16 __spare0[1];
__u64 stx_ino;
__u64 stx_size;
__u64 stx_blocks;
__u64 __spare1[1];
struct statx_timestamp stx_atime;
struct statx_timestamp stx_btime;
struct statx_timestamp stx_ctime;
struct statx_timestamp stx_mtime;
__u32 stx_rdev_major;
__u32 stx_rdev_minor;
__u32 stx_dev_major;
__u32 stx_dev_minor;
__u64 __spare2[14];
};
The defined bits in request_mask and stx_mask are:
STATX_TYPE Want/got stx_mode & S_IFMT
STATX_MODE Want/got stx_mode & ~S_IFMT
STATX_NLINK Want/got stx_nlink
STATX_UID Want/got stx_uid
STATX_GID Want/got stx_gid
STATX_ATIME Want/got stx_atime{,_ns}
STATX_MTIME Want/got stx_mtime{,_ns}
STATX_CTIME Want/got stx_ctime{,_ns}
STATX_INO Want/got stx_ino
STATX_SIZE Want/got stx_size
STATX_BLOCKS Want/got stx_blocks
STATX_BASIC_STATS [The stuff in the normal stat struct]
STATX_BTIME Want/got stx_btime{,_ns}
STATX_ALL [All currently available stuff]
stx_btime is the file creation time, stx_mask is a bitmask indicating the
data provided and __spares*[] are where as-yet undefined fields can be
placed.
Time fields are structures with separate seconds and nanoseconds fields
plus a reserved field in case we want to add even finer resolution. Note
that times will be negative if before 1970; in such a case, the nanosecond
fields will also be negative if not zero.
The bits defined in the stx_attributes field convey information about a
file, how it is accessed, where it is and what it does. The following
attributes map to FS_*_FL flags and are the same numerical value:
STATX_ATTR_COMPRESSED File is compressed by the fs
STATX_ATTR_IMMUTABLE File is marked immutable
STATX_ATTR_APPEND File is append-only
STATX_ATTR_NODUMP File is not to be dumped
STATX_ATTR_ENCRYPTED File requires key to decrypt in fs
Within the kernel, the supported flags are listed by:
KSTAT_ATTR_FS_IOC_FLAGS
[Are any other IOC flags of sufficient general interest to be exposed
through this interface?]
New flags include:
STATX_ATTR_AUTOMOUNT Object is an automount trigger
These are for the use of GUI tools that might want to mark files specially,
depending on what they are.
Fields in struct statx come in a number of classes:
(0) stx_dev_*, stx_blksize.
These are local system information and are always available.
(1) stx_mode, stx_nlinks, stx_uid, stx_gid, stx_[amc]time, stx_ino,
stx_size, stx_blocks.
These will be returned whether the caller asks for them or not. The
corresponding bits in stx_mask will be set to indicate whether they
actually have valid values.
If the caller didn't ask for them, then they may be approximated. For
example, NFS won't waste any time updating them from the server,
unless as a byproduct of updating something requested.
If the values don't actually exist for the underlying object (such as
UID or GID on a DOS file), then the bit won't be set in the stx_mask,
even if the caller asked for the value. In such a case, the returned
value will be a fabrication.
Note that there are instances where the type might not be valid, for
instance Windows reparse points.
(2) stx_rdev_*.
This will be set only if stx_mode indicates we're looking at a
blockdev or a chardev, otherwise will be 0.
(3) stx_btime.
Similar to (1), except this will be set to 0 if it doesn't exist.
=======
TESTING
=======
The following test program can be used to test the statx system call:
samples/statx/test-statx.c
Just compile and run, passing it paths to the files you want to examine.
The file is built automatically if CONFIG_SAMPLES is enabled.
Here's some example output. Firstly, an NFS directory that crosses to
another FSID. Note that the AUTOMOUNT attribute is set because transiting
this directory will cause d_automount to be invoked by the VFS.
[root@andromeda ~]# /tmp/test-statx -A /warthog/data
statx(/warthog/data) = 0
results=7ff
Size: 4096 Blocks: 8 IO Block: 1048576 directory
Device: 00:26 Inode: 1703937 Links: 125
Access: (3777/drwxrwxrwx) Uid: 0 Gid: 4041
Access: 2016-11-24 09:02:12.219699527+0000
Modify: 2016-11-17 10:44:36.225653653+0000
Change: 2016-11-17 10:44:36.225653653+0000
Attributes: 0000000000001000 (-------- -------- -------- -------- -------- -------- ---m---- --------)
Secondly, the result of automounting on that directory.
[root@andromeda ~]# /tmp/test-statx /warthog/data
statx(/warthog/data) = 0
results=7ff
Size: 4096 Blocks: 8 IO Block: 1048576 directory
Device: 00:27 Inode: 2 Links: 125
Access: (3777/drwxrwxrwx) Uid: 0 Gid: 4041
Access: 2016-11-24 09:02:12.219699527+0000
Modify: 2016-11-17 10:44:36.225653653+0000
Change: 2016-11-17 10:44:36.225653653+0000
Signed-off-by: David Howells <dhowells@redhat.com>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
2017-01-31 19:46:22 +03:00
struct inode * inode = d_inode ( path - > dentry ) ;
2022-05-03 18:25:35 +03:00
struct msdos_sb_info * sbi = MSDOS_SB ( inode - > i_sb ) ;
2023-08-07 22:38:33 +03:00
generic_fillattr ( idmap , request_mask , inode , stat ) ;
2022-05-03 18:25:35 +03:00
stat - > blksize = sbi - > cluster_size ;
2013-04-30 03:21:11 +04:00
2022-05-03 18:25:35 +03:00
if ( sbi - > options . nfs = = FAT_NFS_NOSTALE_RO ) {
2013-04-30 03:21:11 +04:00
/* Use i_pos for ino. This is used as fileid of nfs. */
2022-05-03 18:25:35 +03:00
stat - > ino = fat_i_pos_read ( sbi , inode ) ;
2013-04-30 03:21:11 +04:00
}
2022-05-03 18:25:35 +03:00
if ( sbi - > options . isvfat & & request_mask & STATX_BTIME ) {
stat - > result_mask | = STATX_BTIME ;
stat - > btime = MSDOS_I ( inode ) - > i_crtime ;
}
2006-11-16 12:19:28 +03:00
return 0 ;
}
EXPORT_SYMBOL_GPL ( fat_getattr ) ;
2008-06-13 02:21:28 +04:00
static int fat_sanitize_mode ( const struct msdos_sb_info * sbi ,
struct inode * inode , umode_t * mode_ptr )
2008-04-28 13:16:25 +04:00
{
2011-07-26 11:21:30 +04:00
umode_t mask , perm ;
2008-04-28 13:16:25 +04:00
2008-06-13 02:21:28 +04:00
/*
* Note , the basic check is already done by a caller of
2008-11-06 23:53:54 +03:00
* ( attr - > ia_mode & ~ FAT_VALID_MODE )
2008-06-13 02:21:28 +04:00
*/
if ( S_ISREG ( inode - > i_mode ) )
2008-04-28 13:16:25 +04:00
mask = sbi - > options . fs_fmask ;
else
mask = sbi - > options . fs_dmask ;
2008-06-13 02:21:28 +04:00
perm = * mode_ptr & ~ ( S_IFMT | mask ) ;
2008-04-28 13:16:25 +04:00
/*
* Of the r and x bits , all ( subject to umask ) must be present . Of the
* w bits , either all ( subject to umask ) or none must be present .
2008-11-06 23:53:55 +03:00
*
* If fat_mode_can_hold_ro ( inode ) is false , can ' t change w bits .
2008-04-28 13:16:25 +04:00
*/
2008-06-13 02:21:28 +04:00
if ( ( perm & ( S_IRUGO | S_IXUGO ) ) ! = ( inode - > i_mode & ( S_IRUGO | S_IXUGO ) ) )
2008-04-28 13:16:25 +04:00
return - EPERM ;
2008-11-06 23:53:55 +03:00
if ( fat_mode_can_hold_ro ( inode ) ) {
if ( ( perm & S_IWUGO ) & & ( ( perm & S_IWUGO ) ! = ( S_IWUGO & ~ mask ) ) )
return - EPERM ;
} else {
if ( ( perm & S_IWUGO ) ! = ( S_IWUGO & ~ mask ) )
return - EPERM ;
}
2008-04-28 13:16:25 +04:00
2008-06-13 02:21:28 +04:00
* mode_ptr & = S_IFMT | perm ;
2008-04-28 13:16:25 +04:00
return 0 ;
}
2023-01-13 14:49:30 +03:00
static int fat_allow_set_time ( struct mnt_idmap * idmap ,
2021-01-21 16:19:56 +03:00
struct msdos_sb_info * sbi , struct inode * inode )
2008-04-28 13:16:26 +04:00
{
2011-07-26 11:21:30 +04:00
umode_t allow_utime = sbi - > options . allow_utime ;
2008-04-28 13:16:26 +04:00
2023-01-13 14:49:30 +03:00
if ( ! vfsuid_eq_kuid ( i_uid_into_vfsuid ( idmap , inode ) ,
2022-09-09 12:30:19 +03:00
current_fsuid ( ) ) ) {
2023-01-13 14:49:30 +03:00
if ( vfsgid_in_group_p ( i_gid_into_vfsgid ( idmap , inode ) ) )
2008-04-28 13:16:26 +04:00
allow_utime > > = 3 ;
if ( allow_utime & MAY_WRITE )
return 1 ;
}
/* use a default check */
return 0 ;
}
2008-08-02 08:59:37 +04:00
# define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
2008-11-06 23:53:54 +03:00
/* valid file mode bits */
# define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO)
2008-08-02 08:59:37 +04:00
2023-01-13 14:49:11 +03:00
int fat_setattr ( struct mnt_idmap * idmap , struct dentry * dentry ,
2021-01-21 16:19:43 +03:00
struct iattr * attr )
2008-04-28 13:16:25 +04:00
{
struct msdos_sb_info * sbi = MSDOS_SB ( dentry - > d_sb ) ;
2015-03-18 01:25:59 +03:00
struct inode * inode = d_inode ( dentry ) ;
2008-04-28 13:16:26 +04:00
unsigned int ia_valid ;
2008-11-06 23:53:55 +03:00
int error ;
2008-04-28 13:16:25 +04:00
2008-04-28 13:16:26 +04:00
/* Check for setting the inode time. */
ia_valid = attr - > ia_valid ;
2008-08-02 08:59:37 +04:00
if ( ia_valid & TIMES_SET_FLAGS ) {
2023-01-13 14:49:30 +03:00
if ( fat_allow_set_time ( idmap , sbi , inode ) )
2008-08-02 08:59:37 +04:00
attr - > ia_valid & = ~ TIMES_SET_FLAGS ;
2008-04-28 13:16:26 +04:00
}
2023-01-13 14:49:11 +03:00
error = setattr_prepare ( idmap , dentry , attr ) ;
2008-04-28 13:16:26 +04:00
attr - > ia_valid = ia_valid ;
2008-04-28 13:16:25 +04:00
if ( error ) {
if ( sbi - > options . quiet )
error = 0 ;
goto out ;
}
2008-06-13 02:21:28 +04:00
2010-06-04 13:30:03 +04:00
/*
* Expand the file . Since inode_setattr ( ) updates - > i_size
* before calling the - > truncate ( ) , but FAT needs to fill the
* hole before it . XXX : this is no longer true with new truncate
* sequence .
*/
if ( attr - > ia_valid & ATTR_SIZE ) {
2011-06-24 22:29:45 +04:00
inode_dio_wait ( inode ) ;
2010-06-04 13:30:03 +04:00
if ( attr - > ia_size > inode - > i_size ) {
error = fat_cont_expand ( inode , attr - > ia_size ) ;
if ( error | | attr - > ia_valid = = ATTR_SIZE )
goto out ;
attr - > ia_valid & = ~ ATTR_SIZE ;
}
}
2008-04-28 13:16:25 +04:00
if ( ( ( attr - > ia_valid & ATTR_UID ) & &
2023-01-13 14:49:32 +03:00
( ! uid_eq ( from_vfsuid ( idmap , i_user_ns ( inode ) , attr - > ia_vfsuid ) ,
2022-06-21 17:14:54 +03:00
sbi - > options . fs_uid ) ) ) | |
2008-04-28 13:16:25 +04:00
( ( attr - > ia_valid & ATTR_GID ) & &
2023-01-13 14:49:32 +03:00
( ! gid_eq ( from_vfsgid ( idmap , i_user_ns ( inode ) , attr - > ia_vfsgid ) ,
2022-06-21 17:14:54 +03:00
sbi - > options . fs_gid ) ) ) | |
2008-04-28 13:16:26 +04:00
( ( attr - > ia_valid & ATTR_MODE ) & &
2008-11-06 23:53:54 +03:00
( attr - > ia_mode & ~ FAT_VALID_MODE ) ) )
2008-04-28 13:16:25 +04:00
error = - EPERM ;
if ( error ) {
if ( sbi - > options . quiet )
error = 0 ;
goto out ;
}
2008-06-13 02:21:28 +04:00
/*
* We don ' t return - EPERM here . Yes , strange , but this is too
* old behavior .
*/
if ( attr - > ia_valid & ATTR_MODE ) {
if ( fat_sanitize_mode ( sbi , inode , & attr - > ia_mode ) < 0 )
attr - > ia_valid & = ~ ATTR_MODE ;
}
2008-04-28 13:16:25 +04:00
2010-05-26 19:05:38 +04:00
if ( attr - > ia_valid & ATTR_SIZE ) {
2014-12-13 03:57:26 +03:00
error = fat_block_truncate_page ( inode , attr - > ia_size ) ;
if ( error )
goto out ;
2011-06-24 22:29:40 +04:00
down_write ( & MSDOS_I ( inode ) - > truncate_lock ) ;
2010-06-04 13:30:04 +04:00
truncate_setsize ( inode , attr - > ia_size ) ;
fat_truncate_blocks ( inode , attr - > ia_size ) ;
2011-06-24 22:29:40 +04:00
up_write ( & MSDOS_I ( inode ) - > truncate_lock ) ;
2010-05-26 19:05:38 +04:00
}
2018-10-31 01:07:01 +03:00
/*
* setattr_copy can ' t truncate these appropriately , so we ' ll
* copy them ourselves
*/
if ( attr - > ia_valid & ATTR_ATIME )
fat_truncate_time ( inode , & attr - > ia_atime , S_ATIME ) ;
if ( attr - > ia_valid & ATTR_CTIME )
fat_truncate_time ( inode , & attr - > ia_ctime , S_CTIME ) ;
if ( attr - > ia_valid & ATTR_MTIME )
fat_truncate_time ( inode , & attr - > ia_mtime , S_MTIME ) ;
attr - > ia_valid & = ~ ( ATTR_ATIME | ATTR_CTIME | ATTR_MTIME ) ;
2023-01-13 14:49:11 +03:00
setattr_copy ( idmap , inode , attr ) ;
2010-05-26 19:05:38 +04:00
mark_inode_dirty ( inode ) ;
2008-04-28 13:16:25 +04:00
out :
return error ;
}
EXPORT_SYMBOL_GPL ( fat_setattr ) ;
2007-02-12 11:55:38 +03:00
const struct inode_operations fat_file_inode_operations = {
2008-04-28 13:16:25 +04:00
. setattr = fat_setattr ,
2006-11-16 12:19:28 +03:00
. getattr = fat_getattr ,
2018-10-31 01:06:53 +03:00
. update_time = fat_update_time ,
2005-04-17 02:20:36 +04:00
} ;