2007-10-21 23:42:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) Neil Brown 2002
|
|
|
|
* Copyright (C) Christoph Hellwig 2007
|
|
|
|
*
|
|
|
|
* This file contains the code mapping from inodes to NFS file handles,
|
|
|
|
* and for mapping back from file handles to dentries.
|
|
|
|
*
|
|
|
|
* For details on why we do all the strange and hairy things in here
|
2009-10-27 18:41:35 +00:00
|
|
|
* take a look at Documentation/filesystems/nfs/Exporting.
|
2007-10-21 23:42:19 +00:00
|
|
|
*/
|
2007-07-17 11:04:28 +00:00
|
|
|
#include <linux/exportfs.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/file.h>
|
|
|
|
#include <linux/module.h>
|
2007-07-17 11:04:30 +00:00
|
|
|
#include <linux/mount.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/namei.h>
|
2008-11-13 23:39:22 +00:00
|
|
|
#include <linux/sched.h>
|
2017-02-02 16:54:15 +00:00
|
|
|
#include <linux/cred.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-07-17 11:04:31 +00:00
|
|
|
#define dprintk(fmt, args...) do{}while(0)
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
|
2012-06-26 17:58:53 +00:00
|
|
|
static int get_name(const struct path *path, char *name, struct dentry *child);
|
2007-07-17 11:04:31 +00:00
|
|
|
|
|
|
|
|
2007-10-21 23:42:19 +00:00
|
|
|
static int exportfs_get_name(struct vfsmount *mnt, struct dentry *dir,
|
|
|
|
char *name, struct dentry *child)
|
2007-07-17 11:04:31 +00:00
|
|
|
{
|
2007-10-21 23:42:17 +00:00
|
|
|
const struct export_operations *nop = dir->d_sb->s_export_op;
|
2012-06-26 17:58:53 +00:00
|
|
|
struct path path = {.mnt = mnt, .dentry = dir};
|
2007-07-17 11:04:31 +00:00
|
|
|
|
|
|
|
if (nop->get_name)
|
|
|
|
return nop->get_name(dir, name, child);
|
|
|
|
else
|
2012-06-26 17:58:53 +00:00
|
|
|
return get_name(&path, name, child);
|
2007-07-17 11:04:31 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-07-17 11:04:32 +00:00
|
|
|
/*
|
|
|
|
* Check if the dentry or any of it's aliases is acceptable.
|
|
|
|
*/
|
2006-01-19 01:43:52 +00:00
|
|
|
static struct dentry *
|
|
|
|
find_acceptable_alias(struct dentry *result,
|
|
|
|
int (*acceptable)(void *context, struct dentry *dentry),
|
|
|
|
void *context)
|
|
|
|
{
|
|
|
|
struct dentry *dentry, *toput = NULL;
|
2011-01-07 06:50:06 +00:00
|
|
|
struct inode *inode;
|
2006-01-19 01:43:52 +00:00
|
|
|
|
2007-07-17 11:04:32 +00:00
|
|
|
if (acceptable(context, result))
|
|
|
|
return result;
|
|
|
|
|
2011-01-07 06:50:06 +00:00
|
|
|
inode = result->d_inode;
|
|
|
|
spin_lock(&inode->i_lock);
|
2014-10-26 23:19:16 +00:00
|
|
|
hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
|
2011-01-07 06:49:43 +00:00
|
|
|
dget(dentry);
|
2011-01-07 06:50:06 +00:00
|
|
|
spin_unlock(&inode->i_lock);
|
2006-01-19 01:43:52 +00:00
|
|
|
if (toput)
|
|
|
|
dput(toput);
|
|
|
|
if (dentry != result && acceptable(context, dentry)) {
|
|
|
|
dput(result);
|
|
|
|
return dentry;
|
|
|
|
}
|
2011-01-07 06:50:06 +00:00
|
|
|
spin_lock(&inode->i_lock);
|
2006-01-19 01:43:52 +00:00
|
|
|
toput = dentry;
|
|
|
|
}
|
2011-01-07 06:50:06 +00:00
|
|
|
spin_unlock(&inode->i_lock);
|
2006-01-19 01:43:52 +00:00
|
|
|
|
|
|
|
if (toput)
|
|
|
|
dput(toput);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-10-17 01:09:30 +00:00
|
|
|
static bool dentry_connected(struct dentry *dentry)
|
|
|
|
{
|
|
|
|
dget(dentry);
|
|
|
|
while (dentry->d_flags & DCACHE_DISCONNECTED) {
|
|
|
|
struct dentry *parent = dget_parent(dentry);
|
|
|
|
|
|
|
|
dput(dentry);
|
|
|
|
if (IS_ROOT(dentry)) {
|
|
|
|
dput(parent);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
dentry = parent;
|
|
|
|
}
|
|
|
|
dput(dentry);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:15:13 +00:00
|
|
|
static void clear_disconnected(struct dentry *dentry)
|
|
|
|
{
|
|
|
|
dget(dentry);
|
|
|
|
while (dentry->d_flags & DCACHE_DISCONNECTED) {
|
|
|
|
struct dentry *parent = dget_parent(dentry);
|
|
|
|
|
|
|
|
WARN_ON_ONCE(IS_ROOT(dentry));
|
|
|
|
|
|
|
|
spin_lock(&dentry->d_lock);
|
|
|
|
dentry->d_flags &= ~DCACHE_DISCONNECTED;
|
|
|
|
spin_unlock(&dentry->d_lock);
|
|
|
|
|
|
|
|
dput(dentry);
|
|
|
|
dentry = parent;
|
|
|
|
}
|
|
|
|
dput(dentry);
|
|
|
|
}
|
|
|
|
|
2013-10-17 15:13:00 +00:00
|
|
|
/*
|
|
|
|
* Reconnect a directory dentry with its parent.
|
|
|
|
*
|
|
|
|
* This can return a dentry, or NULL, or an error.
|
|
|
|
*
|
|
|
|
* In the first case the returned dentry is the parent of the given
|
|
|
|
* dentry, and may itself need to be reconnected to its parent.
|
|
|
|
*
|
|
|
|
* In the NULL case, a concurrent VFS operation has either renamed or
|
|
|
|
* removed this directory. The concurrent operation has reconnected our
|
|
|
|
* dentry, so we no longer need to.
|
|
|
|
*/
|
|
|
|
static struct dentry *reconnect_one(struct vfsmount *mnt,
|
|
|
|
struct dentry *dentry, char *nbuf)
|
|
|
|
{
|
|
|
|
struct dentry *parent;
|
|
|
|
struct dentry *tmp;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
parent = ERR_PTR(-EACCES);
|
2016-01-22 20:40:57 +00:00
|
|
|
inode_lock(dentry->d_inode);
|
2013-10-17 15:13:00 +00:00
|
|
|
if (mnt->mnt_sb->s_export_op->get_parent)
|
|
|
|
parent = mnt->mnt_sb->s_export_op->get_parent(dentry);
|
2016-01-22 20:40:57 +00:00
|
|
|
inode_unlock(dentry->d_inode);
|
2013-10-17 15:13:00 +00:00
|
|
|
|
|
|
|
if (IS_ERR(parent)) {
|
|
|
|
dprintk("%s: get_parent of %ld failed, err %d\n",
|
|
|
|
__func__, dentry->d_inode->i_ino, PTR_ERR(parent));
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
dprintk("%s: find name of %lu in %lu\n", __func__,
|
|
|
|
dentry->d_inode->i_ino, parent->d_inode->i_ino);
|
|
|
|
err = exportfs_get_name(mnt, parent, nbuf, dentry);
|
|
|
|
if (err == -ENOENT)
|
|
|
|
goto out_reconnected;
|
|
|
|
if (err)
|
|
|
|
goto out_err;
|
|
|
|
dprintk("%s: found name: %s\n", __func__, nbuf);
|
2016-04-14 23:12:42 +00:00
|
|
|
tmp = lookup_one_len_unlocked(nbuf, parent, strlen(nbuf));
|
2013-10-17 15:13:00 +00:00
|
|
|
if (IS_ERR(tmp)) {
|
|
|
|
dprintk("%s: lookup failed: %d\n", __func__, PTR_ERR(tmp));
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
if (tmp != dentry) {
|
2016-04-14 23:12:42 +00:00
|
|
|
/*
|
|
|
|
* Somebody has renamed it since exportfs_get_name();
|
|
|
|
* great, since it could've only been renamed if it
|
|
|
|
* got looked up and thus connected, and it would
|
|
|
|
* remain connected afterwards. We are done.
|
|
|
|
*/
|
2013-10-17 15:13:00 +00:00
|
|
|
dput(tmp);
|
|
|
|
goto out_reconnected;
|
|
|
|
}
|
|
|
|
dput(tmp);
|
|
|
|
if (IS_ROOT(dentry)) {
|
|
|
|
err = -ESTALE;
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
return parent;
|
|
|
|
|
|
|
|
out_err:
|
|
|
|
dput(parent);
|
|
|
|
return ERR_PTR(err);
|
|
|
|
out_reconnected:
|
|
|
|
dput(parent);
|
|
|
|
/*
|
|
|
|
* Someone must have renamed our entry into another parent, in
|
|
|
|
* which case it has been reconnected by the rename.
|
|
|
|
*
|
|
|
|
* Or someone removed it entirely, in which case filehandle
|
|
|
|
* lookup will succeed but the directory is now IS_DEAD and
|
|
|
|
* subsequent operations on it will fail.
|
|
|
|
*
|
|
|
|
* Alternatively, maybe there was no race at all, and the
|
|
|
|
* filesystem is just corrupt and gave us a parent that doesn't
|
|
|
|
* actually contain any entry pointing to this inode. So,
|
|
|
|
* double check that this worked and return -ESTALE if not:
|
|
|
|
*/
|
|
|
|
if (!dentry_connected(dentry))
|
|
|
|
return ERR_PTR(-ESTALE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-07-17 11:04:33 +00:00
|
|
|
/*
|
|
|
|
* Make sure target_dir is fully connected to the dentry tree.
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2013-10-23 00:59:19 +00:00
|
|
|
* On successful return, DCACHE_DISCONNECTED will be cleared on
|
|
|
|
* target_dir, and target_dir->d_parent->...->d_parent will reach the
|
|
|
|
* root of the filesystem.
|
|
|
|
*
|
|
|
|
* Whenever DCACHE_DISCONNECTED is unset, target_dir is fully connected.
|
|
|
|
* But the converse is not true: target_dir may have DCACHE_DISCONNECTED
|
|
|
|
* set but already be connected. In that case we'll verify the
|
|
|
|
* connection to root and then clear the flag.
|
|
|
|
*
|
|
|
|
* Note that target_dir could be removed by a concurrent operation. In
|
|
|
|
* that case reconnect_path may still succeed with target_dir fully
|
|
|
|
* connected, but further operations using the filehandle will fail when
|
|
|
|
* necessary (due to S_DEAD being set on the directory).
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2007-07-17 11:04:33 +00:00
|
|
|
static int
|
2008-08-11 16:39:47 +00:00
|
|
|
reconnect_path(struct vfsmount *mnt, struct dentry *target_dir, char *nbuf)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2013-10-18 01:34:21 +00:00
|
|
|
struct dentry *dentry, *parent;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-10-18 01:34:21 +00:00
|
|
|
dentry = dget(target_dir);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-10-18 01:34:21 +00:00
|
|
|
while (dentry->d_flags & DCACHE_DISCONNECTED) {
|
2013-10-18 01:42:35 +00:00
|
|
|
BUG_ON(dentry == mnt->mnt_sb->s_root);
|
2013-10-16 19:48:53 +00:00
|
|
|
|
2013-10-18 01:34:21 +00:00
|
|
|
if (IS_ROOT(dentry))
|
|
|
|
parent = reconnect_one(mnt, dentry, nbuf);
|
|
|
|
else
|
|
|
|
parent = dget_parent(dentry);
|
|
|
|
|
|
|
|
if (!parent)
|
2013-09-09 20:15:13 +00:00
|
|
|
break;
|
2013-10-18 01:42:35 +00:00
|
|
|
dput(dentry);
|
2013-10-18 01:34:21 +00:00
|
|
|
if (IS_ERR(parent))
|
|
|
|
return PTR_ERR(parent);
|
|
|
|
dentry = parent;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2013-10-18 01:34:21 +00:00
|
|
|
dput(dentry);
|
2013-10-17 01:09:30 +00:00
|
|
|
clear_disconnected(target_dir);
|
2007-07-17 11:04:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
struct getdents_callback {
|
2013-05-15 17:52:59 +00:00
|
|
|
struct dir_context ctx;
|
2005-04-16 22:20:36 +00:00
|
|
|
char *name; /* name that was found. It already points to a
|
|
|
|
buffer NAME_MAX+1 is size */
|
2013-09-10 15:41:12 +00:00
|
|
|
u64 ino; /* the inum we are looking for */
|
2005-04-16 22:20:36 +00:00
|
|
|
int found; /* inode matched? */
|
|
|
|
int sequence; /* sequence counter */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A rather strange filldir function to capture
|
|
|
|
* the name matching the specified inode number.
|
|
|
|
*/
|
2014-10-30 16:37:34 +00:00
|
|
|
static int filldir_one(struct dir_context *ctx, const char *name, int len,
|
2006-10-03 08:13:46 +00:00
|
|
|
loff_t pos, u64 ino, unsigned int d_type)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2014-10-30 16:37:34 +00:00
|
|
|
struct getdents_callback *buf =
|
|
|
|
container_of(ctx, struct getdents_callback, ctx);
|
2005-04-16 22:20:36 +00:00
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
buf->sequence++;
|
2013-09-06 20:55:36 +00:00
|
|
|
if (buf->ino == ino && len <= NAME_MAX) {
|
2005-04-16 22:20:36 +00:00
|
|
|
memcpy(buf->name, name, len);
|
|
|
|
buf->name[len] = '\0';
|
|
|
|
buf->found = 1;
|
|
|
|
result = -1;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get_name - default export_operations->get_name function
|
2014-06-04 23:11:15 +00:00
|
|
|
* @path: the directory in which to find a name
|
2005-04-16 22:20:36 +00:00
|
|
|
* @name: a pointer to a %NAME_MAX+1 char buffer to store the name
|
|
|
|
* @child: the dentry for the child directory.
|
|
|
|
*
|
|
|
|
* calls readdir on the parent until it finds an entry with
|
|
|
|
* the same inode number as the child, and returns that.
|
|
|
|
*/
|
2012-06-26 17:58:53 +00:00
|
|
|
static int get_name(const struct path *path, char *name, struct dentry *child)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-11-13 23:39:22 +00:00
|
|
|
const struct cred *cred = current_cred();
|
2012-06-26 17:58:53 +00:00
|
|
|
struct inode *dir = path->dentry->d_inode;
|
2005-04-16 22:20:36 +00:00
|
|
|
int error;
|
|
|
|
struct file *file;
|
2013-09-10 15:41:12 +00:00
|
|
|
struct kstat stat;
|
|
|
|
struct path child_path = {
|
|
|
|
.mnt = path->mnt,
|
|
|
|
.dentry = child,
|
|
|
|
};
|
2013-05-23 02:22:04 +00:00
|
|
|
struct getdents_callback buffer = {
|
|
|
|
.ctx.actor = filldir_one,
|
|
|
|
.name = name,
|
|
|
|
};
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
error = -ENOTDIR;
|
|
|
|
if (!dir || !S_ISDIR(dir->i_mode))
|
|
|
|
goto out;
|
|
|
|
error = -EINVAL;
|
|
|
|
if (!dir->i_fop)
|
|
|
|
goto out;
|
2013-09-10 15:41:12 +00:00
|
|
|
/*
|
|
|
|
* inode->i_ino is unsigned long, kstat->ino is u64, so the
|
|
|
|
* former would be insufficient on 32-bit hosts when the
|
|
|
|
* filesystem supports 64-bit inode numbers. So we need to
|
|
|
|
* actually call ->getattr, not just read i_ino:
|
|
|
|
*/
|
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 16:46:22 +00:00
|
|
|
error = vfs_getattr_nosec(&child_path, &stat,
|
|
|
|
STATX_INO, AT_STATX_SYNC_AS_STAT);
|
2013-09-10 15:41:12 +00:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
buffer.ino = stat.ino;
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Open the directory ...
|
|
|
|
*/
|
2012-06-26 17:58:53 +00:00
|
|
|
file = dentry_open(path, O_RDONLY, cred);
|
2005-04-16 22:20:36 +00:00
|
|
|
error = PTR_ERR(file);
|
|
|
|
if (IS_ERR(file))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
error = -EINVAL;
|
2016-04-21 03:08:32 +00:00
|
|
|
if (!file->f_op->iterate && !file->f_op->iterate_shared)
|
2005-04-16 22:20:36 +00:00
|
|
|
goto out_close;
|
|
|
|
|
|
|
|
buffer.sequence = 0;
|
|
|
|
while (1) {
|
|
|
|
int old_seq = buffer.sequence;
|
|
|
|
|
2013-05-15 17:52:59 +00:00
|
|
|
error = iterate_dir(file, &buffer.ctx);
|
2008-08-24 11:29:52 +00:00
|
|
|
if (buffer.found) {
|
|
|
|
error = 0;
|
|
|
|
break;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (error < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
error = -ENOENT;
|
|
|
|
if (old_seq == buffer.sequence)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
out_close:
|
|
|
|
fput(file);
|
|
|
|
out:
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* export_encode_fh - default export_operations->encode_fh function
|
2012-04-02 18:34:06 +00:00
|
|
|
* @inode: the object to encode
|
2014-06-04 23:11:15 +00:00
|
|
|
* @fid: where to store the file handle fragment
|
2005-04-16 22:20:36 +00:00
|
|
|
* @max_len: maximum length to store there
|
2012-04-02 18:34:06 +00:00
|
|
|
* @parent: parent directory inode, if wanted
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* This default encode_fh function assumes that the 32 inode number
|
|
|
|
* is suitable for locating an inode, and that the generation number
|
|
|
|
* can be used to check that it is still valid. It places them in the
|
|
|
|
* filehandle fragment where export_decode_fh expects to find them.
|
|
|
|
*/
|
2012-04-02 18:34:06 +00:00
|
|
|
static int export_encode_fh(struct inode *inode, struct fid *fid,
|
|
|
|
int *max_len, struct inode *parent)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
int len = *max_len;
|
2007-10-21 23:42:03 +00:00
|
|
|
int type = FILEID_INO32_GEN;
|
2011-01-29 13:13:25 +00:00
|
|
|
|
2012-04-02 18:34:06 +00:00
|
|
|
if (parent && (len < 4)) {
|
2011-01-29 13:13:25 +00:00
|
|
|
*max_len = 4;
|
2012-08-29 14:10:10 +00:00
|
|
|
return FILEID_INVALID;
|
2011-01-29 13:13:25 +00:00
|
|
|
} else if (len < 2) {
|
|
|
|
*max_len = 2;
|
2012-08-29 14:10:10 +00:00
|
|
|
return FILEID_INVALID;
|
2011-01-29 13:13:25 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
len = 2;
|
2007-10-21 23:42:03 +00:00
|
|
|
fid->i32.ino = inode->i_ino;
|
|
|
|
fid->i32.gen = inode->i_generation;
|
2012-04-02 18:34:06 +00:00
|
|
|
if (parent) {
|
2007-10-21 23:42:03 +00:00
|
|
|
fid->i32.parent_ino = parent->i_ino;
|
|
|
|
fid->i32.parent_gen = parent->i_generation;
|
2005-04-16 22:20:36 +00:00
|
|
|
len = 4;
|
2007-10-21 23:42:03 +00:00
|
|
|
type = FILEID_INO32_GEN_PARENT;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
*max_len = len;
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2012-12-18 00:05:08 +00:00
|
|
|
int exportfs_encode_inode_fh(struct inode *inode, struct fid *fid,
|
|
|
|
int *max_len, struct inode *parent)
|
|
|
|
{
|
|
|
|
const struct export_operations *nop = inode->i_sb->s_export_op;
|
|
|
|
|
|
|
|
if (nop && nop->encode_fh)
|
|
|
|
return nop->encode_fh(inode, fid->raw, max_len, parent);
|
|
|
|
|
|
|
|
return export_encode_fh(inode, fid, max_len, parent);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(exportfs_encode_inode_fh);
|
|
|
|
|
2007-10-21 23:42:03 +00:00
|
|
|
int exportfs_encode_fh(struct dentry *dentry, struct fid *fid, int *max_len,
|
2007-07-17 11:04:30 +00:00
|
|
|
int connectable)
|
|
|
|
{
|
2007-07-17 11:04:31 +00:00
|
|
|
int error;
|
2012-04-02 18:34:06 +00:00
|
|
|
struct dentry *p = NULL;
|
|
|
|
struct inode *inode = dentry->d_inode, *parent = NULL;
|
2007-07-17 11:04:30 +00:00
|
|
|
|
2012-04-02 18:34:06 +00:00
|
|
|
if (connectable && !S_ISDIR(inode->i_mode)) {
|
|
|
|
p = dget_parent(dentry);
|
|
|
|
/*
|
|
|
|
* note that while p might've ceased to be our parent already,
|
|
|
|
* it's still pinned by and still positive.
|
|
|
|
*/
|
|
|
|
parent = p->d_inode;
|
|
|
|
}
|
2012-12-18 00:05:08 +00:00
|
|
|
|
|
|
|
error = exportfs_encode_inode_fh(inode, fid, max_len, parent);
|
2012-04-02 18:34:06 +00:00
|
|
|
dput(p);
|
2007-07-17 11:04:31 +00:00
|
|
|
|
|
|
|
return error;
|
2007-07-17 11:04:30 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(exportfs_encode_fh);
|
|
|
|
|
2007-10-21 23:42:03 +00:00
|
|
|
struct dentry *exportfs_decode_fh(struct vfsmount *mnt, struct fid *fid,
|
|
|
|
int fh_len, int fileid_type,
|
|
|
|
int (*acceptable)(void *, struct dentry *), void *context)
|
2007-07-17 11:04:30 +00:00
|
|
|
{
|
2007-10-21 23:42:17 +00:00
|
|
|
const struct export_operations *nop = mnt->mnt_sb->s_export_op;
|
2007-10-21 23:42:05 +00:00
|
|
|
struct dentry *result, *alias;
|
2008-08-11 16:39:47 +00:00
|
|
|
char nbuf[NAME_MAX+1];
|
2007-10-21 23:42:05 +00:00
|
|
|
int err;
|
2007-07-17 11:04:30 +00:00
|
|
|
|
2007-10-21 23:42:05 +00:00
|
|
|
/*
|
|
|
|
* Try to get any dentry for the given file handle from the filesystem.
|
|
|
|
*/
|
2011-01-29 13:13:26 +00:00
|
|
|
if (!nop || !nop->fh_to_dentry)
|
|
|
|
return ERR_PTR(-ESTALE);
|
2007-10-21 23:42:05 +00:00
|
|
|
result = nop->fh_to_dentry(mnt->mnt_sb, fid, fh_len, fileid_type);
|
2016-08-04 00:19:06 +00:00
|
|
|
if (PTR_ERR(result) == -ENOMEM)
|
|
|
|
return ERR_CAST(result);
|
|
|
|
if (IS_ERR_OR_NULL(result))
|
|
|
|
return ERR_PTR(-ESTALE);
|
2007-10-21 23:42:05 +00:00
|
|
|
|
VFS: (Scripted) Convert S_ISLNK/DIR/REG(dentry->d_inode) to d_is_*(dentry)
Convert the following where appropriate:
(1) S_ISLNK(dentry->d_inode) to d_is_symlink(dentry).
(2) S_ISREG(dentry->d_inode) to d_is_reg(dentry).
(3) S_ISDIR(dentry->d_inode) to d_is_dir(dentry). This is actually more
complicated than it appears as some calls should be converted to
d_can_lookup() instead. The difference is whether the directory in
question is a real dir with a ->lookup op or whether it's a fake dir with
a ->d_automount op.
In some circumstances, we can subsume checks for dentry->d_inode not being
NULL into this, provided we the code isn't in a filesystem that expects
d_inode to be NULL if the dirent really *is* negative (ie. if we're going to
use d_inode() rather than d_backing_inode() to get the inode pointer).
Note that the dentry type field may be set to something other than
DCACHE_MISS_TYPE when d_inode is NULL in the case of unionmount, where the VFS
manages the fall-through from a negative dentry to a lower layer. In such a
case, the dentry type of the negative union dentry is set to the same as the
type of the lower dentry.
However, if you know d_inode is not NULL at the call site, then you can use
the d_is_xxx() functions even in a filesystem.
There is one further complication: a 0,0 chardev dentry may be labelled
DCACHE_WHITEOUT_TYPE rather than DCACHE_SPECIAL_TYPE. Strictly, this was
intended for special directory entry types that don't have attached inodes.
The following perl+coccinelle script was used:
use strict;
my @callers;
open($fd, 'git grep -l \'S_IS[A-Z].*->d_inode\' |') ||
die "Can't grep for S_ISDIR and co. callers";
@callers = <$fd>;
close($fd);
unless (@callers) {
print "No matches\n";
exit(0);
}
my @cocci = (
'@@',
'expression E;',
'@@',
'',
'- S_ISLNK(E->d_inode->i_mode)',
'+ d_is_symlink(E)',
'',
'@@',
'expression E;',
'@@',
'',
'- S_ISDIR(E->d_inode->i_mode)',
'+ d_is_dir(E)',
'',
'@@',
'expression E;',
'@@',
'',
'- S_ISREG(E->d_inode->i_mode)',
'+ d_is_reg(E)' );
my $coccifile = "tmp.sp.cocci";
open($fd, ">$coccifile") || die $coccifile;
print($fd "$_\n") || die $coccifile foreach (@cocci);
close($fd);
foreach my $file (@callers) {
chomp $file;
print "Processing ", $file, "\n";
system("spatch", "--sp-file", $coccifile, $file, "--in-place", "--no-show-diff") == 0 ||
die "spatch failed";
}
[AV: overlayfs parts skipped]
Signed-off-by: David Howells <dhowells@redhat.com>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
2015-01-29 12:02:35 +00:00
|
|
|
if (d_is_dir(result)) {
|
2007-10-21 23:42:05 +00:00
|
|
|
/*
|
|
|
|
* This request is for a directory.
|
|
|
|
*
|
|
|
|
* On the positive side there is only one dentry for each
|
|
|
|
* directory inode. On the negative side this implies that we
|
|
|
|
* to ensure our dentry is connected all the way up to the
|
|
|
|
* filesystem root.
|
|
|
|
*/
|
|
|
|
if (result->d_flags & DCACHE_DISCONNECTED) {
|
2008-08-11 16:39:47 +00:00
|
|
|
err = reconnect_path(mnt, result, nbuf);
|
2007-10-21 23:42:05 +00:00
|
|
|
if (err)
|
|
|
|
goto err_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!acceptable(context, result)) {
|
|
|
|
err = -EACCES;
|
|
|
|
goto err_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2007-07-17 11:04:31 +00:00
|
|
|
} else {
|
2007-10-21 23:42:05 +00:00
|
|
|
/*
|
|
|
|
* It's not a directory. Life is a little more complicated.
|
|
|
|
*/
|
|
|
|
struct dentry *target_dir, *nresult;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if either the dentry we just got from the filesystem
|
|
|
|
* or any alias for it is acceptable. This is always true
|
|
|
|
* if this filesystem is exported without the subtreecheck
|
|
|
|
* option. If the filesystem is exported with the subtree
|
|
|
|
* check option there's a fair chance we need to look at
|
|
|
|
* the parent directory in the file handle and make sure
|
|
|
|
* it's connected to the filesystem root.
|
|
|
|
*/
|
|
|
|
alias = find_acceptable_alias(result, acceptable, context);
|
|
|
|
if (alias)
|
|
|
|
return alias;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to extract a dentry for the parent directory from the
|
|
|
|
* file handle. If this fails we'll have to give up.
|
|
|
|
*/
|
|
|
|
err = -ESTALE;
|
|
|
|
if (!nop->fh_to_parent)
|
|
|
|
goto err_result;
|
|
|
|
|
|
|
|
target_dir = nop->fh_to_parent(mnt->mnt_sb, fid,
|
|
|
|
fh_len, fileid_type);
|
2008-12-08 23:24:18 +00:00
|
|
|
if (!target_dir)
|
|
|
|
goto err_result;
|
2007-10-21 23:42:05 +00:00
|
|
|
err = PTR_ERR(target_dir);
|
|
|
|
if (IS_ERR(target_dir))
|
|
|
|
goto err_result;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* And as usual we need to make sure the parent directory is
|
|
|
|
* connected to the filesystem root. The VFS really doesn't
|
|
|
|
* like disconnected directories..
|
|
|
|
*/
|
2008-08-11 16:39:47 +00:00
|
|
|
err = reconnect_path(mnt, target_dir, nbuf);
|
2007-10-21 23:42:05 +00:00
|
|
|
if (err) {
|
|
|
|
dput(target_dir);
|
|
|
|
goto err_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now that we've got both a well-connected parent and a
|
|
|
|
* dentry for the inode we're after, make sure that our
|
|
|
|
* inode is actually connected to the parent.
|
|
|
|
*/
|
2007-10-21 23:42:19 +00:00
|
|
|
err = exportfs_get_name(mnt, target_dir, nbuf, result);
|
2007-10-21 23:42:05 +00:00
|
|
|
if (!err) {
|
2016-01-22 20:40:57 +00:00
|
|
|
inode_lock(target_dir->d_inode);
|
2007-10-21 23:42:05 +00:00
|
|
|
nresult = lookup_one_len(nbuf, target_dir,
|
|
|
|
strlen(nbuf));
|
2016-01-22 20:40:57 +00:00
|
|
|
inode_unlock(target_dir->d_inode);
|
2007-10-21 23:42:05 +00:00
|
|
|
if (!IS_ERR(nresult)) {
|
|
|
|
if (nresult->d_inode) {
|
|
|
|
dput(result);
|
|
|
|
result = nresult;
|
|
|
|
} else
|
|
|
|
dput(nresult);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point we are done with the parent, but it's pinned
|
|
|
|
* by the child dentry anyway.
|
|
|
|
*/
|
|
|
|
dput(target_dir);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* And finally make sure the dentry is actually acceptable
|
|
|
|
* to NFSD.
|
|
|
|
*/
|
|
|
|
alias = find_acceptable_alias(result, acceptable, context);
|
|
|
|
if (!alias) {
|
|
|
|
err = -EACCES;
|
|
|
|
goto err_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return alias;
|
2007-07-17 11:04:31 +00:00
|
|
|
}
|
|
|
|
|
2007-10-21 23:42:05 +00:00
|
|
|
err_result:
|
|
|
|
dput(result);
|
2016-08-04 00:19:06 +00:00
|
|
|
if (err != -ENOMEM)
|
|
|
|
err = -ESTALE;
|
2007-10-21 23:42:05 +00:00
|
|
|
return ERR_PTR(err);
|
2007-07-17 11:04:30 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(exportfs_decode_fh);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
MODULE_LICENSE("GPL");
|