xfs_inode.h 15 KB

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