]> git.ipfire.org Git - thirdparty/linux.git/blame - include/linux/fs.h
Merge tag 'x86_cleanups_for_v6.19_rc1' of git://git.kernel.org/pub/scm/linux/kernel...
[thirdparty/linux.git] / include / linux / fs.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2#ifndef _LINUX_FS_H
3#define _LINUX_FS_H
4
f7b3d141 5#include <linux/fs/super.h>
8b17e540 6#include <linux/vfsdebug.h>
1da177e4 7#include <linux/linkage.h>
5dd43ce2 8#include <linux/wait_bit.h>
1da177e4
LT
9#include <linux/kdev_t.h>
10#include <linux/dcache.h>
3f8206d4 11#include <linux/path.h>
1da177e4
LT
12#include <linux/stat.h>
13#include <linux/cache.h>
1da177e4 14#include <linux/list.h>
4f5e65a1 15#include <linux/llist.h>
1da177e4 16#include <linux/radix-tree.h>
b93b0163 17#include <linux/xarray.h>
6b2dbba8 18#include <linux/rbtree.h>
1da177e4 19#include <linux/init.h>
914e2637 20#include <linux/pid.h>
187f1882 21#include <linux/bug.h>
1b1dcc1b 22#include <linux/mutex.h>
c8c06efa 23#include <linux/rwsem.h>
baabda26 24#include <linux/mm_types.h>
3bd858ab 25#include <linux/capability.h>
6188e10d 26#include <linux/semaphore.h>
c75b1d94 27#include <linux/fcntl.h>
ceb5bdc2 28#include <linux/rculist_bl.h>
07b8ce1e 29#include <linux/atomic.h>
83aeeada 30#include <linux/shrinker.h>
c1aab02d 31#include <linux/migrate_mode.h>
92361636 32#include <linux/uidgid.h>
5accdf82 33#include <linux/lockdep.h>
c2b1ad80 34#include <linux/percpu-rwsem.h>
853b39a7 35#include <linux/workqueue.h>
fceef393 36#include <linux/delayed_call.h>
85787090 37#include <linux/uuid.h>
5660e13d 38#include <linux/errseq.h>
d9a08a9e 39#include <linux/ioprio.h>
f1fffbd4
RV
40#include <linux/build_bug.h>
41#include <linux/stddef.h>
a6435940 42#include <linux/mount.h>
e6c9a714 43#include <linux/cred.h>
a793d79e 44#include <linux/mnt_idmapping.h>
8b9f3ac5 45#include <linux/slab.h>
0e4a8621 46#include <linux/maple_tree.h>
fe3944fb 47#include <linux/rw_hint.h>
90ee6ed7 48#include <linux/file_ref.h>
0e152beb 49#include <linux/unicode.h>
1da177e4 50
1da177e4 51#include <asm/byteorder.h>
607ca46e 52#include <uapi/linux/fs.h>
1da177e4 53
52ebea74 54struct bdi_writeback;
2f8b5444 55struct bio;
5a72e899 56struct io_comp_batch;
10c5db28 57struct fiemap_extent_info;
a885c8c4 58struct hd_geometry;
1da177e4 59struct iovec;
92198f7e 60struct kiocb;
57cc7215 61struct kobject;
1da177e4
LT
62struct pipe_inode_info;
63struct poll_table_struct;
64struct kstatfs;
65struct vm_area_struct;
66struct vfsmount;
745ca247 67struct cred;
a509bc1a 68struct swap_info_struct;
55985dd7 69struct seq_file;
3b93f911 70struct iov_iter;
07a3b8d0 71struct fsnotify_mark_connector;
f3a09c92 72struct fs_context;
d7167b14 73struct fs_parameter_spec;
ca115d7e 74struct file_kattr;
6f7db389 75struct iomap_ops;
6976ed2d 76struct delegated_inode;
1da177e4 77
74bf17cf 78extern void __init inode_init(void);
1da177e4 79extern void __init inode_init_early(void);
4248b0da
MG
80extern void __init files_init(void);
81extern void __init files_maxfiles_init(void);
1da177e4 82
518de9b3 83extern unsigned long get_max_files(void);
9b80a184 84extern unsigned int sysctl_nr_open;
dded4f4d 85
ddef7ed2
CH
86typedef __kernel_rwf_t rwf_t;
87
1da177e4
LT
88struct buffer_head;
89typedef int (get_block_t)(struct inode *inode, sector_t iblock,
90 struct buffer_head *bh_result, int create);
187372a3 91typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,
7b7a8665 92 ssize_t bytes, void *private);
1da177e4 93
bbc1096a
DH
94#define MAY_EXEC 0x00000001
95#define MAY_WRITE 0x00000002
96#define MAY_READ 0x00000004
97#define MAY_APPEND 0x00000008
98#define MAY_ACCESS 0x00000010
99#define MAY_OPEN 0x00000020
100#define MAY_CHDIR 0x00000040
101/* called from RCU mode, don't block */
102#define MAY_NOT_BLOCK 0x00000080
103
104/*
105 * flags in file.f_mode. Note that FMODE_READ and FMODE_WRITE must correspond
75abe329 106 * to O_WRONLY and O_RDWR via the strange trick in do_dentry_open()
bbc1096a
DH
107 */
108
109/* file is open for reading */
0a4f544d 110#define FMODE_READ ((__force fmode_t)(1 << 0))
bbc1096a 111/* file is open for writing */
0a4f544d 112#define FMODE_WRITE ((__force fmode_t)(1 << 1))
bbc1096a 113/* file is seekable */
0a4f544d 114#define FMODE_LSEEK ((__force fmode_t)(1 << 2))
bbc1096a 115/* file can be accessed using pread */
0a4f544d 116#define FMODE_PREAD ((__force fmode_t)(1 << 3))
bbc1096a 117/* file can be accessed using pwrite */
0a4f544d 118#define FMODE_PWRITE ((__force fmode_t)(1 << 4))
bbc1096a 119/* File is opened for execution with sys_execve / sys_uselib */
0a4f544d 120#define FMODE_EXEC ((__force fmode_t)(1 << 5))
ddd65e19 121/* File writes are restricted (block device specific) */
1b0aabcc 122#define FMODE_WRITE_RESTRICTED ((__force fmode_t)(1 << 6))
c34fc6f2
PS
123/* File supports atomic writes */
124#define FMODE_CAN_ATOMIC_WRITE ((__force fmode_t)(1 << 7))
0a4f544d 125
c34fc6f2 126/* FMODE_* bit 8 */
0a4f544d 127
bbc1096a 128/* 32bit hashes as llseek() offset (for directories) */
0a4f544d 129#define FMODE_32BITHASH ((__force fmode_t)(1 << 9))
bbc1096a 130/* 64bit hashes as llseek() offset (for directories) */
0a4f544d 131#define FMODE_64BITHASH ((__force fmode_t)(1 << 10))
bbc1096a
DH
132
133/*
134 * Don't update ctime and mtime.
135 *
136 * Currently a special hack for the XFS open_by_handle ioctl, but we'll
137 * hopefully graduate it to a proper O_CMTIME flag supported by open(2) soon.
138 */
0a4f544d 139#define FMODE_NOCMTIME ((__force fmode_t)(1 << 11))
bbc1096a
DH
140
141/* Expect random access pattern */
0a4f544d 142#define FMODE_RANDOM ((__force fmode_t)(1 << 12))
bbc1096a 143
d072148a
CH
144/* Supports IOCB_HAS_METADATA */
145#define FMODE_HAS_METADATA ((__force fmode_t)(1 << 13))
bbc1096a
DH
146
147/* File is opened with O_PATH; almost nothing can be done with it */
0a4f544d 148#define FMODE_PATH ((__force fmode_t)(1 << 14))
bbc1096a 149
2be7d348 150/* File needs atomic accesses to f_pos */
0a4f544d 151#define FMODE_ATOMIC_POS ((__force fmode_t)(1 << 15))
83f936c7 152/* Write access to underlying fs */
0a4f544d 153#define FMODE_WRITER ((__force fmode_t)(1 << 16))
7f7f25e8 154/* Has read method(s) */
0a4f544d 155#define FMODE_CAN_READ ((__force fmode_t)(1 << 17))
7f7f25e8 156/* Has write method(s) */
0a4f544d 157#define FMODE_CAN_WRITE ((__force fmode_t)(1 << 18))
9c225f26 158
0a4f544d
CB
159#define FMODE_OPENED ((__force fmode_t)(1 << 19))
160#define FMODE_CREATED ((__force fmode_t)(1 << 20))
f5d11409 161
10dce8af 162/* File is stream-like */
0a4f544d 163#define FMODE_STREAM ((__force fmode_t)(1 << 21))
10dce8af 164
a2ad63da 165/* File supports DIRECT IO */
0a4f544d 166#define FMODE_CAN_ODIRECT ((__force fmode_t)(1 << 22))
a2ad63da 167
0a4f544d 168#define FMODE_NOREUSE ((__force fmode_t)(1 << 23))
17e81022 169
62d53c4a 170/* File is embedded in backing_file object */
a94204f4
AG
171#define FMODE_BACKING ((__force fmode_t)(1 << 24))
172
173/*
174 * Together with FMODE_NONOTIFY_PERM defines which fsnotify events shouldn't be
175 * generated (see below)
176 */
177#define FMODE_NONOTIFY ((__force fmode_t)(1 << 25))
62d53c4a 178
a94204f4
AG
179/*
180 * Together with FMODE_NONOTIFY defines which fsnotify events shouldn't be
181 * generated (see below)
182 */
183#define FMODE_NONOTIFY_PERM ((__force fmode_t)(1 << 26))
bbc1096a 184
91f9943e 185/* File is capable of returning -EAGAIN if I/O will block */
0a4f544d 186#define FMODE_NOWAIT ((__force fmode_t)(1 << 27))
a07b2000
AV
187
188/* File represents mount that needs unmounting */
0a4f544d 189#define FMODE_NEED_UNMOUNT ((__force fmode_t)(1 << 28))
b745fafa 190
d3b1084d 191/* File does not contribute to nr_files count */
0a4f544d 192#define FMODE_NOACCOUNT ((__force fmode_t)(1 << 29))
80175539 193
a94204f4
AG
194/*
195 * The two FMODE_NONOTIFY* define which fsnotify events should not be generated
0d4c4d4e
AG
196 * for an open file. These are the possible values of
197 * (f->f_mode & FMODE_FSNOTIFY_MASK) and their meaning:
a94204f4
AG
198 *
199 * FMODE_NONOTIFY - suppress all (incl. non-permission) events.
200 * FMODE_NONOTIFY_PERM - suppress permission (incl. pre-content) events.
0d4c4d4e 201 * FMODE_NONOTIFY | FMODE_NONOTIFY_PERM - suppress only FAN_ACCESS_PERM.
a94204f4
AG
202 */
203#define FMODE_FSNOTIFY_MASK \
204 (FMODE_NONOTIFY | FMODE_NONOTIFY_PERM)
205
206#define FMODE_FSNOTIFY_NONE(mode) \
207 ((mode & FMODE_FSNOTIFY_MASK) == FMODE_NONOTIFY)
208#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
0d4c4d4e 209#define FMODE_FSNOTIFY_HSM(mode) \
a94204f4
AG
210 ((mode & FMODE_FSNOTIFY_MASK) == 0 || \
211 (mode & FMODE_FSNOTIFY_MASK) == (FMODE_NONOTIFY | FMODE_NONOTIFY_PERM))
0d4c4d4e 212#define FMODE_FSNOTIFY_ACCESS_PERM(mode) \
a94204f4
AG
213 ((mode & FMODE_FSNOTIFY_MASK) == 0)
214#else
0d4c4d4e 215#define FMODE_FSNOTIFY_ACCESS_PERM(mode) 0
a94204f4
AG
216#define FMODE_FSNOTIFY_HSM(mode) 0
217#endif
218
1da177e4
LT
219/*
220 * Attribute flags. These should be or-ed together to figure out what
221 * has been changed!
222 */
9767d749
MS
223#define ATTR_MODE (1 << 0)
224#define ATTR_UID (1 << 1)
225#define ATTR_GID (1 << 2)
226#define ATTR_SIZE (1 << 3)
227#define ATTR_ATIME (1 << 4)
228#define ATTR_MTIME (1 << 5)
229#define ATTR_CTIME (1 << 6)
230#define ATTR_ATIME_SET (1 << 7)
231#define ATTR_MTIME_SET (1 << 8)
232#define ATTR_FORCE (1 << 9) /* Not a change, but a change it */
afc5b36e 233#define ATTR_CTIME_SET (1 << 10)
9767d749
MS
234#define ATTR_KILL_SUID (1 << 11)
235#define ATTR_KILL_SGID (1 << 12)
236#define ATTR_FILE (1 << 13)
237#define ATTR_KILL_PRIV (1 << 14)
238#define ATTR_OPEN (1 << 15) /* Truncating from open(O_TRUNC) */
239#define ATTR_TIMES_SET (1 << 16)
f2b20f6e 240#define ATTR_TOUCH (1 << 17)
7e8ae848 241#define ATTR_DELEG (1 << 18) /* Delegated attrs. Don't break write delegations */
1da177e4 242
787fb6bc
MS
243/*
244 * Whiteout is represented by a char device. The following constants define the
245 * mode and device number to use.
246 */
247#define WHITEOUT_MODE 0
248#define WHITEOUT_DEV 0
249
1da177e4
LT
250/*
251 * This is the Inode Attributes structure, used for notify_change(). It
252 * uses the above definitions as flags, to know which values have changed.
253 * Also, in this manner, a Filesystem can look at only the values it cares
254 * about. Basically, these are the attributes that the VFS layer can
255 * request to change from the FS layer.
256 *
257 * Derek Atkins <warlord@MIT.EDU> 94-10-20
258 */
259struct iattr {
260 unsigned int ia_valid;
261 umode_t ia_mode;
45c31150
CB
262 /*
263 * The two anonymous unions wrap structures with the same member.
264 *
265 * Filesystems raising FS_ALLOW_IDMAP need to use ia_vfs{g,u}id which
266 * are a dedicated type requiring the filesystem to use the dedicated
267 * helpers. Other filesystem can continue to use ia_{g,u}id until they
268 * have been ported.
b27c82e1
CB
269 *
270 * They always contain the same value. In other words FS_ALLOW_IDMAP
271 * pass down the same value on idmapped mounts as they would on regular
272 * mounts.
45c31150
CB
273 */
274 union {
275 kuid_t ia_uid;
276 vfsuid_t ia_vfsuid;
277 };
278 union {
279 kgid_t ia_gid;
280 vfsgid_t ia_vfsgid;
281 };
1da177e4 282 loff_t ia_size;
95582b00
DD
283 struct timespec64 ia_atime;
284 struct timespec64 ia_mtime;
285 struct timespec64 ia_ctime;
cc4e69de
MS
286
287 /*
25985edc 288 * Not an attribute, but an auxiliary info for filesystems wanting to
cc4e69de
MS
289 * implement an ftruncate() like method. NOTE: filesystem should
290 * check for (ia_valid & ATTR_FILE), and not for (ia_file != NULL).
291 */
292 struct file *ia_file;
1da177e4
LT
293};
294
69c433ed
MS
295/*
296 * Maximum number of layers of fs stack. Needs to be limited to
297 * prevent kernel stack overflow
298 */
299#define FILESYSTEM_MAX_STACK_DEPTH 2
300
994fc28c
ZB
301/**
302 * enum positive_aop_returns - aop return codes with specific semantics
303 *
304 * @AOP_WRITEPAGE_ACTIVATE: Informs the caller that page writeback has
305 * completed, that the page is still locked, and
306 * should be considered active. The VM uses this hint
307 * to return the page to the active list -- it won't
308 * be a candidate for writeback again in the near
309 * future. Other callers must be careful to unlock
310 * the page if they get this return. Returned by
311 * writepage();
312 *
313 * @AOP_TRUNCATED_PAGE: The AOP method that was handed a locked page has
314 * unlocked it and the page might have been truncated.
315 * The caller should back up to acquiring a new page and
316 * trying again. The aop will be taking reasonable
317 * precautions not to livelock. If the caller held a page
318 * reference, it should drop it before retrying. Returned
7e0a1265 319 * by read_folio().
994fc28c
ZB
320 *
321 * address_space_operation functions return these large constants to indicate
322 * special semantics to the caller. These are much larger than the bytes in a
323 * page to allow for functions that return the number of bytes operated on in a
324 * given page.
325 */
326
327enum positive_aop_returns {
328 AOP_WRITEPAGE_ACTIVATE = 0x80000,
329 AOP_TRUNCATED_PAGE = 0x80001,
330};
331
1da177e4
LT
332/*
333 * oh the beauties of C type declarations.
334 */
335struct page;
336struct address_space;
337struct writeback_control;
8151b4c8 338struct readahead_control;
1da177e4 339
ce71bfea
JA
340/* Match RWF_* bits to IOCB bits */
341#define IOCB_HIPRI (__force int) RWF_HIPRI
342#define IOCB_DSYNC (__force int) RWF_DSYNC
343#define IOCB_SYNC (__force int) RWF_SYNC
344#define IOCB_NOWAIT (__force int) RWF_NOWAIT
345#define IOCB_APPEND (__force int) RWF_APPEND
c34fc6f2 346#define IOCB_ATOMIC (__force int) RWF_ATOMIC
af6505e5 347#define IOCB_DONTCACHE (__force int) RWF_DONTCACHE
db2ab24a 348#define IOCB_NOSIGNAL (__force int) RWF_NOSIGNAL
ce71bfea
JA
349
350/* non-RWF related bits - start at 16 */
351#define IOCB_EVENTFD (1 << 16)
352#define IOCB_DIRECT (1 << 17)
353#define IOCB_WRITE (1 << 18)
dd3e6d50 354/* iocb->ki_waitq is valid */
ce71bfea
JA
355#define IOCB_WAITQ (1 << 19)
356#define IOCB_NOIO (1 << 20)
6c7ef543
JA
357/* can use bio alloc cache */
358#define IOCB_ALLOC_CACHE (1 << 21)
b820de74 359/* kiocb is a read or write operation submitted by fs/aio.c. */
f9f85149
CH
360#define IOCB_AIO_RW (1 << 22)
361#define IOCB_HAS_METADATA (1 << 23)
e2e40f2c 362
f6c73a11
RHI
363/* for use in trace events */
364#define TRACE_IOCB_STRINGS \
365 { IOCB_HIPRI, "HIPRI" }, \
366 { IOCB_DSYNC, "DSYNC" }, \
367 { IOCB_SYNC, "SYNC" }, \
368 { IOCB_NOWAIT, "NOWAIT" }, \
369 { IOCB_APPEND, "APPEND" }, \
af6505e5
JA
370 { IOCB_ATOMIC, "ATOMIC" }, \
371 { IOCB_DONTCACHE, "DONTCACHE" }, \
f6c73a11
RHI
372 { IOCB_EVENTFD, "EVENTFD"}, \
373 { IOCB_DIRECT, "DIRECT" }, \
374 { IOCB_WRITE, "WRITE" }, \
375 { IOCB_WAITQ, "WAITQ" }, \
376 { IOCB_NOIO, "NOIO" }, \
9cf3516c 377 { IOCB_ALLOC_CACHE, "ALLOC_CACHE" }, \
6bdd3a01
CH
378 { IOCB_AIO_RW, "AIO_RW" }, \
379 { IOCB_HAS_METADATA, "AIO_HAS_METADATA" }
f6c73a11 380
e2e40f2c
CH
381struct kiocb {
382 struct file *ki_filp;
383 loff_t ki_pos;
6b19b766 384 void (*ki_complete)(struct kiocb *iocb, long ret);
e2e40f2c
CH
385 void *private;
386 int ki_flags;
d9a08a9e 387 u16 ki_ioprio; /* See linux/ioprio.h */
732f25a2 388 u8 ki_write_stream;
f9f85149
CH
389
390 /*
391 * Only used for async buffered reads, where it denotes the page
392 * waitqueue associated with completing the read.
393 * Valid IFF IOCB_WAITQ is set.
394 */
395 struct wait_page_queue *ki_waitq;
84c4e1f8 396};
e2e40f2c
CH
397
398static inline bool is_sync_kiocb(struct kiocb *kiocb)
399{
400 return kiocb->ki_complete == NULL;
401}
402
1da177e4 403struct address_space_operations {
5efe7448 404 int (*read_folio)(struct file *, struct folio *);
1da177e4
LT
405
406 /* Write back some dirty pages from this mapping. */
407 int (*writepages)(struct address_space *, struct writeback_control *);
408
3a3bae50 409 /* Mark a folio dirty. Return true if this dirtied it */
6f31a5a2 410 bool (*dirty_folio)(struct address_space *, struct folio *);
1da177e4 411
8151b4c8 412 void (*readahead)(struct readahead_control *);
1da177e4 413
e9d8e2bf 414 int (*write_begin)(const struct kiocb *, struct address_space *mapping,
9d6b0cd7 415 loff_t pos, unsigned len,
1da86618 416 struct folio **foliop, void **fsdata);
e9d8e2bf 417 int (*write_end)(const struct kiocb *, struct address_space *mapping,
afddba49 418 loff_t pos, unsigned len, unsigned copied,
a225800f 419 struct folio *folio, void *fsdata);
afddba49 420
1da177e4
LT
421 /* Unfortunately this kludge is needed for FIBMAP. Don't use it */
422 sector_t (*bmap)(struct address_space *, sector_t);
128d1f82 423 void (*invalidate_folio) (struct folio *, size_t offset, size_t len);
fa29000b 424 bool (*release_folio)(struct folio *, gfp_t);
d2329aa0 425 void (*free_folio)(struct folio *folio);
c8b8e32d 426 ssize_t (*direct_IO)(struct kiocb *, struct iov_iter *iter);
b969c4ab 427 /*
5490da4f 428 * migrate the contents of a folio to the specified target. If
ef277c73 429 * migrate_mode is MIGRATE_ASYNC, it must not block.
b969c4ab 430 */
5490da4f
MWO
431 int (*migrate_folio)(struct address_space *, struct folio *dst,
432 struct folio *src, enum migrate_mode);
072acba6 433 int (*launder_folio)(struct folio *);
2e7e80f7
MWO
434 bool (*is_partially_uptodate) (struct folio *, size_t from,
435 size_t count);
520f301c 436 void (*is_dirty_writeback) (struct folio *, bool *dirty, bool *wb);
af7628d6 437 int (*error_remove_folio)(struct address_space *, struct folio *);
62c230bc
MG
438
439 /* swapfile support */
a509bc1a
MG
440 int (*swap_activate)(struct swap_info_struct *sis, struct file *file,
441 sector_t *span);
442 void (*swap_deactivate)(struct file *file);
e1209d3a 443 int (*swap_rw)(struct kiocb *iocb, struct iov_iter *iter);
1da177e4
LT
444};
445
7dcda1c9
JA
446extern const struct address_space_operations empty_aops;
447
eb797a8e
MW
448/**
449 * struct address_space - Contents of a cacheable, mappable object.
450 * @host: Owner, either the inode or the block_device.
451 * @i_pages: Cached pages.
730633f0
JK
452 * @invalidate_lock: Guards coherency between page cache contents and
453 * file offset->disk block mappings in the filesystem during invalidates.
454 * It is also used to block modification of page cache contents through
455 * memory mappings.
eb797a8e 456 * @gfp_mask: Memory allocation flags to use for allocating pages.
e8e17ee9 457 * @i_mmap_writable: Number of VM_SHARED, VM_MAYWRITE mappings.
09d91cda 458 * @nr_thps: Number of THPs in the pagecache (non-shmem only).
eb797a8e
MW
459 * @i_mmap: Tree of private and shared mappings.
460 * @i_mmap_rwsem: Protects @i_mmap and @i_mmap_writable.
461 * @nrpages: Number of page entries, protected by the i_pages lock.
eb797a8e
MW
462 * @writeback_index: Writeback starts here.
463 * @a_ops: Methods.
464 * @flags: Error bits and flags (AS_*).
465 * @wb_err: The most recent error which has occurred.
600f111e
MWO
466 * @i_private_lock: For use by the owner of the address_space.
467 * @i_private_list: For use by the owner of the address_space.
468 * @i_private_data: For use by the owner of the address_space.
eb797a8e 469 */
1da177e4 470struct address_space {
eb797a8e
MW
471 struct inode *host;
472 struct xarray i_pages;
730633f0 473 struct rw_semaphore invalidate_lock;
eb797a8e
MW
474 gfp_t gfp_mask;
475 atomic_t i_mmap_writable;
09d91cda
SL
476#ifdef CONFIG_READ_ONLY_THP_FOR_FS
477 /* number of thp, only for non-shmem files */
478 atomic_t nr_thps;
479#endif
eb797a8e 480 struct rb_root_cached i_mmap;
eb797a8e 481 unsigned long nrpages;
eb797a8e
MW
482 pgoff_t writeback_index;
483 const struct address_space_operations *a_ops;
484 unsigned long flags;
5660e13d 485 errseq_t wb_err;
600f111e
MWO
486 spinlock_t i_private_lock;
487 struct list_head i_private_list;
d3b1a9a7 488 struct rw_semaphore i_mmap_rwsem;
600f111e 489 void * i_private_data;
3859a271 490} __attribute__((aligned(sizeof(long)))) __randomize_layout;
1da177e4
LT
491 /*
492 * On most architectures that alignment is already the case; but
25985edc 493 * must be enforced here for CRIS, to let the least significant bit
df25569d 494 * of struct folio's "mapping" pointer be used for FOLIO_MAPPING_ANON.
1da177e4 495 */
1da177e4 496
ff9c745b
MW
497/* XArray tags, for tagging dirty and writeback pages in the pagecache. */
498#define PAGECACHE_TAG_DIRTY XA_MARK_0
499#define PAGECACHE_TAG_WRITEBACK XA_MARK_1
500#define PAGECACHE_TAG_TOWRITE XA_MARK_2
501
1da177e4 502/*
ff9c745b 503 * Returns true if any of the pages in the mapping are marked with the tag.
1da177e4 504 */
b119fb09 505static inline bool mapping_tagged(const struct address_space *mapping, xa_mark_t tag)
ff9c745b
MW
506{
507 return xa_marked(&mapping->i_pages, tag);
508}
1da177e4 509
8b28f621
DB
510static inline void i_mmap_lock_write(struct address_space *mapping)
511{
c8c06efa 512 down_write(&mapping->i_mmap_rwsem);
8b28f621
DB
513}
514
c0d0381a
MK
515static inline int i_mmap_trylock_write(struct address_space *mapping)
516{
517 return down_write_trylock(&mapping->i_mmap_rwsem);
518}
519
8b28f621
DB
520static inline void i_mmap_unlock_write(struct address_space *mapping)
521{
c8c06efa 522 up_write(&mapping->i_mmap_rwsem);
8b28f621
DB
523}
524
6d4675e6
MK
525static inline int i_mmap_trylock_read(struct address_space *mapping)
526{
527 return down_read_trylock(&mapping->i_mmap_rwsem);
528}
529
3dec0ba0
DB
530static inline void i_mmap_lock_read(struct address_space *mapping)
531{
532 down_read(&mapping->i_mmap_rwsem);
533}
534
535static inline void i_mmap_unlock_read(struct address_space *mapping)
536{
537 up_read(&mapping->i_mmap_rwsem);
538}
539
34ae204f
MK
540static inline void i_mmap_assert_locked(struct address_space *mapping)
541{
542 lockdep_assert_held(&mapping->i_mmap_rwsem);
543}
544
545static inline void i_mmap_assert_write_locked(struct address_space *mapping)
546{
547 lockdep_assert_held_write(&mapping->i_mmap_rwsem);
548}
549
1da177e4
LT
550/*
551 * Might pages of this file be mapped into userspace?
552 */
b119fb09 553static inline int mapping_mapped(const struct address_space *mapping)
1da177e4 554{
f808c13f 555 return !RB_EMPTY_ROOT(&mapping->i_mmap.rb_root);
1da177e4
LT
556}
557
558/*
559 * Might pages of this file have been modified in userspace?
e8e17ee9 560 * Note that i_mmap_writable counts all VM_SHARED, VM_MAYWRITE vmas: do_mmap
1da177e4
LT
561 * marks vma as VM_SHARED if it is shared, and the file was opened for
562 * writing i.e. vma may be mprotected writable even if now readonly.
4bb5f5d9
DR
563 *
564 * If i_mmap_writable is negative, no new writable mappings are allowed. You
565 * can only deny writable mappings, if none exists right now.
1da177e4 566 */
b119fb09 567static inline int mapping_writably_mapped(const struct address_space *mapping)
1da177e4 568{
4bb5f5d9
DR
569 return atomic_read(&mapping->i_mmap_writable) > 0;
570}
571
572static inline int mapping_map_writable(struct address_space *mapping)
573{
574 return atomic_inc_unless_negative(&mapping->i_mmap_writable) ?
575 0 : -EPERM;
576}
577
578static inline void mapping_unmap_writable(struct address_space *mapping)
579{
580 atomic_dec(&mapping->i_mmap_writable);
581}
582
583static inline int mapping_deny_writable(struct address_space *mapping)
584{
585 return atomic_dec_unless_positive(&mapping->i_mmap_writable) ?
586 0 : -EBUSY;
587}
588
589static inline void mapping_allow_writable(struct address_space *mapping)
590{
591 atomic_inc(&mapping->i_mmap_writable);
1da177e4
LT
592}
593
594/*
595 * Use sequence counter to get consistent i_size on 32-bit processors.
596 */
597#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
598#include <linux/seqlock.h>
599#define __NEED_I_SIZE_ORDERED
600#define i_size_ordered_init(inode) seqcount_init(&inode->i_size_seqcount)
601#else
602#define i_size_ordered_init(inode) do { } while (0)
603#endif
604
f19d4a8f
AV
605struct posix_acl;
606#define ACL_NOT_CACHED ((void *)(-1))
332f606b
MS
607/*
608 * ACL_DONT_CACHE is for stacked filesystems, that rely on underlying fs to
cac2f8b8
CB
609 * cache the ACL. This also means that ->get_inode_acl() can be called in RCU
610 * mode with the LOOKUP_RCU flag.
332f606b 611 */
2a3a2a3f 612#define ACL_DONT_CACHE ((void *)(-3))
f19d4a8f 613
b8a7a3a6
AG
614static inline struct posix_acl *
615uncached_acl_sentinel(struct task_struct *task)
616{
617 return (void *)task + 1;
618}
619
620static inline bool
621is_uncached_acl(struct posix_acl *acl)
622{
623 return (long)acl & 1;
624}
625
e631df89
MG
626#define IOP_FASTPERM 0x0001
627#define IOP_LOOKUP 0x0002
628#define IOP_NOFOLLOW 0x0004
629#define IOP_XATTR 0x0008
76fca90e 630#define IOP_DEFAULT_READLINK 0x0010
e631df89
MG
631#define IOP_MGTIME 0x0020
632#define IOP_CACHED_LINK 0x0040
633#define IOP_FASTPERM_MAY_EXEC 0x0080
3ddcd056 634
9a98f9e8
JB
635/*
636 * Inode state bits. Protected by inode->i_lock
637 *
638 * Four bits determine the dirty state of the inode: I_DIRTY_SYNC,
639 * I_DIRTY_DATASYNC, I_DIRTY_PAGES, and I_DIRTY_TIME.
640 *
641 * Four bits define the lifetime of an inode. Initially, inodes are I_NEW,
642 * until that flag is cleared. I_WILL_FREE, I_FREEING and I_CLEAR are set at
643 * various stages of removing an inode.
644 *
645 * Two bits are used for locking and completion notification, I_NEW and I_SYNC.
646 *
647 * I_DIRTY_SYNC Inode is dirty, but doesn't have to be written on
648 * fdatasync() (unless I_DIRTY_DATASYNC is also set).
649 * Timestamp updates are the usual cause.
650 * I_DIRTY_DATASYNC Data-related inode changes pending. We keep track of
651 * these changes separately from I_DIRTY_SYNC so that we
652 * don't have to write inode on fdatasync() when only
653 * e.g. the timestamps have changed.
654 * I_DIRTY_PAGES Inode has dirty pages. Inode itself may be clean.
655 * I_DIRTY_TIME The inode itself has dirty timestamps, and the
656 * lazytime mount option is enabled. We keep track of this
657 * separately from I_DIRTY_SYNC in order to implement
658 * lazytime. This gets cleared if I_DIRTY_INODE
659 * (I_DIRTY_SYNC and/or I_DIRTY_DATASYNC) gets set. But
660 * I_DIRTY_TIME can still be set if I_DIRTY_SYNC is already
661 * in place because writeback might already be in progress
662 * and we don't want to lose the time update
663 * I_NEW Serves as both a mutex and completion notification.
664 * New inodes set I_NEW. If two processes both create
665 * the same inode, one of them will release its inode and
666 * wait for I_NEW to be released before returning.
667 * Inodes in I_WILL_FREE, I_FREEING or I_CLEAR state can
668 * also cause waiting on I_NEW, without I_NEW actually
669 * being set. find_inode() uses this to prevent returning
670 * nearly-dead inodes.
671 * I_WILL_FREE Must be set when calling write_inode_now() if i_count
672 * is zero. I_FREEING must be set when I_WILL_FREE is
673 * cleared.
674 * I_FREEING Set when inode is about to be freed but still has dirty
675 * pages or buffers attached or the inode itself is still
676 * dirty.
677 * I_CLEAR Added by clear_inode(). In this state the inode is
678 * clean and can be destroyed. Inode keeps I_FREEING.
679 *
680 * Inodes that are I_WILL_FREE, I_FREEING or I_CLEAR are
681 * prohibited for many purposes. iget() must wait for
682 * the inode to be completely released, then create it
683 * anew. Other functions will just ignore such inodes,
684 * if appropriate. I_NEW is used for waiting.
685 *
686 * I_SYNC Writeback of inode is running. The bit is set during
687 * data writeback, and cleared with a wakeup on the bit
688 * address once it is done. The bit is also used to pin
689 * the inode in memory for flusher thread.
690 *
691 * I_REFERENCED Marks the inode as recently references on the LRU list.
692 *
693 * I_WB_SWITCH Cgroup bdi_writeback switching in progress. Used to
694 * synchronize competing switching instances and to tell
695 * wb stat updates to grab the i_pages lock. See
696 * inode_switch_wbs_work_fn() for details.
697 *
698 * I_OVL_INUSE Used by overlayfs to get exclusive ownership on upper
699 * and work dirs among overlayfs mounts.
700 *
701 * I_CREATING New object's inode in the middle of setting up.
702 *
703 * I_DONTCACHE Evict inode as soon as it is not used anymore.
704 *
705 * I_SYNC_QUEUED Inode is queued in b_io or b_more_io writeback lists.
706 * Used to detect that mark_inode_dirty() should not move
707 * inode between dirty lists.
708 *
709 * I_PINNING_FSCACHE_WB Inode is pinning an fscache object for writeback.
710 *
711 * I_LRU_ISOLATING Inode is pinned being isolated from LRU without holding
712 * i_count.
713 *
714 * Q: What is the difference between I_WILL_FREE and I_FREEING?
715 *
716 * __I_{SYNC,NEW,LRU_ISOLATING} are used to derive unique addresses to wait
717 * upon. There's one free address left.
718 */
719
720enum inode_state_bits {
721 __I_NEW = 0U,
722 __I_SYNC = 1U,
723 __I_LRU_ISOLATING = 2U
724 /* reserved wait address bit 3 */
725};
726
d8753f78 727enum inode_state_flags_enum {
9a98f9e8
JB
728 I_NEW = (1U << __I_NEW),
729 I_SYNC = (1U << __I_SYNC),
730 I_LRU_ISOLATING = (1U << __I_LRU_ISOLATING),
731 /* reserved flag bit 3 */
732 I_DIRTY_SYNC = (1U << 4),
733 I_DIRTY_DATASYNC = (1U << 5),
734 I_DIRTY_PAGES = (1U << 6),
735 I_WILL_FREE = (1U << 7),
736 I_FREEING = (1U << 8),
737 I_CLEAR = (1U << 9),
738 I_REFERENCED = (1U << 10),
739 I_LINKABLE = (1U << 11),
740 I_DIRTY_TIME = (1U << 12),
741 I_WB_SWITCH = (1U << 13),
742 I_OVL_INUSE = (1U << 14),
743 I_CREATING = (1U << 15),
744 I_DONTCACHE = (1U << 16),
745 I_SYNC_QUEUED = (1U << 17),
746 I_PINNING_NETFS_WB = (1U << 18)
747};
748
749#define I_DIRTY_INODE (I_DIRTY_SYNC | I_DIRTY_DATASYNC)
750#define I_DIRTY (I_DIRTY_INODE | I_DIRTY_PAGES)
751#define I_DIRTY_ALL (I_DIRTY | I_DIRTY_TIME)
752
2ed81b4b
MG
753/*
754 * Use inode_state_read() & friends to access.
755 */
756struct inode_state_flags {
757 enum inode_state_flags_enum __state;
758};
759
3ddcd056
LT
760/*
761 * Keep mostly read-only and often accessed (especially for
762 * the RCU path lookup and 'stat' data) fields at the beginning
763 * of the 'struct inode'
764 */
1da177e4 765struct inode {
44a7d7a8 766 umode_t i_mode;
3ddcd056 767 unsigned short i_opflags;
3ddcd056 768 unsigned int i_flags;
3ddcd056
LT
769#ifdef CONFIG_FS_POSIX_ACL
770 struct posix_acl *i_acl;
771 struct posix_acl *i_default_acl;
772#endif
dca3aa66
MG
773 kuid_t i_uid;
774 kgid_t i_gid;
3ddcd056 775
44a7d7a8
NP
776 const struct inode_operations *i_op;
777 struct super_block *i_sb;
3ddcd056 778 struct address_space *i_mapping;
44a7d7a8 779
13e12d14
LT
780#ifdef CONFIG_SECURITY
781 void *i_security;
782#endif
44a7d7a8 783
3ddcd056
LT
784 /* Stat data, not accessed from path walking */
785 unsigned long i_ino;
a78ef704
MS
786 /*
787 * Filesystems may only read i_nlink directly. They shall use the
788 * following functions for modification:
789 *
790 * (set|clear|inc|drop)_nlink
791 * inode_(inc|dec)_link_count
792 */
793 union {
794 const unsigned int i_nlink;
795 unsigned int __i_nlink;
796 };
3ddcd056 797 dev_t i_rdev;
2f9d3df8 798 loff_t i_size;
3aa63a56
JL
799 time64_t i_atime_sec;
800 time64_t i_mtime_sec;
801 time64_t i_ctime_sec;
802 u32 i_atime_nsec;
803 u32 i_mtime_nsec;
804 u32 i_ctime_nsec;
805 u32 i_generation;
6cdbb0ef
TT
806 spinlock_t i_lock; /* i_blocks, i_bytes, maybe i_size */
807 unsigned short i_bytes;
69448867 808 u8 i_blkbits;
fe3944fb 809 enum rw_hint i_write_hint;
3ddcd056
LT
810 blkcnt_t i_blocks;
811
812#ifdef __NEED_I_SIZE_ORDERED
813 seqcount_t i_size_seqcount;
814#endif
815
816 /* Misc */
2ed81b4b 817 struct inode_state_flags i_state;
2b111edb 818 /* 32-bit hole */
9902af79 819 struct rw_semaphore i_rwsem;
13e12d14 820
44a7d7a8 821 unsigned long dirtied_when; /* jiffies of first dirtying */
a2f48706 822 unsigned long dirtied_time_when;
44a7d7a8 823
1da177e4 824 struct hlist_node i_hash;
c7f54084 825 struct list_head i_io_list; /* backing dev IO list */
52ebea74
TH
826#ifdef CONFIG_CGROUP_WRITEBACK
827 struct bdi_writeback *i_wb; /* the associated cgroup wb */
2a814908
TH
828
829 /* foreign inode detection, see wbc_detach_inode() */
830 int i_wb_frn_winner;
831 u16 i_wb_frn_avg_time;
832 u16 i_wb_frn_history;
52ebea74 833#endif
7ccf19a8 834 struct list_head i_lru; /* inode LRU list */
1da177e4 835 struct list_head i_sb_list;
6c60d2b5 836 struct list_head i_wb_list; /* backing dev writeback list */
fa0d7e3d 837 union {
b3d9b7a3 838 struct hlist_head i_dentry;
fa0d7e3d
NP
839 struct rcu_head i_rcu;
840 };
f02a9ad1 841 atomic64_t i_version;
8019ad13 842 atomic64_t i_sequence; /* see futex */
2f9d3df8 843 atomic_t i_count;
bd5fe6c5 844 atomic_t i_dio_count;
6cdbb0ef 845 atomic_t i_writecount;
387e3746 846#if defined(CONFIG_IMA) || defined(CONFIG_FILE_LOCKING)
d984ea60
MZ
847 atomic_t i_readcount; /* struct files open RO */
848#endif
fdb0da89
AV
849 union {
850 const struct file_operations *i_fop; /* former ->i_op->default_file_ops */
851 void (*free_inode)(struct inode *);
852 };
4a075e39 853 struct file_lock_context *i_flctx;
1da177e4 854 struct address_space i_data;
ea382199
MG
855 union {
856 struct list_head i_devices;
857 int i_linklen;
858 };
4c154168
TT
859 union {
860 struct pipe_inode_info *i_pipe;
577c4eb0 861 struct cdev *i_cdev;
61ba64fc 862 char *i_link;
84e710da 863 unsigned i_dir_seq;
4c154168 864 };
1da177e4 865
1da177e4 866
3be25f49
EP
867#ifdef CONFIG_FSNOTIFY
868 __u32 i_fsnotify_mask; /* all events this inode cares about */
3aa63a56 869 /* 32-bit hole reserved for expanding i_fsnotify_mask */
08991e83 870 struct fsnotify_mark_connector __rcu *i_fsnotify_marks;
0eeca283
RL
871#endif
872
8e18e294 873 void *i_private; /* fs or device private pointer */
3859a271 874} __randomize_layout;
1da177e4 875
d8753f78
MG
876/*
877 * i_state handling
878 *
879 * We hide all of it behind helpers so that we can validate consumers.
880 */
881static inline enum inode_state_flags_enum inode_state_read_once(struct inode *inode)
882{
2ed81b4b 883 return READ_ONCE(inode->i_state.__state);
d8753f78
MG
884}
885
886static inline enum inode_state_flags_enum inode_state_read(struct inode *inode)
887{
888 lockdep_assert_held(&inode->i_lock);
2ed81b4b 889 return inode->i_state.__state;
d8753f78
MG
890}
891
892static inline void inode_state_set_raw(struct inode *inode,
893 enum inode_state_flags_enum flags)
894{
2ed81b4b 895 WRITE_ONCE(inode->i_state.__state, inode->i_state.__state | flags);
d8753f78
MG
896}
897
898static inline void inode_state_set(struct inode *inode,
899 enum inode_state_flags_enum flags)
900{
901 lockdep_assert_held(&inode->i_lock);
902 inode_state_set_raw(inode, flags);
903}
904
905static inline void inode_state_clear_raw(struct inode *inode,
906 enum inode_state_flags_enum flags)
907{
2ed81b4b 908 WRITE_ONCE(inode->i_state.__state, inode->i_state.__state & ~flags);
d8753f78
MG
909}
910
911static inline void inode_state_clear(struct inode *inode,
912 enum inode_state_flags_enum flags)
913{
914 lockdep_assert_held(&inode->i_lock);
915 inode_state_clear_raw(inode, flags);
916}
917
918static inline void inode_state_assign_raw(struct inode *inode,
919 enum inode_state_flags_enum flags)
920{
2ed81b4b 921 WRITE_ONCE(inode->i_state.__state, flags);
d8753f78
MG
922}
923
924static inline void inode_state_assign(struct inode *inode,
925 enum inode_state_flags_enum flags)
926{
927 lockdep_assert_held(&inode->i_lock);
928 inode_state_assign_raw(inode, flags);
929}
930
931static inline void inode_state_replace_raw(struct inode *inode,
932 enum inode_state_flags_enum clearflags,
933 enum inode_state_flags_enum setflags)
934{
935 enum inode_state_flags_enum flags;
2ed81b4b 936 flags = inode->i_state.__state;
d8753f78
MG
937 flags &= ~clearflags;
938 flags |= setflags;
939 inode_state_assign_raw(inode, flags);
940}
941
942static inline void inode_state_replace(struct inode *inode,
943 enum inode_state_flags_enum clearflags,
944 enum inode_state_flags_enum setflags)
945{
946 lockdep_assert_held(&inode->i_lock);
947 inode_state_replace_raw(inode, clearflags, setflags);
948}
949
ea382199
MG
950static inline void inode_set_cached_link(struct inode *inode, char *link, int linklen)
951{
3eb7e951
MG
952 VFS_WARN_ON_INODE(strlen(link) != linklen, inode);
953 VFS_WARN_ON_INODE(inode->i_opflags & IOP_CACHED_LINK, inode);
ea382199
MG
954 inode->i_link = link;
955 inode->i_linklen = linklen;
956 inode->i_opflags |= IOP_CACHED_LINK;
957}
958
da18ecbf
CB
959/*
960 * Get bit address from inode->i_state to use with wait_var_event()
961 * infrastructre.
962 */
963#define inode_state_wait_address(inode, bit) ((char *)&(inode)->i_state + (bit))
964
965struct wait_queue_head *inode_bit_waitqueue(struct wait_bit_queue_entry *wqe,
966 struct inode *inode, u32 bit);
967
968static inline void inode_wake_up_bit(struct inode *inode, u32 bit)
969{
970 /* Caller is responsible for correct memory barriers. */
971 wake_up_var(inode_state_wait_address(inode, bit));
972}
973
50e17c00
DD
974struct timespec64 timestamp_truncate(struct timespec64 t, struct inode *inode);
975
93407472
FF
976static inline unsigned int i_blocksize(const struct inode *node)
977{
978 return (1 << node->i_blkbits);
979}
980
1d3382cb
AV
981static inline int inode_unhashed(struct inode *inode)
982{
983 return hlist_unhashed(&inode->i_hash);
984}
985
5bef9151
AV
986/*
987 * __mark_inode_dirty expects inodes to be hashed. Since we don't
988 * want special inodes in the fileset inode space, we make them
989 * appear hashed, but do not put on any lists. hlist_del()
990 * will work fine and require no locking.
991 */
992static inline void inode_fake_hash(struct inode *inode)
993{
994 hlist_add_fake(&inode->i_hash);
995}
996
a27628f4 997void wait_on_new_inode(struct inode *inode);
af6023e2 998
f2eace23 999/*
2773d282 1000 * inode->i_rwsem nesting subclasses for the lock validator:
f2eace23
IM
1001 *
1002 * 0: the object of the current VFS operation
1003 * 1: parent
1004 * 2: child/target
40bd22c9
BF
1005 * 3: xattr
1006 * 4: second non-directory
d1b72cc6
MS
1007 * 5: second parent (when locking independent directories in rename)
1008 *
1009 * I_MUTEX_NONDIR2 is for certain operations (such as rename) which lock two
40bd22c9 1010 * non-directories at once.
f2eace23
IM
1011 *
1012 * The locking order between these classes is
d1b72cc6 1013 * parent[2] -> child -> grandchild -> normal -> xattr -> second non-directory
f2eace23
IM
1014 */
1015enum inode_i_mutex_lock_class
1016{
1017 I_MUTEX_NORMAL,
1018 I_MUTEX_PARENT,
1019 I_MUTEX_CHILD,
4df46240 1020 I_MUTEX_XATTR,
d1b72cc6
MS
1021 I_MUTEX_NONDIR2,
1022 I_MUTEX_PARENT2,
f2eace23
IM
1023};
1024
5955102c
AV
1025static inline void inode_lock(struct inode *inode)
1026{
9902af79 1027 down_write(&inode->i_rwsem);
5955102c
AV
1028}
1029
d8c5507c
MK
1030static inline __must_check int inode_lock_killable(struct inode *inode)
1031{
1032 return down_write_killable(&inode->i_rwsem);
1033}
1034
5955102c
AV
1035static inline void inode_unlock(struct inode *inode)
1036{
9902af79
AV
1037 up_write(&inode->i_rwsem);
1038}
1039
1040static inline void inode_lock_shared(struct inode *inode)
1041{
1042 down_read(&inode->i_rwsem);
1043}
1044
d8c5507c
MK
1045static inline __must_check int inode_lock_shared_killable(struct inode *inode)
1046{
1047 return down_read_killable(&inode->i_rwsem);
1048}
1049
9902af79
AV
1050static inline void inode_unlock_shared(struct inode *inode)
1051{
1052 up_read(&inode->i_rwsem);
5955102c
AV
1053}
1054
1055static inline int inode_trylock(struct inode *inode)
1056{
9902af79
AV
1057 return down_write_trylock(&inode->i_rwsem);
1058}
1059
1060static inline int inode_trylock_shared(struct inode *inode)
1061{
1062 return down_read_trylock(&inode->i_rwsem);
5955102c
AV
1063}
1064
1065static inline int inode_is_locked(struct inode *inode)
1066{
9902af79 1067 return rwsem_is_locked(&inode->i_rwsem);
5955102c
AV
1068}
1069
1070static inline void inode_lock_nested(struct inode *inode, unsigned subclass)
1071{
9902af79 1072 down_write_nested(&inode->i_rwsem, subclass);
5955102c
AV
1073}
1074
01c2e13d
DW
1075static inline void inode_lock_shared_nested(struct inode *inode, unsigned subclass)
1076{
1077 down_read_nested(&inode->i_rwsem, subclass);
1078}
1079
730633f0
JK
1080static inline void filemap_invalidate_lock(struct address_space *mapping)
1081{
1082 down_write(&mapping->invalidate_lock);
1083}
1084
1085static inline void filemap_invalidate_unlock(struct address_space *mapping)
1086{
1087 up_write(&mapping->invalidate_lock);
1088}
1089
1090static inline void filemap_invalidate_lock_shared(struct address_space *mapping)
1091{
1092 down_read(&mapping->invalidate_lock);
1093}
1094
1095static inline int filemap_invalidate_trylock_shared(
1096 struct address_space *mapping)
1097{
1098 return down_read_trylock(&mapping->invalidate_lock);
1099}
1100
1101static inline void filemap_invalidate_unlock_shared(
1102 struct address_space *mapping)
1103{
1104 up_read(&mapping->invalidate_lock);
1105}
1106
375e289e
BF
1107void lock_two_nondirectories(struct inode *, struct inode*);
1108void unlock_two_nondirectories(struct inode *, struct inode*);
1109
7506ae6a
JK
1110void filemap_invalidate_lock_two(struct address_space *mapping1,
1111 struct address_space *mapping2);
1112void filemap_invalidate_unlock_two(struct address_space *mapping1,
1113 struct address_space *mapping2);
1114
1115
1da177e4
LT
1116/*
1117 * NOTE: in a 32bit arch with a preemptable kernel and
1118 * an UP compile the i_size_read/write must be atomic
1119 * with respect to the local cpu (unlike with preempt disabled),
1120 * but they don't need to be atomic with respect to other cpus like in
1121 * true SMP (so they need either to either locally disable irq around
1122 * the read or for example on x86 they can be still implemented as a
1123 * cmpxchg8b without the need of the lock prefix). For SMP compiles
1124 * and 64bit archs it makes no difference if preempt is enabled or not.
1125 */
48ed214d 1126static inline loff_t i_size_read(const struct inode *inode)
1da177e4
LT
1127{
1128#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
1129 loff_t i_size;
1130 unsigned int seq;
1131
1132 do {
1133 seq = read_seqcount_begin(&inode->i_size_seqcount);
1134 i_size = inode->i_size;
1135 } while (read_seqcount_retry(&inode->i_size_seqcount, seq));
1136 return i_size;
2496396f 1137#elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
1da177e4
LT
1138 loff_t i_size;
1139
1140 preempt_disable();
1141 i_size = inode->i_size;
1142 preempt_enable();
1143 return i_size;
1144#else
d8f899d1
BL
1145 /* Pairs with smp_store_release() in i_size_write() */
1146 return smp_load_acquire(&inode->i_size);
1da177e4
LT
1147#endif
1148}
1149
7762f5a0
MS
1150/*
1151 * NOTE: unlike i_size_read(), i_size_write() does need locking around it
2773d282 1152 * (normally i_rwsem), otherwise on 32bit/SMP an update of i_size_seqcount
7762f5a0
MS
1153 * can be lost, resulting in subsequent i_size_read() calls spinning forever.
1154 */
1da177e4
LT
1155static inline void i_size_write(struct inode *inode, loff_t i_size)
1156{
1157#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
74e3d1e1 1158 preempt_disable();
1da177e4
LT
1159 write_seqcount_begin(&inode->i_size_seqcount);
1160 inode->i_size = i_size;
1161 write_seqcount_end(&inode->i_size_seqcount);
74e3d1e1 1162 preempt_enable();
2496396f 1163#elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
1da177e4
LT
1164 preempt_disable();
1165 inode->i_size = i_size;
1166 preempt_enable();
1167#else
d8f899d1
BL
1168 /*
1169 * Pairs with smp_load_acquire() in i_size_read() to ensure
1170 * changes related to inode size (such as page contents) are
1171 * visible before we see the changed inode size.
1172 */
1173 smp_store_release(&inode->i_size, i_size);
1da177e4
LT
1174#endif
1175}
1176
48ed214d 1177static inline unsigned iminor(const struct inode *inode)
1da177e4
LT
1178{
1179 return MINOR(inode->i_rdev);
1180}
1181
48ed214d 1182static inline unsigned imajor(const struct inode *inode)
1da177e4
LT
1183{
1184 return MAJOR(inode->i_rdev);
1185}
1186
1da177e4 1187struct fown_struct {
1934b212 1188 struct file *file; /* backpointer for security modules */
1da177e4 1189 rwlock_t lock; /* protects pid, uid, euid fields */
609d7fa9
EB
1190 struct pid *pid; /* pid or -pgrp where SIGIO should be sent */
1191 enum pid_type pid_type; /* Kind of process group SIGIO should be sent to */
92361636 1192 kuid_t uid, euid; /* uid/euid of process setting the owner */
1da177e4
LT
1193 int signum; /* posix.1b rt signal to be delivered on IO */
1194};
1195
c790fbf2
MWO
1196/**
1197 * struct file_ra_state - Track a file's readahead state.
1198 * @start: Where the most recent readahead started.
1199 * @size: Number of pages read in the most recent readahead.
84dacdbd
N
1200 * @async_size: Numer of pages that were/are not needed immediately
1201 * and so were/are genuinely "ahead". Start next readahead when
1202 * the first of these pages is accessed.
1203 * @ra_pages: Maximum size of a readahead request, copied from the bdi.
c4602f9f 1204 * @order: Preferred folio order used for most recent readahead.
c790fbf2
MWO
1205 * @mmap_miss: How many mmap accesses missed in the page cache.
1206 * @prev_pos: The last byte in the most recent read request.
84dacdbd
N
1207 *
1208 * When this structure is passed to ->readahead(), the "most recent"
1209 * readahead means the current readahead.
1da177e4
LT
1210 */
1211struct file_ra_state {
c790fbf2
MWO
1212 pgoff_t start;
1213 unsigned int size;
1214 unsigned int async_size;
1215 unsigned int ra_pages;
c4602f9f 1216 unsigned short order;
f5e8b140 1217 unsigned short mmap_miss;
c790fbf2 1218 loff_t prev_pos;
1da177e4 1219};
1da177e4 1220
5ce1110b
FW
1221/*
1222 * Check if @index falls in the readahead windows.
1223 */
1224static inline int ra_has_index(struct file_ra_state *ra, pgoff_t index)
1225{
f9acc8c7
FW
1226 return (index >= ra->start &&
1227 index < ra->start + ra->size);
5ce1110b
FW
1228}
1229
c0390d54
CB
1230/**
1231 * struct file - Represents a file
c0390d54
CB
1232 * @f_lock: Protects f_ep, f_flags. Must not be taken from IRQ context.
1233 * @f_mode: FMODE_* flags often used in hotpaths
1234 * @f_op: file operations
1235 * @f_mapping: Contents of a cacheable, mappable object.
1236 * @private_data: filesystem or driver specific data
1237 * @f_inode: cached inode
1238 * @f_flags: file flags
1239 * @f_iocb_flags: iocb flags
1240 * @f_cred: stashed credentials of creator/opener
e249056c 1241 * @f_owner: file owner
c0390d54 1242 * @f_path: path of the file
2f7d98f1
AV
1243 * @__f_path: writable alias for @f_path; *ONLY* for core VFS and only before
1244 * the file gets open
c0390d54 1245 * @f_pos_lock: lock protecting file position
5e9b50de 1246 * @f_pipe: specific to pipes
c0390d54 1247 * @f_pos: file position
c0390d54 1248 * @f_security: LSM security context of this file
c0390d54
CB
1249 * @f_wb_err: writeback error
1250 * @f_sb_err: per sb writeback errors
1251 * @f_ep: link of all epoll hooks for this file
1252 * @f_task_work: task work entry point
1253 * @f_llist: work queue entrypoint
1254 * @f_ra: file's readahead state
ea566e18 1255 * @f_freeptr: Pointer used by SLAB_TYPESAFE_BY_RCU file cache (don't touch.)
e249056c 1256 * @f_ref: reference count
a7bc2e8d 1257 */
1da177e4 1258struct file {
c0390d54
CB
1259 spinlock_t f_lock;
1260 fmode_t f_mode;
a7bc2e8d 1261 const struct file_operations *f_op;
c0390d54
CB
1262 struct address_space *f_mapping;
1263 void *private_data;
1264 struct inode *f_inode;
1265 unsigned int f_flags;
1266 unsigned int f_iocb_flags;
1267 const struct cred *f_cred;
e249056c 1268 struct fown_struct *f_owner;
c0390d54 1269 /* --- cacheline 1 boundary (64 bytes) --- */
2f7d98f1
AV
1270 union {
1271 const struct path f_path;
1272 struct path __f_path;
1273 };
2f512016 1274 union {
5e9b50de
CB
1275 /* regular files (with FMODE_ATOMIC_POS) and directories */
1276 struct mutex f_pos_lock;
1277 /* pipes */
1278 u64 f_pipe;
e87f2c26 1279 };
c0390d54 1280 loff_t f_pos;
50462062 1281#ifdef CONFIG_SECURITY
c0390d54 1282 void *f_security;
50462062 1283#endif
11068e0b 1284 /* --- cacheline 2 boundary (128 bytes) --- */
c0390d54
CB
1285 errseq_t f_wb_err;
1286 errseq_t f_sb_err;
1da177e4 1287#ifdef CONFIG_EPOLL
c0390d54
CB
1288 struct hlist_head *f_ep;
1289#endif
1290 union {
1291 struct callback_head f_task_work;
1292 struct llist_node f_llist;
1293 struct file_ra_state f_ra;
ea566e18 1294 freeptr_t f_freeptr;
c0390d54 1295 };
e249056c 1296 file_ref_t f_ref;
c0390d54 1297 /* --- cacheline 3 boundary (192 bytes) --- */
3859a271
KC
1298} __randomize_layout
1299 __attribute__((aligned(4))); /* lest something weird decides that 2 is OK */
1da177e4 1300
990d6c2d
AK
1301struct file_handle {
1302 __u32 handle_bytes;
1303 int handle_type;
1304 /* file identifier */
68d6f4f3 1305 unsigned char f_handle[] __counted_by(handle_bytes);
990d6c2d
AK
1306};
1307
cb0942b8
AV
1308static inline struct file *get_file(struct file *f)
1309{
90ee6ed7 1310 file_ref_inc(&f->f_ref);
cb0942b8
AV
1311 return f;
1312}
0ede61d8
CB
1313
1314struct file *get_file_rcu(struct file __rcu **f);
61d4fb0b 1315struct file *get_file_active(struct file **f);
0ede61d8 1316
90ee6ed7 1317#define file_count(f) file_ref_read(&(f)->f_ref)
1da177e4
LT
1318
1319#define MAX_NON_LFS ((1UL<<31) - 1)
1320
1321/* Page cache limit. The filesystems should put that into their s_maxbytes
1322 limits, otherwise bad things can happen in VM. */
1323#if BITS_PER_LONG==32
0cc3b0ec 1324#define MAX_LFS_FILESIZE ((loff_t)ULONG_MAX << PAGE_SHIFT)
1da177e4 1325#elif BITS_PER_LONG==64
0cc3b0ec 1326#define MAX_LFS_FILESIZE ((loff_t)LLONG_MAX)
1da177e4
LT
1327#endif
1328
7ca76311 1329/* legacy typedef, should eventually be removed */
17fa388d 1330typedef void *fl_owner_t;
1da177e4 1331
a7231a97 1332struct file_lock;
c69ff407 1333struct file_lease;
a7231a97 1334
1da177e4
LT
1335/* The following constant reflects the upper bound of the file/locking space */
1336#ifndef OFFSET_MAX
ea258f15
ZL
1337#define OFFSET_MAX type_max(loff_t)
1338#define OFFT_OFFSET_MAX type_max(off_t)
1da177e4
LT
1339#endif
1340
1934b212
CB
1341int file_f_owner_allocate(struct file *file);
1342static inline struct fown_struct *file_f_owner(const struct file *file)
1343{
1344 return READ_ONCE(file->f_owner);
1345}
1346
bfcd17a6
TP
1347extern void send_sigio(struct fown_struct *fown, int fd, int band);
1348
ee296d7c
JL
1349static inline struct inode *file_inode(const struct file *f)
1350{
1351 return f->f_inode;
1352}
1353
3058fca1
AG
1354/*
1355 * file_dentry() is a relic from the days that overlayfs was using files with a
1356 * "fake" path, meaning, f_path on overlayfs and f_inode on underlying fs.
1357 * In those days, file_dentry() was needed to get the underlying fs dentry that
1358 * matches f_inode.
1359 * Files with "fake" path should not exist nowadays, so use an assertion to make
1360 * sure that file_dentry() was not papering over filesystem bugs.
1361 */
d101a125
MS
1362static inline struct dentry *file_dentry(const struct file *file)
1363{
3058fca1
AG
1364 struct dentry *dentry = file->f_path.dentry;
1365
1366 WARN_ON_ONCE(d_inode(dentry) != file_inode(file));
1367 return dentry;
d101a125
MS
1368}
1369
1da177e4 1370struct fasync_struct {
7a107c0f 1371 rwlock_t fa_lock;
989a2979
ED
1372 int magic;
1373 int fa_fd;
1374 struct fasync_struct *fa_next; /* singly linked list */
1375 struct file *fa_file;
1376 struct rcu_head fa_rcu;
1da177e4
LT
1377};
1378
1379#define FASYNC_MAGIC 0x4601
1380
1381/* SMP safe fasync helpers: */
1382extern int fasync_helper(int, struct file *, int, struct fasync_struct **);
f7347ce4
LT
1383extern struct fasync_struct *fasync_insert_entry(int, struct file *, struct fasync_struct **, struct fasync_struct *);
1384extern int fasync_remove_entry(struct file *, struct fasync_struct **);
1385extern struct fasync_struct *fasync_alloc(void);
1386extern void fasync_free(struct fasync_struct *);
1387
1da177e4
LT
1388/* can be called from interrupts */
1389extern void kill_fasync(struct fasync_struct **, int, int);
1da177e4 1390
e0b93edd 1391extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
bccb5c39 1392extern int f_setown(struct file *filp, int who, int force);
1da177e4 1393extern void f_delown(struct file *filp);
609d7fa9 1394extern pid_t f_getown(struct file *filp);
1934b212 1395extern int send_sigurg(struct file *file);
1da177e4
LT
1396
1397/*
1398 * Umount options
1399 */
1400
1401#define MNT_FORCE 0x00000001 /* Attempt to forcibily umount */
1402#define MNT_DETACH 0x00000002 /* Just detach from the tree */
1403#define MNT_EXPIRE 0x00000004 /* Mark for expiry */
db1f05bb
MS
1404#define UMOUNT_NOFOLLOW 0x00000008 /* Don't follow symlink on umount */
1405#define UMOUNT_UNUSED 0x80000000 /* Flag guaranteed to be unused */
1da177e4 1406
a1ec9040
CB
1407static inline struct user_namespace *i_user_ns(const struct inode *inode)
1408{
1409 return inode->i_sb->s_user_ns;
1410}
1411
81754357
SF
1412/* Helper functions so that in most cases filesystems will
1413 * not need to deal directly with kuid_t and kgid_t and can
1414 * instead deal with the raw numeric values that are stored
1415 * in the filesystem.
1416 */
1417static inline uid_t i_uid_read(const struct inode *inode)
1418{
a1ec9040 1419 return from_kuid(i_user_ns(inode), inode->i_uid);
81754357
SF
1420}
1421
1422static inline gid_t i_gid_read(const struct inode *inode)
1423{
a1ec9040 1424 return from_kgid(i_user_ns(inode), inode->i_gid);
81754357
SF
1425}
1426
1427static inline void i_uid_write(struct inode *inode, uid_t uid)
1428{
a1ec9040 1429 inode->i_uid = make_kuid(i_user_ns(inode), uid);
81754357
SF
1430}
1431
1432static inline void i_gid_write(struct inode *inode, gid_t gid)
1433{
a1ec9040 1434 inode->i_gid = make_kgid(i_user_ns(inode), gid);
81754357
SF
1435}
1436
234a3113 1437/**
e67fe633
CB
1438 * i_uid_into_vfsuid - map an inode's i_uid down according to an idmapping
1439 * @idmap: idmap of the mount the inode was found from
234a3113
CB
1440 * @inode: inode to map
1441 *
e67fe633 1442 * Return: whe inode's i_uid mapped down according to @idmap.
234a3113
CB
1443 * If the inode's i_uid has no mapping INVALID_VFSUID is returned.
1444 */
e67fe633 1445static inline vfsuid_t i_uid_into_vfsuid(struct mnt_idmap *idmap,
234a3113
CB
1446 const struct inode *inode)
1447{
4d7ca409 1448 return make_vfsuid(idmap, i_user_ns(inode), inode->i_uid);
e6c9a714
CB
1449}
1450
1f36146a
CB
1451/**
1452 * i_uid_needs_update - check whether inode's i_uid needs to be updated
0dbe12f2 1453 * @idmap: idmap of the mount the inode was found from
1f36146a
CB
1454 * @attr: the new attributes of @inode
1455 * @inode: the inode to update
1456 *
1457 * Check whether the $inode's i_uid field needs to be updated taking idmapped
1458 * mounts into account if the filesystem supports it.
1459 *
1460 * Return: true if @inode's i_uid field needs to be updated, false if not.
1461 */
0dbe12f2 1462static inline bool i_uid_needs_update(struct mnt_idmap *idmap,
1f36146a
CB
1463 const struct iattr *attr,
1464 const struct inode *inode)
1465{
1466 return ((attr->ia_valid & ATTR_UID) &&
1467 !vfsuid_eq(attr->ia_vfsuid,
e67fe633 1468 i_uid_into_vfsuid(idmap, inode)));
1f36146a
CB
1469}
1470
1471/**
1472 * i_uid_update - update @inode's i_uid field
0dbe12f2 1473 * @idmap: idmap of the mount the inode was found from
1f36146a
CB
1474 * @attr: the new attributes of @inode
1475 * @inode: the inode to update
1476 *
1477 * Safely update @inode's i_uid field translating the vfsuid of any idmapped
1478 * mount into the filesystem kuid.
1479 */
0dbe12f2 1480static inline void i_uid_update(struct mnt_idmap *idmap,
1f36146a
CB
1481 const struct iattr *attr,
1482 struct inode *inode)
1483{
1484 if (attr->ia_valid & ATTR_UID)
4d7ca409 1485 inode->i_uid = from_vfsuid(idmap, i_user_ns(inode),
1f36146a
CB
1486 attr->ia_vfsuid);
1487}
1488
234a3113 1489/**
e67fe633
CB
1490 * i_gid_into_vfsgid - map an inode's i_gid down according to an idmapping
1491 * @idmap: idmap of the mount the inode was found from
234a3113
CB
1492 * @inode: inode to map
1493 *
e67fe633 1494 * Return: the inode's i_gid mapped down according to @idmap.
234a3113
CB
1495 * If the inode's i_gid has no mapping INVALID_VFSGID is returned.
1496 */
e67fe633 1497static inline vfsgid_t i_gid_into_vfsgid(struct mnt_idmap *idmap,
234a3113
CB
1498 const struct inode *inode)
1499{
4d7ca409 1500 return make_vfsgid(idmap, i_user_ns(inode), inode->i_gid);
e6c9a714
CB
1501}
1502
1f36146a
CB
1503/**
1504 * i_gid_needs_update - check whether inode's i_gid needs to be updated
0dbe12f2 1505 * @idmap: idmap of the mount the inode was found from
1f36146a
CB
1506 * @attr: the new attributes of @inode
1507 * @inode: the inode to update
1508 *
1509 * Check whether the $inode's i_gid field needs to be updated taking idmapped
1510 * mounts into account if the filesystem supports it.
1511 *
1512 * Return: true if @inode's i_gid field needs to be updated, false if not.
1513 */
0dbe12f2 1514static inline bool i_gid_needs_update(struct mnt_idmap *idmap,
1f36146a
CB
1515 const struct iattr *attr,
1516 const struct inode *inode)
1517{
1518 return ((attr->ia_valid & ATTR_GID) &&
1519 !vfsgid_eq(attr->ia_vfsgid,
e67fe633 1520 i_gid_into_vfsgid(idmap, inode)));
1f36146a
CB
1521}
1522
1523/**
1524 * i_gid_update - update @inode's i_gid field
0dbe12f2 1525 * @idmap: idmap of the mount the inode was found from
1f36146a
CB
1526 * @attr: the new attributes of @inode
1527 * @inode: the inode to update
1528 *
1529 * Safely update @inode's i_gid field translating the vfsgid of any idmapped
1530 * mount into the filesystem kgid.
1531 */
0dbe12f2 1532static inline void i_gid_update(struct mnt_idmap *idmap,
1f36146a
CB
1533 const struct iattr *attr,
1534 struct inode *inode)
1535{
1536 if (attr->ia_valid & ATTR_GID)
4d7ca409 1537 inode->i_gid = from_vfsgid(idmap, i_user_ns(inode),
1f36146a
CB
1538 attr->ia_vfsgid);
1539}
1540
db998553
CB
1541/**
1542 * inode_fsuid_set - initialize inode's i_uid field with callers fsuid
1543 * @inode: inode to initialize
c14329d3 1544 * @idmap: idmap of the mount the inode was found from
db998553
CB
1545 *
1546 * Initialize the i_uid field of @inode. If the inode was found/created via
c14329d3 1547 * an idmapped mount map the caller's fsuid according to @idmap.
db998553
CB
1548 */
1549static inline void inode_fsuid_set(struct inode *inode,
c14329d3 1550 struct mnt_idmap *idmap)
db998553 1551{
c14329d3 1552 inode->i_uid = mapped_fsuid(idmap, i_user_ns(inode));
db998553
CB
1553}
1554
1555/**
1556 * inode_fsgid_set - initialize inode's i_gid field with callers fsgid
1557 * @inode: inode to initialize
c14329d3 1558 * @idmap: idmap of the mount the inode was found from
db998553
CB
1559 *
1560 * Initialize the i_gid field of @inode. If the inode was found/created via
c14329d3 1561 * an idmapped mount map the caller's fsgid according to @idmap.
db998553
CB
1562 */
1563static inline void inode_fsgid_set(struct inode *inode,
c14329d3 1564 struct mnt_idmap *idmap)
db998553 1565{
c14329d3 1566 inode->i_gid = mapped_fsgid(idmap, i_user_ns(inode));
db998553
CB
1567}
1568
8e538913
CB
1569/**
1570 * fsuidgid_has_mapping() - check whether caller's fsuid/fsgid is mapped
1571 * @sb: the superblock we want a mapping in
4609e1f1 1572 * @idmap: idmap of the relevant mount
8e538913
CB
1573 *
1574 * Check whether the caller's fsuid and fsgid have a valid mapping in the
1575 * s_user_ns of the superblock @sb. If the caller is on an idmapped mount map
4609e1f1 1576 * the caller's fsuid and fsgid according to the @idmap first.
8e538913
CB
1577 *
1578 * Return: true if fsuid and fsgid is mapped, false if not.
1579 */
1580static inline bool fsuidgid_has_mapping(struct super_block *sb,
4609e1f1 1581 struct mnt_idmap *idmap)
8e538913 1582{
476860b3
CB
1583 struct user_namespace *fs_userns = sb->s_user_ns;
1584 kuid_t kuid;
1585 kgid_t kgid;
1586
c14329d3 1587 kuid = mapped_fsuid(idmap, fs_userns);
476860b3
CB
1588 if (!uid_valid(kuid))
1589 return false;
c14329d3 1590 kgid = mapped_fsgid(idmap, fs_userns);
476860b3
CB
1591 if (!gid_valid(kgid))
1592 return false;
1593 return kuid_has_mapping(fs_userns, kuid) &&
1594 kgid_has_mapping(fs_userns, kgid);
8e538913
CB
1595}
1596
9b6304c1
JL
1597struct timespec64 current_time(struct inode *inode);
1598struct timespec64 inode_set_ctime_current(struct inode *inode);
7f2c86cb
JL
1599struct timespec64 inode_set_ctime_deleg(struct inode *inode,
1600 struct timespec64 update);
9b6304c1 1601
077c212f
JL
1602static inline time64_t inode_get_atime_sec(const struct inode *inode)
1603{
3aa63a56 1604 return inode->i_atime_sec;
077c212f
JL
1605}
1606
1607static inline long inode_get_atime_nsec(const struct inode *inode)
1608{
3aa63a56 1609 return inode->i_atime_nsec;
077c212f
JL
1610}
1611
1612static inline struct timespec64 inode_get_atime(const struct inode *inode)
1613{
3aa63a56
JL
1614 struct timespec64 ts = { .tv_sec = inode_get_atime_sec(inode),
1615 .tv_nsec = inode_get_atime_nsec(inode) };
1616
1617 return ts;
077c212f
JL
1618}
1619
1620static inline struct timespec64 inode_set_atime_to_ts(struct inode *inode,
1621 struct timespec64 ts)
1622{
3aa63a56
JL
1623 inode->i_atime_sec = ts.tv_sec;
1624 inode->i_atime_nsec = ts.tv_nsec;
077c212f
JL
1625 return ts;
1626}
1627
1628static inline struct timespec64 inode_set_atime(struct inode *inode,
1629 time64_t sec, long nsec)
1630{
1631 struct timespec64 ts = { .tv_sec = sec,
1632 .tv_nsec = nsec };
3aa63a56 1633
077c212f
JL
1634 return inode_set_atime_to_ts(inode, ts);
1635}
1636
1637static inline time64_t inode_get_mtime_sec(const struct inode *inode)
1638{
3aa63a56 1639 return inode->i_mtime_sec;
077c212f
JL
1640}
1641
1642static inline long inode_get_mtime_nsec(const struct inode *inode)
1643{
3aa63a56 1644 return inode->i_mtime_nsec;
077c212f
JL
1645}
1646
1647static inline struct timespec64 inode_get_mtime(const struct inode *inode)
1648{
3aa63a56
JL
1649 struct timespec64 ts = { .tv_sec = inode_get_mtime_sec(inode),
1650 .tv_nsec = inode_get_mtime_nsec(inode) };
1651 return ts;
077c212f
JL
1652}
1653
1654static inline struct timespec64 inode_set_mtime_to_ts(struct inode *inode,
1655 struct timespec64 ts)
1656{
3aa63a56
JL
1657 inode->i_mtime_sec = ts.tv_sec;
1658 inode->i_mtime_nsec = ts.tv_nsec;
077c212f
JL
1659 return ts;
1660}
1661
1662static inline struct timespec64 inode_set_mtime(struct inode *inode,
1663 time64_t sec, long nsec)
1664{
1665 struct timespec64 ts = { .tv_sec = sec,
1666 .tv_nsec = nsec };
1667 return inode_set_mtime_to_ts(inode, ts);
1668}
1669
4e40eff0
JL
1670/*
1671 * Multigrain timestamps
1672 *
1673 * Conditionally use fine-grained ctime and mtime timestamps when there
1674 * are users actively observing them via getattr. The primary use-case
1675 * for this is NFS clients that use the ctime to distinguish between
1676 * different states of the file, and that are often fooled by multiple
1677 * operations that occur in the same coarse-grained timer tick.
1678 */
1679#define I_CTIME_QUERIED ((u32)BIT(31))
1680
077c212f
JL
1681static inline time64_t inode_get_ctime_sec(const struct inode *inode)
1682{
3aa63a56 1683 return inode->i_ctime_sec;
077c212f
JL
1684}
1685
1686static inline long inode_get_ctime_nsec(const struct inode *inode)
1687{
4e40eff0 1688 return inode->i_ctime_nsec & ~I_CTIME_QUERIED;
077c212f
JL
1689}
1690
9b6304c1
JL
1691static inline struct timespec64 inode_get_ctime(const struct inode *inode)
1692{
3aa63a56
JL
1693 struct timespec64 ts = { .tv_sec = inode_get_ctime_sec(inode),
1694 .tv_nsec = inode_get_ctime_nsec(inode) };
1695
1696 return ts;
9b6304c1
JL
1697}
1698
4e40eff0 1699struct timespec64 inode_set_ctime_to_ts(struct inode *inode, struct timespec64 ts);
9b6304c1
JL
1700
1701/**
1702 * inode_set_ctime - set the ctime in the inode
1703 * @inode: inode in which to set the ctime
1704 * @sec: tv_sec value to set
1705 * @nsec: tv_nsec value to set
1706 *
1707 * Set the ctime in @inode to { @sec, @nsec }
1708 */
1709static inline struct timespec64 inode_set_ctime(struct inode *inode,
1710 time64_t sec, long nsec)
1711{
1712 struct timespec64 ts = { .tv_sec = sec,
1713 .tv_nsec = nsec };
1714
1715 return inode_set_ctime_to_ts(inode, ts);
1716}
1da177e4 1717
077c212f
JL
1718struct timespec64 simple_inode_init_ts(struct inode *inode);
1719
1da177e4
LT
1720/*
1721 * Snapshotting support.
1722 */
1da177e4 1723
3d5cd491
AG
1724/**
1725 * file_write_started - check if SB_FREEZE_WRITE is held
1726 * @file: the file we write to
1727 *
1728 * May be false positive with !CONFIG_LOCKDEP/LOCK_STATE_UNKNOWN.
1729 * May be false positive with !S_ISREG, because file_start_write() has
1730 * no effect on !S_ISREG.
1731 */
1732static inline bool file_write_started(const struct file *file)
1733{
1734 if (!S_ISREG(file_inode(file)->i_mode))
1735 return true;
1736 return sb_write_started(file_inode(file)->i_sb);
1737}
1738
21b32e6a
AG
1739/**
1740 * file_write_not_started - check if SB_FREEZE_WRITE is not held
1741 * @file: the file we write to
1742 *
1743 * May be false positive with !CONFIG_LOCKDEP/LOCK_STATE_UNKNOWN.
1744 * May be false positive with !S_ISREG, because file_start_write() has
1745 * no effect on !S_ISREG.
1746 */
1747static inline bool file_write_not_started(const struct file *file)
1748{
1749 if (!S_ISREG(file_inode(file)->i_mode))
1750 return true;
1751 return sb_write_not_started(file_inode(file)->i_sb);
7f8d12ea
NA
1752}
1753
01beba79 1754bool inode_owner_or_capable(struct mnt_idmap *idmap,
21cb47be 1755 const struct inode *inode);
3bd858ab 1756
1da177e4
LT
1757/*
1758 * VFS helper functions..
1759 */
c826229c
JL
1760int vfs_create(struct mnt_idmap *, struct dentry *, umode_t,
1761 struct delegated_inode *);
c54b3869 1762struct dentry *vfs_mkdir(struct mnt_idmap *, struct inode *,
e12d203b 1763 struct dentry *, umode_t, struct delegated_inode *);
abf08576 1764int vfs_mknod(struct mnt_idmap *, struct inode *, struct dentry *,
e8960c1b 1765 umode_t, dev_t, struct delegated_inode *);
abf08576 1766int vfs_symlink(struct mnt_idmap *, struct inode *,
92bf5357 1767 struct dentry *, const char *, struct delegated_inode *);
abf08576 1768int vfs_link(struct dentry *, struct mnt_idmap *, struct inode *,
6976ed2d 1769 struct dentry *, struct delegated_inode *);
4fa76319
JL
1770int vfs_rmdir(struct mnt_idmap *, struct inode *, struct dentry *,
1771 struct delegated_inode *);
abf08576 1772int vfs_unlink(struct mnt_idmap *, struct inode *, struct dentry *,
6976ed2d 1773 struct delegated_inode *);
9fe61450 1774
92cb01c7
CB
1775/**
1776 * struct renamedata - contains all information required for renaming
d7fb2c41 1777 * @mnt_idmap: idmap of the mount in which the rename is happening.
bc924136 1778 * @old_parent: parent of source
92cb01c7 1779 * @old_dentry: source
bc924136 1780 * @new_parent: parent of destination
92cb01c7
CB
1781 * @new_dentry: destination
1782 * @delegated_inode: returns an inode needing a delegation break
1783 * @flags: rename flags
1784 */
9fe61450 1785struct renamedata {
d7fb2c41 1786 struct mnt_idmap *mnt_idmap;
bc924136 1787 struct dentry *old_parent;
9fe61450 1788 struct dentry *old_dentry;
bc924136 1789 struct dentry *new_parent;
9fe61450 1790 struct dentry *new_dentry;
6976ed2d 1791 struct delegated_inode *delegated_inode;
9fe61450
CB
1792 unsigned int flags;
1793} __randomize_layout;
a3c751a5 1794
9fe61450 1795int vfs_rename(struct renamedata *);
a3c751a5 1796
abf08576 1797static inline int vfs_whiteout(struct mnt_idmap *idmap,
6521f891 1798 struct inode *dir, struct dentry *dentry)
a3c751a5 1799{
abf08576 1800 return vfs_mknod(idmap, dir, dentry, S_IFCHR | WHITEOUT_MODE,
e8960c1b 1801 WHITEOUT_DEV, NULL);
a3c751a5 1802}
1da177e4 1803
d56e0ddb
AG
1804struct file *kernel_tmpfile_open(struct mnt_idmap *idmap,
1805 const struct path *parentpath,
1806 umode_t mode, int open_flag,
1807 const struct cred *cred);
cbb0b9d4 1808struct file *kernel_file_open(const struct path *path, int flags,
af58dc1f 1809 const struct cred *cred);
af7bd4dc 1810
8e6c848e
AV
1811int vfs_mkobj(struct dentry *, umode_t,
1812 int (*f)(struct dentry *, umode_t, void *),
1813 void *);
1814
c04011fe 1815int vfs_fchown(struct file *file, uid_t user, gid_t group);
9e96c8c0 1816int vfs_fchmod(struct file *file, umode_t mode);
fd5ad30c 1817int vfs_utimes(const struct path *path, struct timespec64 *times);
c04011fe 1818
2952db0f
AB
1819#ifdef CONFIG_COMPAT
1820extern long compat_ptr_ioctl(struct file *file, unsigned int cmd,
1821 unsigned long arg);
1822#else
1823#define compat_ptr_ioctl NULL
1824#endif
1825
8c744fb8
CH
1826/*
1827 * VFS file helper functions.
1828 */
f2d40141 1829void inode_init_owner(struct mnt_idmap *idmap, struct inode *inode,
21cb47be 1830 const struct inode *dir, umode_t mode);
a2982cc9 1831extern bool may_open_dev(const struct path *path);
9452e93e 1832umode_t mode_strip_sgid(struct mnt_idmap *idmap,
2b3416ce 1833 const struct inode *dir, umode_t mode);
9b6a14f0
YT
1834bool in_group_or_capable(struct mnt_idmap *idmap,
1835 const struct inode *inode, vfsgid_t vfsgid);
c4b929b8 1836
1da177e4
LT
1837/*
1838 * This is the "filldir" function type, used by readdir() to let
1839 * the kernel specify what kind of dirent layout it wants to have.
1840 * This allows the kernel to read directories into kernel space or
1841 * to have different dirent layouts depending on the binary type.
25885a35 1842 * Return 'true' to keep going and 'false' if there are no more entries.
1da177e4 1843 */
ac7576f4 1844struct dir_context;
25885a35 1845typedef bool (*filldir_t)(struct dir_context *, const char *, int, loff_t, u64,
ac7576f4
MS
1846 unsigned);
1847
5c0ba4e0 1848struct dir_context {
a09acf4b 1849 filldir_t actor;
bb6f619b 1850 loff_t pos;
e0410e95
MS
1851 /*
1852 * Filesystems MUST NOT MODIFY count, but may use as a hint:
1853 * 0 unknown
1854 * > 0 space in buffer (assume at least one entry)
1855 * INT_MAX unlimited
1856 */
1857 int count;
5c0ba4e0 1858};
bb6f619b 1859
8d911700
MS
1860/* If OR-ed with d_type, pending signals are not checked */
1861#define FILLDIR_FLAG_NOINTR 0x1000
1862
b4caecd4
CH
1863/*
1864 * These flags let !MMU mmap() govern direct device mapping vs immediate
1865 * copying more easily for MAP_PRIVATE, especially for ROM filesystems.
1866 *
1867 * NOMMU_MAP_COPY: Copy can be mapped (MAP_PRIVATE)
1868 * NOMMU_MAP_DIRECT: Can be mapped directly (MAP_SHARED)
1869 * NOMMU_MAP_READ: Can be mapped for reading
1870 * NOMMU_MAP_WRITE: Can be mapped for writing
1871 * NOMMU_MAP_EXEC: Can be mapped for execution
1872 */
1873#define NOMMU_MAP_COPY 0x00000001
1874#define NOMMU_MAP_DIRECT 0x00000008
1875#define NOMMU_MAP_READ VM_MAYREAD
1876#define NOMMU_MAP_WRITE VM_MAYWRITE
1877#define NOMMU_MAP_EXEC VM_MAYEXEC
1878
1879#define NOMMU_VMFLAGS \
1880 (NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC)
1881
2e5dfc99
DW
1882/*
1883 * These flags control the behavior of the remap_file_range function pointer.
1884 * If it is called with len == 0 that means "remap to end of source file".
5c437fa2 1885 * See Documentation/filesystems/vfs.rst for more details about this call.
2e5dfc99
DW
1886 *
1887 * REMAP_FILE_DEDUP: only remap if contents identical (i.e. deduplicate)
eca3654e 1888 * REMAP_FILE_CAN_SHORTEN: caller can handle a shortened request
2e5dfc99
DW
1889 */
1890#define REMAP_FILE_DEDUP (1 << 0)
eca3654e 1891#define REMAP_FILE_CAN_SHORTEN (1 << 1)
2e5dfc99
DW
1892
1893/*
1894 * These flags signal that the caller is ok with altering various aspects of
1895 * the behavior of the remap operation. The changes must be made by the
1896 * implementation; the vfs remap helper functions can take advantage of them.
1897 * Flags in this category exist to preserve the quirky behavior of the hoisted
1898 * btrfs clone/dedupe ioctls.
2e5dfc99 1899 */
eca3654e 1900#define REMAP_FILE_ADVISORY (REMAP_FILE_CAN_SHORTEN)
b4caecd4 1901
10bc8e4a
AG
1902/*
1903 * These flags control the behavior of vfs_copy_file_range().
1904 * They are not available to the user via syscall.
1905 *
1906 * COPY_FILE_SPLICE: call splice direct instead of fs clone/copy ops
1907 */
1908#define COPY_FILE_SPLICE (1 << 0)
1909
293bc982 1910struct iov_iter;
ee692a21 1911struct io_uring_cmd;
6faddda6 1912struct offset_ctx;
293bc982 1913
210a03c9
CB
1914typedef unsigned int __bitwise fop_flags_t;
1915
1da177e4
LT
1916struct file_operations {
1917 struct module *owner;
210a03c9 1918 fop_flags_t fop_flags;
1da177e4
LT
1919 loff_t (*llseek) (struct file *, loff_t, int);
1920 ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
1da177e4 1921 ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
293bc982
AV
1922 ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);
1923 ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);
5a72e899
JA
1924 int (*iopoll)(struct kiocb *kiocb, struct io_comp_batch *,
1925 unsigned int flags);
61922694 1926 int (*iterate_shared) (struct file *, struct dir_context *);
a3f8683b 1927 __poll_t (*poll) (struct file *, struct poll_table_struct *);
1da177e4
LT
1928 long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
1929 long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
1930 int (*mmap) (struct file *, struct vm_area_struct *);
1931 int (*open) (struct inode *, struct file *);
75e1fcc0 1932 int (*flush) (struct file *, fl_owner_t id);
1da177e4 1933 int (*release) (struct inode *, struct file *);
02c24a82 1934 int (*fsync) (struct file *, loff_t, loff_t, int datasync);
1da177e4
LT
1935 int (*fasync) (int, struct file *, int);
1936 int (*lock) (struct file *, int, struct file_lock *);
1da177e4
LT
1937 unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
1938 int (*check_flags)(int);
1da177e4 1939 int (*flock) (struct file *, int, struct file_lock *);
cbb7e577
JA
1940 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
1941 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
2bfc6685 1942 void (*splice_eof)(struct file *file);
c69ff407 1943 int (*setlease)(struct file *, int, struct file_lease **, void **);
2fe17c10
CH
1944 long (*fallocate)(struct file *file, int mode, loff_t offset,
1945 loff_t len);
a3816ab0 1946 void (*show_fdinfo)(struct seq_file *m, struct file *f);
b4caecd4
CH
1947#ifndef CONFIG_MMU
1948 unsigned (*mmap_capabilities)(struct file *);
1949#endif
04b38d60
CH
1950 ssize_t (*copy_file_range)(struct file *, loff_t, struct file *,
1951 loff_t, size_t, unsigned int);
42ec3d4c
DW
1952 loff_t (*remap_file_range)(struct file *file_in, loff_t pos_in,
1953 struct file *file_out, loff_t pos_out,
1954 loff_t len, unsigned int remap_flags);
45cd0faa 1955 int (*fadvise)(struct file *, loff_t, loff_t, int);
ee692a21 1956 int (*uring_cmd)(struct io_uring_cmd *ioucmd, unsigned int issue_flags);
de97fcb3
JA
1957 int (*uring_cmd_iopoll)(struct io_uring_cmd *, struct io_comp_batch *,
1958 unsigned int poll_flags);
c84bf6dd 1959 int (*mmap_prepare)(struct vm_area_desc *);
3859a271 1960} __randomize_layout;
1da177e4 1961
210a03c9
CB
1962/* Supports async buffered reads */
1963#define FOP_BUFFER_RASYNC ((__force fop_flags_t)(1 << 0))
1964/* Supports async buffered writes */
1965#define FOP_BUFFER_WASYNC ((__force fop_flags_t)(1 << 1))
1966/* Supports synchronous page faults for mappings */
1967#define FOP_MMAP_SYNC ((__force fop_flags_t)(1 << 2))
1968/* Supports non-exclusive O_DIRECT writes from multiple threads */
1969#define FOP_DIO_PARALLEL_WRITE ((__force fop_flags_t)(1 << 3))
886b94d2
MWO
1970/* Contains huge pages */
1971#define FOP_HUGE_PAGES ((__force fop_flags_t)(1 << 4))
641bb439
CB
1972/* Treat loff_t as unsigned (e.g., /dev/mem) */
1973#define FOP_UNSIGNED_OFFSET ((__force fop_flags_t)(1 << 5))
8cf9a01e 1974/* Supports asynchronous lock callbacks */
09ee2a67 1975#define FOP_ASYNC_LOCK ((__force fop_flags_t)(1 << 6))
af6505e5 1976/* File system supports uncached read/write buffered IO */
7b2b67db 1977#define FOP_DONTCACHE ((__force fop_flags_t)(1 << 7))
210a03c9 1978
3e327154
LT
1979/* Wrap a directory iterator that needs exclusive inode access */
1980int wrap_directory_iterator(struct file *, struct dir_context *,
1981 int (*) (struct file *, struct dir_context *));
1982#define WRAP_DIR_ITER(x) \
1983 static int shared_##x(struct file *file , struct dir_context *ctx) \
1984 { return wrap_directory_iterator(file, ctx, x); }
1985
1da177e4 1986struct inode_operations {
00cd8dd3 1987 struct dentry * (*lookup) (struct inode *,struct dentry *, unsigned int);
fceef393 1988 const char * (*get_link) (struct dentry *, struct inode *, struct delayed_call *);
4609e1f1 1989 int (*permission) (struct mnt_idmap *, struct inode *, int);
cac2f8b8 1990 struct posix_acl * (*get_inode_acl)(struct inode *, int, bool);
44a7d7a8
NP
1991
1992 int (*readlink) (struct dentry *, char __user *,int);
44a7d7a8 1993
6c960e68 1994 int (*create) (struct mnt_idmap *, struct inode *,struct dentry *,
549c7297 1995 umode_t, bool);
1da177e4
LT
1996 int (*link) (struct dentry *,struct inode *,struct dentry *);
1997 int (*unlink) (struct inode *,struct dentry *);
7a77db95 1998 int (*symlink) (struct mnt_idmap *, struct inode *,struct dentry *,
549c7297 1999 const char *);
88d5baf6
N
2000 struct dentry *(*mkdir) (struct mnt_idmap *, struct inode *,
2001 struct dentry *, umode_t);
1da177e4 2002 int (*rmdir) (struct inode *,struct dentry *);
5ebb29be 2003 int (*mknod) (struct mnt_idmap *, struct inode *,struct dentry *,
549c7297 2004 umode_t,dev_t);
e18275ae 2005 int (*rename) (struct mnt_idmap *, struct inode *, struct dentry *,
520c8b16 2006 struct inode *, struct dentry *, unsigned int);
c1632a0f 2007 int (*setattr) (struct mnt_idmap *, struct dentry *, struct iattr *);
b74d24f7 2008 int (*getattr) (struct mnt_idmap *, const struct path *,
549c7297 2009 struct kstat *, u32, unsigned int);
1da177e4 2010 ssize_t (*listxattr) (struct dentry *, char *, size_t);
c4b929b8
MF
2011 int (*fiemap)(struct inode *, struct fiemap_extent_info *, u64 start,
2012 u64 len);
913e9928 2013 int (*update_time)(struct inode *, int);
d9585277 2014 int (*atomic_open)(struct inode *, struct dentry *,
30d90494 2015 struct file *, unsigned open_flag,
44907d79 2016 umode_t create_mode);
011e2b71 2017 int (*tmpfile) (struct mnt_idmap *, struct inode *,
863f144f 2018 struct file *, umode_t);
77435322 2019 struct posix_acl *(*get_acl)(struct mnt_idmap *, struct dentry *,
7420332a 2020 int);
13e83a49 2021 int (*set_acl)(struct mnt_idmap *, struct dentry *,
549c7297 2022 struct posix_acl *, int);
8782a9ae 2023 int (*fileattr_set)(struct mnt_idmap *idmap,
ca115d7e
CB
2024 struct dentry *dentry, struct file_kattr *fa);
2025 int (*fileattr_get)(struct dentry *dentry, struct file_kattr *fa);
6faddda6 2026 struct offset_ctx *(*get_offset_ctx)(struct inode *inode);
44a7d7a8 2027} ____cacheline_aligned;
1da177e4 2028
c84bf6dd 2029/* Did the driver provide valid mmap hook configuration? */
b013ed40 2030static inline bool can_mmap_file(struct file *file)
c84bf6dd
LS
2031{
2032 bool has_mmap = file->f_op->mmap;
2033 bool has_mmap_prepare = file->f_op->mmap_prepare;
2034
2035 /* Hooks are mutually exclusive. */
2036 if (WARN_ON_ONCE(has_mmap && has_mmap_prepare))
2037 return false;
6669d1aa 2038 if (!has_mmap && !has_mmap_prepare)
c84bf6dd
LS
2039 return false;
2040
2041 return true;
2042}
2043
f7a741c5
LS
2044int __compat_vma_mmap_prepare(const struct file_operations *f_op,
2045 struct file *file, struct vm_area_struct *vma);
bb666b7c
LS
2046int compat_vma_mmap_prepare(struct file *file, struct vm_area_struct *vma);
2047
20ca475d 2048static inline int vfs_mmap(struct file *file, struct vm_area_struct *vma)
f74ac015 2049{
bb666b7c
LS
2050 if (file->f_op->mmap_prepare)
2051 return compat_vma_mmap_prepare(file, vma);
c84bf6dd 2052
f74ac015
MS
2053 return file->f_op->mmap(file, vma);
2054}
2055
20ca475d 2056static inline int vfs_mmap_prepare(struct file *file, struct vm_area_desc *desc)
c84bf6dd
LS
2057{
2058 return file->f_op->mmap_prepare(desc);
2059}
2060
1da177e4
LT
2061extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
2062extern ssize_t vfs_write(struct file *, const char __user *, size_t, loff_t *);
29732938
ZB
2063extern ssize_t vfs_copy_file_range(struct file *, loff_t , struct file *,
2064 loff_t, size_t, unsigned int);
5b9932f6 2065int remap_verify_area(struct file *file, loff_t pos, loff_t len, bool write);
6f7db389
SR
2066int __generic_remap_file_range_prep(struct file *file_in, loff_t pos_in,
2067 struct file *file_out, loff_t pos_out,
2068 loff_t *len, unsigned int remap_flags,
2069 const struct iomap_ops *dax_read_ops);
2070int generic_remap_file_range_prep(struct file *file_in, loff_t pos_in,
2071 struct file *file_out, loff_t pos_out,
2072 loff_t *count, unsigned int remap_flags);
42ec3d4c
DW
2073extern loff_t vfs_clone_file_range(struct file *file_in, loff_t pos_in,
2074 struct file *file_out, loff_t pos_out,
452ce659 2075 loff_t len, unsigned int remap_flags);
54dbc151
DW
2076extern int vfs_dedupe_file_range(struct file *file,
2077 struct file_dedupe_range *same);
42ec3d4c
DW
2078extern loff_t vfs_dedupe_file_range_one(struct file *src_file, loff_t src_pos,
2079 struct file *dst_file, loff_t dst_pos,
df365836 2080 loff_t len, unsigned int remap_flags);
f1825366 2081
bbc1096a
DH
2082/*
2083 * Inode flags - they have no relation to superblock flags now
2084 */
6414e9b0
EB
2085#define S_SYNC (1 << 0) /* Writes are synced at once */
2086#define S_NOATIME (1 << 1) /* Do not update access times */
2087#define S_APPEND (1 << 2) /* Append-only file */
2088#define S_IMMUTABLE (1 << 3) /* Immutable file */
2089#define S_DEAD (1 << 4) /* removed, but still open directory */
2090#define S_NOQUOTA (1 << 5) /* Inode is not counted to quota */
2091#define S_DIRSYNC (1 << 6) /* Directory modifications are synchronous */
2092#define S_NOCMTIME (1 << 7) /* Do not update file c/mtime */
2093#define S_SWAPFILE (1 << 8) /* Do not truncate: swapon got its bmaps */
2094#define S_PRIVATE (1 << 9) /* Inode is fs-internal */
2095#define S_IMA (1 << 10) /* Inode has an associated IMA struct */
2096#define S_AUTOMOUNT (1 << 11) /* Automount/referral quasi-directory */
2097#define S_NOSEC (1 << 12) /* no suid or xattr security attributes */
6cd176a5 2098#ifdef CONFIG_FS_DAX
6414e9b0 2099#define S_DAX (1 << 13) /* Direct Access, avoiding the page cache */
fbbbad4b 2100#else
6414e9b0 2101#define S_DAX 0 /* Make all the DAX code disappear */
fbbbad4b 2102#endif
6414e9b0
EB
2103#define S_ENCRYPTED (1 << 14) /* Encrypted file (using fs/crypto/) */
2104#define S_CASEFOLD (1 << 15) /* Casefolded file */
2105#define S_VERITY (1 << 16) /* Verity file (using fs/verity/) */
1bd9c4e4 2106#define S_KERNEL_FILE (1 << 17) /* File is in use by the kernel (eg. fs/cachefiles) */
19bbfe7b 2107#define S_ANON_INODE (1 << 19) /* Inode is an anonymous inode */
bbc1096a
DH
2108
2109/*
2110 * Note that nosuid etc flags are inode-specific: setting some file-system
2111 * flags just means all the inodes inherit those flags by default. It might be
2112 * possible to override it selectively if you really wanted to with some
2113 * ioctl() that is not currently implemented.
2114 *
e462ec50 2115 * Exception: SB_RDONLY is always applied to the entire file system.
bbc1096a
DH
2116 *
2117 * Unfortunately, it is possible to change a filesystems flags with it mounted
2118 * with files in use. This means that all of the inodes will not have their
2119 * i_flags updated. Hence, i_flags no longer inherit the superblock mount
2120 * flags, so these have to be checked separately. -- rmk@arm.uk.linux.org
2121 */
2122#define __IS_FLG(inode, flg) ((inode)->i_sb->s_flags & (flg))
2123
94e92e7a 2124#define IS_RDONLY(inode) sb_rdonly((inode)->i_sb)
e462ec50 2125#define IS_SYNC(inode) (__IS_FLG(inode, SB_SYNCHRONOUS) || \
bbc1096a 2126 ((inode)->i_flags & S_SYNC))
e462ec50 2127#define IS_DIRSYNC(inode) (__IS_FLG(inode, SB_SYNCHRONOUS|SB_DIRSYNC) || \
bbc1096a 2128 ((inode)->i_flags & (S_SYNC|S_DIRSYNC)))
e462ec50
DH
2129#define IS_MANDLOCK(inode) __IS_FLG(inode, SB_MANDLOCK)
2130#define IS_NOATIME(inode) __IS_FLG(inode, SB_RDONLY|SB_NOATIME)
2131#define IS_I_VERSION(inode) __IS_FLG(inode, SB_I_VERSION)
bbc1096a
DH
2132
2133#define IS_NOQUOTA(inode) ((inode)->i_flags & S_NOQUOTA)
2134#define IS_APPEND(inode) ((inode)->i_flags & S_APPEND)
2135#define IS_IMMUTABLE(inode) ((inode)->i_flags & S_IMMUTABLE)
e4e8b47a
MK
2136
2137#ifdef CONFIG_FS_POSIX_ACL
e462ec50 2138#define IS_POSIXACL(inode) __IS_FLG(inode, SB_POSIXACL)
e4e8b47a
MK
2139#else
2140#define IS_POSIXACL(inode) 0
2141#endif
bbc1096a
DH
2142
2143#define IS_DEADDIR(inode) ((inode)->i_flags & S_DEAD)
2144#define IS_NOCMTIME(inode) ((inode)->i_flags & S_NOCMTIME)
e964fc77
AD
2145
2146#ifdef CONFIG_SWAP
bbc1096a 2147#define IS_SWAPFILE(inode) ((inode)->i_flags & S_SWAPFILE)
e964fc77
AD
2148#else
2149#define IS_SWAPFILE(inode) ((void)(inode), 0U)
2150#endif
2151
bbc1096a
DH
2152#define IS_PRIVATE(inode) ((inode)->i_flags & S_PRIVATE)
2153#define IS_IMA(inode) ((inode)->i_flags & S_IMA)
2154#define IS_AUTOMOUNT(inode) ((inode)->i_flags & S_AUTOMOUNT)
2155#define IS_NOSEC(inode) ((inode)->i_flags & S_NOSEC)
fbbbad4b 2156#define IS_DAX(inode) ((inode)->i_flags & S_DAX)
2ee6a576 2157#define IS_ENCRYPTED(inode) ((inode)->i_flags & S_ENCRYPTED)
b886ee3e 2158#define IS_CASEFOLDED(inode) ((inode)->i_flags & S_CASEFOLD)
5585f2af 2159#define IS_VERITY(inode) ((inode)->i_flags & S_VERITY)
bbc1096a 2160
787fb6bc
MS
2161#define IS_WHITEOUT(inode) (S_ISCHR(inode->i_mode) && \
2162 (inode)->i_rdev == WHITEOUT_DEV)
19bbfe7b 2163#define IS_ANON_FILE(inode) ((inode)->i_flags & S_ANON_INODE)
787fb6bc 2164
4609e1f1 2165static inline bool HAS_UNMAPPED_ID(struct mnt_idmap *idmap,
ba73d987 2166 struct inode *inode)
0bd23d09 2167{
e67fe633
CB
2168 return !vfsuid_valid(i_uid_into_vfsuid(idmap, inode)) ||
2169 !vfsgid_valid(i_gid_into_vfsgid(idmap, inode));
0bd23d09
EB
2170}
2171
c75b1d94
JA
2172static inline void init_sync_kiocb(struct kiocb *kiocb, struct file *filp)
2173{
2174 *kiocb = (struct kiocb) {
2175 .ki_filp = filp,
164f4064 2176 .ki_flags = filp->f_iocb_flags,
20578bdf 2177 .ki_ioprio = get_current_ioprio(),
c75b1d94
JA
2178 };
2179}
2180
5dcdc43e
JX
2181static inline void kiocb_clone(struct kiocb *kiocb, struct kiocb *kiocb_src,
2182 struct file *filp)
2183{
2184 *kiocb = (struct kiocb) {
2185 .ki_filp = filp,
2186 .ki_flags = kiocb_src->ki_flags,
5dcdc43e
JX
2187 .ki_ioprio = kiocb_src->ki_ioprio,
2188 .ki_pos = kiocb_src->ki_pos,
2189 };
2190}
2191
1da177e4
LT
2192extern void __mark_inode_dirty(struct inode *, int);
2193static inline void mark_inode_dirty(struct inode *inode)
2194{
2195 __mark_inode_dirty(inode, I_DIRTY);
2196}
2197
2198static inline void mark_inode_dirty_sync(struct inode *inode)
2199{
2200 __mark_inode_dirty(inode, I_DIRTY_SYNC);
2201}
2202
37b27bd5
JB
2203static inline int icount_read(const struct inode *inode)
2204{
2205 return atomic_read(&inode->i_count);
2206}
2207
ed296c6c
EB
2208/*
2209 * Returns true if the given inode itself only has dirty timestamps (its pages
2210 * may still be dirty) and isn't currently being allocated or freed.
2211 * Filesystems should call this if when writing an inode when lazytime is
2212 * enabled, they want to opportunistically write the timestamps of other inodes
2213 * located very nearby on-disk, e.g. in the same inode block. This returns true
2214 * if the given inode is in need of such an opportunistic update. Requires
2215 * i_lock, or at least later re-checking under i_lock.
2216 */
2217static inline bool inode_is_dirtytime_only(struct inode *inode)
2218{
f5aa78e2
MG
2219 return (inode_state_read_once(inode) &
2220 (I_DIRTY_TIME | I_NEW | I_FREEING | I_WILL_FREE)) == I_DIRTY_TIME;
ed296c6c
EB
2221}
2222
7ada4db8
MS
2223extern void inc_nlink(struct inode *inode);
2224extern void drop_nlink(struct inode *inode);
2225extern void clear_nlink(struct inode *inode);
2226extern void set_nlink(struct inode *inode, unsigned int nlink);
d8c76e6f
DH
2227
2228static inline void inode_inc_link_count(struct inode *inode)
2229{
2230 inc_nlink(inode);
66d21730
AD
2231 mark_inode_dirty(inode);
2232}
2233
9a53c3a7
DH
2234static inline void inode_dec_link_count(struct inode *inode)
2235{
2236 drop_nlink(inode);
66d21730
AD
2237 mark_inode_dirty(inode);
2238}
2239
c3b2da31
JB
2240enum file_time_flags {
2241 S_ATIME = 1,
2242 S_MTIME = 2,
2243 S_CTIME = 4,
2244 S_VERSION = 8,
2245};
2246
c6718543 2247extern bool atime_needs_update(const struct path *, struct inode *);
badcf2b7 2248extern void touch_atime(const struct path *);
913e9928 2249int inode_update_time(struct inode *inode, int flags);
e60feb44 2250
1da177e4
LT
2251static inline void file_accessed(struct file *file)
2252{
2253 if (!(file->f_flags & O_NOATIME))
68ac1234 2254 touch_atime(&file->f_path);
1da177e4
LT
2255}
2256
e38f7f53 2257extern int file_modified(struct file *file);
66fa3ced 2258int kiocb_modified(struct kiocb *iocb);
e38f7f53 2259
c3765016 2260int sync_inode_metadata(struct inode *inode, int wait);
1da177e4 2261
1da177e4
LT
2262struct file_system_type {
2263 const char *name;
2264 int fs_flags;
bbc1096a
DH
2265#define FS_REQUIRES_DEV 1
2266#define FS_BINARY_MOUNTDATA 2
2267#define FS_HAS_SUBTYPE 4
0c55cfc4 2268#define FS_USERNS_MOUNT 8 /* Can be mounted by userns root */
0b3b094a 2269#define FS_DISALLOW_NOTIFY_PERM 16 /* Disable fanotify permission events */
a6435940 2270#define FS_ALLOW_IDMAP 32 /* FS has been updated to handle vfs idmappings. */
4e40eff0 2271#define FS_MGTIME 64 /* FS uses multigrain timestamps */
a64e5a59 2272#define FS_LBS 128 /* FS supports LBS */
a3f8f866 2273#define FS_POWER_FREEZE 256 /* Always freeze on suspend/hibernate */
bbc1096a 2274#define FS_RENAME_DOES_D_MOVE 32768 /* FS will handle d_move() during rename() internally. */
f3a09c92 2275 int (*init_fs_context)(struct fs_context *);
d7167b14 2276 const struct fs_parameter_spec *parameters;
c96e41e9
AV
2277 struct dentry *(*mount) (struct file_system_type *, int,
2278 const char *, void *);
1da177e4
LT
2279 void (*kill_sb) (struct super_block *);
2280 struct module *owner;
2281 struct file_system_type * next;
a5166169 2282 struct hlist_head fs_supers;
d475fd42 2283
cf516249 2284 struct lock_class_key s_lock_key;
897c6ff9 2285 struct lock_class_key s_umount_key;
51ee049e 2286 struct lock_class_key s_vfs_rename_key;
5accdf82 2287 struct lock_class_key s_writers_key[SB_FREEZE_LEVELS];
d475fd42
PZ
2288
2289 struct lock_class_key i_lock_key;
2290 struct lock_class_key i_mutex_key;
730633f0 2291 struct lock_class_key invalidate_lock_key;
14358e6d 2292 struct lock_class_key i_mutex_dir_key;
1da177e4
LT
2293};
2294
7f78e035
EB
2295#define MODULE_ALIAS_FS(NAME) MODULE_ALIAS("fs-" NAME)
2296
4e40eff0
JL
2297/**
2298 * is_mgtime: is this inode using multigrain timestamps
2299 * @inode: inode to test for multigrain timestamps
2300 *
2301 * Return true if the inode uses multigrain timestamps, false otherwise.
2302 */
2303static inline bool is_mgtime(const struct inode *inode)
2304{
9fed2c0f 2305 return inode->i_opflags & IOP_MGTIME;
4e40eff0
JL
2306}
2307
ea441d11 2308extern struct dentry *mount_subtree(struct vfsmount *mnt, const char *path);
04b94071 2309void retire_super(struct super_block *sb);
1da177e4
LT
2310void generic_shutdown_super(struct super_block *sb);
2311void kill_block_super(struct super_block *sb);
2312void kill_anon_super(struct super_block *sb);
2313void kill_litter_super(struct super_block *sb);
2314void deactivate_super(struct super_block *sb);
74dbbdd7 2315void deactivate_locked_super(struct super_block *sb);
1da177e4 2316int set_anon_super(struct super_block *s, void *data);
cb50b348 2317int set_anon_super_fc(struct super_block *s, struct fs_context *fc);
0ee5dc67
AV
2318int get_anon_bdev(dev_t *);
2319void free_anon_bdev(dev_t);
cb50b348
AV
2320struct super_block *sget_fc(struct fs_context *fc,
2321 int (*test)(struct super_block *, struct fs_context *),
2322 int (*set)(struct super_block *, struct fs_context *));
1da177e4
LT
2323struct super_block *sget(struct file_system_type *type,
2324 int (*test)(struct super_block *,void *),
2325 int (*set)(struct super_block *,void *),
9249e17f 2326 int flags, void *data);
69881be3 2327struct super_block *sget_dev(struct fs_context *fc, dev_t dev);
1da177e4
LT
2328
2329/* Alas, no aliases. Too much hassle with bringing module.h everywhere */
8447d848
MG
2330#define fops_get(fops) ({ \
2331 const struct file_operations *_fops = (fops); \
2332 (((_fops) && try_module_get((_fops)->owner) ? (_fops) : NULL)); \
2333})
2334
2335#define fops_put(fops) ({ \
2336 const struct file_operations *_fops = (fops); \
2337 if (_fops) \
2338 module_put((_fops)->owner); \
2339})
2340
e84f9e57
AV
2341/*
2342 * This one is to be used *ONLY* from ->open() instances.
2343 * fops must be non-NULL, pinned down *and* module dependencies
2344 * should be sufficient to pin the caller down as well.
2345 */
2346#define replace_fops(f, fops) \
2347 do { \
2348 struct file *__file = (f); \
2349 fops_put(__file->f_op); \
2350 BUG_ON(!(__file->f_op = (fops))); \
2351 } while(0)
1da177e4
LT
2352
2353extern int register_filesystem(struct file_system_type *);
2354extern int unregister_filesystem(struct file_system_type *);
f0bb5aaf 2355extern int vfs_statfs(const struct path *, struct kstatfs *);
c8b91acc
AV
2356extern int user_statfs(const char __user *, struct kstatfs *);
2357extern int fd_statfs(int, struct kstatfs *);
fca39346
JK
2358extern __printf(2, 3)
2359int super_setup_bdi_name(struct super_block *sb, char *fmt, ...);
2360extern int super_setup_bdi(struct super_block *sb);
1da177e4 2361
a4af51ce
KO
2362static inline void super_set_uuid(struct super_block *sb, const u8 *uuid, unsigned len)
2363{
2364 if (WARN_ON(len > sizeof(sb->s_uuid)))
2365 len = sizeof(sb->s_uuid);
2366 sb->s_uuid_len = len;
2367 memcpy(&sb->s_uuid, uuid, len);
2368}
2369
ae8c5117
KO
2370/* set sb sysfs name based on sb->s_bdev */
2371static inline void super_set_sysfs_name_bdev(struct super_block *sb)
2372{
2373 snprintf(sb->s_sysfs_name, sizeof(sb->s_sysfs_name), "%pg", sb->s_bdev);
2374}
2375
2376/* set sb sysfs name based on sb->s_uuid */
2377static inline void super_set_sysfs_name_uuid(struct super_block *sb)
2378{
2379 WARN_ON(sb->s_uuid_len != sizeof(sb->s_uuid));
2380 snprintf(sb->s_sysfs_name, sizeof(sb->s_sysfs_name), "%pU", sb->s_uuid.b);
2381}
2382
2383/* set sb sysfs name based on sb->s_id */
2384static inline void super_set_sysfs_name_id(struct super_block *sb)
2385{
2386 strscpy(sb->s_sysfs_name, sb->s_id, sizeof(sb->s_sysfs_name));
2387}
2388
2389/* try to use something standard before you use this */
2390__printf(2, 3)
2391static inline void super_set_sysfs_name_generic(struct super_block *sb, const char *fmt, ...)
2392{
2393 va_list args;
2394
2395 va_start(args, fmt);
2396 vsnprintf(sb->s_sysfs_name, sizeof(sb->s_sysfs_name), fmt, args);
2397 va_end(args);
2398}
2399
5a14696c
BF
2400extern void ihold(struct inode * inode);
2401extern void iput(struct inode *);
12741624 2402void iput_not_last(struct inode *);
541d4c79
JL
2403int inode_update_timestamps(struct inode *inode, int flags);
2404int generic_update_time(struct inode *, int);
5a14696c 2405
f87fd4c2 2406/* /sys/fs */
00d26666 2407extern struct kobject *fs_kobj;
f87fd4c2 2408
09cbfeaf 2409#define MAX_RW_COUNT (INT_MAX & PAGE_MASK)
bfcd17a6 2410
1da177e4 2411/* fs/open.c */
adb5c247 2412struct audit_names;
91a27b2a 2413struct filename {
adb5c247
JL
2414 const char *name; /* pointer to actual string */
2415 const __user char *uptr; /* original userland pointer */
03adc61e 2416 atomic_t refcnt;
1c949843 2417 struct audit_names *aname;
fd2f7cb5 2418 const char iname[];
91a27b2a 2419};
f1fffbd4 2420static_assert(offsetof(struct filename, iname) % sizeof(long) == 0);
1da177e4 2421
4a00c673 2422static inline struct mnt_idmap *file_mnt_idmap(const struct file *file)
256c8aed
CB
2423{
2424 return mnt_idmap(file->f_path.mnt);
2425}
2426
bb49e9e7
CB
2427/**
2428 * is_idmapped_mnt - check whether a mount is mapped
2429 * @mnt: the mount to check
2430 *
256c8aed 2431 * If @mnt has an non @nop_mnt_idmap attached to it then @mnt is mapped.
bb49e9e7
CB
2432 *
2433 * Return: true if mount is mapped, false if not.
2434 */
2435static inline bool is_idmapped_mnt(const struct vfsmount *mnt)
2436{
256c8aed 2437 return mnt_idmap(mnt) != &nop_mnt_idmap;
bb49e9e7
CB
2438}
2439
29d80d50 2440int vfs_truncate(const struct path *, loff_t);
abf08576 2441int do_truncate(struct mnt_idmap *, struct dentry *, loff_t start,
643fe55a 2442 unsigned int time_attrs, struct file *filp);
72c72bdf 2443extern int vfs_fallocate(struct file *file, int mode, loff_t offset,
3e63cbb1 2444 loff_t len);
29d80d50
YT
2445int do_sys_open(int dfd, const char __user *filename, int flags,
2446 umode_t mode);
669abf4e 2447extern struct file *file_open_name(struct filename *, int, umode_t);
a218d0fd 2448extern struct file *filp_open(const char *, int, umode_t);
ffb37ca3 2449extern struct file *file_open_root(const struct path *,
378c6520 2450 const char *, int, umode_t);
ffb37ca3
AV
2451static inline struct file *file_open_root_mnt(struct vfsmount *mnt,
2452 const char *name, int flags, umode_t mode)
2453{
2454 return file_open_root(&(struct path){.mnt = mnt, .dentry = mnt->mnt_root},
2455 name, flags, mode);
2456}
62d53c4a
AG
2457struct file *dentry_open(const struct path *path, int flags,
2458 const struct cred *creds);
ebe55960
AV
2459struct file *dentry_open_nonotify(const struct path *path, int flags,
2460 const struct cred *cred);
62d53c4a
AG
2461struct file *dentry_create(const struct path *path, int flags, umode_t mode,
2462 const struct cred *cred);
dae575e6 2463const struct path *backing_file_user_path(const struct file *f);
62d53c4a
AG
2464
2465/*
08582d67
AG
2466 * When mmapping a file on a stackable filesystem (e.g., overlayfs), the file
2467 * stored in ->vm_file is a backing file whose f_inode is on the underlying
3efdc78f
AV
2468 * filesystem. When the mapped file path and inode number are displayed to
2469 * user (e.g. via /proc/<pid>/maps), these helpers should be used to get the
2470 * path and inode number to display to the user, which is the path of the fd
2471 * that user has requested to map and the inode number that would be returned
2472 * by fstat() on that same fd.
08582d67 2473 */
3efdc78f 2474/* Get the path to display in /proc/<pid>/maps */
4e301d85 2475static inline const struct path *file_user_path(const struct file *f)
62d53c4a
AG
2476{
2477 if (unlikely(f->f_mode & FMODE_BACKING))
def3ae83 2478 return backing_file_user_path(f);
62d53c4a
AG
2479 return &f->f_path;
2480}
3efdc78f 2481/* Get the inode whose inode number to display in /proc/<pid>/maps */
4e301d85 2482static inline const struct inode *file_user_inode(const struct file *f)
3efdc78f
AV
2483{
2484 if (unlikely(f->f_mode & FMODE_BACKING))
2485 return d_inode(backing_file_user_path(f)->dentry);
2486 return file_inode(f);
2487}
62d53c4a 2488
19f391eb
AV
2489static inline struct file *file_clone_open(struct file *file)
2490{
2491 return dentry_open(&file->f_path, file->f_flags, file->f_cred);
2492}
1da177e4 2493extern int filp_close(struct file *, fl_owner_t id);
91a27b2a 2494
dff60734 2495extern struct filename *getname_flags(const char __user *, int);
8228e2c3 2496extern struct filename *getname_uflags(const char __user *, int);
1bb77256
MG
2497static inline struct filename *getname(const char __user *name)
2498{
2499 return getname_flags(name, 0);
2500}
c4ad8f98 2501extern struct filename *getname_kernel(const char *);
e896474f
AV
2502extern struct filename *__getname_maybe_null(const char __user *);
2503static inline struct filename *getname_maybe_null(const char __user *name, int flags)
2504{
2505 if (!(flags & AT_EMPTY_PATH))
2506 return getname(name);
2507
2508 if (!name)
2509 return NULL;
2510 return __getname_maybe_null(name);
2511}
55422d0b 2512extern void putname(struct filename *name);
f9fde814 2513DEFINE_FREE(putname, struct filename *, if (!IS_ERR_OR_NULL(_T)) putname(_T))
91a27b2a 2514
61185101
MG
2515static inline struct filename *refname(struct filename *name)
2516{
2517 atomic_inc(&name->refcnt);
2518 return name;
2519}
2520
30d90494 2521extern int finish_open(struct file *file, struct dentry *dentry,
be12af3e 2522 int (*open)(struct inode *, struct file *));
e45198a6 2523extern int finish_no_open(struct file *file, struct dentry *dentry);
1da177e4 2524
9751b338
MS
2525/* Helper for the simple case when original dentry is used */
2526static inline int finish_open_simple(struct file *file, int error)
2527{
2528 if (error)
2529 return error;
2530
2531 return finish_open(file, file->f_path.dentry, NULL);
2532}
2533
1da177e4
LT
2534/* fs/dcache.c */
2535extern void __init vfs_caches_init_early(void);
4248b0da 2536extern void __init vfs_caches_init(void);
1da177e4 2537
b86c089b
CL
2538extern struct kmem_cache *names_cachep;
2539
a608ca21 2540#define __getname() kmem_cache_alloc(names_cachep, GFP_KERNEL)
3446a8aa 2541#define __putname(name) kmem_cache_free(names_cachep, (void *)(name))
1da177e4 2542
b818f09e 2543void emergency_thaw_all(void);
60b0680f 2544extern int sync_filesystem(struct super_block *);
9361401e 2545extern const struct file_operations def_blk_fops;
4b6f5d20 2546extern const struct file_operations def_chr_fops;
1da177e4
LT
2547
2548/* fs/char_dev.c */
8a932f73 2549#define CHRDEV_MAJOR_MAX 512
49db08c3
LW
2550/* Marks the bottom of the first segment of free char majors */
2551#define CHRDEV_MAJOR_DYN_END 234
a5d31a3f
LG
2552/* Marks the top and bottom of the second segment of free char majors */
2553#define CHRDEV_MAJOR_DYN_EXT_START 511
2554#define CHRDEV_MAJOR_DYN_EXT_END 384
2555
1da177e4
LT
2556extern int alloc_chrdev_region(dev_t *, unsigned, unsigned, const char *);
2557extern int register_chrdev_region(dev_t, unsigned, const char *);
1905b1bf
TH
2558extern int __register_chrdev(unsigned int major, unsigned int baseminor,
2559 unsigned int count, const char *name,
2560 const struct file_operations *fops);
2561extern void __unregister_chrdev(unsigned int major, unsigned int baseminor,
2562 unsigned int count, const char *name);
1da177e4 2563extern void unregister_chrdev_region(dev_t, unsigned);
68eef3b4 2564extern void chrdev_show(struct seq_file *,off_t);
1da177e4 2565
1905b1bf
TH
2566static inline int register_chrdev(unsigned int major, const char *name,
2567 const struct file_operations *fops)
2568{
2569 return __register_chrdev(major, 0, 256, name, fops);
2570}
2571
2572static inline void unregister_chrdev(unsigned int major, const char *name)
2573{
2574 __unregister_chrdev(major, 0, 256, name);
2575}
2576
1da177e4
LT
2577extern void init_special_inode(struct inode *, umode_t, dev_t);
2578
2579/* Invalid inode operations -- fs/bad_inode.c */
2580extern void make_bad_inode(struct inode *);
0e3ef1fe 2581extern bool is_bad_inode(struct inode *);
1da177e4 2582
a823e458
JL
2583extern int __must_check file_fdatawait_range(struct file *file, loff_t lstart,
2584 loff_t lend);
5660e13d
JL
2585extern int __must_check file_check_and_advance_wb_err(struct file *file);
2586extern int __must_check file_write_and_wait_range(struct file *file,
2587 loff_t start, loff_t end);
c28d67b3 2588int filemap_flush_range(struct address_space *mapping, loff_t start,
dddc559f 2589 loff_t end);
5660e13d 2590
a823e458
JL
2591static inline int file_write_and_wait(struct file *file)
2592{
2593 return file_write_and_wait_range(file, 0, LLONG_MAX);
2594}
2595
8018ab05
CH
2596extern int vfs_fsync_range(struct file *file, loff_t start, loff_t end,
2597 int datasync);
2598extern int vfs_fsync(struct file *file, int datasync);
e2592217 2599
22f96b38
JA
2600extern int sync_file_range(struct file *file, loff_t offset, loff_t nbytes,
2601 unsigned int flags);
2602
91b94c5d
AV
2603static inline bool iocb_is_dsync(const struct kiocb *iocb)
2604{
2605 return (iocb->ki_flags & IOCB_DSYNC) ||
2606 IS_SYNC(iocb->ki_filp->f_mapping->host);
2607}
2608
e2592217
CH
2609/*
2610 * Sync the bytes written if this was a synchronous write. Expect ki_pos
2611 * to already be updated for the write, and will return either the amount
2612 * of bytes passed in, or an error if syncing the file failed.
2613 */
2614static inline ssize_t generic_write_sync(struct kiocb *iocb, ssize_t count)
2615{
91b94c5d 2616 if (iocb_is_dsync(iocb)) {
e2592217
CH
2617 int ret = vfs_fsync_range(iocb->ki_filp,
2618 iocb->ki_pos - count, iocb->ki_pos - 1,
2619 (iocb->ki_flags & IOCB_SYNC) ? 0 : 1);
2620 if (ret)
2621 return ret;
1d445757
JA
2622 } else if (iocb->ki_flags & IOCB_DONTCACHE) {
2623 struct address_space *mapping = iocb->ki_filp->f_mapping;
2624
c28d67b3
CH
2625 filemap_flush_range(mapping, iocb->ki_pos - count,
2626 iocb->ki_pos - 1);
e2592217
CH
2627 }
2628
2629 return count;
d311d79d 2630}
e2592217 2631
1da177e4
LT
2632extern void emergency_sync(void);
2633extern void emergency_remount(void);
30460e1e 2634
9361401e 2635#ifdef CONFIG_BLOCK
30460e1e
CM
2636extern int bmap(struct inode *inode, sector_t *block);
2637#else
2638static inline int bmap(struct inode *inode, sector_t *block)
2639{
2640 return -EINVAL;
2641}
9361401e 2642#endif
30460e1e 2643
abf08576 2644int notify_change(struct mnt_idmap *, struct dentry *,
6976ed2d 2645 struct iattr *, struct delegated_inode *);
4609e1f1
CB
2646int inode_permission(struct mnt_idmap *, struct inode *, int);
2647int generic_permission(struct mnt_idmap *, struct inode *, int);
02f92b38
CB
2648static inline int file_permission(struct file *file, int mask)
2649{
4609e1f1 2650 return inode_permission(file_mnt_idmap(file),
47291baa 2651 file_inode(file), mask);
02f92b38
CB
2652}
2653static inline int path_permission(const struct path *path, int mask)
2654{
4609e1f1 2655 return inode_permission(mnt_idmap(path->mnt),
47291baa 2656 d_inode(path->dentry), mask);
02f92b38 2657}
9452e93e 2658int __check_sticky(struct mnt_idmap *idmap, struct inode *dir,
ba73d987 2659 struct inode *inode);
1da177e4 2660
f696a365
MS
2661static inline bool execute_ok(struct inode *inode)
2662{
2663 return (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode);
2664}
2665
6e3e2c43
AV
2666static inline bool inode_wrong_type(const struct inode *inode, umode_t mode)
2667{
2668 return (inode->i_mode ^ mode) & S_IFMT;
2669}
2670
f6c05b9e
AG
2671/**
2672 * file_start_write - get write access to a superblock for regular file io
2673 * @file: the file we want to write to
2674 *
54ca9e91 2675 * This is a variant of sb_start_write() which is a noop on non-regular file.
f6c05b9e
AG
2676 * Should be matched with a call to file_end_write().
2677 */
8d71db4f
AV
2678static inline void file_start_write(struct file *file)
2679{
2680 if (!S_ISREG(file_inode(file)->i_mode))
2681 return;
8a3c84b6 2682 sb_start_write(file_inode(file)->i_sb);
8d71db4f
AV
2683}
2684
5ae98f15
JK
2685static inline bool file_start_write_trylock(struct file *file)
2686{
2687 if (!S_ISREG(file_inode(file)->i_mode))
2688 return true;
8a3c84b6 2689 return sb_start_write_trylock(file_inode(file)->i_sb);
5ae98f15
JK
2690}
2691
f6c05b9e
AG
2692/**
2693 * file_end_write - drop write access to a superblock of a regular file
2694 * @file: the file we wrote to
2695 *
2696 * Should be matched with a call to file_start_write().
2697 */
8d71db4f
AV
2698static inline void file_end_write(struct file *file)
2699{
2700 if (!S_ISREG(file_inode(file)->i_mode))
2701 return;
f6c05b9e 2702 sb_end_write(file_inode(file)->i_sb);
8d71db4f 2703}
bfe219d3 2704
ed0360bb
AG
2705/**
2706 * kiocb_start_write - get write access to a superblock for async file io
2707 * @iocb: the io context we want to submit the write with
2708 *
2709 * This is a variant of sb_start_write() for async io submission.
2710 * Should be matched with a call to kiocb_end_write().
2711 */
2712static inline void kiocb_start_write(struct kiocb *iocb)
2713{
2714 struct inode *inode = file_inode(iocb->ki_filp);
2715
2716 sb_start_write(inode->i_sb);
2717 /*
2718 * Fool lockdep by telling it the lock got released so that it
2719 * doesn't complain about the held lock when we return to userspace.
2720 */
2721 __sb_writers_release(inode->i_sb, SB_FREEZE_WRITE);
2722}
2723
2724/**
2725 * kiocb_end_write - drop write access to a superblock after async file io
2726 * @iocb: the io context we sumbitted the write with
2727 *
2728 * Should be matched with a call to kiocb_start_write().
2729 */
2730static inline void kiocb_end_write(struct kiocb *iocb)
2731{
2732 struct inode *inode = file_inode(iocb->ki_filp);
2733
2734 /*
2735 * Tell lockdep we inherited freeze protection from submission thread.
2736 */
2737 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
2738 sb_end_write(inode->i_sb);
8d71db4f 2739}
bfe219d3 2740
07b8ce1e 2741/*
592ca09b
DH
2742 * This is used for regular files where some users -- especially the
2743 * currently executed binary in a process, previously handled via
2744 * VM_DENYWRITE -- cannot handle concurrent write (and maybe mmap
2745 * read-write shared) accesses.
2746 *
07b8ce1e
AV
2747 * get_write_access() gets write permission for a file.
2748 * put_write_access() releases this write permission.
592ca09b
DH
2749 * deny_write_access() denies write access to a file.
2750 * allow_write_access() re-enables write access to a file.
2751 *
2752 * The i_writecount field of an inode can have the following values:
2753 * 0: no write access, no denied write access
2754 * < 0: (-i_writecount) users that denied write access to the file.
2755 * > 0: (i_writecount) users that have write access to the file.
07b8ce1e
AV
2756 *
2757 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
2758 * except for the cases where we don't hold i_writecount yet. Then we need to
2759 * use {get,deny}_write_access() - these functions check the sign and refuse
2760 * to do the change if sign is wrong.
2761 */
2762static inline int get_write_access(struct inode *inode)
2763{
2764 return atomic_inc_unless_negative(&inode->i_writecount) ? 0 : -ETXTBSY;
2765}
2766static inline int deny_write_access(struct file *file)
2767{
496ad9aa 2768 struct inode *inode = file_inode(file);
07b8ce1e
AV
2769 return atomic_dec_unless_positive(&inode->i_writecount) ? 0 : -ETXTBSY;
2770}
1da177e4
LT
2771static inline void put_write_access(struct inode * inode)
2772{
2773 atomic_dec(&inode->i_writecount);
2774}
2775static inline void allow_write_access(struct file *file)
2776{
2777 if (file)
496ad9aa 2778 atomic_inc(&file_inode(file)->i_writecount);
1da177e4 2779}
0357ef03
AG
2780
2781/*
2782 * Do not prevent write to executable file when watched by pre-content events.
2783 *
2784 * Note that FMODE_FSNOTIFY_HSM mode is set depending on pre-content watches at
2785 * the time of file open and remains constant for entire lifetime of the file,
2786 * so if pre-content watches are added post execution or removed before the end
2787 * of the execution, it will not cause i_writecount reference leak.
2788 */
2789static inline int exe_file_deny_write_access(struct file *exe_file)
2790{
2791 if (unlikely(FMODE_FSNOTIFY_HSM(exe_file->f_mode)))
2792 return 0;
2793 return deny_write_access(exe_file);
2794}
2795static inline void exe_file_allow_write_access(struct file *exe_file)
2796{
2797 if (unlikely(!exe_file || FMODE_FSNOTIFY_HSM(exe_file->f_mode)))
2798 return;
2799 allow_write_access(exe_file);
2800}
2801
95101401
AG
2802static inline void file_set_fsnotify_mode(struct file *file, fmode_t mode)
2803{
2804 file->f_mode &= ~FMODE_FSNOTIFY_MASK;
2805 file->f_mode |= mode;
2806}
2807
f1fe29b4
DH
2808static inline bool inode_is_open_for_write(const struct inode *inode)
2809{
2810 return atomic_read(&inode->i_writecount) > 0;
2811}
2812
387e3746 2813#if defined(CONFIG_IMA) || defined(CONFIG_FILE_LOCKING)
a5c96ebf
MZ
2814static inline void i_readcount_dec(struct inode *inode)
2815{
45e0d4b9 2816 BUG_ON(atomic_dec_return(&inode->i_readcount) < 0);
a5c96ebf
MZ
2817}
2818static inline void i_readcount_inc(struct inode *inode)
2819{
2820 atomic_inc(&inode->i_readcount);
2821}
2822#else
2823static inline void i_readcount_dec(struct inode *inode)
2824{
2825 return;
2826}
2827static inline void i_readcount_inc(struct inode *inode)
2828{
2829 return;
2830}
2831#endif
ed8cae8b 2832extern int do_pipe_flags(int *, int);
1da177e4 2833
bdd1d2d3 2834extern ssize_t kernel_read(struct file *, void *, size_t, loff_t *);
61a707c5 2835ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos);
e13ec939 2836extern ssize_t kernel_write(struct file *, const void *, size_t, loff_t *);
73e18f7c 2837extern ssize_t __kernel_write(struct file *, const void *, size_t, loff_t *);
1da177e4
LT
2838extern struct file * open_exec(const char *);
2839
2840/* fs/dcache.c -- generic fs support functions */
a6e5787f 2841extern bool is_subdir(struct dentry *, struct dentry *);
640eb7e7 2842extern bool path_is_under(const struct path *, const struct path *);
922a6f34 2843u64 vfsmount_to_propagation_flags(struct vfsmount *mnt);
1da177e4 2844
9bf39ab2
MS
2845extern char *file_path(struct file *, char *, int);
2846
42c3732f
CL
2847/**
2848 * is_dot_dotdot - returns true only if @name is "." or ".."
2849 * @name: file name to check
2850 * @len: length of file name, in bytes
2851 */
2852static inline bool is_dot_dotdot(const char *name, size_t len)
2853{
2854 return len && unlikely(name[0] == '.') &&
2855 (len == 1 || (len == 2 && name[1] == '.'));
2856}
2857
0da3e382
CB
2858/**
2859 * name_contains_dotdot - check if a file name contains ".." path components
4e021920 2860 * @name: File path string to check
0da3e382
CB
2861 * Search for ".." surrounded by either '/' or start/end of string.
2862 */
2863static inline bool name_contains_dotdot(const char *name)
2864{
2865 size_t name_len;
2866
2867 name_len = strlen(name);
2868 return strcmp(name, "..") == 0 ||
2869 strncmp(name, "../", 3) == 0 ||
2870 strstr(name, "/../") != NULL ||
2871 (name_len >= 3 && strcmp(name + name_len - 3, "/..") == 0);
2872}
2873
1da177e4
LT
2874#include <linux/err.h>
2875
2876/* needed for stackable file system support */
965c8e59 2877extern loff_t default_llseek(struct file *file, loff_t offset, int whence);
1da177e4 2878
965c8e59 2879extern loff_t vfs_llseek(struct file *file, loff_t offset, int whence);
1da177e4 2880
9897713f
MH
2881extern int inode_init_always_gfp(struct super_block *, struct inode *, gfp_t);
2882static inline int inode_init_always(struct super_block *sb, struct inode *inode)
2883{
2884 return inode_init_always_gfp(sb, inode, GFP_NOFS);
2885}
2886
1da177e4 2887extern void inode_init_once(struct inode *);
2aa15890 2888extern void address_space_init_once(struct address_space *mapping);
1da177e4
LT
2889extern struct inode * igrab(struct inode *);
2890extern ino_t iunique(struct super_block *, ino_t);
2891extern int inode_needs_sync(struct inode *inode);
f99b3917
MG
2892extern int inode_just_drop(struct inode *inode);
2893static inline int inode_generic_drop(struct inode *inode)
9bcb4b73 2894{
88149082 2895 return !inode->i_nlink || inode_unhashed(inode);
9bcb4b73 2896}
2c567af4 2897extern void d_mark_dontcache(struct inode *inode);
1da177e4 2898
88bd5121
AA
2899extern struct inode *ilookup5_nowait(struct super_block *sb,
2900 unsigned long hashval, int (*test)(struct inode *, void *),
a27628f4 2901 void *data, bool *isnew);
1da177e4
LT
2902extern struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
2903 int (*test)(struct inode *, void *), void *data);
2904extern struct inode *ilookup(struct super_block *sb, unsigned long ino);
2905
80ea09a0
MS
2906extern struct inode *inode_insert5(struct inode *inode, unsigned long hashval,
2907 int (*test)(struct inode *, void *),
2908 int (*set)(struct inode *, void *),
2909 void *data);
7180f8d9
MG
2910struct inode *iget5_locked(struct super_block *, unsigned long,
2911 int (*test)(struct inode *, void *),
2912 int (*set)(struct inode *, void *), void *);
2913struct inode *iget5_locked_rcu(struct super_block *, unsigned long,
2914 int (*test)(struct inode *, void *),
2915 int (*set)(struct inode *, void *), void *);
1da177e4 2916extern struct inode * iget_locked(struct super_block *, unsigned long);
fe032c42
TT
2917extern struct inode *find_inode_nowait(struct super_block *,
2918 unsigned long,
2919 int (*match)(struct inode *,
2920 unsigned long, void *),
2921 void *data);
3f19b2ab
DH
2922extern struct inode *find_inode_rcu(struct super_block *, unsigned long,
2923 int (*)(struct inode *, void *), void *);
2924extern struct inode *find_inode_by_ino_rcu(struct super_block *, unsigned long);
261bca86
AV
2925extern int insert_inode_locked4(struct inode *, unsigned long, int (*test)(struct inode *, void *), void *);
2926extern int insert_inode_locked(struct inode *);
e096d0c7
JB
2927#ifdef CONFIG_DEBUG_LOCK_ALLOC
2928extern void lockdep_annotate_inode_mutex_key(struct inode *inode);
2929#else
2930static inline void lockdep_annotate_inode_mutex_key(struct inode *inode) { };
2931#endif
1da177e4 2932extern void unlock_new_inode(struct inode *);
c2b6d621 2933extern void discard_new_inode(struct inode *);
85fe4025 2934extern unsigned int get_next_ino(void);
799ea9e9 2935extern void evict_inodes(struct super_block *sb);
3e9d80a8 2936void dump_mapping(const struct address_space *);
1da177e4 2937
e809d5f0 2938/*
b6334e2c 2939 * Userspace may rely on the inode number being non-zero. For example, glibc
e809d5f0
CD
2940 * simply ignores files with zero i_ino in unlink() and other places.
2941 *
2942 * As an additional complication, if userspace was compiled with
2943 * _FILE_OFFSET_BITS=32 on a 64-bit kernel we'll only end up reading out the
2944 * lower 32 bits, so we need to check that those aren't zero explicitly. With
2945 * _FILE_OFFSET_BITS=64, this may cause some harmless false-negatives, but
2946 * better safe than sorry.
2947 */
2948static inline bool is_zero_ino(ino_t ino)
2949{
2950 return (u32)ino == 0;
2951}
2952
88d2ae0e
KO
2953static inline void __iget(struct inode *inode)
2954{
dc816f8d 2955 lockdep_assert_held(&inode->i_lock);
88d2ae0e
KO
2956 atomic_inc(&inode->i_count);
2957}
2958
b46980fe 2959extern void iget_failed(struct inode *);
dbd5768f 2960extern void clear_inode(struct inode *);
2e00c97e 2961extern void __destroy_inode(struct inode *);
1479be62
MG
2962struct inode *alloc_inode(struct super_block *sb);
2963static inline struct inode *new_inode_pseudo(struct super_block *sb)
2964{
2965 return alloc_inode(sb);
2966}
a209dfc7 2967extern struct inode *new_inode(struct super_block *sb);
ff0c7d15 2968extern void free_inode_nonrcu(struct inode *inode);
9452e93e 2969extern int setattr_should_drop_suidgid(struct mnt_idmap *, struct inode *);
5fa8e0a1 2970extern int file_remove_privs(struct file *);
4f704d9a
CB
2971int setattr_should_drop_sgid(struct mnt_idmap *idmap,
2972 const struct inode *inode);
1da177e4 2973
8b9f3ac5
MS
2974/*
2975 * This must be used for allocating filesystems specific inodes to set
2976 * up the inode reclaim context correctly.
2977 */
a5674119 2978#define alloc_inode_sb(_sb, _cache, _gfp) kmem_cache_alloc_lru(_cache, &_sb->s_inode_lru, _gfp)
8b9f3ac5 2979
1da177e4 2980extern void __insert_inode_hash(struct inode *, unsigned long hashval);
646ec461
CH
2981static inline void insert_inode_hash(struct inode *inode)
2982{
1da177e4
LT
2983 __insert_inode_hash(inode, inode->i_ino);
2984}
f2ee7abf
ED
2985
2986extern void __remove_inode_hash(struct inode *);
2987static inline void remove_inode_hash(struct inode *inode)
2988{
cbedaac6 2989 if (!inode_unhashed(inode) && !hlist_fake(&inode->i_hash))
f2ee7abf
ED
2990 __remove_inode_hash(inode);
2991}
2992
646ec461 2993extern void inode_sb_list_add(struct inode *inode);
4c6b4087 2994extern void inode_lru_list_add(struct inode *inode);
1da177e4 2995
5b44297b
LS
2996int generic_file_mmap(struct file *, struct vm_area_struct *);
2997int generic_file_mmap_prepare(struct vm_area_desc *desc);
2998int generic_file_readonly_mmap(struct file *, struct vm_area_struct *);
2999int generic_file_readonly_mmap_prepare(struct vm_area_desc *desc);
3309dd04 3000extern ssize_t generic_write_checks(struct kiocb *, struct iov_iter *);
f6f7a25a 3001int generic_write_checks_count(struct kiocb *iocb, loff_t *count);
02e83f46
DW
3002extern int generic_write_check_limits(struct file *file, loff_t pos,
3003 loff_t *count);
a3171351 3004extern int generic_file_rw_checks(struct file *file_in, struct file *file_out);
87fa0f3e
CH
3005ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *to,
3006 ssize_t already_read);
ed978a81 3007extern ssize_t generic_file_read_iter(struct kiocb *, struct iov_iter *);
8174202b 3008extern ssize_t __generic_file_write_iter(struct kiocb *, struct iov_iter *);
8174202b 3009extern ssize_t generic_file_write_iter(struct kiocb *, struct iov_iter *);
1af5bb49 3010extern ssize_t generic_file_direct_write(struct kiocb *, struct iov_iter *);
800ba295 3011ssize_t generic_perform_write(struct kiocb *, struct iov_iter *);
44fff0fa
CH
3012ssize_t direct_write_fallback(struct kiocb *iocb, struct iov_iter *iter,
3013 ssize_t direct_written, ssize_t buffered_written);
88e6faef 3014
18e9710e 3015ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos,
ddef7ed2 3016 rwf_t flags);
abbb6589 3017ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos,
ddef7ed2 3018 rwf_t flags);
5dcdc43e
JX
3019ssize_t vfs_iocb_iter_read(struct file *file, struct kiocb *iocb,
3020 struct iov_iter *iter);
3021ssize_t vfs_iocb_iter_write(struct file *file, struct kiocb *iocb,
3022 struct iov_iter *iter);
dbe4e192 3023
88e6faef 3024/* fs/splice.c */
07073eb0
DH
3025ssize_t filemap_splice_read(struct file *in, loff_t *ppos,
3026 struct pipe_inode_info *pipe,
3027 size_t len, unsigned int flags);
69df79a4
DH
3028ssize_t copy_splice_read(struct file *in, loff_t *ppos,
3029 struct pipe_inode_info *pipe,
3030 size_t len, unsigned int flags);
8d020765 3031extern ssize_t iter_file_splice_write(struct pipe_inode_info *,
cbb7e577 3032 struct file *, loff_t *, size_t, unsigned int);
1c118596 3033
88e6faef 3034
1da177e4
LT
3035extern void
3036file_ra_state_init(struct file_ra_state *ra, struct address_space *mapping);
965c8e59 3037extern loff_t noop_llseek(struct file *file, loff_t offset, int whence);
46a1c2c7 3038extern loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize);
965c8e59 3039extern loff_t generic_file_llseek(struct file *file, loff_t offset, int whence);
5760495a 3040extern loff_t generic_file_llseek_size(struct file *file, loff_t offset,
965c8e59 3041 int whence, loff_t maxsize, loff_t eof);
d688d65a
CB
3042loff_t generic_llseek_cookie(struct file *file, loff_t offset, int whence,
3043 u64 *cookie);
1bf9d14d
AV
3044extern loff_t fixed_size_llseek(struct file *file, loff_t offset,
3045 int whence, loff_t size);
b25472f9
AV
3046extern loff_t no_seek_end_llseek_size(struct file *, loff_t, int, loff_t);
3047extern loff_t no_seek_end_llseek(struct file *, loff_t, int);
87112933 3048int rw_verify_area(int, struct file *, const loff_t *, size_t);
1da177e4
LT
3049extern int generic_file_open(struct inode * inode, struct file * filp);
3050extern int nonseekable_open(struct inode * inode, struct file * filp);
10dce8af 3051extern int stream_open(struct inode * inode, struct file * filp);
1da177e4 3052
9361401e 3053#ifdef CONFIG_BLOCK
8a4c1e42 3054typedef void (dio_submit_t)(struct bio *bio, struct inode *inode,
facd07b0 3055 loff_t file_offset);
1da177e4
LT
3056
3057enum {
1e431f5c
CH
3058 /* need locking between buffered and direct access */
3059 DIO_LOCKING = 0x01,
3060
3061 /* filesystem does not support filling holes */
3062 DIO_SKIP_HOLES = 0x02,
1da177e4
LT
3063};
3064
17f8c842
OS
3065ssize_t __blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,
3066 struct block_device *bdev, struct iov_iter *iter,
c8b8e32d 3067 get_block_t get_block,
0aaf08de 3068 dio_iodone_t end_io,
17f8c842 3069 int flags);
7bb46a67 3070
17f8c842
OS
3071static inline ssize_t blockdev_direct_IO(struct kiocb *iocb,
3072 struct inode *inode,
c8b8e32d 3073 struct iov_iter *iter,
17f8c842 3074 get_block_t get_block)
1da177e4 3075{
17f8c842 3076 return __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev, iter,
0aaf08de 3077 get_block, NULL, DIO_LOCKING | DIO_SKIP_HOLES);
1da177e4 3078}
9361401e 3079#endif
1da177e4 3080
1c48d441 3081bool inode_dio_finished(const struct inode *inode);
1d59d61f 3082void inode_dio_wait(struct inode *inode);
1c48d441 3083void inode_dio_wait_interruptible(struct inode *inode);
fe0f07d0 3084
39015399 3085/**
fe0f07d0
JA
3086 * inode_dio_begin - signal start of a direct I/O requests
3087 * @inode: inode the direct I/O happens on
3088 *
3089 * This is called once we've finished processing a direct I/O request,
3090 * and is used to wake up callers waiting for direct I/O to be quiesced.
3091 */
3092static inline void inode_dio_begin(struct inode *inode)
3093{
3094 atomic_inc(&inode->i_dio_count);
3095}
3096
39015399 3097/**
fe0f07d0
JA
3098 * inode_dio_end - signal finish of a direct I/O requests
3099 * @inode: inode the direct I/O happens on
3100 *
3101 * This is called once we've finished processing a direct I/O request,
3102 * and is used to wake up callers waiting for direct I/O to be quiesced.
3103 */
3104static inline void inode_dio_end(struct inode *inode)
3105{
3106 if (atomic_dec_and_test(&inode->i_dio_count))
1c48d441 3107 wake_up_var(&inode->i_dio_count);
fe0f07d0 3108}
1d59d61f 3109
5f16f322
TT
3110extern void inode_set_flags(struct inode *inode, unsigned int flags,
3111 unsigned int mask);
3112
4b6f5d20 3113extern const struct file_operations generic_ro_fops;
1da177e4
LT
3114
3115#define special_file(m) (S_ISCHR(m)||S_ISBLK(m)||S_ISFIFO(m)||S_ISSOCK(m))
3116
ea382199 3117extern int readlink_copy(char __user *, int, const char *, int);
1da177e4 3118extern int page_readlink(struct dentry *, char __user *, int);
b4c173df
MS
3119extern const char *page_get_link_raw(struct dentry *, struct inode *,
3120 struct delayed_call *);
fceef393
AV
3121extern const char *page_get_link(struct dentry *, struct inode *,
3122 struct delayed_call *);
3123extern void page_put_link(void *);
1da177e4 3124extern int page_symlink(struct inode *inode, const char *symname, int len);
c5ef1c42 3125extern const struct inode_operations page_symlink_inode_operations;
fceef393 3126extern void kfree_link(void *);
4e40eff0 3127void fill_mg_cmtime(struct kstat *stat, u32 request_mask, struct inode *inode);
0d72b928 3128void generic_fillattr(struct mnt_idmap *, u32, struct inode *, struct kstat *);
4f911138 3129void generic_fill_statx_attr(struct inode *inode, struct kstat *stat);
0f9ca80f
PS
3130void generic_fill_statx_atomic_writes(struct kstat *stat,
3131 unsigned int unit_min,
5d894321
JG
3132 unsigned int unit_max,
3133 unsigned int unit_max_opt);
a528d35e
DH
3134extern int vfs_getattr_nosec(const struct path *, struct kstat *, u32, unsigned int);
3135extern int vfs_getattr(const struct path *, struct kstat *, u32, unsigned int);
b462707e 3136void __inode_add_bytes(struct inode *inode, loff_t bytes);
1da177e4 3137void inode_add_bytes(struct inode *inode, loff_t bytes);
1c8924eb 3138void __inode_sub_bytes(struct inode *inode, loff_t bytes);
1da177e4 3139void inode_sub_bytes(struct inode *inode, loff_t bytes);
f4a8116a
JK
3140static inline loff_t __inode_get_bytes(struct inode *inode)
3141{
3142 return (((loff_t)inode->i_blocks) << 9) + inode->i_bytes;
3143}
1da177e4
LT
3144loff_t inode_get_bytes(struct inode *inode);
3145void inode_set_bytes(struct inode *inode, loff_t bytes);
fceef393
AV
3146const char *simple_get_link(struct dentry *, struct inode *,
3147 struct delayed_call *);
61ba64fc 3148extern const struct inode_operations simple_symlink_inode_operations;
1da177e4 3149
5c0ba4e0 3150extern int iterate_dir(struct file *, struct dir_context *);
1da177e4 3151
09f1bde4
CH
3152int vfs_fstatat(int dfd, const char __user *filename, struct kstat *stat,
3153 int flags);
da9aa5d9 3154int vfs_fstat(int fd, struct kstat *stat);
a528d35e
DH
3155
3156static inline int vfs_stat(const char __user *filename, struct kstat *stat)
3157{
0b2c6693 3158 return vfs_fstatat(AT_FDCWD, filename, stat, 0);
a528d35e
DH
3159}
3160static inline int vfs_lstat(const char __user *name, struct kstat *stat)
3161{
0b2c6693 3162 return vfs_fstatat(AT_FDCWD, name, stat, AT_SYMLINK_NOFOLLOW);
a528d35e 3163}
a528d35e 3164
d60874cd 3165extern const char *vfs_get_link(struct dentry *, struct delayed_call *);
fd4a0edf 3166extern int vfs_readlink(struct dentry *, char __user *, int);
1da177e4 3167
ee416bcd 3168extern struct file_system_type *get_filesystem(struct file_system_type *fs);
c18479fe 3169extern void put_filesystem(struct file_system_type *fs);
1da177e4 3170extern struct file_system_type *get_fs_type(const char *name);
1da177e4 3171extern void drop_super(struct super_block *sb);
ba6379f7 3172extern void drop_super_exclusive(struct super_block *sb);
b47e42d1 3173extern void iterate_supers(void (*f)(struct super_block *, void *), void *arg);
43e15cdb
AV
3174extern void iterate_supers_type(struct file_system_type *,
3175 void (*)(struct super_block *, void *), void *);
a3f8f866 3176void filesystems_freeze(bool freeze_all);
1af33317 3177void filesystems_thaw(void);
1da177e4 3178
4037d966
N
3179void end_dirop(struct dentry *de);
3180
1da177e4
LT
3181extern int dcache_dir_open(struct inode *, struct file *);
3182extern int dcache_dir_close(struct inode *, struct file *);
3183extern loff_t dcache_dir_lseek(struct file *, loff_t, int);
5f99f4e7 3184extern int dcache_readdir(struct file *, struct dir_context *);
c1632a0f 3185extern int simple_setattr(struct mnt_idmap *, struct dentry *,
549c7297 3186 struct iattr *);
b74d24f7 3187extern int simple_getattr(struct mnt_idmap *, const struct path *,
549c7297 3188 struct kstat *, u32, unsigned int);
726c3342 3189extern int simple_statfs(struct dentry *, struct kstatfs *);
20955e89 3190extern int simple_open(struct inode *inode, struct file *file);
1da177e4
LT
3191extern int simple_link(struct dentry *, struct inode *, struct dentry *);
3192extern int simple_unlink(struct inode *, struct dentry *);
3193extern int simple_rmdir(struct inode *, struct dentry *);
0c476792
JL
3194void simple_rename_timestamp(struct inode *old_dir, struct dentry *old_dentry,
3195 struct inode *new_dir, struct dentry *new_dentry);
6429e463
LB
3196extern int simple_rename_exchange(struct inode *old_dir, struct dentry *old_dentry,
3197 struct inode *new_dir, struct dentry *new_dentry);
e18275ae 3198extern int simple_rename(struct mnt_idmap *, struct inode *,
549c7297
CB
3199 struct dentry *, struct inode *, struct dentry *,
3200 unsigned int);
a3d1e7eb
AV
3201extern void simple_recursive_removal(struct dentry *,
3202 void (*callback)(struct dentry *));
9fd45235
AV
3203extern void locked_recursive_removal(struct dentry *,
3204 void (*callback)(struct dentry *));
02c24a82 3205extern int noop_fsync(struct file *, loff_t, loff_t, int);
f44c7763 3206extern ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1da177e4 3207extern int simple_empty(struct dentry *);
e9d8e2bf
TC
3208extern int simple_write_begin(const struct kiocb *iocb,
3209 struct address_space *mapping,
3210 loff_t pos, unsigned len,
3211 struct folio **foliop, void **fsdata);
c1e3dbe9 3212extern const struct address_space_operations ram_aops;
b26d4cd3 3213extern int always_delete_dentry(const struct dentry *);
6987843f 3214extern struct inode *alloc_anon_inode(struct super_block *);
cbe4134e
SG
3215struct inode *anon_inode_make_secure_inode(struct super_block *sb, const char *name,
3216 const struct inode *context_inode);
c69ff407 3217extern int simple_nosetlease(struct file *, int, struct file_lease **, void **);
1da177e4 3218
00cd8dd3 3219extern struct dentry *simple_lookup(struct inode *, struct dentry *, unsigned int flags);
1da177e4 3220extern ssize_t generic_read_dir(struct file *, char __user *, size_t, loff_t *);
4b6f5d20 3221extern const struct file_operations simple_dir_operations;
c5ef1c42 3222extern const struct inode_operations simple_dir_inode_operations;
fbabfd0f
EB
3223extern void make_empty_dir_inode(struct inode *inode);
3224extern bool is_empty_dir_inode(struct inode *inode);
cda37124 3225struct tree_descr { const char *name; const struct file_operations *ops; int mode; };
1da177e4 3226struct dentry *d_alloc_name(struct dentry *, const char *);
cda37124
EB
3227extern int simple_fill_super(struct super_block *, unsigned long,
3228 const struct tree_descr *);
1f5ce9e9 3229extern int simple_pin_fs(struct file_system_type *, struct vfsmount **mount, int *count);
1da177e4 3230extern void simple_release_fs(struct vfsmount **mount, int *count);
59200f45 3231struct dentry *simple_start_creating(struct dentry *, const char *);
1da177e4 3232
93b07113
AM
3233extern ssize_t simple_read_from_buffer(void __user *to, size_t count,
3234 loff_t *ppos, const void *from, size_t available);
6a727b43
JS
3235extern ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
3236 const void __user *from, size_t count);
1da177e4 3237
6faddda6 3238struct offset_ctx {
0e4a8621
CL
3239 struct maple_tree mt;
3240 unsigned long next_offset;
6faddda6
CL
3241};
3242
3243void simple_offset_init(struct offset_ctx *octx);
3244int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry);
3245void simple_offset_remove(struct offset_ctx *octx, struct dentry *dentry);
5a1a25be
CL
3246int simple_offset_rename(struct inode *old_dir, struct dentry *old_dentry,
3247 struct inode *new_dir, struct dentry *new_dentry);
6faddda6
CL
3248int simple_offset_rename_exchange(struct inode *old_dir,
3249 struct dentry *old_dentry,
3250 struct inode *new_dir,
3251 struct dentry *new_dentry);
3252void simple_offset_destroy(struct offset_ctx *octx);
3253
3254extern const struct file_operations simple_offset_dir_operations;
3255
ac13a829 3256extern int __generic_file_fsync(struct file *, loff_t, loff_t, int);
02c24a82 3257extern int generic_file_fsync(struct file *, loff_t, loff_t, int);
d5aacad5 3258
30ca22c7
PL
3259extern int generic_check_addressable(unsigned, u64);
3260
70dfe3f0 3261extern void generic_set_sb_d_ops(struct super_block *sb);
6a79a4e1
GKB
3262extern int generic_ci_match(const struct inode *parent,
3263 const struct qstr *name,
3264 const struct qstr *folded_name,
3265 const u8 *de_name, u32 de_name_len);
c843843e 3266
0e152beb 3267#if IS_ENABLED(CONFIG_UNICODE)
458532c8
AA
3268int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str);
3269int generic_ci_d_compare(const struct dentry *dentry, unsigned int len,
3270 const char *str, const struct qstr *name);
3271
0e152beb
AA
3272/**
3273 * generic_ci_validate_strict_name - Check if a given name is suitable
3274 * for a directory
3275 *
3276 * This functions checks if the proposed filename is valid for the
3277 * parent directory. That means that only valid UTF-8 filenames will be
3278 * accepted for casefold directories from filesystems created with the
3279 * strict encoding flag. That also means that any name will be
3280 * accepted for directories that doesn't have casefold enabled, or
3281 * aren't being strict with the encoding.
3282 *
3283 * @dir: inode of the directory where the new file will be created
3284 * @name: name of the new file
3285 *
3286 * Return:
33b091c0
AA
3287 * * True: if the filename is suitable for this directory. It can be
3288 * true if a given name is not suitable for a strict encoding
3289 * directory, but the directory being used isn't strict
0e152beb 3290 * * False if the filename isn't suitable for this directory. This only
33b091c0
AA
3291 * happens when a directory is casefolded and the filesystem is strict
3292 * about its encoding.
0e152beb 3293 */
ca97d6c6
AV
3294static inline bool generic_ci_validate_strict_name(struct inode *dir,
3295 const struct qstr *name)
0e152beb
AA
3296{
3297 if (!IS_CASEFOLDED(dir) || !sb_has_strict_encoding(dir->i_sb))
3298 return true;
3299
3300 /*
3301 * A casefold dir must have a encoding set, unless the filesystem
3302 * is corrupted
3303 */
3304 if (WARN_ON_ONCE(!dir->i_sb->s_encoding))
3305 return true;
3306
3307 return !utf8_validate(dir->i_sb->s_encoding, name);
3308}
3309#else
ca97d6c6
AV
3310static inline bool generic_ci_validate_strict_name(struct inode *dir,
3311 const struct qstr *name)
0e152beb
AA
3312{
3313 return true;
3314}
3315#endif
3316
4609e1f1 3317int may_setattr(struct mnt_idmap *idmap, struct inode *inode,
7bb698f0 3318 unsigned int ia_valid);
c1632a0f 3319int setattr_prepare(struct mnt_idmap *, struct dentry *, struct iattr *);
25d9e2d1 3320extern int inode_newsize_ok(const struct inode *, loff_t offset);
c1632a0f 3321void setattr_copy(struct mnt_idmap *, struct inode *inode,
2f221d6f 3322 const struct iattr *attr);
1da177e4 3323
c3b2da31 3324extern int file_update_time(struct file *file);
1da177e4 3325
de195c67
LS
3326static inline bool file_is_dax(const struct file *file)
3327{
3328 return file && IS_DAX(file->f_mapping->host);
3329}
3330
f05a3849 3331static inline bool vma_is_dax(const struct vm_area_struct *vma)
baabda26 3332{
de195c67 3333 return file_is_dax(vma->vm_file);
baabda26
DW
3334}
3335
2bb6d283
DW
3336static inline bool vma_is_fsdax(struct vm_area_struct *vma)
3337{
3338 struct inode *inode;
3339
52650c8b 3340 if (!IS_ENABLED(CONFIG_FS_DAX) || !vma->vm_file)
2bb6d283
DW
3341 return false;
3342 if (!vma_is_dax(vma))
3343 return false;
3344 inode = file_inode(vma->vm_file);
230f5a89 3345 if (S_ISCHR(inode->i_mode))
2bb6d283
DW
3346 return false; /* device-dax */
3347 return true;
3348}
3349
2ba48ce5
AV
3350static inline int iocb_flags(struct file *file)
3351{
3352 int res = 0;
3353 if (file->f_flags & O_APPEND)
3354 res |= IOCB_APPEND;
efbe3c24 3355 if (file->f_flags & O_DIRECT)
2ba48ce5 3356 res |= IOCB_DIRECT;
91b94c5d 3357 if (file->f_flags & O_DSYNC)
dde0c2e7
CH
3358 res |= IOCB_DSYNC;
3359 if (file->f_flags & __O_SYNC)
3360 res |= IOCB_SYNC;
2ba48ce5
AV
3361 return res;
3362}
3363
c34fc6f2
PS
3364static inline int kiocb_set_rw_flags(struct kiocb *ki, rwf_t flags,
3365 int rw_type)
fdd2f5b7 3366{
1752f0ad
PB
3367 int kiocb_flags = 0;
3368
ce71bfea
JA
3369 /* make sure there's no overlap between RWF and private IOCB flags */
3370 BUILD_BUG_ON((__force int) RWF_SUPPORTED & IOCB_EVENTFD);
3371
1752f0ad
PB
3372 if (!flags)
3373 return 0;
fdd2f5b7
GR
3374 if (unlikely(flags & ~RWF_SUPPORTED))
3375 return -EOPNOTSUPP;
73fa7547
RF
3376 if (unlikely((flags & RWF_APPEND) && (flags & RWF_NOAPPEND)))
3377 return -EINVAL;
fdd2f5b7 3378
b745fafa 3379 if (flags & RWF_NOWAIT) {
91f9943e 3380 if (!(ki->ki_filp->f_mode & FMODE_NOWAIT))
b745fafa 3381 return -EOPNOTSUPP;
b745fafa 3382 }
c34fc6f2
PS
3383 if (flags & RWF_ATOMIC) {
3384 if (rw_type != WRITE)
3385 return -EOPNOTSUPP;
3386 if (!(ki->ki_filp->f_mode & FMODE_CAN_ATOMIC_WRITE))
3387 return -EOPNOTSUPP;
3388 }
af6505e5
JA
3389 if (flags & RWF_DONTCACHE) {
3390 /* file system must support it */
3391 if (!(ki->ki_filp->f_op->fop_flags & FOP_DONTCACHE))
3392 return -EOPNOTSUPP;
3393 /* DAX mappings not supported */
3394 if (IS_DAX(ki->ki_filp->f_mapping->host))
3395 return -EOPNOTSUPP;
3396 }
ce71bfea 3397 kiocb_flags |= (__force int) (flags & RWF_SUPPORTED);
fdd2f5b7 3398 if (flags & RWF_SYNC)
ce71bfea 3399 kiocb_flags |= IOCB_DSYNC;
1752f0ad 3400
73fa7547
RF
3401 if ((flags & RWF_NOAPPEND) && (ki->ki_flags & IOCB_APPEND)) {
3402 if (IS_APPEND(file_inode(ki->ki_filp)))
3403 return -EPERM;
3404 ki->ki_flags &= ~IOCB_APPEND;
3405 }
3406
1752f0ad 3407 ki->ki_flags |= kiocb_flags;
fdd2f5b7
GR
3408 return 0;
3409}
3410
1da177e4
LT
3411/* Transaction based IO helpers */
3412
3413/*
3414 * An argresp is stored in an allocated page and holds the
3415 * size of the argument or response, along with its content
3416 */
3417struct simple_transaction_argresp {
3418 ssize_t size;
5e01fdff 3419 char data[];
1da177e4
LT
3420};
3421
3422#define SIMPLE_TRANSACTION_LIMIT (PAGE_SIZE - sizeof(struct simple_transaction_argresp))
3423
3424char *simple_transaction_get(struct file *file, const char __user *buf,
3425 size_t size);
3426ssize_t simple_transaction_read(struct file *file, char __user *buf,
3427 size_t size, loff_t *pos);
3428int simple_transaction_release(struct inode *inode, struct file *file);
3429
76791ab2 3430void simple_transaction_set(struct file *file, size_t n);
1da177e4 3431
acaefc25
AB
3432/*
3433 * simple attribute files
3434 *
3435 * These attributes behave similar to those in sysfs:
3436 *
3437 * Writing to an attribute immediately sets a value, an open file can be
3438 * written to multiple times.
3439 *
3440 * Reading from an attribute creates a buffer from the value that might get
3441 * read with multiple read calls. When the attribute has been read
3442 * completely, no further read calls are possible until the file is opened
3443 * again.
3444 *
3445 * All attributes contain a text representation of a numeric value
3446 * that are accessed with the get() and set() functions.
3447 */
2e41f274 3448#define DEFINE_SIMPLE_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, __is_signed) \
acaefc25
AB
3449static int __fops ## _open(struct inode *inode, struct file *file) \
3450{ \
3451 __simple_attr_check_format(__fmt, 0ull); \
3452 return simple_attr_open(inode, file, __get, __set, __fmt); \
3453} \
828c0950 3454static const struct file_operations __fops = { \
acaefc25
AB
3455 .owner = THIS_MODULE, \
3456 .open = __fops ## _open, \
74bedc4d 3457 .release = simple_attr_release, \
acaefc25 3458 .read = simple_attr_read, \
2e41f274 3459 .write = (__is_signed) ? simple_attr_write_signed : simple_attr_write, \
1ec5584e 3460 .llseek = generic_file_llseek, \
68be3029 3461}
acaefc25 3462
2e41f274
AM
3463#define DEFINE_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt) \
3464 DEFINE_SIMPLE_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, false)
3465
3466#define DEFINE_SIMPLE_ATTRIBUTE_SIGNED(__fops, __get, __set, __fmt) \
3467 DEFINE_SIMPLE_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, true)
3468
b9075fa9
JP
3469static inline __printf(1, 2)
3470void __simple_attr_check_format(const char *fmt, ...)
acaefc25
AB
3471{
3472 /* don't do anything, just let the compiler check the arguments; */
3473}
3474
3475int simple_attr_open(struct inode *inode, struct file *file,
8b88b099 3476 int (*get)(void *, u64 *), int (*set)(void *, u64),
acaefc25 3477 const char *fmt);
74bedc4d 3478int simple_attr_release(struct inode *inode, struct file *file);
acaefc25
AB
3479ssize_t simple_attr_read(struct file *file, char __user *buf,
3480 size_t len, loff_t *ppos);
3481ssize_t simple_attr_write(struct file *file, const char __user *buf,
3482 size_t len, loff_t *ppos);
2e41f274
AM
3483ssize_t simple_attr_write_signed(struct file *file, const char __user *buf,
3484 size_t len, loff_t *ppos);
acaefc25 3485
4be28540 3486struct ctl_table;
6e7c1770 3487int __init list_bdev_fs_names(char *buf, size_t size);
62239ac2 3488
3cd90ea4 3489#define __FMODE_EXEC ((__force int) FMODE_EXEC)
1a44bc8c 3490
6d125529 3491#define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
ebe55960 3492#define OPEN_FMODE(flag) ((__force fmode_t)((flag + 1) & O_ACCMODE))
5300990c 3493
d37177ba 3494static inline bool is_sxid(umode_t mode)
69b45732 3495{
8d84e39d 3496 return mode & (S_ISUID | S_ISGID);
69b45732
AK
3497}
3498
9452e93e 3499static inline int check_sticky(struct mnt_idmap *idmap,
ba73d987 3500 struct inode *dir, struct inode *inode)
cbdf35bc
MS
3501{
3502 if (!(dir->i_mode & S_ISVTX))
3503 return 0;
3504
9452e93e 3505 return __check_sticky(idmap, dir, inode);
cbdf35bc
MS
3506}
3507
69b45732
AK
3508static inline void inode_has_no_xattr(struct inode *inode)
3509{
e462ec50 3510 if (!is_sxid(inode->i_mode) && (inode->i_sb->s_flags & SB_NOSEC))
69b45732
AK
3511 inode->i_flags |= S_NOSEC;
3512}
3513
a7400222
AV
3514static inline bool is_root_inode(struct inode *inode)
3515{
3516 return inode == inode->i_sb->s_root->d_inode;
3517}
3518
5f99f4e7
AV
3519static inline bool dir_emit(struct dir_context *ctx,
3520 const char *name, int namelen,
3521 u64 ino, unsigned type)
3522{
25885a35 3523 return ctx->actor(ctx, name, namelen, ctx->pos, ino, type);
5f99f4e7
AV
3524}
3525static inline bool dir_emit_dot(struct file *file, struct dir_context *ctx)
3526{
3527 return ctx->actor(ctx, ".", 1, ctx->pos,
25885a35 3528 file->f_path.dentry->d_inode->i_ino, DT_DIR);
5f99f4e7
AV
3529}
3530static inline bool dir_emit_dotdot(struct file *file, struct dir_context *ctx)
3531{
3532 return ctx->actor(ctx, "..", 2, ctx->pos,
f378ec4e 3533 d_parent_ino(file->f_path.dentry), DT_DIR);
5f99f4e7
AV
3534}
3535static inline bool dir_emit_dots(struct file *file, struct dir_context *ctx)
3536{
3537 if (ctx->pos == 0) {
3538 if (!dir_emit_dot(file, ctx))
3539 return false;
3540 ctx->pos = 1;
3541 }
3542 if (ctx->pos == 1) {
3543 if (!dir_emit_dotdot(file, ctx))
3544 return false;
3545 ctx->pos = 2;
3546 }
3547 return true;
3548}
5ded75ec
AV
3549static inline bool dir_relax(struct inode *inode)
3550{
5955102c
AV
3551 inode_unlock(inode);
3552 inode_lock(inode);
5ded75ec
AV
3553 return !IS_DEADDIR(inode);
3554}
5f99f4e7 3555
ae05327a
AV
3556static inline bool dir_relax_shared(struct inode *inode)
3557{
3558 inode_unlock_shared(inode);
3559 inode_lock_shared(inode);
3560 return !IS_DEADDIR(inode);
3561}
3562
90f8572b 3563extern bool path_noexec(const struct path *path);
21fc61c7 3564extern void inode_nohighmem(struct inode *inode);
90f8572b 3565
45cd0faa
AG
3566/* mm/fadvise.c */
3567extern int vfs_fadvise(struct file *file, loff_t offset, loff_t len,
3568 int advice);
cf1ea059
JK
3569extern int generic_fadvise(struct file *file, loff_t offset, loff_t len,
3570 int advice);
45cd0faa 3571
1bc6d445
CB
3572static inline bool vfs_empty_path(int dfd, const char __user *path)
3573{
3574 char c;
3575
3576 if (dfd < 0)
3577 return false;
3578
3579 /* We now allow NULL to be used for empty path. */
3580 if (!path)
3581 return true;
3582
3583 if (unlikely(get_user(c, path)))
3584 return false;
3585
3586 return !c;
3587}
3588
c3be7ebb 3589int generic_atomic_write_valid(struct kiocb *iocb, struct iov_iter *iter);
c34fc6f2 3590
3c17001b
CB
3591static inline bool extensible_ioctl_valid(unsigned int cmd_a,
3592 unsigned int cmd_b, size_t min_size)
3593{
3594 if (_IOC_DIR(cmd_a) != _IOC_DIR(cmd_b))
3595 return false;
3596 if (_IOC_TYPE(cmd_a) != _IOC_TYPE(cmd_b))
3597 return false;
3598 if (_IOC_NR(cmd_a) != _IOC_NR(cmd_b))
3599 return false;
3600 if (_IOC_SIZE(cmd_a) < min_size)
3601 return false;
3602 return true;
3603}
3604
1da177e4 3605#endif /* _LINUX_FS_H */