1 #ifndef _LINUX_FS_H
2 #define _LINUX_FS_H
3
4 #include <linux/linkage.h>
5 #include <linux/wait.h>
6 #include <linux/kdev_t.h>
7 #include <linux/dcache.h>
8 #include <linux/path.h>
9 #include <linux/stat.h>
10 #include <linux/cache.h>
11 #include <linux/list.h>
12 #include <linux/list_lru.h>
13 #include <linux/llist.h>
14 #include <linux/radix-tree.h>
15 #include <linux/rbtree.h>
16 #include <linux/init.h>
17 #include <linux/pid.h>
18 #include <linux/bug.h>
19 #include <linux/mutex.h>
20 #include <linux/rwsem.h>
21 #include <linux/mm_types.h>
22 #include <linux/capability.h>
23 #include <linux/semaphore.h>
24 #include <linux/fcntl.h>
25 #include <linux/fiemap.h>
26 #include <linux/rculist_bl.h>
27 #include <linux/atomic.h>
28 #include <linux/shrinker.h>
29 #include <linux/migrate_mode.h>
30 #include <linux/uidgid.h>
31 #include <linux/lockdep.h>
32 #include <linux/percpu-rwsem.h>
33 #include <linux/blk_types.h>
34 #include <linux/workqueue.h>
35 #include <linux/percpu-rwsem.h>
36 #include <linux/delayed_call.h>
37
38 #include <asm/byteorder.h>
39 #include <uapi/linux/fs.h>
40
41 struct backing_dev_info;
42 struct bdi_writeback;
43 struct export_operations;
44 struct hd_geometry;
45 struct iovec;
46 struct kiocb;
47 struct kobject;
48 struct pipe_inode_info;
49 struct poll_table_struct;
50 struct kstatfs;
51 struct vm_area_struct;
52 struct vfsmount;
53 struct cred;
54 struct swap_info_struct;
55 struct seq_file;
56 struct workqueue_struct;
57 struct iov_iter;
58 struct fscrypt_info;
59 struct fscrypt_operations;
60
61 extern void __init inode_init(void);
62 extern void __init inode_init_early(void);
63 extern void __init files_init(void);
64 extern void __init files_maxfiles_init(void);
65
66 extern struct files_stat_struct files_stat;
67 extern unsigned long get_max_files(void);
68 extern unsigned int sysctl_nr_open;
69 extern struct inodes_stat_t inodes_stat;
70 extern int leases_enable, lease_break_time;
71 extern int sysctl_protected_symlinks;
72 extern int sysctl_protected_hardlinks;
73
74 struct buffer_head;
75 typedef int (get_block_t)(struct inode *inode, sector_t iblock,
76 struct buffer_head *bh_result, int create);
77 typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,
78 ssize_t bytes, void *private);
79
80 #define MAY_EXEC 0x00000001
81 #define MAY_WRITE 0x00000002
82 #define MAY_READ 0x00000004
83 #define MAY_APPEND 0x00000008
84 #define MAY_ACCESS 0x00000010
85 #define MAY_OPEN 0x00000020
86 #define MAY_CHDIR 0x00000040
87 /* called from RCU mode, don't block */
88 #define MAY_NOT_BLOCK 0x00000080
89
90 /*
91 * flags in file.f_mode. Note that FMODE_READ and FMODE_WRITE must correspond
92 * to O_WRONLY and O_RDWR via the strange trick in __dentry_open()
93 */
94
95 /* file is open for reading */
96 #define FMODE_READ ((__force fmode_t)0x1)
97 /* file is open for writing */
98 #define FMODE_WRITE ((__force fmode_t)0x2)
99 /* file is seekable */
100 #define FMODE_LSEEK ((__force fmode_t)0x4)
101 /* file can be accessed using pread */
102 #define FMODE_PREAD ((__force fmode_t)0x8)
103 /* file can be accessed using pwrite */
104 #define FMODE_PWRITE ((__force fmode_t)0x10)
105 /* File is opened for execution with sys_execve / sys_uselib */
106 #define FMODE_EXEC ((__force fmode_t)0x20)
107 /* File is opened with O_NDELAY (only set for block devices) */
108 #define FMODE_NDELAY ((__force fmode_t)0x40)
109 /* File is opened with O_EXCL (only set for block devices) */
110 #define FMODE_EXCL ((__force fmode_t)0x80)
111 /* File is opened using open(.., 3, ..) and is writeable only for ioctls
112 (specialy hack for floppy.c) */
113 #define FMODE_WRITE_IOCTL ((__force fmode_t)0x100)
114 /* 32bit hashes as llseek() offset (for directories) */
115 #define FMODE_32BITHASH ((__force fmode_t)0x200)
116 /* 64bit hashes as llseek() offset (for directories) */
117 #define FMODE_64BITHASH ((__force fmode_t)0x400)
118
119 /*
120 * Don't update ctime and mtime.
121 *
122 * Currently a special hack for the XFS open_by_handle ioctl, but we'll
123 * hopefully graduate it to a proper O_CMTIME flag supported by open(2) soon.
124 */
125 #define FMODE_NOCMTIME ((__force fmode_t)0x800)
126
127 /* Expect random access pattern */
128 #define FMODE_RANDOM ((__force fmode_t)0x1000)
129
130 /* File is huge (eg. /dev/kmem): treat loff_t as unsigned */
131 #define FMODE_UNSIGNED_OFFSET ((__force fmode_t)0x2000)
132
133 /* File is opened with O_PATH; almost nothing can be done with it */
134 #define FMODE_PATH ((__force fmode_t)0x4000)
135
136 /* File needs atomic accesses to f_pos */
137 #define FMODE_ATOMIC_POS ((__force fmode_t)0x8000)
138 /* Write access to underlying fs */
139 #define FMODE_WRITER ((__force fmode_t)0x10000)
140 /* Has read method(s) */
141 #define FMODE_CAN_READ ((__force fmode_t)0x20000)
142 /* Has write method(s) */
143 #define FMODE_CAN_WRITE ((__force fmode_t)0x40000)
144
145 /* File was opened by fanotify and shouldn't generate fanotify events */
146 #define FMODE_NONOTIFY ((__force fmode_t)0x4000000)
147
148 /* File is capable of returning -EAGAIN if I/O will block */
149 #define FMODE_NOWAIT ((__force fmode_t)0x8000000)
150
151 /*
152 * Flag for rw_copy_check_uvector and compat_rw_copy_check_uvector
153 * that indicates that they should check the contents of the iovec are
154 * valid, but not check the memory that the iovec elements
155 * points too.
156 */
157 #define CHECK_IOVEC_ONLY -1
158
159 /*
160 * The below are the various read and write flags that we support. Some of
161 * them include behavioral modifiers that send information down to the
162 * block layer and IO scheduler. They should be used along with a req_op.
163 * Terminology:
164 *
165 * The block layer uses device plugging to defer IO a little bit, in
166 * the hope that we will see more IO very shortly. This increases
167 * coalescing of adjacent IO and thus reduces the number of IOs we
168 * have to send to the device. It also allows for better queuing,
169 * if the IO isn't mergeable. If the caller is going to be waiting
170 * for the IO, then he must ensure that the device is unplugged so
171 * that the IO is dispatched to the driver.
172 *
173 * All IO is handled async in Linux. This is fine for background
174 * writes, but for reads or writes that someone waits for completion
175 * on, we want to notify the block layer and IO scheduler so that they
176 * know about it. That allows them to make better scheduling
177 * decisions. So when the below references 'sync' and 'async', it
178 * is referencing this priority hint.
179 *
180 * With that in mind, the available types are:
181 *
182 * READ A normal read operation. Device will be plugged.
183 * READ_SYNC A synchronous read. Device is not plugged, caller can
184 * immediately wait on this read without caring about
185 * unplugging.
186 * WRITE A normal async write. Device will be plugged.
187 * WRITE_SYNC Synchronous write. Identical to WRITE, but passes down
188 * the hint that someone will be waiting on this IO
189 * shortly. The write equivalent of READ_SYNC.
190 * WRITE_ODIRECT Special case write for O_DIRECT only.
191 * WRITE_FLUSH Like WRITE_SYNC but with preceding cache flush.
192 * WRITE_FUA Like WRITE_SYNC but data is guaranteed to be on
193 * non-volatile media on completion.
194 * WRITE_FLUSH_FUA Combination of WRITE_FLUSH and FUA. The IO is preceded
195 * by a cache flush and data is guaranteed to be on
196 * non-volatile media on completion.
197 *
198 */
199 #define RW_MASK REQ_OP_WRITE
200
201 #define READ REQ_OP_READ
202 #define WRITE REQ_OP_WRITE
203
204 #define READ_SYNC REQ_SYNC
205 #define WRITE_SYNC (REQ_SYNC | REQ_NOIDLE)
206 #define WRITE_ODIRECT REQ_SYNC
207 #define WRITE_FLUSH (REQ_SYNC | REQ_NOIDLE | REQ_PREFLUSH)
208 #define WRITE_FUA (REQ_SYNC | REQ_NOIDLE | REQ_FUA)
209 #define WRITE_FLUSH_FUA (REQ_SYNC | REQ_NOIDLE | REQ_PREFLUSH | REQ_FUA)
210
211 /*
212 * Attribute flags. These should be or-ed together to figure out what
213 * has been changed!
214 */
215 #define ATTR_MODE (1 << 0)
216 #define ATTR_UID (1 << 1)
217 #define ATTR_GID (1 << 2)
218 #define ATTR_SIZE (1 << 3)
219 #define ATTR_ATIME (1 << 4)
220 #define ATTR_MTIME (1 << 5)
221 #define ATTR_CTIME (1 << 6)
222 #define ATTR_ATIME_SET (1 << 7)
223 #define ATTR_MTIME_SET (1 << 8)
224 #define ATTR_FORCE (1 << 9) /* Not a change, but a change it */
225 #define ATTR_ATTR_FLAG (1 << 10)
226 #define ATTR_KILL_SUID (1 << 11)
227 #define ATTR_KILL_SGID (1 << 12)
228 #define ATTR_FILE (1 << 13)
229 #define ATTR_KILL_PRIV (1 << 14)
230 #define ATTR_OPEN (1 << 15) /* Truncating from open(O_TRUNC) */
231 #define ATTR_TIMES_SET (1 << 16)
232 #define ATTR_TOUCH (1 << 17)
233
234 /*
235 * Whiteout is represented by a char device. The following constants define the
236 * mode and device number to use.
237 */
238 #define WHITEOUT_MODE 0
239 #define WHITEOUT_DEV 0
240
241 /*
242 * This is the Inode Attributes structure, used for notify_change(). It
243 * uses the above definitions as flags, to know which values have changed.
244 * Also, in this manner, a Filesystem can look at only the values it cares
245 * about. Basically, these are the attributes that the VFS layer can
246 * request to change from the FS layer.
247 *
248 * Derek Atkins <warlord@MIT.EDU> 94-10-20
249 */
250 struct iattr {
251 unsigned int ia_valid;
252 umode_t ia_mode;
253 kuid_t ia_uid;
254 kgid_t ia_gid;
255 loff_t ia_size;
256 struct timespec ia_atime;
257 struct timespec ia_mtime;
258 struct timespec ia_ctime;
259
260 /*
261 * Not an attribute, but an auxiliary info for filesystems wanting to
262 * implement an ftruncate() like method. NOTE: filesystem should
263 * check for (ia_valid & ATTR_FILE), and not for (ia_file != NULL).
264 */
265 struct file *ia_file;
266 };
267
268 /*
269 * Includes for diskquotas.
270 */
271 #include <linux/quota.h>
272
273 /*
274 * Maximum number of layers of fs stack. Needs to be limited to
275 * prevent kernel stack overflow
276 */
277 #define FILESYSTEM_MAX_STACK_DEPTH 2
278
279 /**
280 * enum positive_aop_returns - aop return codes with specific semantics
281 *
282 * @AOP_WRITEPAGE_ACTIVATE: Informs the caller that page writeback has
283 * completed, that the page is still locked, and
284 * should be considered active. The VM uses this hint
285 * to return the page to the active list -- it won't
286 * be a candidate for writeback again in the near
287 * future. Other callers must be careful to unlock
288 * the page if they get this return. Returned by
289 * writepage();
290 *
291 * @AOP_TRUNCATED_PAGE: The AOP method that was handed a locked page has
292 * unlocked it and the page might have been truncated.
293 * The caller should back up to acquiring a new page and
294 * trying again. The aop will be taking reasonable
295 * precautions not to livelock. If the caller held a page
296 * reference, it should drop it before retrying. Returned
297 * by readpage().
298 *
299 * address_space_operation functions return these large constants to indicate
300 * special semantics to the caller. These are much larger than the bytes in a
301 * page to allow for functions that return the number of bytes operated on in a
302 * given page.
303 */
304
305 enum positive_aop_returns {
306 AOP_WRITEPAGE_ACTIVATE = 0x80000,
307 AOP_TRUNCATED_PAGE = 0x80001,
308 };
309
310 #define AOP_FLAG_UNINTERRUPTIBLE 0x0001 /* will not do a short write */
311 #define AOP_FLAG_CONT_EXPAND 0x0002 /* called from cont_expand */
312 #define AOP_FLAG_NOFS 0x0004 /* used by filesystem to direct
313 * helper code (eg buffer layer)
314 * to clear GFP_FS from alloc */
315
316 /*
317 * oh the beauties of C type declarations.
318 */
319 struct page;
320 struct address_space;
321 struct writeback_control;
322
323 /*
324 * Write life time hint values.
325 */
326 enum rw_hint {
327 WRITE_LIFE_NOT_SET = 0,
328 WRITE_LIFE_NONE = RWH_WRITE_LIFE_NONE,
329 WRITE_LIFE_SHORT = RWH_WRITE_LIFE_SHORT,
330 WRITE_LIFE_MEDIUM = RWH_WRITE_LIFE_MEDIUM,
331 WRITE_LIFE_LONG = RWH_WRITE_LIFE_LONG,
332 WRITE_LIFE_EXTREME = RWH_WRITE_LIFE_EXTREME,
333 };
334
335 #define IOCB_EVENTFD (1 << 0)
336 #define IOCB_APPEND (1 << 1)
337 #define IOCB_DIRECT (1 << 2)
338 #define IOCB_HIPRI (1 << 3)
339 #define IOCB_DSYNC (1 << 4)
340 #define IOCB_SYNC (1 << 5)
341 #define IOCB_WRITE (1 << 6)
342 #define IOCB_NOWAIT (1 << 7)
343
344 struct kiocb {
345 struct file *ki_filp;
346 loff_t ki_pos;
347 void (*ki_complete)(struct kiocb *iocb, long ret, long ret2);
348 void *private;
349 int ki_flags;
350 enum rw_hint ki_hint;
351 };
352
is_sync_kiocb(struct kiocb * kiocb)353 static inline bool is_sync_kiocb(struct kiocb *kiocb)
354 {
355 return kiocb->ki_complete == NULL;
356 }
357
358 static inline int iocb_flags(struct file *file);
359
init_sync_kiocb(struct kiocb * kiocb,struct file * filp)360 static inline void init_sync_kiocb(struct kiocb *kiocb, struct file *filp)
361 {
362 *kiocb = (struct kiocb) {
363 .ki_filp = filp,
364 .ki_flags = iocb_flags(filp),
365 };
366 }
367
368 /*
369 * "descriptor" for what we're up to with a read.
370 * This allows us to use the same read code yet
371 * have multiple different users of the data that
372 * we read from a file.
373 *
374 * The simplest case just copies the data to user
375 * mode.
376 */
377 typedef struct {
378 size_t written;
379 size_t count;
380 union {
381 char __user *buf;
382 void *data;
383 } arg;
384 int error;
385 } read_descriptor_t;
386
387 typedef int (*read_actor_t)(read_descriptor_t *, struct page *,
388 unsigned long, unsigned long);
389
390 struct address_space_operations {
391 int (*writepage)(struct page *page, struct writeback_control *wbc);
392 int (*readpage)(struct file *, struct page *);
393
394 /* Write back some dirty pages from this mapping. */
395 int (*writepages)(struct address_space *, struct writeback_control *);
396
397 /* Set a page dirty. Return true if this dirtied it */
398 int (*set_page_dirty)(struct page *page);
399
400 int (*readpages)(struct file *filp, struct address_space *mapping,
401 struct list_head *pages, unsigned nr_pages);
402
403 int (*write_begin)(struct file *, struct address_space *mapping,
404 loff_t pos, unsigned len, unsigned flags,
405 struct page **pagep, void **fsdata);
406 int (*write_end)(struct file *, struct address_space *mapping,
407 loff_t pos, unsigned len, unsigned copied,
408 struct page *page, void *fsdata);
409
410 /* Unfortunately this kludge is needed for FIBMAP. Don't use it */
411 sector_t (*bmap)(struct address_space *, sector_t);
412 void (*invalidatepage) (struct page *, unsigned int, unsigned int);
413 int (*releasepage) (struct page *, gfp_t);
414 void (*freepage)(struct page *);
415 ssize_t (*direct_IO)(struct kiocb *, struct iov_iter *iter);
416 /*
417 * migrate the contents of a page to the specified target. If
418 * migrate_mode is MIGRATE_ASYNC, it must not block.
419 */
420 int (*migratepage) (struct address_space *,
421 struct page *, struct page *, enum migrate_mode);
422 bool (*isolate_page)(struct page *, isolate_mode_t);
423 void (*putback_page)(struct page *);
424 int (*launder_page) (struct page *);
425 int (*is_partially_uptodate) (struct page *, unsigned long,
426 unsigned long);
427 void (*is_dirty_writeback) (struct page *, bool *, bool *);
428 int (*error_remove_page)(struct address_space *, struct page *);
429
430 /* swapfile support */
431 int (*swap_activate)(struct swap_info_struct *sis, struct file *file,
432 sector_t *span);
433 void (*swap_deactivate)(struct file *file);
434 };
435
436 extern const struct address_space_operations empty_aops;
437
438 /*
439 * pagecache_write_begin/pagecache_write_end must be used by general code
440 * to write into the pagecache.
441 */
442 int pagecache_write_begin(struct file *, struct address_space *mapping,
443 loff_t pos, unsigned len, unsigned flags,
444 struct page **pagep, void **fsdata);
445
446 int pagecache_write_end(struct file *, struct address_space *mapping,
447 loff_t pos, unsigned len, unsigned copied,
448 struct page *page, void *fsdata);
449
450 struct address_space {
451 struct inode *host; /* owner: inode, block_device */
452 struct radix_tree_root page_tree; /* radix tree of all pages */
453 spinlock_t tree_lock; /* and lock protecting it */
454 atomic_t i_mmap_writable;/* count VM_SHARED mappings */
455 struct rb_root i_mmap; /* tree of private and shared mappings */
456 struct rw_semaphore i_mmap_rwsem; /* protect tree, count, list */
457 /* Protected by tree_lock together with the radix tree */
458 unsigned long nrpages; /* number of total pages */
459 /* number of shadow or DAX exceptional entries */
460 unsigned long nrexceptional;
461 pgoff_t writeback_index;/* writeback starts here */
462 const struct address_space_operations *a_ops; /* methods */
463 unsigned long flags; /* error bits */
464 spinlock_t private_lock; /* for use by the address_space */
465 gfp_t gfp_mask; /* implicit gfp mask for allocations */
466 struct list_head private_list; /* ditto */
467 void *private_data; /* ditto */
468 } __attribute__((aligned(sizeof(long))));
469 /*
470 * On most architectures that alignment is already the case; but
471 * must be enforced here for CRIS, to let the least significant bit
472 * of struct page's "mapping" pointer be used for PAGE_MAPPING_ANON.
473 */
474 struct request_queue;
475
476 struct block_device {
477 dev_t bd_dev; /* not a kdev_t - it's a search key */
478 int bd_openers;
479 struct inode * bd_inode; /* will die */
480 struct super_block * bd_super;
481 struct mutex bd_mutex; /* open/close mutex */
482 void * bd_claiming;
483 void * bd_holder;
484 int bd_holders;
485 bool bd_write_holder;
486 #ifdef CONFIG_SYSFS
487 struct list_head bd_holder_disks;
488 #endif
489 struct block_device * bd_contains;
490 unsigned bd_block_size;
491 struct hd_struct * bd_part;
492 /* number of times partitions within this device have been opened. */
493 unsigned bd_part_count;
494 int bd_invalidated;
495 struct gendisk * bd_disk;
496 struct request_queue * bd_queue;
497 struct list_head bd_list;
498 /*
499 * Private data. You must have bd_claim'ed the block_device
500 * to use this. NOTE: bd_claim allows an owner to claim
501 * the same device multiple times, the owner must take special
502 * care to not mess up bd_private for that case.
503 */
504 unsigned long bd_private;
505
506 /* The counter of freeze processes */
507 int bd_fsfreeze_count;
508 /* Mutex for freeze */
509 struct mutex bd_fsfreeze_mutex;
510 };
511
512 /*
513 * Radix-tree tags, for tagging dirty and writeback pages within the pagecache
514 * radix trees
515 */
516 #define PAGECACHE_TAG_DIRTY 0
517 #define PAGECACHE_TAG_WRITEBACK 1
518 #define PAGECACHE_TAG_TOWRITE 2
519
520 int mapping_tagged(struct address_space *mapping, int tag);
521
i_mmap_lock_write(struct address_space * mapping)522 static inline void i_mmap_lock_write(struct address_space *mapping)
523 {
524 down_write(&mapping->i_mmap_rwsem);
525 }
526
i_mmap_unlock_write(struct address_space * mapping)527 static inline void i_mmap_unlock_write(struct address_space *mapping)
528 {
529 up_write(&mapping->i_mmap_rwsem);
530 }
531
i_mmap_lock_read(struct address_space * mapping)532 static inline void i_mmap_lock_read(struct address_space *mapping)
533 {
534 down_read(&mapping->i_mmap_rwsem);
535 }
536
i_mmap_unlock_read(struct address_space * mapping)537 static inline void i_mmap_unlock_read(struct address_space *mapping)
538 {
539 up_read(&mapping->i_mmap_rwsem);
540 }
541
542 /*
543 * Might pages of this file be mapped into userspace?
544 */
mapping_mapped(struct address_space * mapping)545 static inline int mapping_mapped(struct address_space *mapping)
546 {
547 return !RB_EMPTY_ROOT(&mapping->i_mmap);
548 }
549
550 /*
551 * Might pages of this file have been modified in userspace?
552 * Note that i_mmap_writable counts all VM_SHARED vmas: do_mmap_pgoff
553 * marks vma as VM_SHARED if it is shared, and the file was opened for
554 * writing i.e. vma may be mprotected writable even if now readonly.
555 *
556 * If i_mmap_writable is negative, no new writable mappings are allowed. You
557 * can only deny writable mappings, if none exists right now.
558 */
mapping_writably_mapped(struct address_space * mapping)559 static inline int mapping_writably_mapped(struct address_space *mapping)
560 {
561 return atomic_read(&mapping->i_mmap_writable) > 0;
562 }
563
mapping_map_writable(struct address_space * mapping)564 static inline int mapping_map_writable(struct address_space *mapping)
565 {
566 return atomic_inc_unless_negative(&mapping->i_mmap_writable) ?
567 0 : -EPERM;
568 }
569
mapping_unmap_writable(struct address_space * mapping)570 static inline void mapping_unmap_writable(struct address_space *mapping)
571 {
572 atomic_dec(&mapping->i_mmap_writable);
573 }
574
mapping_deny_writable(struct address_space * mapping)575 static inline int mapping_deny_writable(struct address_space *mapping)
576 {
577 return atomic_dec_unless_positive(&mapping->i_mmap_writable) ?
578 0 : -EBUSY;
579 }
580
mapping_allow_writable(struct address_space * mapping)581 static inline void mapping_allow_writable(struct address_space *mapping)
582 {
583 atomic_inc(&mapping->i_mmap_writable);
584 }
585
586 /*
587 * Use sequence counter to get consistent i_size on 32-bit processors.
588 */
589 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
590 #include <linux/seqlock.h>
591 #define __NEED_I_SIZE_ORDERED
592 #define i_size_ordered_init(inode) seqcount_init(&inode->i_size_seqcount)
593 #else
594 #define i_size_ordered_init(inode) do { } while (0)
595 #endif
596
597 struct posix_acl;
598 #define ACL_NOT_CACHED ((void *)(-1))
599 #define ACL_DONT_CACHE ((void *)(-3))
600
601 static inline struct posix_acl *
uncached_acl_sentinel(struct task_struct * task)602 uncached_acl_sentinel(struct task_struct *task)
603 {
604 return (void *)task + 1;
605 }
606
607 static inline bool
is_uncached_acl(struct posix_acl * acl)608 is_uncached_acl(struct posix_acl *acl)
609 {
610 return (long)acl & 1;
611 }
612
613 #define IOP_FASTPERM 0x0001
614 #define IOP_LOOKUP 0x0002
615 #define IOP_NOFOLLOW 0x0004
616 #define IOP_XATTR 0x0008
617
618 /*
619 * Keep mostly read-only and often accessed (especially for
620 * the RCU path lookup and 'stat' data) fields at the beginning
621 * of the 'struct inode'
622 */
623 struct inode {
624 umode_t i_mode;
625 unsigned short i_opflags;
626 kuid_t i_uid;
627 kgid_t i_gid;
628 unsigned int i_flags;
629
630 #ifdef CONFIG_FS_POSIX_ACL
631 struct posix_acl *i_acl;
632 struct posix_acl *i_default_acl;
633 #endif
634
635 const struct inode_operations *i_op;
636 struct super_block *i_sb;
637 struct address_space *i_mapping;
638
639 #ifdef CONFIG_SECURITY
640 void *i_security;
641 #endif
642
643 /* Stat data, not accessed from path walking */
644 unsigned long i_ino;
645 /*
646 * Filesystems may only read i_nlink directly. They shall use the
647 * following functions for modification:
648 *
649 * (set|clear|inc|drop)_nlink
650 * inode_(inc|dec)_link_count
651 */
652 union {
653 const unsigned int i_nlink;
654 unsigned int __i_nlink;
655 };
656 dev_t i_rdev;
657 loff_t i_size;
658 struct timespec i_atime;
659 struct timespec i_mtime;
660 struct timespec i_ctime;
661 spinlock_t i_lock; /* i_blocks, i_bytes, maybe i_size */
662 unsigned short i_bytes;
663 unsigned int i_blkbits;
664 enum rw_hint i_write_hint;
665 blkcnt_t i_blocks;
666
667 #ifdef __NEED_I_SIZE_ORDERED
668 seqcount_t i_size_seqcount;
669 #endif
670
671 /* Misc */
672 unsigned long i_state;
673 struct rw_semaphore i_rwsem;
674
675 unsigned long dirtied_when; /* jiffies of first dirtying */
676 unsigned long dirtied_time_when;
677
678 struct hlist_node i_hash;
679 struct list_head i_io_list; /* backing dev IO list */
680 #ifdef CONFIG_CGROUP_WRITEBACK
681 struct bdi_writeback *i_wb; /* the associated cgroup wb */
682
683 /* foreign inode detection, see wbc_detach_inode() */
684 int i_wb_frn_winner;
685 u16 i_wb_frn_avg_time;
686 u16 i_wb_frn_history;
687 #endif
688 struct list_head i_lru; /* inode LRU list */
689 struct list_head i_sb_list;
690 struct list_head i_wb_list; /* backing dev writeback list */
691 union {
692 struct hlist_head i_dentry;
693 struct rcu_head i_rcu;
694 };
695 u64 i_version;
696 atomic_t i_count;
697 atomic_t i_dio_count;
698 atomic_t i_writecount;
699 #ifdef CONFIG_IMA
700 atomic_t i_readcount; /* struct files open RO */
701 #endif
702 const struct file_operations *i_fop; /* former ->i_op->default_file_ops */
703 struct file_lock_context *i_flctx;
704 struct address_space i_data;
705 struct list_head i_devices;
706 union {
707 struct pipe_inode_info *i_pipe;
708 struct block_device *i_bdev;
709 struct cdev *i_cdev;
710 char *i_link;
711 unsigned i_dir_seq;
712 };
713
714 __u32 i_generation;
715
716 #ifdef CONFIG_FSNOTIFY
717 __u32 i_fsnotify_mask; /* all events this inode cares about */
718 struct hlist_head i_fsnotify_marks;
719 #endif
720
721 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
722 struct fscrypt_info *i_crypt_info;
723 #endif
724
725 void *i_private; /* fs or device private pointer */
726 };
727
i_blocksize(const struct inode * node)728 static inline unsigned int i_blocksize(const struct inode *node)
729 {
730 return (1 << node->i_blkbits);
731 }
732
inode_unhashed(struct inode * inode)733 static inline int inode_unhashed(struct inode *inode)
734 {
735 return hlist_unhashed(&inode->i_hash);
736 }
737
738 /*
739 * inode->i_mutex nesting subclasses for the lock validator:
740 *
741 * 0: the object of the current VFS operation
742 * 1: parent
743 * 2: child/target
744 * 3: xattr
745 * 4: second non-directory
746 * 5: second parent (when locking independent directories in rename)
747 *
748 * I_MUTEX_NONDIR2 is for certain operations (such as rename) which lock two
749 * non-directories at once.
750 *
751 * The locking order between these classes is
752 * parent[2] -> child -> grandchild -> normal -> xattr -> second non-directory
753 */
754 enum inode_i_mutex_lock_class
755 {
756 I_MUTEX_NORMAL,
757 I_MUTEX_PARENT,
758 I_MUTEX_CHILD,
759 I_MUTEX_XATTR,
760 I_MUTEX_NONDIR2,
761 I_MUTEX_PARENT2,
762 };
763
inode_lock(struct inode * inode)764 static inline void inode_lock(struct inode *inode)
765 {
766 down_write(&inode->i_rwsem);
767 }
768
inode_unlock(struct inode * inode)769 static inline void inode_unlock(struct inode *inode)
770 {
771 up_write(&inode->i_rwsem);
772 }
773
inode_lock_shared(struct inode * inode)774 static inline void inode_lock_shared(struct inode *inode)
775 {
776 down_read(&inode->i_rwsem);
777 }
778
inode_unlock_shared(struct inode * inode)779 static inline void inode_unlock_shared(struct inode *inode)
780 {
781 up_read(&inode->i_rwsem);
782 }
783
inode_trylock(struct inode * inode)784 static inline int inode_trylock(struct inode *inode)
785 {
786 return down_write_trylock(&inode->i_rwsem);
787 }
788
inode_trylock_shared(struct inode * inode)789 static inline int inode_trylock_shared(struct inode *inode)
790 {
791 return down_read_trylock(&inode->i_rwsem);
792 }
793
inode_is_locked(struct inode * inode)794 static inline int inode_is_locked(struct inode *inode)
795 {
796 return rwsem_is_locked(&inode->i_rwsem);
797 }
798
inode_lock_nested(struct inode * inode,unsigned subclass)799 static inline void inode_lock_nested(struct inode *inode, unsigned subclass)
800 {
801 down_write_nested(&inode->i_rwsem, subclass);
802 }
803
804 void lock_two_nondirectories(struct inode *, struct inode*);
805 void unlock_two_nondirectories(struct inode *, struct inode*);
806
807 /*
808 * NOTE: in a 32bit arch with a preemptable kernel and
809 * an UP compile the i_size_read/write must be atomic
810 * with respect to the local cpu (unlike with preempt disabled),
811 * but they don't need to be atomic with respect to other cpus like in
812 * true SMP (so they need either to either locally disable irq around
813 * the read or for example on x86 they can be still implemented as a
814 * cmpxchg8b without the need of the lock prefix). For SMP compiles
815 * and 64bit archs it makes no difference if preempt is enabled or not.
816 */
i_size_read(const struct inode * inode)817 static inline loff_t i_size_read(const struct inode *inode)
818 {
819 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
820 loff_t i_size;
821 unsigned int seq;
822
823 do {
824 seq = read_seqcount_begin(&inode->i_size_seqcount);
825 i_size = inode->i_size;
826 } while (read_seqcount_retry(&inode->i_size_seqcount, seq));
827 return i_size;
828 #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPT)
829 loff_t i_size;
830
831 preempt_disable();
832 i_size = inode->i_size;
833 preempt_enable();
834 return i_size;
835 #else
836 return inode->i_size;
837 #endif
838 }
839
840 /*
841 * NOTE: unlike i_size_read(), i_size_write() does need locking around it
842 * (normally i_mutex), otherwise on 32bit/SMP an update of i_size_seqcount
843 * can be lost, resulting in subsequent i_size_read() calls spinning forever.
844 */
i_size_write(struct inode * inode,loff_t i_size)845 static inline void i_size_write(struct inode *inode, loff_t i_size)
846 {
847 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
848 preempt_disable();
849 write_seqcount_begin(&inode->i_size_seqcount);
850 inode->i_size = i_size;
851 write_seqcount_end(&inode->i_size_seqcount);
852 preempt_enable();
853 #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPT)
854 preempt_disable();
855 inode->i_size = i_size;
856 preempt_enable();
857 #else
858 inode->i_size = i_size;
859 #endif
860 }
861
iminor(const struct inode * inode)862 static inline unsigned iminor(const struct inode *inode)
863 {
864 return MINOR(inode->i_rdev);
865 }
866
imajor(const struct inode * inode)867 static inline unsigned imajor(const struct inode *inode)
868 {
869 return MAJOR(inode->i_rdev);
870 }
871
872 extern struct block_device *I_BDEV(struct inode *inode);
873
874 struct fown_struct {
875 rwlock_t lock; /* protects pid, uid, euid fields */
876 struct pid *pid; /* pid or -pgrp where SIGIO should be sent */
877 enum pid_type pid_type; /* Kind of process group SIGIO should be sent to */
878 kuid_t uid, euid; /* uid/euid of process setting the owner */
879 int signum; /* posix.1b rt signal to be delivered on IO */
880 };
881
882 /*
883 * Track a single file's readahead state
884 */
885 struct file_ra_state {
886 pgoff_t start; /* where readahead started */
887 unsigned int size; /* # of readahead pages */
888 unsigned int async_size; /* do asynchronous readahead when
889 there are only # of pages ahead */
890
891 unsigned int ra_pages; /* Maximum readahead window */
892 unsigned int mmap_miss; /* Cache miss stat for mmap accesses */
893 loff_t prev_pos; /* Cache last read() position */
894 };
895
896 /*
897 * Check if @index falls in the readahead windows.
898 */
ra_has_index(struct file_ra_state * ra,pgoff_t index)899 static inline int ra_has_index(struct file_ra_state *ra, pgoff_t index)
900 {
901 return (index >= ra->start &&
902 index < ra->start + ra->size);
903 }
904
905 struct file {
906 union {
907 struct llist_node fu_llist;
908 struct rcu_head fu_rcuhead;
909 } f_u;
910 struct path f_path;
911 struct inode *f_inode; /* cached value */
912 const struct file_operations *f_op;
913
914 /*
915 * Protects f_ep_links, f_flags.
916 * Must not be taken from IRQ context.
917 */
918 spinlock_t f_lock;
919 atomic_long_t f_count;
920 unsigned int f_flags;
921 fmode_t f_mode;
922 struct mutex f_pos_lock;
923 loff_t f_pos;
924 struct fown_struct f_owner;
925 const struct cred *f_cred;
926 struct file_ra_state f_ra;
927
928 u64 f_version;
929 #ifdef CONFIG_SECURITY
930 void *f_security;
931 #endif
932 /* needed for tty driver, and maybe others */
933 void *private_data;
934
935 #ifdef CONFIG_EPOLL
936 /* Used by fs/eventpoll.c to link all the hooks to this file */
937 struct list_head f_ep_links;
938 struct list_head f_tfile_llink;
939 #endif /* #ifdef CONFIG_EPOLL */
940 struct address_space *f_mapping;
941 } __attribute__((aligned(4))); /* lest something weird decides that 2 is OK */
942
943 struct file_handle {
944 __u32 handle_bytes;
945 int handle_type;
946 /* file identifier */
947 unsigned char f_handle[0];
948 };
949
get_file(struct file * f)950 static inline struct file *get_file(struct file *f)
951 {
952 atomic_long_inc(&f->f_count);
953 return f;
954 }
955 #define get_file_rcu(x) atomic_long_inc_not_zero(&(x)->f_count)
956 #define fput_atomic(x) atomic_long_add_unless(&(x)->f_count, -1, 1)
957 #define file_count(x) atomic_long_read(&(x)->f_count)
958
959 #define MAX_NON_LFS ((1UL<<31) - 1)
960
961 /* Page cache limit. The filesystems should put that into their s_maxbytes
962 limits, otherwise bad things can happen in VM. */
963 #if BITS_PER_LONG==32
964 #define MAX_LFS_FILESIZE ((loff_t)ULONG_MAX << PAGE_SHIFT)
965 #elif BITS_PER_LONG==64
966 #define MAX_LFS_FILESIZE ((loff_t)LLONG_MAX)
967 #endif
968
969 #define FL_POSIX 1
970 #define FL_FLOCK 2
971 #define FL_DELEG 4 /* NFSv4 delegation */
972 #define FL_ACCESS 8 /* not trying to lock, just looking */
973 #define FL_EXISTS 16 /* when unlocking, test for existence */
974 #define FL_LEASE 32 /* lease held on this file */
975 #define FL_CLOSE 64 /* unlock on close */
976 #define FL_SLEEP 128 /* A blocking lock */
977 #define FL_DOWNGRADE_PENDING 256 /* Lease is being downgraded */
978 #define FL_UNLOCK_PENDING 512 /* Lease is being broken */
979 #define FL_OFDLCK 1024 /* lock is "owned" by struct file */
980 #define FL_LAYOUT 2048 /* outstanding pNFS layout */
981
982 /*
983 * Special return value from posix_lock_file() and vfs_lock_file() for
984 * asynchronous locking.
985 */
986 #define FILE_LOCK_DEFERRED 1
987
988 /* legacy typedef, should eventually be removed */
989 typedef void *fl_owner_t;
990
991 struct file_lock;
992
993 struct file_lock_operations {
994 void (*fl_copy_lock)(struct file_lock *, struct file_lock *);
995 void (*fl_release_private)(struct file_lock *);
996 };
997
998 struct lock_manager_operations {
999 int (*lm_compare_owner)(struct file_lock *, struct file_lock *);
1000 unsigned long (*lm_owner_key)(struct file_lock *);
1001 fl_owner_t (*lm_get_owner)(fl_owner_t);
1002 void (*lm_put_owner)(fl_owner_t);
1003 void (*lm_notify)(struct file_lock *); /* unblock callback */
1004 int (*lm_grant)(struct file_lock *, int);
1005 bool (*lm_break)(struct file_lock *);
1006 int (*lm_change)(struct file_lock *, int, struct list_head *);
1007 void (*lm_setup)(struct file_lock *, void **);
1008 };
1009
1010 struct lock_manager {
1011 struct list_head list;
1012 /*
1013 * NFSv4 and up also want opens blocked during the grace period;
1014 * NLM doesn't care:
1015 */
1016 bool block_opens;
1017 };
1018
1019 struct net;
1020 void locks_start_grace(struct net *, struct lock_manager *);
1021 void locks_end_grace(struct lock_manager *);
1022 int locks_in_grace(struct net *);
1023 int opens_in_grace(struct net *);
1024
1025 /* that will die - we need it for nfs_lock_info */
1026 #include <linux/nfs_fs_i.h>
1027
1028 /*
1029 * struct file_lock represents a generic "file lock". It's used to represent
1030 * POSIX byte range locks, BSD (flock) locks, and leases. It's important to
1031 * note that the same struct is used to represent both a request for a lock and
1032 * the lock itself, but the same object is never used for both.
1033 *
1034 * FIXME: should we create a separate "struct lock_request" to help distinguish
1035 * these two uses?
1036 *
1037 * The varous i_flctx lists are ordered by:
1038 *
1039 * 1) lock owner
1040 * 2) lock range start
1041 * 3) lock range end
1042 *
1043 * Obviously, the last two criteria only matter for POSIX locks.
1044 */
1045 struct file_lock {
1046 struct file_lock *fl_next; /* singly linked list for this inode */
1047 struct list_head fl_list; /* link into file_lock_context */
1048 struct hlist_node fl_link; /* node in global lists */
1049 struct list_head fl_block; /* circular list of blocked processes */
1050 fl_owner_t fl_owner;
1051 unsigned int fl_flags;
1052 unsigned char fl_type;
1053 unsigned int fl_pid;
1054 int fl_link_cpu; /* what cpu's list is this on? */
1055 struct pid *fl_nspid;
1056 wait_queue_head_t fl_wait;
1057 struct file *fl_file;
1058 loff_t fl_start;
1059 loff_t fl_end;
1060
1061 struct fasync_struct * fl_fasync; /* for lease break notifications */
1062 /* for lease breaks: */
1063 unsigned long fl_break_time;
1064 unsigned long fl_downgrade_time;
1065
1066 const struct file_lock_operations *fl_ops; /* Callbacks for filesystems */
1067 const struct lock_manager_operations *fl_lmops; /* Callbacks for lockmanagers */
1068 union {
1069 struct nfs_lock_info nfs_fl;
1070 struct nfs4_lock_info nfs4_fl;
1071 struct {
1072 struct list_head link; /* link in AFS vnode's pending_locks list */
1073 int state; /* state of grant or error if -ve */
1074 } afs;
1075 } fl_u;
1076 };
1077
1078 struct file_lock_context {
1079 spinlock_t flc_lock;
1080 struct list_head flc_flock;
1081 struct list_head flc_posix;
1082 struct list_head flc_lease;
1083 };
1084
1085 /* The following constant reflects the upper bound of the file/locking space */
1086 #ifndef OFFSET_MAX
1087 #define INT_LIMIT(x) (~((x)1 << (sizeof(x)*8 - 1)))
1088 #define OFFSET_MAX INT_LIMIT(loff_t)
1089 #define OFFT_OFFSET_MAX INT_LIMIT(off_t)
1090 #endif
1091
1092 extern void send_sigio(struct fown_struct *fown, int fd, int band);
1093
1094 /*
1095 * Return the inode to use for locking
1096 *
1097 * For overlayfs this should be the overlay inode, not the real inode returned
1098 * by file_inode(). For any other fs file_inode(filp) and locks_inode(filp) are
1099 * equal.
1100 */
locks_inode(const struct file * f)1101 static inline struct inode *locks_inode(const struct file *f)
1102 {
1103 return f->f_path.dentry->d_inode;
1104 }
1105
1106 #ifdef CONFIG_FILE_LOCKING
1107 extern int fcntl_getlk(struct file *, unsigned int, struct flock __user *);
1108 extern int fcntl_setlk(unsigned int, struct file *, unsigned int,
1109 struct flock __user *);
1110
1111 #if BITS_PER_LONG == 32
1112 extern int fcntl_getlk64(struct file *, unsigned int, struct flock64 __user *);
1113 extern int fcntl_setlk64(unsigned int, struct file *, unsigned int,
1114 struct flock64 __user *);
1115 #endif
1116
1117 extern int fcntl_setlease(unsigned int fd, struct file *filp, long arg);
1118 extern int fcntl_getlease(struct file *filp);
1119
1120 /* fs/locks.c */
1121 void locks_free_lock_context(struct inode *inode);
1122 void locks_free_lock(struct file_lock *fl);
1123 extern void locks_init_lock(struct file_lock *);
1124 extern struct file_lock * locks_alloc_lock(void);
1125 extern void locks_copy_lock(struct file_lock *, struct file_lock *);
1126 extern void locks_copy_conflock(struct file_lock *, struct file_lock *);
1127 extern void locks_remove_posix(struct file *, fl_owner_t);
1128 extern void locks_remove_file(struct file *);
1129 extern void locks_release_private(struct file_lock *);
1130 extern void posix_test_lock(struct file *, struct file_lock *);
1131 extern int posix_lock_file(struct file *, struct file_lock *, struct file_lock *);
1132 extern int posix_unblock_lock(struct file_lock *);
1133 extern int vfs_test_lock(struct file *, struct file_lock *);
1134 extern int vfs_lock_file(struct file *, unsigned int, struct file_lock *, struct file_lock *);
1135 extern int vfs_cancel_lock(struct file *filp, struct file_lock *fl);
1136 extern int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl);
1137 extern int __break_lease(struct inode *inode, unsigned int flags, unsigned int type);
1138 extern void lease_get_mtime(struct inode *, struct timespec *time);
1139 extern int generic_setlease(struct file *, long, struct file_lock **, void **priv);
1140 extern int vfs_setlease(struct file *, long, struct file_lock **, void **);
1141 extern int lease_modify(struct file_lock *, int, struct list_head *);
1142 struct files_struct;
1143 extern void show_fd_locks(struct seq_file *f,
1144 struct file *filp, struct files_struct *files);
1145 #else /* !CONFIG_FILE_LOCKING */
fcntl_getlk(struct file * file,unsigned int cmd,struct flock __user * user)1146 static inline int fcntl_getlk(struct file *file, unsigned int cmd,
1147 struct flock __user *user)
1148 {
1149 return -EINVAL;
1150 }
1151
fcntl_setlk(unsigned int fd,struct file * file,unsigned int cmd,struct flock __user * user)1152 static inline int fcntl_setlk(unsigned int fd, struct file *file,
1153 unsigned int cmd, struct flock __user *user)
1154 {
1155 return -EACCES;
1156 }
1157
1158 #if BITS_PER_LONG == 32
fcntl_getlk64(struct file * file,unsigned int cmd,struct flock64 __user * user)1159 static inline int fcntl_getlk64(struct file *file, unsigned int cmd,
1160 struct flock64 __user *user)
1161 {
1162 return -EINVAL;
1163 }
1164
fcntl_setlk64(unsigned int fd,struct file * file,unsigned int cmd,struct flock64 __user * user)1165 static inline int fcntl_setlk64(unsigned int fd, struct file *file,
1166 unsigned int cmd, struct flock64 __user *user)
1167 {
1168 return -EACCES;
1169 }
1170 #endif
fcntl_setlease(unsigned int fd,struct file * filp,long arg)1171 static inline int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
1172 {
1173 return -EINVAL;
1174 }
1175
fcntl_getlease(struct file * filp)1176 static inline int fcntl_getlease(struct file *filp)
1177 {
1178 return F_UNLCK;
1179 }
1180
1181 static inline void
locks_free_lock_context(struct inode * inode)1182 locks_free_lock_context(struct inode *inode)
1183 {
1184 }
1185
locks_init_lock(struct file_lock * fl)1186 static inline void locks_init_lock(struct file_lock *fl)
1187 {
1188 return;
1189 }
1190
locks_copy_conflock(struct file_lock * new,struct file_lock * fl)1191 static inline void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
1192 {
1193 return;
1194 }
1195
locks_copy_lock(struct file_lock * new,struct file_lock * fl)1196 static inline void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
1197 {
1198 return;
1199 }
1200
locks_remove_posix(struct file * filp,fl_owner_t owner)1201 static inline void locks_remove_posix(struct file *filp, fl_owner_t owner)
1202 {
1203 return;
1204 }
1205
locks_remove_file(struct file * filp)1206 static inline void locks_remove_file(struct file *filp)
1207 {
1208 return;
1209 }
1210
posix_test_lock(struct file * filp,struct file_lock * fl)1211 static inline void posix_test_lock(struct file *filp, struct file_lock *fl)
1212 {
1213 return;
1214 }
1215
posix_lock_file(struct file * filp,struct file_lock * fl,struct file_lock * conflock)1216 static inline int posix_lock_file(struct file *filp, struct file_lock *fl,
1217 struct file_lock *conflock)
1218 {
1219 return -ENOLCK;
1220 }
1221
posix_unblock_lock(struct file_lock * waiter)1222 static inline int posix_unblock_lock(struct file_lock *waiter)
1223 {
1224 return -ENOENT;
1225 }
1226
vfs_test_lock(struct file * filp,struct file_lock * fl)1227 static inline int vfs_test_lock(struct file *filp, struct file_lock *fl)
1228 {
1229 return 0;
1230 }
1231
vfs_lock_file(struct file * filp,unsigned int cmd,struct file_lock * fl,struct file_lock * conf)1232 static inline int vfs_lock_file(struct file *filp, unsigned int cmd,
1233 struct file_lock *fl, struct file_lock *conf)
1234 {
1235 return -ENOLCK;
1236 }
1237
vfs_cancel_lock(struct file * filp,struct file_lock * fl)1238 static inline int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
1239 {
1240 return 0;
1241 }
1242
locks_lock_inode_wait(struct inode * inode,struct file_lock * fl)1243 static inline int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl)
1244 {
1245 return -ENOLCK;
1246 }
1247
__break_lease(struct inode * inode,unsigned int mode,unsigned int type)1248 static inline int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
1249 {
1250 return 0;
1251 }
1252
lease_get_mtime(struct inode * inode,struct timespec * time)1253 static inline void lease_get_mtime(struct inode *inode, struct timespec *time)
1254 {
1255 return;
1256 }
1257
generic_setlease(struct file * filp,long arg,struct file_lock ** flp,void ** priv)1258 static inline int generic_setlease(struct file *filp, long arg,
1259 struct file_lock **flp, void **priv)
1260 {
1261 return -EINVAL;
1262 }
1263
vfs_setlease(struct file * filp,long arg,struct file_lock ** lease,void ** priv)1264 static inline int vfs_setlease(struct file *filp, long arg,
1265 struct file_lock **lease, void **priv)
1266 {
1267 return -EINVAL;
1268 }
1269
lease_modify(struct file_lock * fl,int arg,struct list_head * dispose)1270 static inline int lease_modify(struct file_lock *fl, int arg,
1271 struct list_head *dispose)
1272 {
1273 return -EINVAL;
1274 }
1275
1276 struct files_struct;
show_fd_locks(struct seq_file * f,struct file * filp,struct files_struct * files)1277 static inline void show_fd_locks(struct seq_file *f,
1278 struct file *filp, struct files_struct *files) {}
1279 #endif /* !CONFIG_FILE_LOCKING */
1280
file_inode(const struct file * f)1281 static inline struct inode *file_inode(const struct file *f)
1282 {
1283 return f->f_inode;
1284 }
1285
file_dentry(const struct file * file)1286 static inline struct dentry *file_dentry(const struct file *file)
1287 {
1288 return d_real(file->f_path.dentry, file_inode(file), 0);
1289 }
1290
locks_lock_file_wait(struct file * filp,struct file_lock * fl)1291 static inline int locks_lock_file_wait(struct file *filp, struct file_lock *fl)
1292 {
1293 return locks_lock_inode_wait(locks_inode(filp), fl);
1294 }
1295
1296 struct fasync_struct {
1297 spinlock_t fa_lock;
1298 int magic;
1299 int fa_fd;
1300 struct fasync_struct *fa_next; /* singly linked list */
1301 struct file *fa_file;
1302 struct rcu_head fa_rcu;
1303 };
1304
1305 #define FASYNC_MAGIC 0x4601
1306
1307 /* SMP safe fasync helpers: */
1308 extern int fasync_helper(int, struct file *, int, struct fasync_struct **);
1309 extern struct fasync_struct *fasync_insert_entry(int, struct file *, struct fasync_struct **, struct fasync_struct *);
1310 extern int fasync_remove_entry(struct file *, struct fasync_struct **);
1311 extern struct fasync_struct *fasync_alloc(void);
1312 extern void fasync_free(struct fasync_struct *);
1313
1314 /* can be called from interrupts */
1315 extern void kill_fasync(struct fasync_struct **, int, int);
1316
1317 extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
1318 extern void f_setown(struct file *filp, unsigned long arg, int force);
1319 extern void f_delown(struct file *filp);
1320 extern pid_t f_getown(struct file *filp);
1321 extern int send_sigurg(struct fown_struct *fown);
1322
1323 struct mm_struct;
1324
1325 /*
1326 * Umount options
1327 */
1328
1329 #define MNT_FORCE 0x00000001 /* Attempt to forcibily umount */
1330 #define MNT_DETACH 0x00000002 /* Just detach from the tree */
1331 #define MNT_EXPIRE 0x00000004 /* Mark for expiry */
1332 #define UMOUNT_NOFOLLOW 0x00000008 /* Don't follow symlink on umount */
1333 #define UMOUNT_UNUSED 0x80000000 /* Flag guaranteed to be unused */
1334
1335 /* sb->s_iflags */
1336 #define SB_I_CGROUPWB 0x00000001 /* cgroup-aware writeback enabled */
1337 #define SB_I_NOEXEC 0x00000002 /* Ignore executables on this fs */
1338 #define SB_I_NODEV 0x00000004 /* Ignore devices on this fs */
1339 #define SB_I_MULTIROOT 0x00000008 /* Multiple roots to the dentry tree */
1340
1341 /* sb->s_iflags to limit user namespace mounts */
1342 #define SB_I_USERNS_VISIBLE 0x00000010 /* fstype already mounted */
1343
1344 /* Possible states of 'frozen' field */
1345 enum {
1346 SB_UNFROZEN = 0, /* FS is unfrozen */
1347 SB_FREEZE_WRITE = 1, /* Writes, dir ops, ioctls frozen */
1348 SB_FREEZE_PAGEFAULT = 2, /* Page faults stopped as well */
1349 SB_FREEZE_FS = 3, /* For internal FS use (e.g. to stop
1350 * internal threads if needed) */
1351 SB_FREEZE_COMPLETE = 4, /* ->freeze_fs finished successfully */
1352 };
1353
1354 #define SB_FREEZE_LEVELS (SB_FREEZE_COMPLETE - 1)
1355
1356 struct sb_writers {
1357 int frozen; /* Is sb frozen? */
1358 wait_queue_head_t wait_unfrozen; /* for get_super_thawed() */
1359 struct percpu_rw_semaphore rw_sem[SB_FREEZE_LEVELS];
1360 };
1361
1362 struct super_block {
1363 struct list_head s_list; /* Keep this first */
1364 dev_t s_dev; /* search index; _not_ kdev_t */
1365 unsigned char s_blocksize_bits;
1366 unsigned long s_blocksize;
1367 loff_t s_maxbytes; /* Max file size */
1368 struct file_system_type *s_type;
1369 const struct super_operations *s_op;
1370 const struct dquot_operations *dq_op;
1371 const struct quotactl_ops *s_qcop;
1372 const struct export_operations *s_export_op;
1373 unsigned long s_flags;
1374 unsigned long s_iflags; /* internal SB_I_* flags */
1375 unsigned long s_magic;
1376 struct dentry *s_root;
1377 struct rw_semaphore s_umount;
1378 int s_count;
1379 atomic_t s_active;
1380 #ifdef CONFIG_SECURITY
1381 void *s_security;
1382 #endif
1383 const struct xattr_handler **s_xattr;
1384
1385 const struct fscrypt_operations *s_cop;
1386
1387 struct hlist_bl_head s_anon; /* anonymous dentries for (nfs) exporting */
1388 struct list_head s_mounts; /* list of mounts; _not_ for fs use */
1389 struct block_device *s_bdev;
1390 struct backing_dev_info *s_bdi;
1391 struct mtd_info *s_mtd;
1392 struct hlist_node s_instances;
1393 unsigned int s_quota_types; /* Bitmask of supported quota types */
1394 struct quota_info s_dquot; /* Diskquota specific options */
1395
1396 struct sb_writers s_writers;
1397
1398 char s_id[32]; /* Informational name */
1399 u8 s_uuid[16]; /* UUID */
1400
1401 void *s_fs_info; /* Filesystem private info */
1402 unsigned int s_max_links;
1403 fmode_t s_mode;
1404
1405 /* Granularity of c/m/atime in ns.
1406 Cannot be worse than a second */
1407 u32 s_time_gran;
1408
1409 /*
1410 * The next field is for VFS *only*. No filesystems have any business
1411 * even looking at it. You had been warned.
1412 */
1413 struct mutex s_vfs_rename_mutex; /* Kludge */
1414
1415 /*
1416 * Filesystem subtype. If non-empty the filesystem type field
1417 * in /proc/mounts will be "type.subtype"
1418 */
1419 char *s_subtype;
1420
1421 /*
1422 * Saved mount options for lazy filesystems using
1423 * generic_show_options()
1424 */
1425 char __rcu *s_options;
1426 const struct dentry_operations *s_d_op; /* default d_op for dentries */
1427
1428 /*
1429 * Saved pool identifier for cleancache (-1 means none)
1430 */
1431 int cleancache_poolid;
1432
1433 struct shrinker s_shrink; /* per-sb shrinker handle */
1434
1435 /* Number of inodes with nlink == 0 but still referenced */
1436 atomic_long_t s_remove_count;
1437
1438 /* Being remounted read-only */
1439 int s_readonly_remount;
1440
1441 /* AIO completions deferred from interrupt context */
1442 struct workqueue_struct *s_dio_done_wq;
1443 struct hlist_head s_pins;
1444
1445 /*
1446 * Owning user namespace and default context in which to
1447 * interpret filesystem uids, gids, quotas, device nodes,
1448 * xattrs and security labels.
1449 */
1450 struct user_namespace *s_user_ns;
1451
1452 /*
1453 * Keep the lru lists last in the structure so they always sit on their
1454 * own individual cachelines.
1455 */
1456 struct list_lru s_dentry_lru ____cacheline_aligned_in_smp;
1457 struct list_lru s_inode_lru ____cacheline_aligned_in_smp;
1458 struct rcu_head rcu;
1459 struct work_struct destroy_work;
1460
1461 struct mutex s_sync_lock; /* sync serialisation lock */
1462
1463 /*
1464 * Indicates how deep in a filesystem stack this SB is
1465 */
1466 int s_stack_depth;
1467
1468 /* s_inode_list_lock protects s_inodes */
1469 spinlock_t s_inode_list_lock ____cacheline_aligned_in_smp;
1470 struct list_head s_inodes; /* all inodes */
1471
1472 spinlock_t s_inode_wblist_lock;
1473 struct list_head s_inodes_wb; /* writeback inodes */
1474 };
1475
1476 /* Helper functions so that in most cases filesystems will
1477 * not need to deal directly with kuid_t and kgid_t and can
1478 * instead deal with the raw numeric values that are stored
1479 * in the filesystem.
1480 */
i_uid_read(const struct inode * inode)1481 static inline uid_t i_uid_read(const struct inode *inode)
1482 {
1483 return from_kuid(inode->i_sb->s_user_ns, inode->i_uid);
1484 }
1485
i_gid_read(const struct inode * inode)1486 static inline gid_t i_gid_read(const struct inode *inode)
1487 {
1488 return from_kgid(inode->i_sb->s_user_ns, inode->i_gid);
1489 }
1490
i_uid_write(struct inode * inode,uid_t uid)1491 static inline void i_uid_write(struct inode *inode, uid_t uid)
1492 {
1493 inode->i_uid = make_kuid(inode->i_sb->s_user_ns, uid);
1494 }
1495
i_gid_write(struct inode * inode,gid_t gid)1496 static inline void i_gid_write(struct inode *inode, gid_t gid)
1497 {
1498 inode->i_gid = make_kgid(inode->i_sb->s_user_ns, gid);
1499 }
1500
1501 extern struct timespec current_fs_time(struct super_block *sb);
1502 extern struct timespec current_time(struct inode *inode);
1503
1504 /*
1505 * Snapshotting support.
1506 */
1507
1508 void __sb_end_write(struct super_block *sb, int level);
1509 int __sb_start_write(struct super_block *sb, int level, bool wait);
1510
1511 #define __sb_writers_acquired(sb, lev) \
1512 percpu_rwsem_acquire(&(sb)->s_writers.rw_sem[(lev)-1], 1, _THIS_IP_)
1513 #define __sb_writers_release(sb, lev) \
1514 percpu_rwsem_release(&(sb)->s_writers.rw_sem[(lev)-1], 1, _THIS_IP_)
1515
1516 /**
1517 * sb_end_write - drop write access to a superblock
1518 * @sb: the super we wrote to
1519 *
1520 * Decrement number of writers to the filesystem. Wake up possible waiters
1521 * wanting to freeze the filesystem.
1522 */
sb_end_write(struct super_block * sb)1523 static inline void sb_end_write(struct super_block *sb)
1524 {
1525 __sb_end_write(sb, SB_FREEZE_WRITE);
1526 }
1527
1528 /**
1529 * sb_end_pagefault - drop write access to a superblock from a page fault
1530 * @sb: the super we wrote to
1531 *
1532 * Decrement number of processes handling write page fault to the filesystem.
1533 * Wake up possible waiters wanting to freeze the filesystem.
1534 */
sb_end_pagefault(struct super_block * sb)1535 static inline void sb_end_pagefault(struct super_block *sb)
1536 {
1537 __sb_end_write(sb, SB_FREEZE_PAGEFAULT);
1538 }
1539
1540 /**
1541 * sb_end_intwrite - drop write access to a superblock for internal fs purposes
1542 * @sb: the super we wrote to
1543 *
1544 * Decrement fs-internal number of writers to the filesystem. Wake up possible
1545 * waiters wanting to freeze the filesystem.
1546 */
sb_end_intwrite(struct super_block * sb)1547 static inline void sb_end_intwrite(struct super_block *sb)
1548 {
1549 __sb_end_write(sb, SB_FREEZE_FS);
1550 }
1551
1552 /**
1553 * sb_start_write - get write access to a superblock
1554 * @sb: the super we write to
1555 *
1556 * When a process wants to write data or metadata to a file system (i.e. dirty
1557 * a page or an inode), it should embed the operation in a sb_start_write() -
1558 * sb_end_write() pair to get exclusion against file system freezing. This
1559 * function increments number of writers preventing freezing. If the file
1560 * system is already frozen, the function waits until the file system is
1561 * thawed.
1562 *
1563 * Since freeze protection behaves as a lock, users have to preserve
1564 * ordering of freeze protection and other filesystem locks. Generally,
1565 * freeze protection should be the outermost lock. In particular, we have:
1566 *
1567 * sb_start_write
1568 * -> i_mutex (write path, truncate, directory ops, ...)
1569 * -> s_umount (freeze_super, thaw_super)
1570 */
sb_start_write(struct super_block * sb)1571 static inline void sb_start_write(struct super_block *sb)
1572 {
1573 __sb_start_write(sb, SB_FREEZE_WRITE, true);
1574 }
1575
sb_start_write_trylock(struct super_block * sb)1576 static inline int sb_start_write_trylock(struct super_block *sb)
1577 {
1578 return __sb_start_write(sb, SB_FREEZE_WRITE, false);
1579 }
1580
1581 /**
1582 * sb_start_pagefault - get write access to a superblock from a page fault
1583 * @sb: the super we write to
1584 *
1585 * When a process starts handling write page fault, it should embed the
1586 * operation into sb_start_pagefault() - sb_end_pagefault() pair to get
1587 * exclusion against file system freezing. This is needed since the page fault
1588 * is going to dirty a page. This function increments number of running page
1589 * faults preventing freezing. If the file system is already frozen, the
1590 * function waits until the file system is thawed.
1591 *
1592 * Since page fault freeze protection behaves as a lock, users have to preserve
1593 * ordering of freeze protection and other filesystem locks. It is advised to
1594 * put sb_start_pagefault() close to mmap_sem in lock ordering. Page fault
1595 * handling code implies lock dependency:
1596 *
1597 * mmap_sem
1598 * -> sb_start_pagefault
1599 */
sb_start_pagefault(struct super_block * sb)1600 static inline void sb_start_pagefault(struct super_block *sb)
1601 {
1602 __sb_start_write(sb, SB_FREEZE_PAGEFAULT, true);
1603 }
1604
1605 /*
1606 * sb_start_intwrite - get write access to a superblock for internal fs purposes
1607 * @sb: the super we write to
1608 *
1609 * This is the third level of protection against filesystem freezing. It is
1610 * free for use by a filesystem. The only requirement is that it must rank
1611 * below sb_start_pagefault.
1612 *
1613 * For example filesystem can call sb_start_intwrite() when starting a
1614 * transaction which somewhat eases handling of freezing for internal sources
1615 * of filesystem changes (internal fs threads, discarding preallocation on file
1616 * close, etc.).
1617 */
sb_start_intwrite(struct super_block * sb)1618 static inline void sb_start_intwrite(struct super_block *sb)
1619 {
1620 __sb_start_write(sb, SB_FREEZE_FS, true);
1621 }
1622
1623
1624 extern bool inode_owner_or_capable(const struct inode *inode);
1625
1626 /*
1627 * VFS helper functions..
1628 */
1629 extern int vfs_create(struct inode *, struct dentry *, umode_t, bool);
1630 extern int vfs_create2(struct vfsmount *, struct inode *, struct dentry *, umode_t, bool);
1631 extern int vfs_mkdir(struct inode *, struct dentry *, umode_t);
1632 extern int vfs_mkdir2(struct vfsmount *, struct inode *, struct dentry *, umode_t);
1633 extern int vfs_mknod(struct inode *, struct dentry *, umode_t, dev_t);
1634 extern int vfs_mknod2(struct vfsmount *, struct inode *, struct dentry *, umode_t, dev_t);
1635 extern int vfs_symlink(struct inode *, struct dentry *, const char *);
1636 extern int vfs_symlink2(struct vfsmount *, struct inode *, struct dentry *, const char *);
1637 extern int vfs_link(struct dentry *, struct inode *, struct dentry *, struct inode **);
1638 extern int vfs_link2(struct vfsmount *, struct dentry *, struct inode *, struct dentry *, struct inode **);
1639 extern int vfs_rmdir(struct inode *, struct dentry *);
1640 extern int vfs_rmdir2(struct vfsmount *, struct inode *, struct dentry *);
1641 extern int vfs_unlink(struct inode *, struct dentry *, struct inode **);
1642 extern int vfs_unlink2(struct vfsmount *, struct inode *, struct dentry *, struct inode **);
1643 extern int vfs_rename(struct inode *, struct dentry *, struct inode *, struct dentry *, struct inode **, unsigned int);
1644 extern int vfs_rename2(struct vfsmount *, struct inode *, struct dentry *, struct inode *, struct dentry *, struct inode **, unsigned int);
1645 extern int vfs_whiteout(struct inode *, struct dentry *);
1646
1647 /*
1648 * VFS file helper functions.
1649 */
1650 extern void inode_init_owner(struct inode *inode, const struct inode *dir,
1651 umode_t mode);
1652 extern bool may_open_dev(const struct path *path);
1653 /*
1654 * VFS FS_IOC_FIEMAP helper definitions.
1655 */
1656 struct fiemap_extent_info {
1657 unsigned int fi_flags; /* Flags as passed from user */
1658 unsigned int fi_extents_mapped; /* Number of mapped extents */
1659 unsigned int fi_extents_max; /* Size of fiemap_extent array */
1660 struct fiemap_extent __user *fi_extents_start; /* Start of
1661 fiemap_extent array */
1662 };
1663 int fiemap_fill_next_extent(struct fiemap_extent_info *info, u64 logical,
1664 u64 phys, u64 len, u32 flags);
1665 int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags);
1666
1667 /*
1668 * File types
1669 *
1670 * NOTE! These match bits 12..15 of stat.st_mode
1671 * (ie "(i_mode >> 12) & 15").
1672 */
1673 #define DT_UNKNOWN 0
1674 #define DT_FIFO 1
1675 #define DT_CHR 2
1676 #define DT_DIR 4
1677 #define DT_BLK 6
1678 #define DT_REG 8
1679 #define DT_LNK 10
1680 #define DT_SOCK 12
1681 #define DT_WHT 14
1682
1683 /*
1684 * This is the "filldir" function type, used by readdir() to let
1685 * the kernel specify what kind of dirent layout it wants to have.
1686 * This allows the kernel to read directories into kernel space or
1687 * to have different dirent layouts depending on the binary type.
1688 */
1689 struct dir_context;
1690 typedef int (*filldir_t)(struct dir_context *, const char *, int, loff_t, u64,
1691 unsigned);
1692
1693 struct dir_context {
1694 const filldir_t actor;
1695 loff_t pos;
1696 };
1697
1698 struct block_device_operations;
1699
1700 /* These macros are for out of kernel modules to test that
1701 * the kernel supports the unlocked_ioctl and compat_ioctl
1702 * fields in struct file_operations. */
1703 #define HAVE_COMPAT_IOCTL 1
1704 #define HAVE_UNLOCKED_IOCTL 1
1705
1706 /*
1707 * These flags let !MMU mmap() govern direct device mapping vs immediate
1708 * copying more easily for MAP_PRIVATE, especially for ROM filesystems.
1709 *
1710 * NOMMU_MAP_COPY: Copy can be mapped (MAP_PRIVATE)
1711 * NOMMU_MAP_DIRECT: Can be mapped directly (MAP_SHARED)
1712 * NOMMU_MAP_READ: Can be mapped for reading
1713 * NOMMU_MAP_WRITE: Can be mapped for writing
1714 * NOMMU_MAP_EXEC: Can be mapped for execution
1715 */
1716 #define NOMMU_MAP_COPY 0x00000001
1717 #define NOMMU_MAP_DIRECT 0x00000008
1718 #define NOMMU_MAP_READ VM_MAYREAD
1719 #define NOMMU_MAP_WRITE VM_MAYWRITE
1720 #define NOMMU_MAP_EXEC VM_MAYEXEC
1721
1722 #define NOMMU_VMFLAGS \
1723 (NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC)
1724
1725
1726 struct iov_iter;
1727
1728 struct file_operations {
1729 struct module *owner;
1730 loff_t (*llseek) (struct file *, loff_t, int);
1731 ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
1732 ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
1733 ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);
1734 ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);
1735 int (*iterate) (struct file *, struct dir_context *);
1736 int (*iterate_shared) (struct file *, struct dir_context *);
1737 unsigned int (*poll) (struct file *, struct poll_table_struct *);
1738 long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
1739 long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
1740 int (*mmap) (struct file *, struct vm_area_struct *);
1741 int (*open) (struct inode *, struct file *);
1742 int (*flush) (struct file *, fl_owner_t id);
1743 int (*release) (struct inode *, struct file *);
1744 int (*fsync) (struct file *, loff_t, loff_t, int datasync);
1745 int (*fasync) (int, struct file *, int);
1746 int (*lock) (struct file *, int, struct file_lock *);
1747 ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
1748 unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
1749 int (*check_flags)(int);
1750 int (*flock) (struct file *, int, struct file_lock *);
1751 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
1752 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
1753 int (*setlease)(struct file *, long, struct file_lock **, void **);
1754 long (*fallocate)(struct file *file, int mode, loff_t offset,
1755 loff_t len);
1756 void (*show_fdinfo)(struct seq_file *m, struct file *f);
1757 #ifndef CONFIG_MMU
1758 unsigned (*mmap_capabilities)(struct file *);
1759 #endif
1760 ssize_t (*copy_file_range)(struct file *, loff_t, struct file *,
1761 loff_t, size_t, unsigned int);
1762 int (*clone_file_range)(struct file *, loff_t, struct file *, loff_t,
1763 u64);
1764 ssize_t (*dedupe_file_range)(struct file *, u64, u64, struct file *,
1765 u64);
1766 };
1767
1768 struct inode_operations {
1769 struct dentry * (*lookup) (struct inode *,struct dentry *, unsigned int);
1770 const char * (*get_link) (struct dentry *, struct inode *, struct delayed_call *);
1771 int (*permission) (struct inode *, int);
1772 int (*permission2) (struct vfsmount *, struct inode *, int);
1773 struct posix_acl * (*get_acl)(struct inode *, int);
1774
1775 int (*readlink) (struct dentry *, char __user *,int);
1776
1777 int (*create) (struct inode *,struct dentry *, umode_t, bool);
1778 int (*link) (struct dentry *,struct inode *,struct dentry *);
1779 int (*unlink) (struct inode *,struct dentry *);
1780 int (*symlink) (struct inode *,struct dentry *,const char *);
1781 int (*mkdir) (struct inode *,struct dentry *,umode_t);
1782 int (*rmdir) (struct inode *,struct dentry *);
1783 int (*mknod) (struct inode *,struct dentry *,umode_t,dev_t);
1784 int (*rename) (struct inode *, struct dentry *,
1785 struct inode *, struct dentry *, unsigned int);
1786 int (*setattr) (struct dentry *, struct iattr *);
1787 int (*setattr2) (struct vfsmount *, struct dentry *, struct iattr *);
1788 int (*getattr) (struct vfsmount *mnt, struct dentry *, struct kstat *);
1789 ssize_t (*listxattr) (struct dentry *, char *, size_t);
1790 int (*fiemap)(struct inode *, struct fiemap_extent_info *, u64 start,
1791 u64 len);
1792 int (*update_time)(struct inode *, struct timespec *, int);
1793 int (*atomic_open)(struct inode *, struct dentry *,
1794 struct file *, unsigned open_flag,
1795 umode_t create_mode, int *opened);
1796 int (*tmpfile) (struct inode *, struct dentry *, umode_t);
1797 int (*set_acl)(struct inode *, struct posix_acl *, int);
1798 } ____cacheline_aligned;
1799
1800 ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
1801 unsigned long nr_segs, unsigned long fast_segs,
1802 struct iovec *fast_pointer,
1803 struct iovec **ret_pointer);
1804
1805 extern ssize_t __vfs_read(struct file *, char __user *, size_t, loff_t *);
1806 extern ssize_t __vfs_write(struct file *, const char __user *, size_t, loff_t *);
1807 extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
1808 extern ssize_t vfs_write(struct file *, const char __user *, size_t, loff_t *);
1809 extern ssize_t vfs_readv(struct file *, const struct iovec __user *,
1810 unsigned long, loff_t *, int);
1811 extern ssize_t vfs_writev(struct file *, const struct iovec __user *,
1812 unsigned long, loff_t *, int);
1813 extern ssize_t vfs_copy_file_range(struct file *, loff_t , struct file *,
1814 loff_t, size_t, unsigned int);
1815 extern int vfs_clone_file_range(struct file *file_in, loff_t pos_in,
1816 struct file *file_out, loff_t pos_out, u64 len);
1817 extern int vfs_dedupe_file_range(struct file *file,
1818 struct file_dedupe_range *same);
1819
1820 struct super_operations {
1821 struct inode *(*alloc_inode)(struct super_block *sb);
1822 void (*destroy_inode)(struct inode *);
1823
1824 void (*dirty_inode) (struct inode *, int flags);
1825 int (*write_inode) (struct inode *, struct writeback_control *wbc);
1826 int (*drop_inode) (struct inode *);
1827 void (*evict_inode) (struct inode *);
1828 void (*put_super) (struct super_block *);
1829 int (*sync_fs)(struct super_block *sb, int wait);
1830 int (*freeze_super) (struct super_block *);
1831 int (*freeze_fs) (struct super_block *);
1832 int (*thaw_super) (struct super_block *);
1833 int (*unfreeze_fs) (struct super_block *);
1834 int (*statfs) (struct dentry *, struct kstatfs *);
1835 int (*remount_fs) (struct super_block *, int *, char *);
1836 int (*remount_fs2) (struct vfsmount *, struct super_block *, int *, char *);
1837 void *(*clone_mnt_data) (void *);
1838 void (*copy_mnt_data) (void *, void *);
1839 void (*umount_begin) (struct super_block *);
1840
1841 int (*show_options)(struct seq_file *, struct dentry *);
1842 int (*show_options2)(struct vfsmount *,struct seq_file *, struct dentry *);
1843 int (*show_devname)(struct seq_file *, struct dentry *);
1844 int (*show_path)(struct seq_file *, struct dentry *);
1845 int (*show_stats)(struct seq_file *, struct dentry *);
1846 #ifdef CONFIG_QUOTA
1847 ssize_t (*quota_read)(struct super_block *, int, char *, size_t, loff_t);
1848 ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t);
1849 struct dquot **(*get_dquots)(struct inode *);
1850 #endif
1851 int (*bdev_try_to_free_page)(struct super_block*, struct page*, gfp_t);
1852 long (*nr_cached_objects)(struct super_block *,
1853 struct shrink_control *);
1854 long (*free_cached_objects)(struct super_block *,
1855 struct shrink_control *);
1856 };
1857
1858 /*
1859 * Inode flags - they have no relation to superblock flags now
1860 */
1861 #define S_SYNC 1 /* Writes are synced at once */
1862 #define S_NOATIME 2 /* Do not update access times */
1863 #define S_APPEND 4 /* Append-only file */
1864 #define S_IMMUTABLE 8 /* Immutable file */
1865 #define S_DEAD 16 /* removed, but still open directory */
1866 #define S_NOQUOTA 32 /* Inode is not counted to quota */
1867 #define S_DIRSYNC 64 /* Directory modifications are synchronous */
1868 #define S_NOCMTIME 128 /* Do not update file c/mtime */
1869 #define S_SWAPFILE 256 /* Do not truncate: swapon got its bmaps */
1870 #define S_PRIVATE 512 /* Inode is fs-internal */
1871 #define S_IMA 1024 /* Inode has an associated IMA struct */
1872 #define S_AUTOMOUNT 2048 /* Automount/referral quasi-directory */
1873 #define S_NOSEC 4096 /* no suid or xattr security attributes */
1874 #ifdef CONFIG_FS_DAX
1875 #define S_DAX 8192 /* Direct Access, avoiding the page cache */
1876 #else
1877 #define S_DAX 0 /* Make all the DAX code disappear */
1878 #endif
1879 #define S_ENCRYPTED 16384 /* Encrypted file (using fs/crypto/) */
1880
1881 /*
1882 * Note that nosuid etc flags are inode-specific: setting some file-system
1883 * flags just means all the inodes inherit those flags by default. It might be
1884 * possible to override it selectively if you really wanted to with some
1885 * ioctl() that is not currently implemented.
1886 *
1887 * Exception: MS_RDONLY is always applied to the entire file system.
1888 *
1889 * Unfortunately, it is possible to change a filesystems flags with it mounted
1890 * with files in use. This means that all of the inodes will not have their
1891 * i_flags updated. Hence, i_flags no longer inherit the superblock mount
1892 * flags, so these have to be checked separately. -- rmk@arm.uk.linux.org
1893 */
1894 #define __IS_FLG(inode, flg) ((inode)->i_sb->s_flags & (flg))
1895
1896 #define IS_RDONLY(inode) ((inode)->i_sb->s_flags & MS_RDONLY)
1897 #define IS_SYNC(inode) (__IS_FLG(inode, MS_SYNCHRONOUS) || \
1898 ((inode)->i_flags & S_SYNC))
1899 #define IS_DIRSYNC(inode) (__IS_FLG(inode, MS_SYNCHRONOUS|MS_DIRSYNC) || \
1900 ((inode)->i_flags & (S_SYNC|S_DIRSYNC)))
1901 #define IS_MANDLOCK(inode) __IS_FLG(inode, MS_MANDLOCK)
1902 #define IS_NOATIME(inode) __IS_FLG(inode, MS_RDONLY|MS_NOATIME)
1903 #define IS_I_VERSION(inode) __IS_FLG(inode, MS_I_VERSION)
1904
1905 #define IS_NOQUOTA(inode) ((inode)->i_flags & S_NOQUOTA)
1906 #define IS_APPEND(inode) ((inode)->i_flags & S_APPEND)
1907 #define IS_IMMUTABLE(inode) ((inode)->i_flags & S_IMMUTABLE)
1908 #define IS_POSIXACL(inode) __IS_FLG(inode, MS_POSIXACL)
1909
1910 #define IS_DEADDIR(inode) ((inode)->i_flags & S_DEAD)
1911 #define IS_NOCMTIME(inode) ((inode)->i_flags & S_NOCMTIME)
1912 #define IS_SWAPFILE(inode) ((inode)->i_flags & S_SWAPFILE)
1913 #define IS_PRIVATE(inode) ((inode)->i_flags & S_PRIVATE)
1914 #define IS_IMA(inode) ((inode)->i_flags & S_IMA)
1915 #define IS_AUTOMOUNT(inode) ((inode)->i_flags & S_AUTOMOUNT)
1916 #define IS_NOSEC(inode) ((inode)->i_flags & S_NOSEC)
1917 #define IS_DAX(inode) ((inode)->i_flags & S_DAX)
1918 #define IS_ENCRYPTED(inode) ((inode)->i_flags & S_ENCRYPTED)
1919
1920 #define IS_WHITEOUT(inode) (S_ISCHR(inode->i_mode) && \
1921 (inode)->i_rdev == WHITEOUT_DEV)
1922
HAS_UNMAPPED_ID(struct inode * inode)1923 static inline bool HAS_UNMAPPED_ID(struct inode *inode)
1924 {
1925 return !uid_valid(inode->i_uid) || !gid_valid(inode->i_gid);
1926 }
1927
1928 /*
1929 * Inode state bits. Protected by inode->i_lock
1930 *
1931 * Three bits determine the dirty state of the inode, I_DIRTY_SYNC,
1932 * I_DIRTY_DATASYNC and I_DIRTY_PAGES.
1933 *
1934 * Four bits define the lifetime of an inode. Initially, inodes are I_NEW,
1935 * until that flag is cleared. I_WILL_FREE, I_FREEING and I_CLEAR are set at
1936 * various stages of removing an inode.
1937 *
1938 * Two bits are used for locking and completion notification, I_NEW and I_SYNC.
1939 *
1940 * I_DIRTY_SYNC Inode is dirty, but doesn't have to be written on
1941 * fdatasync(). i_atime is the usual cause.
1942 * I_DIRTY_DATASYNC Data-related inode changes pending. We keep track of
1943 * these changes separately from I_DIRTY_SYNC so that we
1944 * don't have to write inode on fdatasync() when only
1945 * mtime has changed in it.
1946 * I_DIRTY_PAGES Inode has dirty pages. Inode itself may be clean.
1947 * I_NEW Serves as both a mutex and completion notification.
1948 * New inodes set I_NEW. If two processes both create
1949 * the same inode, one of them will release its inode and
1950 * wait for I_NEW to be released before returning.
1951 * Inodes in I_WILL_FREE, I_FREEING or I_CLEAR state can
1952 * also cause waiting on I_NEW, without I_NEW actually
1953 * being set. find_inode() uses this to prevent returning
1954 * nearly-dead inodes.
1955 * I_WILL_FREE Must be set when calling write_inode_now() if i_count
1956 * is zero. I_FREEING must be set when I_WILL_FREE is
1957 * cleared.
1958 * I_FREEING Set when inode is about to be freed but still has dirty
1959 * pages or buffers attached or the inode itself is still
1960 * dirty.
1961 * I_CLEAR Added by clear_inode(). In this state the inode is
1962 * clean and can be destroyed. Inode keeps I_FREEING.
1963 *
1964 * Inodes that are I_WILL_FREE, I_FREEING or I_CLEAR are
1965 * prohibited for many purposes. iget() must wait for
1966 * the inode to be completely released, then create it
1967 * anew. Other functions will just ignore such inodes,
1968 * if appropriate. I_NEW is used for waiting.
1969 *
1970 * I_SYNC Writeback of inode is running. The bit is set during
1971 * data writeback, and cleared with a wakeup on the bit
1972 * address once it is done. The bit is also used to pin
1973 * the inode in memory for flusher thread.
1974 *
1975 * I_REFERENCED Marks the inode as recently references on the LRU list.
1976 *
1977 * I_DIO_WAKEUP Never set. Only used as a key for wait_on_bit().
1978 *
1979 * I_WB_SWITCH Cgroup bdi_writeback switching in progress. Used to
1980 * synchronize competing switching instances and to tell
1981 * wb stat updates to grab mapping->tree_lock. See
1982 * inode_switch_wb_work_fn() for details.
1983 *
1984 * Q: What is the difference between I_WILL_FREE and I_FREEING?
1985 */
1986 #define I_DIRTY_SYNC (1 << 0)
1987 #define I_DIRTY_DATASYNC (1 << 1)
1988 #define I_DIRTY_PAGES (1 << 2)
1989 #define __I_NEW 3
1990 #define I_NEW (1 << __I_NEW)
1991 #define I_WILL_FREE (1 << 4)
1992 #define I_FREEING (1 << 5)
1993 #define I_CLEAR (1 << 6)
1994 #define __I_SYNC 7
1995 #define I_SYNC (1 << __I_SYNC)
1996 #define I_REFERENCED (1 << 8)
1997 #define __I_DIO_WAKEUP 9
1998 #define I_DIO_WAKEUP (1 << __I_DIO_WAKEUP)
1999 #define I_LINKABLE (1 << 10)
2000 #define I_DIRTY_TIME (1 << 11)
2001 #define __I_DIRTY_TIME_EXPIRED 12
2002 #define I_DIRTY_TIME_EXPIRED (1 << __I_DIRTY_TIME_EXPIRED)
2003 #define I_WB_SWITCH (1 << 13)
2004
2005 #define I_DIRTY (I_DIRTY_SYNC | I_DIRTY_DATASYNC | I_DIRTY_PAGES)
2006 #define I_DIRTY_ALL (I_DIRTY | I_DIRTY_TIME)
2007
2008 extern void __mark_inode_dirty(struct inode *, int);
mark_inode_dirty(struct inode * inode)2009 static inline void mark_inode_dirty(struct inode *inode)
2010 {
2011 __mark_inode_dirty(inode, I_DIRTY);
2012 }
2013
mark_inode_dirty_sync(struct inode * inode)2014 static inline void mark_inode_dirty_sync(struct inode *inode)
2015 {
2016 __mark_inode_dirty(inode, I_DIRTY_SYNC);
2017 }
2018
2019 extern void inc_nlink(struct inode *inode);
2020 extern void drop_nlink(struct inode *inode);
2021 extern void clear_nlink(struct inode *inode);
2022 extern void set_nlink(struct inode *inode, unsigned int nlink);
2023
inode_inc_link_count(struct inode * inode)2024 static inline void inode_inc_link_count(struct inode *inode)
2025 {
2026 inc_nlink(inode);
2027 mark_inode_dirty(inode);
2028 }
2029
inode_dec_link_count(struct inode * inode)2030 static inline void inode_dec_link_count(struct inode *inode)
2031 {
2032 drop_nlink(inode);
2033 mark_inode_dirty(inode);
2034 }
2035
2036 /**
2037 * inode_inc_iversion - increments i_version
2038 * @inode: inode that need to be updated
2039 *
2040 * Every time the inode is modified, the i_version field will be incremented.
2041 * The filesystem has to be mounted with i_version flag
2042 */
2043
inode_inc_iversion(struct inode * inode)2044 static inline void inode_inc_iversion(struct inode *inode)
2045 {
2046 spin_lock(&inode->i_lock);
2047 inode->i_version++;
2048 spin_unlock(&inode->i_lock);
2049 }
2050
2051 enum file_time_flags {
2052 S_ATIME = 1,
2053 S_MTIME = 2,
2054 S_CTIME = 4,
2055 S_VERSION = 8,
2056 };
2057
2058 extern void touch_atime(const struct path *);
file_accessed(struct file * file)2059 static inline void file_accessed(struct file *file)
2060 {
2061 if (!(file->f_flags & O_NOATIME))
2062 touch_atime(&file->f_path);
2063 }
2064
2065 int sync_inode(struct inode *inode, struct writeback_control *wbc);
2066 int sync_inode_metadata(struct inode *inode, int wait);
2067
2068 struct file_system_type {
2069 const char *name;
2070 int fs_flags;
2071 #define FS_REQUIRES_DEV 1
2072 #define FS_BINARY_MOUNTDATA 2
2073 #define FS_HAS_SUBTYPE 4
2074 #define FS_USERNS_MOUNT 8 /* Can be mounted by userns root */
2075 #define FS_RENAME_DOES_D_MOVE 32768 /* FS will handle d_move() during rename() internally. */
2076 struct dentry *(*mount) (struct file_system_type *, int,
2077 const char *, void *);
2078 struct dentry *(*mount2) (struct vfsmount *, struct file_system_type *, int,
2079 const char *, void *);
2080 void *(*alloc_mnt_data) (void);
2081 void (*kill_sb) (struct super_block *);
2082 struct module *owner;
2083 struct file_system_type * next;
2084 struct hlist_head fs_supers;
2085
2086 struct lock_class_key s_lock_key;
2087 struct lock_class_key s_umount_key;
2088 struct lock_class_key s_vfs_rename_key;
2089 struct lock_class_key s_writers_key[SB_FREEZE_LEVELS];
2090
2091 struct lock_class_key i_lock_key;
2092 struct lock_class_key i_mutex_key;
2093 struct lock_class_key i_mutex_dir_key;
2094 };
2095
2096 #define MODULE_ALIAS_FS(NAME) MODULE_ALIAS("fs-" NAME)
2097
2098 extern struct dentry *mount_ns(struct file_system_type *fs_type,
2099 int flags, void *data, void *ns, struct user_namespace *user_ns,
2100 int (*fill_super)(struct super_block *, void *, int));
2101 extern struct dentry *mount_bdev(struct file_system_type *fs_type,
2102 int flags, const char *dev_name, void *data,
2103 int (*fill_super)(struct super_block *, void *, int));
2104 extern struct dentry *mount_single(struct file_system_type *fs_type,
2105 int flags, void *data,
2106 int (*fill_super)(struct super_block *, void *, int));
2107 extern struct dentry *mount_nodev(struct file_system_type *fs_type,
2108 int flags, void *data,
2109 int (*fill_super)(struct super_block *, void *, int));
2110 extern struct dentry *mount_subtree(struct vfsmount *mnt, const char *path);
2111 void generic_shutdown_super(struct super_block *sb);
2112 void kill_block_super(struct super_block *sb);
2113 void kill_anon_super(struct super_block *sb);
2114 void kill_litter_super(struct super_block *sb);
2115 void deactivate_super(struct super_block *sb);
2116 void deactivate_locked_super(struct super_block *sb);
2117 int set_anon_super(struct super_block *s, void *data);
2118 int get_anon_bdev(dev_t *);
2119 void free_anon_bdev(dev_t);
2120 struct super_block *sget_userns(struct file_system_type *type,
2121 int (*test)(struct super_block *,void *),
2122 int (*set)(struct super_block *,void *),
2123 int flags, struct user_namespace *user_ns,
2124 void *data);
2125 struct super_block *sget(struct file_system_type *type,
2126 int (*test)(struct super_block *,void *),
2127 int (*set)(struct super_block *,void *),
2128 int flags, void *data);
2129 extern struct dentry *mount_pseudo_xattr(struct file_system_type *, char *,
2130 const struct super_operations *ops,
2131 const struct xattr_handler **xattr,
2132 const struct dentry_operations *dops,
2133 unsigned long);
2134
2135 static inline struct dentry *
mount_pseudo(struct file_system_type * fs_type,char * name,const struct super_operations * ops,const struct dentry_operations * dops,unsigned long magic)2136 mount_pseudo(struct file_system_type *fs_type, char *name,
2137 const struct super_operations *ops,
2138 const struct dentry_operations *dops, unsigned long magic)
2139 {
2140 return mount_pseudo_xattr(fs_type, name, ops, NULL, dops, magic);
2141 }
2142
2143 /* Alas, no aliases. Too much hassle with bringing module.h everywhere */
2144 #define fops_get(fops) \
2145 (((fops) && try_module_get((fops)->owner) ? (fops) : NULL))
2146 #define fops_put(fops) \
2147 do { if (fops) module_put((fops)->owner); } while(0)
2148 /*
2149 * This one is to be used *ONLY* from ->open() instances.
2150 * fops must be non-NULL, pinned down *and* module dependencies
2151 * should be sufficient to pin the caller down as well.
2152 */
2153 #define replace_fops(f, fops) \
2154 do { \
2155 struct file *__file = (f); \
2156 fops_put(__file->f_op); \
2157 BUG_ON(!(__file->f_op = (fops))); \
2158 } while(0)
2159
2160 extern int register_filesystem(struct file_system_type *);
2161 extern int unregister_filesystem(struct file_system_type *);
2162 extern struct vfsmount *kern_mount_data(struct file_system_type *, void *data);
2163 #define kern_mount(type) kern_mount_data(type, NULL)
2164 extern void kern_unmount(struct vfsmount *mnt);
2165 extern int may_umount_tree(struct vfsmount *);
2166 extern int may_umount(struct vfsmount *);
2167 extern long do_mount(const char *, const char __user *,
2168 const char *, unsigned long, void *);
2169 extern struct vfsmount *collect_mounts(struct path *);
2170 extern void drop_collected_mounts(struct vfsmount *);
2171 extern int iterate_mounts(int (*)(struct vfsmount *, void *), void *,
2172 struct vfsmount *);
2173 extern int vfs_statfs(struct path *, struct kstatfs *);
2174 extern int user_statfs(const char __user *, struct kstatfs *);
2175 extern int fd_statfs(int, struct kstatfs *);
2176 extern int vfs_ustat(dev_t, struct kstatfs *);
2177 extern int freeze_super(struct super_block *super);
2178 extern int thaw_super(struct super_block *super);
2179 extern bool our_mnt(struct vfsmount *mnt);
2180
2181 extern int current_umask(void);
2182
2183 extern void ihold(struct inode * inode);
2184 extern void iput(struct inode *);
2185 extern int generic_update_time(struct inode *, struct timespec *, int);
2186
2187 /* /sys/fs */
2188 extern struct kobject *fs_kobj;
2189
2190 #define MAX_RW_COUNT (INT_MAX & PAGE_MASK)
2191
2192 #ifdef CONFIG_MANDATORY_FILE_LOCKING
2193 extern int locks_mandatory_locked(struct file *);
2194 extern int locks_mandatory_area(struct inode *, struct file *, loff_t, loff_t, unsigned char);
2195
2196 /*
2197 * Candidates for mandatory locking have the setgid bit set
2198 * but no group execute bit - an otherwise meaningless combination.
2199 */
2200
__mandatory_lock(struct inode * ino)2201 static inline int __mandatory_lock(struct inode *ino)
2202 {
2203 return (ino->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID;
2204 }
2205
2206 /*
2207 * ... and these candidates should be on MS_MANDLOCK mounted fs,
2208 * otherwise these will be advisory locks
2209 */
2210
mandatory_lock(struct inode * ino)2211 static inline int mandatory_lock(struct inode *ino)
2212 {
2213 return IS_MANDLOCK(ino) && __mandatory_lock(ino);
2214 }
2215
locks_verify_locked(struct file * file)2216 static inline int locks_verify_locked(struct file *file)
2217 {
2218 if (mandatory_lock(locks_inode(file)))
2219 return locks_mandatory_locked(file);
2220 return 0;
2221 }
2222
locks_verify_truncate(struct inode * inode,struct file * f,loff_t size)2223 static inline int locks_verify_truncate(struct inode *inode,
2224 struct file *f,
2225 loff_t size)
2226 {
2227 if (!inode->i_flctx || !mandatory_lock(inode))
2228 return 0;
2229
2230 if (size < inode->i_size) {
2231 return locks_mandatory_area(inode, f, size, inode->i_size - 1,
2232 F_WRLCK);
2233 } else {
2234 return locks_mandatory_area(inode, f, inode->i_size, size - 1,
2235 F_WRLCK);
2236 }
2237 }
2238
2239 #else /* !CONFIG_MANDATORY_FILE_LOCKING */
2240
locks_mandatory_locked(struct file * file)2241 static inline int locks_mandatory_locked(struct file *file)
2242 {
2243 return 0;
2244 }
2245
locks_mandatory_area(struct inode * inode,struct file * filp,loff_t start,loff_t end,unsigned char type)2246 static inline int locks_mandatory_area(struct inode *inode, struct file *filp,
2247 loff_t start, loff_t end, unsigned char type)
2248 {
2249 return 0;
2250 }
2251
__mandatory_lock(struct inode * inode)2252 static inline int __mandatory_lock(struct inode *inode)
2253 {
2254 return 0;
2255 }
2256
mandatory_lock(struct inode * inode)2257 static inline int mandatory_lock(struct inode *inode)
2258 {
2259 return 0;
2260 }
2261
locks_verify_locked(struct file * file)2262 static inline int locks_verify_locked(struct file *file)
2263 {
2264 return 0;
2265 }
2266
locks_verify_truncate(struct inode * inode,struct file * filp,size_t size)2267 static inline int locks_verify_truncate(struct inode *inode, struct file *filp,
2268 size_t size)
2269 {
2270 return 0;
2271 }
2272
2273 #endif /* CONFIG_MANDATORY_FILE_LOCKING */
2274
2275
2276 #ifdef CONFIG_FILE_LOCKING
break_lease(struct inode * inode,unsigned int mode)2277 static inline int break_lease(struct inode *inode, unsigned int mode)
2278 {
2279 /*
2280 * Since this check is lockless, we must ensure that any refcounts
2281 * taken are done before checking i_flctx->flc_lease. Otherwise, we
2282 * could end up racing with tasks trying to set a new lease on this
2283 * file.
2284 */
2285 smp_mb();
2286 if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease))
2287 return __break_lease(inode, mode, FL_LEASE);
2288 return 0;
2289 }
2290
break_deleg(struct inode * inode,unsigned int mode)2291 static inline int break_deleg(struct inode *inode, unsigned int mode)
2292 {
2293 /*
2294 * Since this check is lockless, we must ensure that any refcounts
2295 * taken are done before checking i_flctx->flc_lease. Otherwise, we
2296 * could end up racing with tasks trying to set a new lease on this
2297 * file.
2298 */
2299 smp_mb();
2300 if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease))
2301 return __break_lease(inode, mode, FL_DELEG);
2302 return 0;
2303 }
2304
try_break_deleg(struct inode * inode,struct inode ** delegated_inode)2305 static inline int try_break_deleg(struct inode *inode, struct inode **delegated_inode)
2306 {
2307 int ret;
2308
2309 ret = break_deleg(inode, O_WRONLY|O_NONBLOCK);
2310 if (ret == -EWOULDBLOCK && delegated_inode) {
2311 *delegated_inode = inode;
2312 ihold(inode);
2313 }
2314 return ret;
2315 }
2316
break_deleg_wait(struct inode ** delegated_inode)2317 static inline int break_deleg_wait(struct inode **delegated_inode)
2318 {
2319 int ret;
2320
2321 ret = break_deleg(*delegated_inode, O_WRONLY);
2322 iput(*delegated_inode);
2323 *delegated_inode = NULL;
2324 return ret;
2325 }
2326
break_layout(struct inode * inode,bool wait)2327 static inline int break_layout(struct inode *inode, bool wait)
2328 {
2329 smp_mb();
2330 if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease))
2331 return __break_lease(inode,
2332 wait ? O_WRONLY : O_WRONLY | O_NONBLOCK,
2333 FL_LAYOUT);
2334 return 0;
2335 }
2336
2337 #else /* !CONFIG_FILE_LOCKING */
break_lease(struct inode * inode,unsigned int mode)2338 static inline int break_lease(struct inode *inode, unsigned int mode)
2339 {
2340 return 0;
2341 }
2342
break_deleg(struct inode * inode,unsigned int mode)2343 static inline int break_deleg(struct inode *inode, unsigned int mode)
2344 {
2345 return 0;
2346 }
2347
try_break_deleg(struct inode * inode,struct inode ** delegated_inode)2348 static inline int try_break_deleg(struct inode *inode, struct inode **delegated_inode)
2349 {
2350 return 0;
2351 }
2352
break_deleg_wait(struct inode ** delegated_inode)2353 static inline int break_deleg_wait(struct inode **delegated_inode)
2354 {
2355 BUG();
2356 return 0;
2357 }
2358
break_layout(struct inode * inode,bool wait)2359 static inline int break_layout(struct inode *inode, bool wait)
2360 {
2361 return 0;
2362 }
2363
2364 #endif /* CONFIG_FILE_LOCKING */
2365
2366 /* fs/open.c */
2367 struct audit_names;
2368 struct filename {
2369 const char *name; /* pointer to actual string */
2370 const __user char *uptr; /* original userland pointer */
2371 struct audit_names *aname;
2372 int refcnt;
2373 const char iname[];
2374 };
2375
2376 extern long vfs_truncate(const struct path *, loff_t);
2377 extern int do_truncate(struct dentry *, loff_t start, unsigned int time_attrs,
2378 struct file *filp);
2379 extern int do_truncate2(struct vfsmount *, struct dentry *, loff_t start,
2380 unsigned int time_attrs, struct file *filp);
2381 extern int vfs_fallocate(struct file *file, int mode, loff_t offset,
2382 loff_t len);
2383 extern long do_sys_open(int dfd, const char __user *filename, int flags,
2384 umode_t mode);
2385 extern struct file *file_open_name(struct filename *, int, umode_t);
2386 extern struct file *filp_open(const char *, int, umode_t);
2387 extern struct file *file_open_root(struct dentry *, struct vfsmount *,
2388 const char *, int, umode_t);
2389 extern struct file * dentry_open(const struct path *, int, const struct cred *);
2390 extern int filp_close(struct file *, fl_owner_t id);
2391
2392 extern struct filename *getname_flags(const char __user *, int, int *);
2393 extern struct filename *getname(const char __user *);
2394 extern struct filename *getname_kernel(const char *);
2395 extern void putname(struct filename *name);
2396
2397 enum {
2398 FILE_CREATED = 1,
2399 FILE_OPENED = 2
2400 };
2401 extern int finish_open(struct file *file, struct dentry *dentry,
2402 int (*open)(struct inode *, struct file *),
2403 int *opened);
2404 extern int finish_no_open(struct file *file, struct dentry *dentry);
2405
2406 /* fs/ioctl.c */
2407
2408 extern int ioctl_preallocate(struct file *filp, void __user *argp);
2409
2410 /* fs/dcache.c */
2411 extern void __init vfs_caches_init_early(void);
2412 extern void __init vfs_caches_init(void);
2413
2414 extern struct kmem_cache *names_cachep;
2415
2416 #define __getname() kmem_cache_alloc(names_cachep, GFP_KERNEL)
2417 #define __putname(name) kmem_cache_free(names_cachep, (void *)(name))
2418
2419 #ifdef CONFIG_BLOCK
2420 extern int register_blkdev(unsigned int, const char *);
2421 extern void unregister_blkdev(unsigned int, const char *);
2422 extern struct block_device *bdget(dev_t);
2423 extern struct block_device *bdgrab(struct block_device *bdev);
2424 extern void bd_set_size(struct block_device *, loff_t size);
2425 extern void bd_forget(struct inode *inode);
2426 extern void bdput(struct block_device *);
2427 extern void invalidate_bdev(struct block_device *);
2428 extern void iterate_bdevs(void (*)(struct block_device *, void *), void *);
2429 extern int sync_blockdev(struct block_device *bdev);
2430 extern void kill_bdev(struct block_device *);
2431 extern struct super_block *freeze_bdev(struct block_device *);
2432 extern void emergency_thaw_all(void);
2433 extern int thaw_bdev(struct block_device *bdev, struct super_block *sb);
2434 extern int fsync_bdev(struct block_device *);
2435
2436 extern struct super_block *blockdev_superblock;
2437
sb_is_blkdev_sb(struct super_block * sb)2438 static inline bool sb_is_blkdev_sb(struct super_block *sb)
2439 {
2440 return sb == blockdev_superblock;
2441 }
2442 #else
bd_forget(struct inode * inode)2443 static inline void bd_forget(struct inode *inode) {}
sync_blockdev(struct block_device * bdev)2444 static inline int sync_blockdev(struct block_device *bdev) { return 0; }
kill_bdev(struct block_device * bdev)2445 static inline void kill_bdev(struct block_device *bdev) {}
invalidate_bdev(struct block_device * bdev)2446 static inline void invalidate_bdev(struct block_device *bdev) {}
2447
freeze_bdev(struct block_device * sb)2448 static inline struct super_block *freeze_bdev(struct block_device *sb)
2449 {
2450 return NULL;
2451 }
2452
thaw_bdev(struct block_device * bdev,struct super_block * sb)2453 static inline int thaw_bdev(struct block_device *bdev, struct super_block *sb)
2454 {
2455 return 0;
2456 }
2457
iterate_bdevs(void (* f)(struct block_device *,void *),void * arg)2458 static inline void iterate_bdevs(void (*f)(struct block_device *, void *), void *arg)
2459 {
2460 }
2461
sb_is_blkdev_sb(struct super_block * sb)2462 static inline bool sb_is_blkdev_sb(struct super_block *sb)
2463 {
2464 return false;
2465 }
2466 #endif
2467 extern int sync_filesystem(struct super_block *);
2468 extern const struct file_operations def_blk_fops;
2469 extern const struct file_operations def_chr_fops;
2470 #ifdef CONFIG_BLOCK
2471 extern int ioctl_by_bdev(struct block_device *, unsigned, unsigned long);
2472 extern int blkdev_ioctl(struct block_device *, fmode_t, unsigned, unsigned long);
2473 extern long compat_blkdev_ioctl(struct file *, unsigned, unsigned long);
2474 extern int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder);
2475 extern struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
2476 void *holder);
2477 extern struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode,
2478 void *holder);
2479 extern void blkdev_put(struct block_device *bdev, fmode_t mode);
2480 extern int __blkdev_reread_part(struct block_device *bdev);
2481 extern int blkdev_reread_part(struct block_device *bdev);
2482
2483 #ifdef CONFIG_SYSFS
2484 extern int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk);
2485 extern void bd_unlink_disk_holder(struct block_device *bdev,
2486 struct gendisk *disk);
2487 #else
bd_link_disk_holder(struct block_device * bdev,struct gendisk * disk)2488 static inline int bd_link_disk_holder(struct block_device *bdev,
2489 struct gendisk *disk)
2490 {
2491 return 0;
2492 }
bd_unlink_disk_holder(struct block_device * bdev,struct gendisk * disk)2493 static inline void bd_unlink_disk_holder(struct block_device *bdev,
2494 struct gendisk *disk)
2495 {
2496 }
2497 #endif
2498 #endif
2499
2500 /* fs/char_dev.c */
2501 #define CHRDEV_MAJOR_HASH_SIZE 255
2502 /* Marks the bottom of the first segment of free char majors */
2503 #define CHRDEV_MAJOR_DYN_END 234
2504 extern int alloc_chrdev_region(dev_t *, unsigned, unsigned, const char *);
2505 extern int register_chrdev_region(dev_t, unsigned, const char *);
2506 extern int __register_chrdev(unsigned int major, unsigned int baseminor,
2507 unsigned int count, const char *name,
2508 const struct file_operations *fops);
2509 extern void __unregister_chrdev(unsigned int major, unsigned int baseminor,
2510 unsigned int count, const char *name);
2511 extern void unregister_chrdev_region(dev_t, unsigned);
2512 extern void chrdev_show(struct seq_file *,off_t);
2513
register_chrdev(unsigned int major,const char * name,const struct file_operations * fops)2514 static inline int register_chrdev(unsigned int major, const char *name,
2515 const struct file_operations *fops)
2516 {
2517 return __register_chrdev(major, 0, 256, name, fops);
2518 }
2519
unregister_chrdev(unsigned int major,const char * name)2520 static inline void unregister_chrdev(unsigned int major, const char *name)
2521 {
2522 __unregister_chrdev(major, 0, 256, name);
2523 }
2524
2525 /* fs/block_dev.c */
2526 #define BDEVNAME_SIZE 32 /* Largest string for a blockdev identifier */
2527 #define BDEVT_SIZE 10 /* Largest string for MAJ:MIN for blkdev */
2528
2529 #ifdef CONFIG_BLOCK
2530 #define BLKDEV_MAJOR_HASH_SIZE 255
2531 extern const char *__bdevname(dev_t, char *buffer);
2532 extern const char *bdevname(struct block_device *bdev, char *buffer);
2533 extern struct block_device *lookup_bdev(const char *);
2534 extern void blkdev_show(struct seq_file *,off_t);
2535
2536 #else
2537 #define BLKDEV_MAJOR_HASH_SIZE 0
2538 #endif
2539
2540 extern void init_special_inode(struct inode *, umode_t, dev_t);
2541
2542 /* Invalid inode operations -- fs/bad_inode.c */
2543 extern void make_bad_inode(struct inode *);
2544 extern bool is_bad_inode(struct inode *);
2545
2546 #ifdef CONFIG_BLOCK
op_is_write(unsigned int op)2547 static inline bool op_is_write(unsigned int op)
2548 {
2549 return op == REQ_OP_READ ? false : true;
2550 }
2551
2552 /*
2553 * return data direction, READ or WRITE
2554 */
bio_data_dir(struct bio * bio)2555 static inline int bio_data_dir(struct bio *bio)
2556 {
2557 return op_is_write(bio_op(bio)) ? WRITE : READ;
2558 }
2559
2560 extern void check_disk_size_change(struct gendisk *disk,
2561 struct block_device *bdev);
2562 extern int revalidate_disk(struct gendisk *);
2563 extern int check_disk_change(struct block_device *);
2564 extern int __invalidate_device(struct block_device *, bool);
2565 extern int invalidate_partition(struct gendisk *, int);
2566 #endif
2567 unsigned long invalidate_mapping_pages(struct address_space *mapping,
2568 pgoff_t start, pgoff_t end);
2569
invalidate_remote_inode(struct inode * inode)2570 static inline void invalidate_remote_inode(struct inode *inode)
2571 {
2572 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
2573 S_ISLNK(inode->i_mode))
2574 invalidate_mapping_pages(inode->i_mapping, 0, -1);
2575 }
2576 extern int invalidate_inode_pages2(struct address_space *mapping);
2577 extern int invalidate_inode_pages2_range(struct address_space *mapping,
2578 pgoff_t start, pgoff_t end);
2579 extern int write_inode_now(struct inode *, int);
2580 extern int filemap_fdatawrite(struct address_space *);
2581 extern int filemap_flush(struct address_space *);
2582 extern int filemap_fdatawait(struct address_space *);
2583 extern void filemap_fdatawait_keep_errors(struct address_space *);
2584 extern int filemap_fdatawait_range(struct address_space *, loff_t lstart,
2585 loff_t lend);
2586 extern int filemap_write_and_wait(struct address_space *mapping);
2587 extern int filemap_write_and_wait_range(struct address_space *mapping,
2588 loff_t lstart, loff_t lend);
2589 extern int __filemap_fdatawrite_range(struct address_space *mapping,
2590 loff_t start, loff_t end, int sync_mode);
2591 extern int filemap_fdatawrite_range(struct address_space *mapping,
2592 loff_t start, loff_t end);
2593 extern int filemap_check_errors(struct address_space *mapping);
2594
2595 extern int vfs_fsync_range(struct file *file, loff_t start, loff_t end,
2596 int datasync);
2597 extern int vfs_fsync(struct file *file, int datasync);
2598
2599 /*
2600 * Sync the bytes written if this was a synchronous write. Expect ki_pos
2601 * to already be updated for the write, and will return either the amount
2602 * of bytes passed in, or an error if syncing the file failed.
2603 */
generic_write_sync(struct kiocb * iocb,ssize_t count)2604 static inline ssize_t generic_write_sync(struct kiocb *iocb, ssize_t count)
2605 {
2606 if (iocb->ki_flags & IOCB_DSYNC) {
2607 int ret = vfs_fsync_range(iocb->ki_filp,
2608 iocb->ki_pos - count, iocb->ki_pos - 1,
2609 (iocb->ki_flags & IOCB_SYNC) ? 0 : 1);
2610 if (ret)
2611 return ret;
2612 }
2613
2614 return count;
2615 }
2616
2617 extern void emergency_sync(void);
2618 extern void emergency_remount(void);
2619 #ifdef CONFIG_BLOCK
2620 extern sector_t bmap(struct inode *, sector_t);
2621 #endif
2622 extern int notify_change(struct dentry *, struct iattr *, struct inode **);
2623 extern int notify_change2(struct vfsmount *, struct dentry *, struct iattr *, struct inode **);
2624 extern int inode_permission(struct inode *, int);
2625 extern int inode_permission2(struct vfsmount *, struct inode *, int);
2626 extern int __inode_permission(struct inode *, int);
2627 extern int __inode_permission2(struct vfsmount *, struct inode *, int);
2628 extern int generic_permission(struct inode *, int);
2629 extern int __check_sticky(struct inode *dir, struct inode *inode);
2630
execute_ok(struct inode * inode)2631 static inline bool execute_ok(struct inode *inode)
2632 {
2633 return (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode);
2634 }
2635
file_start_write(struct file * file)2636 static inline void file_start_write(struct file *file)
2637 {
2638 if (!S_ISREG(file_inode(file)->i_mode))
2639 return;
2640 __sb_start_write(file_inode(file)->i_sb, SB_FREEZE_WRITE, true);
2641 }
2642
file_start_write_trylock(struct file * file)2643 static inline bool file_start_write_trylock(struct file *file)
2644 {
2645 if (!S_ISREG(file_inode(file)->i_mode))
2646 return true;
2647 return __sb_start_write(file_inode(file)->i_sb, SB_FREEZE_WRITE, false);
2648 }
2649
file_end_write(struct file * file)2650 static inline void file_end_write(struct file *file)
2651 {
2652 if (!S_ISREG(file_inode(file)->i_mode))
2653 return;
2654 __sb_end_write(file_inode(file)->i_sb, SB_FREEZE_WRITE);
2655 }
2656
2657 /*
2658 * get_write_access() gets write permission for a file.
2659 * put_write_access() releases this write permission.
2660 * This is used for regular files.
2661 * We cannot support write (and maybe mmap read-write shared) accesses and
2662 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
2663 * can have the following values:
2664 * 0: no writers, no VM_DENYWRITE mappings
2665 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
2666 * > 0: (i_writecount) users are writing to the file.
2667 *
2668 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
2669 * except for the cases where we don't hold i_writecount yet. Then we need to
2670 * use {get,deny}_write_access() - these functions check the sign and refuse
2671 * to do the change if sign is wrong.
2672 */
get_write_access(struct inode * inode)2673 static inline int get_write_access(struct inode *inode)
2674 {
2675 return atomic_inc_unless_negative(&inode->i_writecount) ? 0 : -ETXTBSY;
2676 }
deny_write_access(struct file * file)2677 static inline int deny_write_access(struct file *file)
2678 {
2679 struct inode *inode = file_inode(file);
2680 return atomic_dec_unless_positive(&inode->i_writecount) ? 0 : -ETXTBSY;
2681 }
put_write_access(struct inode * inode)2682 static inline void put_write_access(struct inode * inode)
2683 {
2684 atomic_dec(&inode->i_writecount);
2685 }
allow_write_access(struct file * file)2686 static inline void allow_write_access(struct file *file)
2687 {
2688 if (file)
2689 atomic_inc(&file_inode(file)->i_writecount);
2690 }
inode_is_open_for_write(const struct inode * inode)2691 static inline bool inode_is_open_for_write(const struct inode *inode)
2692 {
2693 return atomic_read(&inode->i_writecount) > 0;
2694 }
2695
2696 #ifdef CONFIG_IMA
i_readcount_dec(struct inode * inode)2697 static inline void i_readcount_dec(struct inode *inode)
2698 {
2699 BUG_ON(!atomic_read(&inode->i_readcount));
2700 atomic_dec(&inode->i_readcount);
2701 }
i_readcount_inc(struct inode * inode)2702 static inline void i_readcount_inc(struct inode *inode)
2703 {
2704 atomic_inc(&inode->i_readcount);
2705 }
2706 #else
i_readcount_dec(struct inode * inode)2707 static inline void i_readcount_dec(struct inode *inode)
2708 {
2709 return;
2710 }
i_readcount_inc(struct inode * inode)2711 static inline void i_readcount_inc(struct inode *inode)
2712 {
2713 return;
2714 }
2715 #endif
2716 extern int do_pipe_flags(int *, int);
2717
2718 #define __kernel_read_file_id(id) \
2719 id(UNKNOWN, unknown) \
2720 id(FIRMWARE, firmware) \
2721 id(FIRMWARE_PREALLOC_BUFFER, firmware) \
2722 id(MODULE, kernel-module) \
2723 id(KEXEC_IMAGE, kexec-image) \
2724 id(KEXEC_INITRAMFS, kexec-initramfs) \
2725 id(POLICY, security-policy) \
2726 id(MAX_ID, )
2727
2728 #define __fid_enumify(ENUM, dummy) READING_ ## ENUM,
2729 #define __fid_stringify(dummy, str) #str,
2730
2731 enum kernel_read_file_id {
2732 __kernel_read_file_id(__fid_enumify)
2733 };
2734
2735 static const char * const kernel_read_file_str[] = {
2736 __kernel_read_file_id(__fid_stringify)
2737 };
2738
kernel_read_file_id_str(enum kernel_read_file_id id)2739 static inline const char *kernel_read_file_id_str(enum kernel_read_file_id id)
2740 {
2741 if (id < 0 || id >= READING_MAX_ID)
2742 return kernel_read_file_str[READING_UNKNOWN];
2743
2744 return kernel_read_file_str[id];
2745 }
2746
2747 extern int kernel_read(struct file *, loff_t, char *, unsigned long);
2748 extern int kernel_read_file(struct file *, void **, loff_t *, loff_t,
2749 enum kernel_read_file_id);
2750 extern int kernel_read_file_from_path(char *, void **, loff_t *, loff_t,
2751 enum kernel_read_file_id);
2752 extern int kernel_read_file_from_fd(int, void **, loff_t *, loff_t,
2753 enum kernel_read_file_id);
2754 extern ssize_t kernel_write(struct file *, const char *, size_t, loff_t);
2755 extern ssize_t __kernel_write(struct file *, const char *, size_t, loff_t *);
2756 extern struct file * open_exec(const char *);
2757
2758 /* fs/dcache.c -- generic fs support functions */
2759 extern bool is_subdir(struct dentry *, struct dentry *);
2760 extern bool path_is_under(struct path *, struct path *);
2761
2762 extern char *file_path(struct file *, char *, int);
2763
2764 #include <linux/err.h>
2765
2766 /* needed for stackable file system support */
2767 extern loff_t default_llseek(struct file *file, loff_t offset, int whence);
2768
2769 extern loff_t vfs_llseek(struct file *file, loff_t offset, int whence);
2770
2771 extern int inode_init_always(struct super_block *, struct inode *);
2772 extern void inode_init_once(struct inode *);
2773 extern void address_space_init_once(struct address_space *mapping);
2774 extern struct inode * igrab(struct inode *);
2775 extern ino_t iunique(struct super_block *, ino_t);
2776 extern int inode_needs_sync(struct inode *inode);
2777 extern int generic_delete_inode(struct inode *inode);
generic_drop_inode(struct inode * inode)2778 static inline int generic_drop_inode(struct inode *inode)
2779 {
2780 return !inode->i_nlink || inode_unhashed(inode);
2781 }
2782
2783 extern struct inode *ilookup5_nowait(struct super_block *sb,
2784 unsigned long hashval, int (*test)(struct inode *, void *),
2785 void *data);
2786 extern struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
2787 int (*test)(struct inode *, void *), void *data);
2788 extern struct inode *ilookup(struct super_block *sb, unsigned long ino);
2789
2790 extern struct inode * iget5_locked(struct super_block *, unsigned long, int (*test)(struct inode *, void *), int (*set)(struct inode *, void *), void *);
2791 extern struct inode * iget_locked(struct super_block *, unsigned long);
2792 extern struct inode *find_inode_nowait(struct super_block *,
2793 unsigned long,
2794 int (*match)(struct inode *,
2795 unsigned long, void *),
2796 void *data);
2797 extern int insert_inode_locked4(struct inode *, unsigned long, int (*test)(struct inode *, void *), void *);
2798 extern int insert_inode_locked(struct inode *);
2799 #ifdef CONFIG_DEBUG_LOCK_ALLOC
2800 extern void lockdep_annotate_inode_mutex_key(struct inode *inode);
2801 #else
lockdep_annotate_inode_mutex_key(struct inode * inode)2802 static inline void lockdep_annotate_inode_mutex_key(struct inode *inode) { };
2803 #endif
2804 extern void unlock_new_inode(struct inode *);
2805 extern unsigned int get_next_ino(void);
2806 extern void evict_inodes(struct super_block *sb);
2807
2808 extern void __iget(struct inode * inode);
2809 extern void iget_failed(struct inode *);
2810 extern void clear_inode(struct inode *);
2811 extern void __destroy_inode(struct inode *);
2812 extern struct inode *new_inode_pseudo(struct super_block *sb);
2813 extern struct inode *new_inode(struct super_block *sb);
2814 extern void free_inode_nonrcu(struct inode *inode);
2815 extern int should_remove_suid(struct dentry *);
2816 extern int file_remove_privs(struct file *);
2817
2818 extern void __insert_inode_hash(struct inode *, unsigned long hashval);
insert_inode_hash(struct inode * inode)2819 static inline void insert_inode_hash(struct inode *inode)
2820 {
2821 __insert_inode_hash(inode, inode->i_ino);
2822 }
2823
2824 extern void __remove_inode_hash(struct inode *);
remove_inode_hash(struct inode * inode)2825 static inline void remove_inode_hash(struct inode *inode)
2826 {
2827 if (!inode_unhashed(inode) && !hlist_fake(&inode->i_hash))
2828 __remove_inode_hash(inode);
2829 }
2830
2831 extern void inode_sb_list_add(struct inode *inode);
2832
2833 #ifdef CONFIG_BLOCK
2834 extern blk_qc_t submit_bio(struct bio *);
2835 extern int bdev_read_only(struct block_device *);
2836 #endif
2837 extern int set_blocksize(struct block_device *, int);
2838 extern int sb_set_blocksize(struct super_block *, int);
2839 extern int sb_min_blocksize(struct super_block *, int);
2840
2841 extern int generic_file_mmap(struct file *, struct vm_area_struct *);
2842 extern int generic_file_readonly_mmap(struct file *, struct vm_area_struct *);
2843 extern ssize_t generic_write_checks(struct kiocb *, struct iov_iter *);
2844 extern ssize_t generic_file_read_iter(struct kiocb *, struct iov_iter *);
2845 extern ssize_t __generic_file_write_iter(struct kiocb *, struct iov_iter *);
2846 extern ssize_t generic_file_write_iter(struct kiocb *, struct iov_iter *);
2847 extern ssize_t generic_file_direct_write(struct kiocb *, struct iov_iter *);
2848 extern ssize_t generic_perform_write(struct file *, struct iov_iter *, loff_t);
2849
2850 ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos);
2851 ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos);
2852
2853 /* fs/block_dev.c */
2854 extern ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to);
2855 extern ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from);
2856 extern int blkdev_fsync(struct file *filp, loff_t start, loff_t end,
2857 int datasync);
2858 extern void block_sync_page(struct page *page);
2859
2860 /* fs/splice.c */
2861 extern ssize_t generic_file_splice_read(struct file *, loff_t *,
2862 struct pipe_inode_info *, size_t, unsigned int);
2863 extern ssize_t iter_file_splice_write(struct pipe_inode_info *,
2864 struct file *, loff_t *, size_t, unsigned int);
2865 extern ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe,
2866 struct file *out, loff_t *, size_t len, unsigned int flags);
2867 extern long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
2868 loff_t *opos, size_t len, unsigned int flags);
2869
2870
2871 extern void
2872 file_ra_state_init(struct file_ra_state *ra, struct address_space *mapping);
2873 extern loff_t noop_llseek(struct file *file, loff_t offset, int whence);
2874 extern loff_t no_llseek(struct file *file, loff_t offset, int whence);
2875 extern loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize);
2876 extern loff_t generic_file_llseek(struct file *file, loff_t offset, int whence);
2877 extern loff_t generic_file_llseek_size(struct file *file, loff_t offset,
2878 int whence, loff_t maxsize, loff_t eof);
2879 extern loff_t fixed_size_llseek(struct file *file, loff_t offset,
2880 int whence, loff_t size);
2881 extern loff_t no_seek_end_llseek_size(struct file *, loff_t, int, loff_t);
2882 extern loff_t no_seek_end_llseek(struct file *, loff_t, int);
2883 extern int generic_file_open(struct inode * inode, struct file * filp);
2884 extern int nonseekable_open(struct inode * inode, struct file * filp);
2885
2886 #ifdef CONFIG_BLOCK
2887 typedef void (dio_submit_t)(struct bio *bio, struct inode *inode,
2888 loff_t file_offset);
2889
2890 enum {
2891 /* need locking between buffered and direct access */
2892 DIO_LOCKING = 0x01,
2893
2894 /* filesystem does not support filling holes */
2895 DIO_SKIP_HOLES = 0x02,
2896
2897 /* filesystem can handle aio writes beyond i_size */
2898 DIO_ASYNC_EXTEND = 0x04,
2899
2900 /* inode/fs/bdev does not need truncate protection */
2901 DIO_SKIP_DIO_COUNT = 0x08,
2902 };
2903
2904 void dio_end_io(struct bio *bio, int error);
2905
2906 ssize_t __blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,
2907 struct block_device *bdev, struct iov_iter *iter,
2908 get_block_t get_block,
2909 dio_iodone_t end_io, dio_submit_t submit_io,
2910 int flags);
2911
blockdev_direct_IO(struct kiocb * iocb,struct inode * inode,struct iov_iter * iter,get_block_t get_block)2912 static inline ssize_t blockdev_direct_IO(struct kiocb *iocb,
2913 struct inode *inode,
2914 struct iov_iter *iter,
2915 get_block_t get_block)
2916 {
2917 return __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev, iter,
2918 get_block, NULL, NULL, DIO_LOCKING | DIO_SKIP_HOLES);
2919 }
2920 #endif
2921
2922 void inode_dio_wait(struct inode *inode);
2923
2924 /*
2925 * inode_dio_begin - signal start of a direct I/O requests
2926 * @inode: inode the direct I/O happens on
2927 *
2928 * This is called once we've finished processing a direct I/O request,
2929 * and is used to wake up callers waiting for direct I/O to be quiesced.
2930 */
inode_dio_begin(struct inode * inode)2931 static inline void inode_dio_begin(struct inode *inode)
2932 {
2933 atomic_inc(&inode->i_dio_count);
2934 }
2935
2936 /*
2937 * inode_dio_end - signal finish of a direct I/O requests
2938 * @inode: inode the direct I/O happens on
2939 *
2940 * This is called once we've finished processing a direct I/O request,
2941 * and is used to wake up callers waiting for direct I/O to be quiesced.
2942 */
inode_dio_end(struct inode * inode)2943 static inline void inode_dio_end(struct inode *inode)
2944 {
2945 if (atomic_dec_and_test(&inode->i_dio_count))
2946 wake_up_bit(&inode->i_state, __I_DIO_WAKEUP);
2947 }
2948
2949 extern void inode_set_flags(struct inode *inode, unsigned int flags,
2950 unsigned int mask);
2951
2952 extern const struct file_operations generic_ro_fops;
2953
2954 #define special_file(m) (S_ISCHR(m)||S_ISBLK(m)||S_ISFIFO(m)||S_ISSOCK(m))
2955
2956 extern int readlink_copy(char __user *, int, const char *);
2957 extern int page_readlink(struct dentry *, char __user *, int);
2958 extern const char *page_get_link(struct dentry *, struct inode *,
2959 struct delayed_call *);
2960 extern void page_put_link(void *);
2961 extern int __page_symlink(struct inode *inode, const char *symname, int len,
2962 int nofs);
2963 extern int page_symlink(struct inode *inode, const char *symname, int len);
2964 extern const struct inode_operations page_symlink_inode_operations;
2965 extern void kfree_link(void *);
2966 extern int generic_readlink(struct dentry *, char __user *, int);
2967 extern void generic_fillattr(struct inode *, struct kstat *);
2968 int vfs_getattr_nosec(struct path *path, struct kstat *stat);
2969 extern int vfs_getattr(struct path *, struct kstat *);
2970 void __inode_add_bytes(struct inode *inode, loff_t bytes);
2971 void inode_add_bytes(struct inode *inode, loff_t bytes);
2972 void __inode_sub_bytes(struct inode *inode, loff_t bytes);
2973 void inode_sub_bytes(struct inode *inode, loff_t bytes);
2974 loff_t inode_get_bytes(struct inode *inode);
2975 void inode_set_bytes(struct inode *inode, loff_t bytes);
2976 const char *simple_get_link(struct dentry *, struct inode *,
2977 struct delayed_call *);
2978 extern const struct inode_operations simple_symlink_inode_operations;
2979
2980 extern int iterate_dir(struct file *, struct dir_context *);
2981
2982 extern int vfs_stat(const char __user *, struct kstat *);
2983 extern int vfs_lstat(const char __user *, struct kstat *);
2984 extern int vfs_fstat(unsigned int, struct kstat *);
2985 extern int vfs_fstatat(int , const char __user *, struct kstat *, int);
2986 extern const char *vfs_get_link(struct dentry *, struct delayed_call *);
2987
2988 extern int __generic_block_fiemap(struct inode *inode,
2989 struct fiemap_extent_info *fieinfo,
2990 loff_t start, loff_t len,
2991 get_block_t *get_block);
2992 extern int generic_block_fiemap(struct inode *inode,
2993 struct fiemap_extent_info *fieinfo, u64 start,
2994 u64 len, get_block_t *get_block);
2995
2996 extern void get_filesystem(struct file_system_type *fs);
2997 extern void put_filesystem(struct file_system_type *fs);
2998 extern struct file_system_type *get_fs_type(const char *name);
2999 extern struct super_block *get_super(struct block_device *);
3000 extern struct super_block *get_super_thawed(struct block_device *);
3001 extern struct super_block *get_active_super(struct block_device *bdev);
3002 extern void drop_super(struct super_block *sb);
3003 extern void iterate_supers(void (*)(struct super_block *, void *), void *);
3004 extern void iterate_supers_type(struct file_system_type *,
3005 void (*)(struct super_block *, void *), void *);
3006
3007 extern int dcache_dir_open(struct inode *, struct file *);
3008 extern int dcache_dir_close(struct inode *, struct file *);
3009 extern loff_t dcache_dir_lseek(struct file *, loff_t, int);
3010 extern int dcache_readdir(struct file *, struct dir_context *);
3011 extern int simple_setattr(struct dentry *, struct iattr *);
3012 extern int simple_getattr(struct vfsmount *, struct dentry *, struct kstat *);
3013 extern int simple_statfs(struct dentry *, struct kstatfs *);
3014 extern int simple_open(struct inode *inode, struct file *file);
3015 extern int simple_link(struct dentry *, struct inode *, struct dentry *);
3016 extern int simple_unlink(struct inode *, struct dentry *);
3017 extern int simple_rmdir(struct inode *, struct dentry *);
3018 extern int simple_rename(struct inode *, struct dentry *,
3019 struct inode *, struct dentry *, unsigned int);
3020 extern int noop_fsync(struct file *, loff_t, loff_t, int);
3021 extern int simple_empty(struct dentry *);
3022 extern int simple_readpage(struct file *file, struct page *page);
3023 extern int simple_write_begin(struct file *file, struct address_space *mapping,
3024 loff_t pos, unsigned len, unsigned flags,
3025 struct page **pagep, void **fsdata);
3026 extern int simple_write_end(struct file *file, struct address_space *mapping,
3027 loff_t pos, unsigned len, unsigned copied,
3028 struct page *page, void *fsdata);
3029 extern int always_delete_dentry(const struct dentry *);
3030 extern struct inode *alloc_anon_inode(struct super_block *);
3031 extern int simple_nosetlease(struct file *, long, struct file_lock **, void **);
3032 extern const struct dentry_operations simple_dentry_operations;
3033
3034 extern struct dentry *simple_lookup(struct inode *, struct dentry *, unsigned int flags);
3035 extern ssize_t generic_read_dir(struct file *, char __user *, size_t, loff_t *);
3036 extern const struct file_operations simple_dir_operations;
3037 extern const struct inode_operations simple_dir_inode_operations;
3038 extern void make_empty_dir_inode(struct inode *inode);
3039 extern bool is_empty_dir_inode(struct inode *inode);
3040 struct tree_descr { char *name; const struct file_operations *ops; int mode; };
3041 struct dentry *d_alloc_name(struct dentry *, const char *);
3042 extern int simple_fill_super(struct super_block *, unsigned long, struct tree_descr *);
3043 extern int simple_pin_fs(struct file_system_type *, struct vfsmount **mount, int *count);
3044 extern void simple_release_fs(struct vfsmount **mount, int *count);
3045
3046 extern ssize_t simple_read_from_buffer(void __user *to, size_t count,
3047 loff_t *ppos, const void *from, size_t available);
3048 extern ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
3049 const void __user *from, size_t count);
3050
3051 extern int __generic_file_fsync(struct file *, loff_t, loff_t, int);
3052 extern int generic_file_fsync(struct file *, loff_t, loff_t, int);
3053
3054 extern int generic_check_addressable(unsigned, u64);
3055
3056 #ifdef CONFIG_MIGRATION
3057 extern int buffer_migrate_page(struct address_space *,
3058 struct page *, struct page *,
3059 enum migrate_mode);
3060 #else
3061 #define buffer_migrate_page NULL
3062 #endif
3063
3064 extern int setattr_prepare(struct dentry *, struct iattr *);
3065 extern int inode_newsize_ok(const struct inode *, loff_t offset);
3066 extern void setattr_copy(struct inode *inode, const struct iattr *attr);
3067
3068 extern int file_update_time(struct file *file);
3069
3070 extern int generic_show_options(struct seq_file *m, struct dentry *root);
3071 extern void save_mount_options(struct super_block *sb, char *options);
3072 extern void replace_mount_options(struct super_block *sb, char *options);
3073
io_is_direct(struct file * filp)3074 static inline bool io_is_direct(struct file *filp)
3075 {
3076 return (filp->f_flags & O_DIRECT) || IS_DAX(filp->f_mapping->host);
3077 }
3078
vma_is_dax(struct vm_area_struct * vma)3079 static inline bool vma_is_dax(struct vm_area_struct *vma)
3080 {
3081 return vma->vm_file && IS_DAX(vma->vm_file->f_mapping->host);
3082 }
3083
vma_is_fsdax(struct vm_area_struct * vma)3084 static inline bool vma_is_fsdax(struct vm_area_struct *vma)
3085 {
3086 struct inode *inode;
3087
3088 if (!vma->vm_file)
3089 return false;
3090 if (!vma_is_dax(vma))
3091 return false;
3092 inode = file_inode(vma->vm_file);
3093 if (S_ISCHR(inode->i_mode))
3094 return false; /* device-dax */
3095 return true;
3096 }
3097
iocb_flags(struct file * file)3098 static inline int iocb_flags(struct file *file)
3099 {
3100 int res = 0;
3101 if (file->f_flags & O_APPEND)
3102 res |= IOCB_APPEND;
3103 if (io_is_direct(file))
3104 res |= IOCB_DIRECT;
3105 if ((file->f_flags & O_DSYNC) || IS_SYNC(file->f_mapping->host))
3106 res |= IOCB_DSYNC;
3107 if (file->f_flags & __O_SYNC)
3108 res |= IOCB_SYNC;
3109 return res;
3110 }
3111
parent_ino(struct dentry * dentry)3112 static inline ino_t parent_ino(struct dentry *dentry)
3113 {
3114 ino_t res;
3115
3116 /*
3117 * Don't strictly need d_lock here? If the parent ino could change
3118 * then surely we'd have a deeper race in the caller?
3119 */
3120 spin_lock(&dentry->d_lock);
3121 res = dentry->d_parent->d_inode->i_ino;
3122 spin_unlock(&dentry->d_lock);
3123 return res;
3124 }
3125
3126 /* Transaction based IO helpers */
3127
3128 /*
3129 * An argresp is stored in an allocated page and holds the
3130 * size of the argument or response, along with its content
3131 */
3132 struct simple_transaction_argresp {
3133 ssize_t size;
3134 char data[0];
3135 };
3136
3137 #define SIMPLE_TRANSACTION_LIMIT (PAGE_SIZE - sizeof(struct simple_transaction_argresp))
3138
3139 char *simple_transaction_get(struct file *file, const char __user *buf,
3140 size_t size);
3141 ssize_t simple_transaction_read(struct file *file, char __user *buf,
3142 size_t size, loff_t *pos);
3143 int simple_transaction_release(struct inode *inode, struct file *file);
3144
3145 void simple_transaction_set(struct file *file, size_t n);
3146
3147 /*
3148 * simple attribute files
3149 *
3150 * These attributes behave similar to those in sysfs:
3151 *
3152 * Writing to an attribute immediately sets a value, an open file can be
3153 * written to multiple times.
3154 *
3155 * Reading from an attribute creates a buffer from the value that might get
3156 * read with multiple read calls. When the attribute has been read
3157 * completely, no further read calls are possible until the file is opened
3158 * again.
3159 *
3160 * All attributes contain a text representation of a numeric value
3161 * that are accessed with the get() and set() functions.
3162 */
3163 #define DEFINE_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt) \
3164 static int __fops ## _open(struct inode *inode, struct file *file) \
3165 { \
3166 __simple_attr_check_format(__fmt, 0ull); \
3167 return simple_attr_open(inode, file, __get, __set, __fmt); \
3168 } \
3169 static const struct file_operations __fops = { \
3170 .owner = THIS_MODULE, \
3171 .open = __fops ## _open, \
3172 .release = simple_attr_release, \
3173 .read = simple_attr_read, \
3174 .write = simple_attr_write, \
3175 .llseek = generic_file_llseek, \
3176 }
3177
3178 static inline __printf(1, 2)
__simple_attr_check_format(const char * fmt,...)3179 void __simple_attr_check_format(const char *fmt, ...)
3180 {
3181 /* don't do anything, just let the compiler check the arguments; */
3182 }
3183
3184 int simple_attr_open(struct inode *inode, struct file *file,
3185 int (*get)(void *, u64 *), int (*set)(void *, u64),
3186 const char *fmt);
3187 int simple_attr_release(struct inode *inode, struct file *file);
3188 ssize_t simple_attr_read(struct file *file, char __user *buf,
3189 size_t len, loff_t *ppos);
3190 ssize_t simple_attr_write(struct file *file, const char __user *buf,
3191 size_t len, loff_t *ppos);
3192
3193 struct ctl_table;
3194 int proc_nr_files(struct ctl_table *table, int write,
3195 void __user *buffer, size_t *lenp, loff_t *ppos);
3196 int proc_nr_dentry(struct ctl_table *table, int write,
3197 void __user *buffer, size_t *lenp, loff_t *ppos);
3198 int proc_nr_inodes(struct ctl_table *table, int write,
3199 void __user *buffer, size_t *lenp, loff_t *ppos);
3200 int __init get_filesystem_list(char *buf);
3201
3202 #define __FMODE_EXEC ((__force int) FMODE_EXEC)
3203 #define __FMODE_NONOTIFY ((__force int) FMODE_NONOTIFY)
3204
3205 #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
3206 #define OPEN_FMODE(flag) ((__force fmode_t)(((flag + 1) & O_ACCMODE) | \
3207 (flag & __FMODE_NONOTIFY)))
3208
is_sxid(umode_t mode)3209 static inline bool is_sxid(umode_t mode)
3210 {
3211 return (mode & S_ISUID) || ((mode & S_ISGID) && (mode & S_IXGRP));
3212 }
3213
check_sticky(struct inode * dir,struct inode * inode)3214 static inline int check_sticky(struct inode *dir, struct inode *inode)
3215 {
3216 if (!(dir->i_mode & S_ISVTX))
3217 return 0;
3218
3219 return __check_sticky(dir, inode);
3220 }
3221
inode_has_no_xattr(struct inode * inode)3222 static inline void inode_has_no_xattr(struct inode *inode)
3223 {
3224 if (!is_sxid(inode->i_mode) && (inode->i_sb->s_flags & MS_NOSEC))
3225 inode->i_flags |= S_NOSEC;
3226 }
3227
is_root_inode(struct inode * inode)3228 static inline bool is_root_inode(struct inode *inode)
3229 {
3230 return inode == inode->i_sb->s_root->d_inode;
3231 }
3232
dir_emit(struct dir_context * ctx,const char * name,int namelen,u64 ino,unsigned type)3233 static inline bool dir_emit(struct dir_context *ctx,
3234 const char *name, int namelen,
3235 u64 ino, unsigned type)
3236 {
3237 return ctx->actor(ctx, name, namelen, ctx->pos, ino, type) == 0;
3238 }
dir_emit_dot(struct file * file,struct dir_context * ctx)3239 static inline bool dir_emit_dot(struct file *file, struct dir_context *ctx)
3240 {
3241 return ctx->actor(ctx, ".", 1, ctx->pos,
3242 file->f_path.dentry->d_inode->i_ino, DT_DIR) == 0;
3243 }
dir_emit_dotdot(struct file * file,struct dir_context * ctx)3244 static inline bool dir_emit_dotdot(struct file *file, struct dir_context *ctx)
3245 {
3246 return ctx->actor(ctx, "..", 2, ctx->pos,
3247 parent_ino(file->f_path.dentry), DT_DIR) == 0;
3248 }
dir_emit_dots(struct file * file,struct dir_context * ctx)3249 static inline bool dir_emit_dots(struct file *file, struct dir_context *ctx)
3250 {
3251 if (ctx->pos == 0) {
3252 if (!dir_emit_dot(file, ctx))
3253 return false;
3254 ctx->pos = 1;
3255 }
3256 if (ctx->pos == 1) {
3257 if (!dir_emit_dotdot(file, ctx))
3258 return false;
3259 ctx->pos = 2;
3260 }
3261 return true;
3262 }
dir_relax(struct inode * inode)3263 static inline bool dir_relax(struct inode *inode)
3264 {
3265 inode_unlock(inode);
3266 inode_lock(inode);
3267 return !IS_DEADDIR(inode);
3268 }
3269
dir_relax_shared(struct inode * inode)3270 static inline bool dir_relax_shared(struct inode *inode)
3271 {
3272 inode_unlock_shared(inode);
3273 inode_lock_shared(inode);
3274 return !IS_DEADDIR(inode);
3275 }
3276
3277 extern bool path_noexec(const struct path *path);
3278 extern void inode_nohighmem(struct inode *inode);
3279
3280 #endif /* _LINUX_FS_H */
3281