0001 =======================
0002 Direct Access for files
0003 =======================
0004
0005 Motivation
0006 ----------
0007
0008 The page cache is usually used to buffer reads and writes to files.
0009 It is also used to provide the pages which are mapped into userspace
0010 by a call to mmap.
0011
0012 For block devices that are memory-like, the page cache pages would be
0013 unnecessary copies of the original storage. The `DAX` code removes the
0014 extra copy by performing reads and writes directly to the storage device.
0015 For file mappings, the storage device is mapped directly into userspace.
0016
0017
0018 Usage
0019 -----
0020
0021 If you have a block device which supports `DAX`, you can make a filesystem
0022 on it as usual. The `DAX` code currently only supports files with a block
0023 size equal to your kernel's `PAGE_SIZE`, so you may need to specify a block
0024 size when creating the filesystem.
0025
0026 Currently 5 filesystems support `DAX`: ext2, ext4, xfs, virtiofs and erofs.
0027 Enabling `DAX` on them is different.
0028
0029 Enabling DAX on ext2 and erofs
0030 ------------------------------
0031
0032 When mounting the filesystem, use the ``-o dax`` option on the command line or
0033 add 'dax' to the options in ``/etc/fstab``. This works to enable `DAX` on all files
0034 within the filesystem. It is equivalent to the ``-o dax=always`` behavior below.
0035
0036
0037 Enabling DAX on xfs and ext4
0038 ----------------------------
0039
0040 Summary
0041 -------
0042
0043 1. There exists an in-kernel file access mode flag `S_DAX` that corresponds to
0044 the statx flag `STATX_ATTR_DAX`. See the manpage for statx(2) for details
0045 about this access mode.
0046
0047 2. There exists a persistent flag `FS_XFLAG_DAX` that can be applied to regular
0048 files and directories. This advisory flag can be set or cleared at any
0049 time, but doing so does not immediately affect the `S_DAX` state.
0050
0051 3. If the persistent `FS_XFLAG_DAX` flag is set on a directory, this flag will
0052 be inherited by all regular files and subdirectories that are subsequently
0053 created in this directory. Files and subdirectories that exist at the time
0054 this flag is set or cleared on the parent directory are not modified by
0055 this modification of the parent directory.
0056
0057 4. There exist dax mount options which can override `FS_XFLAG_DAX` in the
0058 setting of the `S_DAX` flag. Given underlying storage which supports `DAX` the
0059 following hold:
0060
0061 ``-o dax=inode`` means "follow `FS_XFLAG_DAX`" and is the default.
0062
0063 ``-o dax=never`` means "never set `S_DAX`, ignore `FS_XFLAG_DAX`."
0064
0065 ``-o dax=always`` means "always set `S_DAX` ignore `FS_XFLAG_DAX`."
0066
0067 ``-o dax`` is a legacy option which is an alias for ``dax=always``.
0068
0069 .. warning::
0070
0071 The option ``-o dax`` may be removed in the future so ``-o dax=always`` is
0072 the preferred method for specifying this behavior.
0073
0074 .. note::
0075
0076 Modifications to and the inheritance behavior of `FS_XFLAG_DAX` remain
0077 the same even when the filesystem is mounted with a dax option. However,
0078 in-core inode state (`S_DAX`) will be overridden until the filesystem is
0079 remounted with dax=inode and the inode is evicted from kernel memory.
0080
0081 5. The `S_DAX` policy can be changed via:
0082
0083 a) Setting the parent directory `FS_XFLAG_DAX` as needed before files are
0084 created
0085
0086 b) Setting the appropriate dax="foo" mount option
0087
0088 c) Changing the `FS_XFLAG_DAX` flag on existing regular files and
0089 directories. This has runtime constraints and limitations that are
0090 described in 6) below.
0091
0092 6. When changing the `S_DAX` policy via toggling the persistent `FS_XFLAG_DAX`
0093 flag, the change to existing regular files won't take effect until the
0094 files are closed by all processes.
0095
0096
0097 Details
0098 -------
0099
0100 There are 2 per-file dax flags. One is a persistent inode setting (`FS_XFLAG_DAX`)
0101 and the other is a volatile flag indicating the active state of the feature
0102 (`S_DAX`).
0103
0104 `FS_XFLAG_DAX` is preserved within the filesystem. This persistent config
0105 setting can be set, cleared and/or queried using the `FS_IOC_FS`[`GS`]`ETXATTR` ioctl
0106 (see ioctl_xfs_fsgetxattr(2)) or an utility such as 'xfs_io'.
0107
0108 New files and directories automatically inherit `FS_XFLAG_DAX` from
0109 their parent directory **when created**. Therefore, setting `FS_XFLAG_DAX` at
0110 directory creation time can be used to set a default behavior for an entire
0111 sub-tree.
0112
0113 To clarify inheritance, here are 3 examples:
0114
0115 Example A:
0116
0117 .. code-block:: shell
0118
0119 mkdir -p a/b/c
0120 xfs_io -c 'chattr +x' a
0121 mkdir a/b/c/d
0122 mkdir a/e
0123
0124 ------[outcome]------
0125
0126 dax: a,e
0127 no dax: b,c,d
0128
0129 Example B:
0130
0131 .. code-block:: shell
0132
0133 mkdir a
0134 xfs_io -c 'chattr +x' a
0135 mkdir -p a/b/c/d
0136
0137 ------[outcome]------
0138
0139 dax: a,b,c,d
0140 no dax:
0141
0142 Example C:
0143
0144 .. code-block:: shell
0145
0146 mkdir -p a/b/c
0147 xfs_io -c 'chattr +x' c
0148 mkdir a/b/c/d
0149
0150 ------[outcome]------
0151
0152 dax: c,d
0153 no dax: a,b
0154
0155 The current enabled state (`S_DAX`) is set when a file inode is instantiated in
0156 memory by the kernel. It is set based on the underlying media support, the
0157 value of `FS_XFLAG_DAX` and the filesystem's dax mount option.
0158
0159 statx can be used to query `S_DAX`.
0160
0161 .. note::
0162
0163 That only regular files will ever have `S_DAX` set and therefore statx
0164 will never indicate that `S_DAX` is set on directories.
0165
0166 Setting the `FS_XFLAG_DAX` flag (specifically or through inheritance) occurs even
0167 if the underlying media does not support dax and/or the filesystem is
0168 overridden with a mount option.
0169
0170
0171 Enabling DAX on virtiofs
0172 ----------------------------
0173 The semantic of DAX on virtiofs is basically equal to that on ext4 and xfs,
0174 except that when '-o dax=inode' is specified, virtiofs client derives the hint
0175 whether DAX shall be enabled or not from virtiofs server through FUSE protocol,
0176 rather than the persistent `FS_XFLAG_DAX` flag. That is, whether DAX shall be
0177 enabled or not is completely determined by virtiofs server, while virtiofs
0178 server itself may deploy various algorithm making this decision, e.g. depending
0179 on the persistent `FS_XFLAG_DAX` flag on the host.
0180
0181 It is still supported to set or clear persistent `FS_XFLAG_DAX` flag inside
0182 guest, but it is not guaranteed that DAX will be enabled or disabled for
0183 corresponding file then. Users inside guest still need to call statx(2) and
0184 check the statx flag `STATX_ATTR_DAX` to see if DAX is enabled for this file.
0185
0186
0187 Implementation Tips for Block Driver Writers
0188 --------------------------------------------
0189
0190 To support `DAX` in your block driver, implement the 'direct_access'
0191 block device operation. It is used to translate the sector number
0192 (expressed in units of 512-byte sectors) to a page frame number (pfn)
0193 that identifies the physical page for the memory. It also returns a
0194 kernel virtual address that can be used to access the memory.
0195
0196 The direct_access method takes a 'size' parameter that indicates the
0197 number of bytes being requested. The function should return the number
0198 of bytes that can be contiguously accessed at that offset. It may also
0199 return a negative errno if an error occurs.
0200
0201 In order to support this method, the storage must be byte-accessible by
0202 the CPU at all times. If your device uses paging techniques to expose
0203 a large amount of memory through a smaller window, then you cannot
0204 implement direct_access. Equally, if your device can occasionally
0205 stall the CPU for an extended period, you should also not attempt to
0206 implement direct_access.
0207
0208 These block devices may be used for inspiration:
0209 - brd: RAM backed block device driver
0210 - dcssblk: s390 dcss block device driver
0211 - pmem: NVDIMM persistent memory driver
0212
0213
0214 Implementation Tips for Filesystem Writers
0215 ------------------------------------------
0216
0217 Filesystem support consists of:
0218
0219 * Adding support to mark inodes as being `DAX` by setting the `S_DAX` flag in
0220 i_flags
0221 * Implementing ->read_iter and ->write_iter operations which use
0222 :c:func:`dax_iomap_rw()` when inode has `S_DAX` flag set
0223 * Implementing an mmap file operation for `DAX` files which sets the
0224 `VM_MIXEDMAP` and `VM_HUGEPAGE` flags on the `VMA`, and setting the vm_ops to
0225 include handlers for fault, pmd_fault, page_mkwrite, pfn_mkwrite. These
0226 handlers should probably call :c:func:`dax_iomap_fault()` passing the
0227 appropriate fault size and iomap operations.
0228 * Calling :c:func:`iomap_zero_range()` passing appropriate iomap operations
0229 instead of :c:func:`block_truncate_page()` for `DAX` files
0230 * Ensuring that there is sufficient locking between reads, writes,
0231 truncates and page faults
0232
0233 The iomap handlers for allocating blocks must make sure that allocated blocks
0234 are zeroed out and converted to written extents before being returned to avoid
0235 exposure of uninitialized data through mmap.
0236
0237 These filesystems may be used for inspiration:
0238
0239 .. seealso::
0240
0241 ext2: see Documentation/filesystems/ext2.rst
0242
0243 .. seealso::
0244
0245 xfs: see Documentation/admin-guide/xfs.rst
0246
0247 .. seealso::
0248
0249 ext4: see Documentation/filesystems/ext4/
0250
0251
0252 Handling Media Errors
0253 ---------------------
0254
0255 The libnvdimm subsystem stores a record of known media error locations for
0256 each pmem block device (in gendisk->badblocks). If we fault at such location,
0257 or one with a latent error not yet discovered, the application can expect
0258 to receive a `SIGBUS`. Libnvdimm also allows clearing of these errors by simply
0259 writing the affected sectors (through the pmem driver, and if the underlying
0260 NVDIMM supports the clear_poison DSM defined by ACPI).
0261
0262 Since `DAX` IO normally doesn't go through the ``driver/bio`` path, applications or
0263 sysadmins have an option to restore the lost data from a prior ``backup/inbuilt``
0264 redundancy in the following ways:
0265
0266 1. Delete the affected file, and restore from a backup (sysadmin route):
0267 This will free the filesystem blocks that were being used by the file,
0268 and the next time they're allocated, they will be zeroed first, which
0269 happens through the driver, and will clear bad sectors.
0270
0271 2. Truncate or hole-punch the part of the file that has a bad-block (at least
0272 an entire aligned sector has to be hole-punched, but not necessarily an
0273 entire filesystem block).
0274
0275 These are the two basic paths that allow `DAX` filesystems to continue operating
0276 in the presence of media errors. More robust error recovery mechanisms can be
0277 built on top of this in the future, for example, involving redundancy/mirroring
0278 provided at the block layer through DM, or additionally, at the filesystem
0279 level. These would have to rely on the above two tenets, that error clearing
0280 can happen either by sending an IO through the driver, or zeroing (also through
0281 the driver).
0282
0283
0284 Shortcomings
0285 ------------
0286
0287 Even if the kernel or its modules are stored on a filesystem that supports
0288 `DAX` on a block device that supports `DAX`, they will still be copied into RAM.
0289
0290 The DAX code does not work correctly on architectures which have virtually
0291 mapped caches such as ARM, MIPS and SPARC.
0292
0293 Calling :c:func:`get_user_pages()` on a range of user memory that has been
0294 mmaped from a `DAX` file will fail when there are no 'struct page' to describe
0295 those pages. This problem has been addressed in some device drivers
0296 by adding optional struct page support for pages under the control of
0297 the driver (see `CONFIG_NVDIMM_PFN` in ``drivers/nvdimm`` for an example of
0298 how to do this). In the non struct page cases `O_DIRECT` reads/writes to
0299 those memory ranges from a non-`DAX` file will fail
0300
0301
0302 .. note::
0303
0304 `O_DIRECT` reads/writes _of a `DAX` file do work, it is the memory that
0305 is being accessed that is key here). Other things that will not work in
0306 the non struct page case include RDMA, :c:func:`sendfile()` and
0307 :c:func:`splice()`.