xfs_inode.h 15 KB

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