xfs_inode.h 15 KB

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