xfs_inode.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #ifndef __XFS_INODE_H__
  7. #define __XFS_INODE_H__
  8. #include "xfs_inode_buf.h"
  9. #include "xfs_inode_fork.h"
  10. /*
  11. * Kernel only inode definitions
  12. */
  13. struct xfs_dinode;
  14. struct xfs_inode;
  15. struct xfs_buf;
  16. struct xfs_bmbt_irec;
  17. struct xfs_inode_log_item;
  18. struct xfs_mount;
  19. struct xfs_trans;
  20. struct xfs_dquot;
  21. typedef struct xfs_inode {
  22. /* Inode linking and identification information. */
  23. struct xfs_mount *i_mount; /* fs mount struct ptr */
  24. struct xfs_dquot *i_udquot; /* user dquot */
  25. struct xfs_dquot *i_gdquot; /* group dquot */
  26. struct xfs_dquot *i_pdquot; /* project dquot */
  27. /* Inode location stuff */
  28. xfs_ino_t i_ino; /* inode number (agno/agino)*/
  29. struct xfs_imap i_imap; /* location for xfs_imap() */
  30. /* Extent information. */
  31. struct xfs_ifork *i_afp; /* attribute fork pointer */
  32. struct xfs_ifork *i_cowfp; /* copy on write extents */
  33. struct xfs_ifork i_df; /* data fork */
  34. /* Transaction and locking information. */
  35. struct xfs_inode_log_item *i_itemp; /* logging information */
  36. mrlock_t i_lock; /* inode lock */
  37. mrlock_t i_mmaplock; /* inode mmap IO lock */
  38. atomic_t i_pincount; /* inode pin count */
  39. /*
  40. * Bitsets of inode metadata that have been checked and/or are sick.
  41. * Callers must hold i_flags_lock before accessing this field.
  42. */
  43. uint16_t i_checked;
  44. uint16_t i_sick;
  45. spinlock_t i_flags_lock; /* inode i_flags lock */
  46. /* Miscellaneous state. */
  47. unsigned long i_flags; /* see defined flags below */
  48. uint64_t i_delayed_blks; /* count of delay alloc blks */
  49. struct xfs_icdinode i_d; /* most of ondisk inode */
  50. /* VFS inode */
  51. struct inode i_vnode; /* embedded VFS inode */
  52. /* pending io completions */
  53. spinlock_t i_ioend_lock;
  54. struct work_struct i_ioend_work;
  55. struct list_head i_ioend_list;
  56. } xfs_inode_t;
  57. /* Convert from vfs inode to xfs inode */
  58. static inline struct xfs_inode *XFS_I(struct inode *inode)
  59. {
  60. return container_of(inode, struct xfs_inode, i_vnode);
  61. }
  62. /* convert from xfs inode to vfs inode */
  63. static inline struct inode *VFS_I(struct xfs_inode *ip)
  64. {
  65. return &ip->i_vnode;
  66. }
  67. /*
  68. * For regular files we only update the on-disk filesize when actually
  69. * writing data back to disk. Until then only the copy in the VFS inode
  70. * is uptodate.
  71. */
  72. static inline xfs_fsize_t XFS_ISIZE(struct xfs_inode *ip)
  73. {
  74. if (S_ISREG(VFS_I(ip)->i_mode))
  75. return i_size_read(VFS_I(ip));
  76. return ip->i_d.di_size;
  77. }
  78. /*
  79. * If this I/O goes past the on-disk inode size update it unless it would
  80. * be past the current in-core inode size.
  81. */
  82. static inline xfs_fsize_t
  83. xfs_new_eof(struct xfs_inode *ip, xfs_fsize_t new_size)
  84. {
  85. xfs_fsize_t i_size = i_size_read(VFS_I(ip));
  86. if (new_size > i_size || new_size < 0)
  87. new_size = i_size;
  88. return new_size > ip->i_d.di_size ? new_size : 0;
  89. }
  90. /*
  91. * i_flags helper functions
  92. */
  93. static inline void
  94. __xfs_iflags_set(xfs_inode_t *ip, unsigned short flags)
  95. {
  96. ip->i_flags |= flags;
  97. }
  98. static inline void
  99. xfs_iflags_set(xfs_inode_t *ip, unsigned short flags)
  100. {
  101. spin_lock(&ip->i_flags_lock);
  102. __xfs_iflags_set(ip, flags);
  103. spin_unlock(&ip->i_flags_lock);
  104. }
  105. static inline void
  106. xfs_iflags_clear(xfs_inode_t *ip, unsigned short flags)
  107. {
  108. spin_lock(&ip->i_flags_lock);
  109. ip->i_flags &= ~flags;
  110. spin_unlock(&ip->i_flags_lock);
  111. }
  112. static inline int
  113. __xfs_iflags_test(xfs_inode_t *ip, unsigned short flags)
  114. {
  115. return (ip->i_flags & flags);
  116. }
  117. static inline int
  118. xfs_iflags_test(xfs_inode_t *ip, unsigned short flags)
  119. {
  120. int ret;
  121. spin_lock(&ip->i_flags_lock);
  122. ret = __xfs_iflags_test(ip, flags);
  123. spin_unlock(&ip->i_flags_lock);
  124. return ret;
  125. }
  126. static inline int
  127. xfs_iflags_test_and_clear(xfs_inode_t *ip, unsigned short flags)
  128. {
  129. int ret;
  130. spin_lock(&ip->i_flags_lock);
  131. ret = ip->i_flags & flags;
  132. if (ret)
  133. ip->i_flags &= ~flags;
  134. spin_unlock(&ip->i_flags_lock);
  135. return ret;
  136. }
  137. static inline int
  138. xfs_iflags_test_and_set(xfs_inode_t *ip, unsigned short flags)
  139. {
  140. int ret;
  141. spin_lock(&ip->i_flags_lock);
  142. ret = ip->i_flags & flags;
  143. if (!ret)
  144. ip->i_flags |= flags;
  145. spin_unlock(&ip->i_flags_lock);
  146. return ret;
  147. }
  148. static inline prid_t
  149. xfs_get_initial_prid(struct xfs_inode *dp)
  150. {
  151. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  152. return dp->i_d.di_projid;
  153. return XFS_PROJID_DEFAULT;
  154. }
  155. static inline bool xfs_is_reflink_inode(struct xfs_inode *ip)
  156. {
  157. return ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK;
  158. }
  159. /*
  160. * Check if an inode has any data in the COW fork. This might be often false
  161. * even for inodes with the reflink flag when there is no pending COW operation.
  162. */
  163. static inline bool xfs_inode_has_cow_data(struct xfs_inode *ip)
  164. {
  165. return ip->i_cowfp && ip->i_cowfp->if_bytes;
  166. }
  167. static inline bool xfs_inode_has_bigtime(struct xfs_inode *ip)
  168. {
  169. return ip->i_d.di_flags2 & XFS_DIFLAG2_BIGTIME;
  170. }
  171. /*
  172. * Return the buftarg used for data allocations on a given inode.
  173. */
  174. #define xfs_inode_buftarg(ip) \
  175. (XFS_IS_REALTIME_INODE(ip) ? \
  176. (ip)->i_mount->m_rtdev_targp : (ip)->i_mount->m_ddev_targp)
  177. /*
  178. * In-core inode flags.
  179. */
  180. #define XFS_IRECLAIM (1 << 0) /* started reclaiming this inode */
  181. #define XFS_ISTALE (1 << 1) /* inode has been staled */
  182. #define XFS_IRECLAIMABLE (1 << 2) /* inode can be reclaimed */
  183. #define __XFS_INEW_BIT 3 /* inode has just been allocated */
  184. #define XFS_INEW (1 << __XFS_INEW_BIT)
  185. #define XFS_ITRUNCATED (1 << 5) /* truncated down so flush-on-close */
  186. #define XFS_IDIRTY_RELEASE (1 << 6) /* dirty release already seen */
  187. #define XFS_IFLUSHING (1 << 7) /* inode is being flushed */
  188. #define __XFS_IPINNED_BIT 8 /* wakeup key for zero pin count */
  189. #define XFS_IPINNED (1 << __XFS_IPINNED_BIT)
  190. #define XFS_IEOFBLOCKS (1 << 9) /* has the preallocblocks tag set */
  191. /*
  192. * If this unlinked inode is in the middle of recovery, don't let drop_inode
  193. * truncate and free the inode. This can happen if we iget the inode during
  194. * log recovery to replay a bmap operation on the inode.
  195. */
  196. #define XFS_IRECOVERY (1 << 11)
  197. #define XFS_ICOWBLOCKS (1 << 12)/* has the cowblocks tag set */
  198. /*
  199. * Per-lifetime flags need to be reset when re-using a reclaimable inode during
  200. * inode lookup. This prevents unintended behaviour on the new inode from
  201. * ocurring.
  202. */
  203. #define XFS_IRECLAIM_RESET_FLAGS \
  204. (XFS_IRECLAIMABLE | XFS_IRECLAIM | \
  205. XFS_IDIRTY_RELEASE | XFS_ITRUNCATED)
  206. /*
  207. * Flags for inode locking.
  208. * Bit ranges: 1<<1 - 1<<16-1 -- iolock/ilock modes (bitfield)
  209. * 1<<16 - 1<<32-1 -- lockdep annotation (integers)
  210. */
  211. #define XFS_IOLOCK_EXCL (1<<0)
  212. #define XFS_IOLOCK_SHARED (1<<1)
  213. #define XFS_ILOCK_EXCL (1<<2)
  214. #define XFS_ILOCK_SHARED (1<<3)
  215. #define XFS_MMAPLOCK_EXCL (1<<4)
  216. #define XFS_MMAPLOCK_SHARED (1<<5)
  217. #define XFS_LOCK_MASK (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED \
  218. | XFS_ILOCK_EXCL | XFS_ILOCK_SHARED \
  219. | XFS_MMAPLOCK_EXCL | XFS_MMAPLOCK_SHARED)
  220. #define XFS_LOCK_FLAGS \
  221. { XFS_IOLOCK_EXCL, "IOLOCK_EXCL" }, \
  222. { XFS_IOLOCK_SHARED, "IOLOCK_SHARED" }, \
  223. { XFS_ILOCK_EXCL, "ILOCK_EXCL" }, \
  224. { XFS_ILOCK_SHARED, "ILOCK_SHARED" }, \
  225. { XFS_MMAPLOCK_EXCL, "MMAPLOCK_EXCL" }, \
  226. { XFS_MMAPLOCK_SHARED, "MMAPLOCK_SHARED" }
  227. /*
  228. * Flags for lockdep annotations.
  229. *
  230. * XFS_LOCK_PARENT - for directory operations that require locking a
  231. * parent directory inode and a child entry inode. IOLOCK requires nesting,
  232. * MMAPLOCK does not support this class, ILOCK requires a single subclass
  233. * to differentiate parent from child.
  234. *
  235. * XFS_LOCK_RTBITMAP/XFS_LOCK_RTSUM - the realtime device bitmap and summary
  236. * inodes do not participate in the normal lock order, and thus have their
  237. * own subclasses.
  238. *
  239. * XFS_LOCK_INUMORDER - for locking several inodes at the some time
  240. * with xfs_lock_inodes(). This flag is used as the starting subclass
  241. * and each subsequent lock acquired will increment the subclass by one.
  242. * However, MAX_LOCKDEP_SUBCLASSES == 8, which means we are greatly
  243. * limited to the subclasses we can represent via nesting. We need at least
  244. * 5 inodes nest depth for the ILOCK through rename, and we also have to support
  245. * XFS_ILOCK_PARENT, which gives 6 subclasses. Then we have XFS_ILOCK_RTBITMAP
  246. * and XFS_ILOCK_RTSUM, which are another 2 unique subclasses, so that's all
  247. * 8 subclasses supported by lockdep.
  248. *
  249. * This also means we have to number the sub-classes in the lowest bits of
  250. * the mask we keep, and we have to ensure we never exceed 3 bits of lockdep
  251. * mask and we can't use bit-masking to build the subclasses. What a mess.
  252. *
  253. * Bit layout:
  254. *
  255. * Bit Lock Region
  256. * 16-19 XFS_IOLOCK_SHIFT dependencies
  257. * 20-23 XFS_MMAPLOCK_SHIFT dependencies
  258. * 24-31 XFS_ILOCK_SHIFT dependencies
  259. *
  260. * IOLOCK values
  261. *
  262. * 0-3 subclass value
  263. * 4-7 unused
  264. *
  265. * MMAPLOCK values
  266. *
  267. * 0-3 subclass value
  268. * 4-7 unused
  269. *
  270. * ILOCK values
  271. * 0-4 subclass values
  272. * 5 PARENT subclass (not nestable)
  273. * 6 RTBITMAP subclass (not nestable)
  274. * 7 RTSUM subclass (not nestable)
  275. *
  276. */
  277. #define XFS_IOLOCK_SHIFT 16
  278. #define XFS_IOLOCK_MAX_SUBCLASS 3
  279. #define XFS_IOLOCK_DEP_MASK 0x000f0000
  280. #define XFS_MMAPLOCK_SHIFT 20
  281. #define XFS_MMAPLOCK_NUMORDER 0
  282. #define XFS_MMAPLOCK_MAX_SUBCLASS 3
  283. #define XFS_MMAPLOCK_DEP_MASK 0x00f00000
  284. #define XFS_ILOCK_SHIFT 24
  285. #define XFS_ILOCK_PARENT_VAL 5
  286. #define XFS_ILOCK_MAX_SUBCLASS (XFS_ILOCK_PARENT_VAL - 1)
  287. #define XFS_ILOCK_RTBITMAP_VAL 6
  288. #define XFS_ILOCK_RTSUM_VAL 7
  289. #define XFS_ILOCK_DEP_MASK 0xff000000
  290. #define XFS_ILOCK_PARENT (XFS_ILOCK_PARENT_VAL << XFS_ILOCK_SHIFT)
  291. #define XFS_ILOCK_RTBITMAP (XFS_ILOCK_RTBITMAP_VAL << XFS_ILOCK_SHIFT)
  292. #define XFS_ILOCK_RTSUM (XFS_ILOCK_RTSUM_VAL << XFS_ILOCK_SHIFT)
  293. #define XFS_LOCK_SUBCLASS_MASK (XFS_IOLOCK_DEP_MASK | \
  294. XFS_MMAPLOCK_DEP_MASK | \
  295. XFS_ILOCK_DEP_MASK)
  296. #define XFS_IOLOCK_DEP(flags) (((flags) & XFS_IOLOCK_DEP_MASK) \
  297. >> XFS_IOLOCK_SHIFT)
  298. #define XFS_MMAPLOCK_DEP(flags) (((flags) & XFS_MMAPLOCK_DEP_MASK) \
  299. >> XFS_MMAPLOCK_SHIFT)
  300. #define XFS_ILOCK_DEP(flags) (((flags) & XFS_ILOCK_DEP_MASK) \
  301. >> XFS_ILOCK_SHIFT)
  302. /*
  303. * Layouts are broken in the BREAK_WRITE case to ensure that
  304. * layout-holders do not collide with local writes. Additionally,
  305. * layouts are broken in the BREAK_UNMAP case to make sure the
  306. * layout-holder has a consistent view of the file's extent map. While
  307. * BREAK_WRITE breaks can be satisfied by recalling FL_LAYOUT leases,
  308. * BREAK_UNMAP breaks additionally require waiting for busy dax-pages to
  309. * go idle.
  310. */
  311. enum layout_break_reason {
  312. BREAK_WRITE,
  313. BREAK_UNMAP,
  314. };
  315. /*
  316. * For multiple groups support: if S_ISGID bit is set in the parent
  317. * directory, group of new file is set to that of the parent, and
  318. * new subdirectory gets S_ISGID bit from parent.
  319. */
  320. #define XFS_INHERIT_GID(pip) \
  321. (((pip)->i_mount->m_flags & XFS_MOUNT_GRPID) || \
  322. (VFS_I(pip)->i_mode & S_ISGID))
  323. int xfs_release(struct xfs_inode *ip);
  324. void xfs_inactive(struct xfs_inode *ip);
  325. int xfs_lookup(struct xfs_inode *dp, struct xfs_name *name,
  326. struct xfs_inode **ipp, struct xfs_name *ci_name);
  327. int xfs_create(struct xfs_inode *dp, struct xfs_name *name,
  328. umode_t mode, dev_t rdev, struct xfs_inode **ipp);
  329. int xfs_create_tmpfile(struct xfs_inode *dp, umode_t mode,
  330. struct xfs_inode **ipp);
  331. int xfs_remove(struct xfs_inode *dp, struct xfs_name *name,
  332. struct xfs_inode *ip);
  333. int xfs_link(struct xfs_inode *tdp, struct xfs_inode *sip,
  334. struct xfs_name *target_name);
  335. int xfs_rename(struct xfs_inode *src_dp, struct xfs_name *src_name,
  336. struct xfs_inode *src_ip, struct xfs_inode *target_dp,
  337. struct xfs_name *target_name,
  338. struct xfs_inode *target_ip, unsigned int flags);
  339. void xfs_ilock(xfs_inode_t *, uint);
  340. int xfs_ilock_nowait(xfs_inode_t *, uint);
  341. void xfs_iunlock(xfs_inode_t *, uint);
  342. void xfs_ilock_demote(xfs_inode_t *, uint);
  343. int xfs_isilocked(xfs_inode_t *, uint);
  344. uint xfs_ilock_data_map_shared(struct xfs_inode *);
  345. uint xfs_ilock_attr_map_shared(struct xfs_inode *);
  346. uint xfs_ip2xflags(struct xfs_inode *);
  347. int xfs_ifree(struct xfs_trans *, struct xfs_inode *);
  348. int xfs_itruncate_extents_flags(struct xfs_trans **,
  349. struct xfs_inode *, int, xfs_fsize_t, int);
  350. void xfs_iext_realloc(xfs_inode_t *, int, int);
  351. int xfs_log_force_inode(struct xfs_inode *ip);
  352. void xfs_iunpin_wait(xfs_inode_t *);
  353. #define xfs_ipincount(ip) ((unsigned int) atomic_read(&ip->i_pincount))
  354. int xfs_iflush_cluster(struct xfs_buf *);
  355. void xfs_lock_two_inodes(struct xfs_inode *ip0, uint ip0_mode,
  356. struct xfs_inode *ip1, uint ip1_mode);
  357. xfs_extlen_t xfs_get_extsz_hint(struct xfs_inode *ip);
  358. xfs_extlen_t xfs_get_cowextsz_hint(struct xfs_inode *ip);
  359. int xfs_dir_ialloc(struct xfs_trans **, struct xfs_inode *, umode_t,
  360. xfs_nlink_t, dev_t, prid_t,
  361. struct xfs_inode **);
  362. static inline int
  363. xfs_itruncate_extents(
  364. struct xfs_trans **tpp,
  365. struct xfs_inode *ip,
  366. int whichfork,
  367. xfs_fsize_t new_size)
  368. {
  369. return xfs_itruncate_extents_flags(tpp, ip, whichfork, new_size, 0);
  370. }
  371. /* from xfs_file.c */
  372. enum xfs_prealloc_flags {
  373. XFS_PREALLOC_SET = (1 << 1),
  374. XFS_PREALLOC_CLEAR = (1 << 2),
  375. XFS_PREALLOC_SYNC = (1 << 3),
  376. XFS_PREALLOC_INVISIBLE = (1 << 4),
  377. };
  378. int xfs_update_prealloc_flags(struct xfs_inode *ip,
  379. enum xfs_prealloc_flags flags);
  380. int xfs_break_layouts(struct inode *inode, uint *iolock,
  381. enum layout_break_reason reason);
  382. /* from xfs_iops.c */
  383. extern void xfs_setup_inode(struct xfs_inode *ip);
  384. extern void xfs_setup_iops(struct xfs_inode *ip);
  385. extern void xfs_diflags_to_iflags(struct xfs_inode *ip, bool init);
  386. /*
  387. * When setting up a newly allocated inode, we need to call
  388. * xfs_finish_inode_setup() once the inode is fully instantiated at
  389. * the VFS level to prevent the rest of the world seeing the inode
  390. * before we've completed instantiation. Otherwise we can do it
  391. * the moment the inode lookup is complete.
  392. */
  393. static inline void xfs_finish_inode_setup(struct xfs_inode *ip)
  394. {
  395. xfs_iflags_clear(ip, XFS_INEW);
  396. barrier();
  397. unlock_new_inode(VFS_I(ip));
  398. wake_up_bit(&ip->i_flags, __XFS_INEW_BIT);
  399. }
  400. static inline void xfs_setup_existing_inode(struct xfs_inode *ip)
  401. {
  402. xfs_setup_inode(ip);
  403. xfs_setup_iops(ip);
  404. xfs_finish_inode_setup(ip);
  405. }
  406. void xfs_irele(struct xfs_inode *ip);
  407. extern struct kmem_zone *xfs_inode_zone;
  408. /* The default CoW extent size hint. */
  409. #define XFS_DEFAULT_COWEXTSZ_HINT 32
  410. int xfs_iunlink_init(struct xfs_perag *pag);
  411. void xfs_iunlink_destroy(struct xfs_perag *pag);
  412. void xfs_end_io(struct work_struct *work);
  413. int xfs_ilock2_io_mmap(struct xfs_inode *ip1, struct xfs_inode *ip2);
  414. void xfs_iunlock2_io_mmap(struct xfs_inode *ip1, struct xfs_inode *ip2);
  415. #endif /* __XFS_INODE_H__ */