xfs_inode.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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_iolock; /* inode IO lock */
  53. mrlock_t i_mmaplock; /* inode mmap IO lock */
  54. atomic_t i_pincount; /* inode pin count */
  55. spinlock_t i_flags_lock; /* inode i_flags lock */
  56. /* Miscellaneous state. */
  57. unsigned long i_flags; /* see defined flags below */
  58. unsigned int i_delayed_blks; /* count of delay alloc blks */
  59. struct xfs_icdinode i_d; /* most of ondisk inode */
  60. xfs_extnum_t i_cnextents; /* # of extents in cow fork */
  61. unsigned int i_cformat; /* format of cow fork */
  62. /* VFS inode */
  63. struct inode i_vnode; /* embedded VFS inode */
  64. } xfs_inode_t;
  65. /* Convert from vfs inode to xfs inode */
  66. static inline struct xfs_inode *XFS_I(struct inode *inode)
  67. {
  68. return container_of(inode, struct xfs_inode, i_vnode);
  69. }
  70. /* convert from xfs inode to vfs inode */
  71. static inline struct inode *VFS_I(struct xfs_inode *ip)
  72. {
  73. return &ip->i_vnode;
  74. }
  75. /*
  76. * For regular files we only update the on-disk filesize when actually
  77. * writing data back to disk. Until then only the copy in the VFS inode
  78. * is uptodate.
  79. */
  80. static inline xfs_fsize_t XFS_ISIZE(struct xfs_inode *ip)
  81. {
  82. if (S_ISREG(VFS_I(ip)->i_mode))
  83. return i_size_read(VFS_I(ip));
  84. return ip->i_d.di_size;
  85. }
  86. /*
  87. * If this I/O goes past the on-disk inode size update it unless it would
  88. * be past the current in-core inode size.
  89. */
  90. static inline xfs_fsize_t
  91. xfs_new_eof(struct xfs_inode *ip, xfs_fsize_t new_size)
  92. {
  93. xfs_fsize_t i_size = i_size_read(VFS_I(ip));
  94. if (new_size > i_size || new_size < 0)
  95. new_size = i_size;
  96. return new_size > ip->i_d.di_size ? new_size : 0;
  97. }
  98. /*
  99. * i_flags helper functions
  100. */
  101. static inline void
  102. __xfs_iflags_set(xfs_inode_t *ip, unsigned short flags)
  103. {
  104. ip->i_flags |= flags;
  105. }
  106. static inline void
  107. xfs_iflags_set(xfs_inode_t *ip, unsigned short flags)
  108. {
  109. spin_lock(&ip->i_flags_lock);
  110. __xfs_iflags_set(ip, flags);
  111. spin_unlock(&ip->i_flags_lock);
  112. }
  113. static inline void
  114. xfs_iflags_clear(xfs_inode_t *ip, unsigned short flags)
  115. {
  116. spin_lock(&ip->i_flags_lock);
  117. ip->i_flags &= ~flags;
  118. spin_unlock(&ip->i_flags_lock);
  119. }
  120. static inline int
  121. __xfs_iflags_test(xfs_inode_t *ip, unsigned short flags)
  122. {
  123. return (ip->i_flags & flags);
  124. }
  125. static inline int
  126. xfs_iflags_test(xfs_inode_t *ip, unsigned short flags)
  127. {
  128. int ret;
  129. spin_lock(&ip->i_flags_lock);
  130. ret = __xfs_iflags_test(ip, flags);
  131. spin_unlock(&ip->i_flags_lock);
  132. return ret;
  133. }
  134. static inline int
  135. xfs_iflags_test_and_clear(xfs_inode_t *ip, unsigned short flags)
  136. {
  137. int ret;
  138. spin_lock(&ip->i_flags_lock);
  139. ret = ip->i_flags & flags;
  140. if (ret)
  141. ip->i_flags &= ~flags;
  142. spin_unlock(&ip->i_flags_lock);
  143. return ret;
  144. }
  145. static inline int
  146. xfs_iflags_test_and_set(xfs_inode_t *ip, unsigned short flags)
  147. {
  148. int ret;
  149. spin_lock(&ip->i_flags_lock);
  150. ret = ip->i_flags & flags;
  151. if (!ret)
  152. ip->i_flags |= flags;
  153. spin_unlock(&ip->i_flags_lock);
  154. return ret;
  155. }
  156. /*
  157. * Project quota id helpers (previously projid was 16bit only
  158. * and using two 16bit values to hold new 32bit projid was chosen
  159. * to retain compatibility with "old" filesystems).
  160. */
  161. static inline prid_t
  162. xfs_get_projid(struct xfs_inode *ip)
  163. {
  164. return (prid_t)ip->i_d.di_projid_hi << 16 | ip->i_d.di_projid_lo;
  165. }
  166. static inline void
  167. xfs_set_projid(struct xfs_inode *ip,
  168. prid_t projid)
  169. {
  170. ip->i_d.di_projid_hi = (__uint16_t) (projid >> 16);
  171. ip->i_d.di_projid_lo = (__uint16_t) (projid & 0xffff);
  172. }
  173. static inline prid_t
  174. xfs_get_initial_prid(struct xfs_inode *dp)
  175. {
  176. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  177. return xfs_get_projid(dp);
  178. return XFS_PROJID_DEFAULT;
  179. }
  180. static inline bool xfs_is_reflink_inode(struct xfs_inode *ip)
  181. {
  182. return ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK;
  183. }
  184. /*
  185. * In-core inode flags.
  186. */
  187. #define XFS_IRECLAIM (1 << 0) /* started reclaiming this inode */
  188. #define XFS_ISTALE (1 << 1) /* inode has been staled */
  189. #define XFS_IRECLAIMABLE (1 << 2) /* inode can be reclaimed */
  190. #define __XFS_INEW_BIT 3 /* inode has just been allocated */
  191. #define XFS_INEW (1 << __XFS_INEW_BIT)
  192. #define XFS_ITRUNCATED (1 << 5) /* truncated down so flush-on-close */
  193. #define XFS_IDIRTY_RELEASE (1 << 6) /* dirty release already seen */
  194. #define __XFS_IFLOCK_BIT 7 /* inode is being flushed right now */
  195. #define XFS_IFLOCK (1 << __XFS_IFLOCK_BIT)
  196. #define __XFS_IPINNED_BIT 8 /* wakeup key for zero pin count */
  197. #define XFS_IPINNED (1 << __XFS_IPINNED_BIT)
  198. #define XFS_IDONTCACHE (1 << 9) /* don't cache the inode long term */
  199. #define XFS_IEOFBLOCKS (1 << 10)/* has the preallocblocks tag set */
  200. /*
  201. * If this unlinked inode is in the middle of recovery, don't let drop_inode
  202. * truncate and free the inode. This can happen if we iget the inode during
  203. * log recovery to replay a bmap operation on the inode.
  204. */
  205. #define XFS_IRECOVERY (1 << 11)
  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 PARENT subclass values
  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_PARENT_VAL 4
  311. #define XFS_IOLOCK_MAX_SUBCLASS (XFS_IOLOCK_PARENT_VAL - 1)
  312. #define XFS_IOLOCK_DEP_MASK 0x000f0000
  313. #define XFS_IOLOCK_PARENT (XFS_IOLOCK_PARENT_VAL << XFS_IOLOCK_SHIFT)
  314. #define XFS_MMAPLOCK_SHIFT 20
  315. #define XFS_MMAPLOCK_NUMORDER 0
  316. #define XFS_MMAPLOCK_MAX_SUBCLASS 3
  317. #define XFS_MMAPLOCK_DEP_MASK 0x00f00000
  318. #define XFS_ILOCK_SHIFT 24
  319. #define XFS_ILOCK_PARENT_VAL 5
  320. #define XFS_ILOCK_MAX_SUBCLASS (XFS_ILOCK_PARENT_VAL - 1)
  321. #define XFS_ILOCK_RTBITMAP_VAL 6
  322. #define XFS_ILOCK_RTSUM_VAL 7
  323. #define XFS_ILOCK_DEP_MASK 0xff000000
  324. #define XFS_ILOCK_PARENT (XFS_ILOCK_PARENT_VAL << XFS_ILOCK_SHIFT)
  325. #define XFS_ILOCK_RTBITMAP (XFS_ILOCK_RTBITMAP_VAL << XFS_ILOCK_SHIFT)
  326. #define XFS_ILOCK_RTSUM (XFS_ILOCK_RTSUM_VAL << XFS_ILOCK_SHIFT)
  327. #define XFS_LOCK_SUBCLASS_MASK (XFS_IOLOCK_DEP_MASK | \
  328. XFS_MMAPLOCK_DEP_MASK | \
  329. XFS_ILOCK_DEP_MASK)
  330. #define XFS_IOLOCK_DEP(flags) (((flags) & XFS_IOLOCK_DEP_MASK) \
  331. >> XFS_IOLOCK_SHIFT)
  332. #define XFS_MMAPLOCK_DEP(flags) (((flags) & XFS_MMAPLOCK_DEP_MASK) \
  333. >> XFS_MMAPLOCK_SHIFT)
  334. #define XFS_ILOCK_DEP(flags) (((flags) & XFS_ILOCK_DEP_MASK) \
  335. >> XFS_ILOCK_SHIFT)
  336. /*
  337. * For multiple groups support: if S_ISGID bit is set in the parent
  338. * directory, group of new file is set to that of the parent, and
  339. * new subdirectory gets S_ISGID bit from parent.
  340. */
  341. #define XFS_INHERIT_GID(pip) \
  342. (((pip)->i_mount->m_flags & XFS_MOUNT_GRPID) || \
  343. (VFS_I(pip)->i_mode & S_ISGID))
  344. int xfs_release(struct xfs_inode *ip);
  345. void xfs_inactive(struct xfs_inode *ip);
  346. int xfs_lookup(struct xfs_inode *dp, struct xfs_name *name,
  347. struct xfs_inode **ipp, struct xfs_name *ci_name);
  348. int xfs_create(struct xfs_inode *dp, struct xfs_name *name,
  349. umode_t mode, xfs_dev_t rdev, struct xfs_inode **ipp);
  350. int xfs_create_tmpfile(struct xfs_inode *dp, struct dentry *dentry,
  351. umode_t mode, struct xfs_inode **ipp);
  352. int xfs_remove(struct xfs_inode *dp, struct xfs_name *name,
  353. struct xfs_inode *ip);
  354. int xfs_link(struct xfs_inode *tdp, struct xfs_inode *sip,
  355. struct xfs_name *target_name);
  356. int xfs_rename(struct xfs_inode *src_dp, struct xfs_name *src_name,
  357. struct xfs_inode *src_ip, struct xfs_inode *target_dp,
  358. struct xfs_name *target_name,
  359. struct xfs_inode *target_ip, unsigned int flags);
  360. void xfs_ilock(xfs_inode_t *, uint);
  361. int xfs_ilock_nowait(xfs_inode_t *, uint);
  362. void xfs_iunlock(xfs_inode_t *, uint);
  363. void xfs_ilock_demote(xfs_inode_t *, uint);
  364. int xfs_isilocked(xfs_inode_t *, uint);
  365. uint xfs_ilock_data_map_shared(struct xfs_inode *);
  366. uint xfs_ilock_attr_map_shared(struct xfs_inode *);
  367. uint xfs_ip2xflags(struct xfs_inode *);
  368. int xfs_ifree(struct xfs_trans *, xfs_inode_t *,
  369. struct xfs_defer_ops *);
  370. int xfs_itruncate_extents(struct xfs_trans **, struct xfs_inode *,
  371. int, xfs_fsize_t);
  372. void xfs_iext_realloc(xfs_inode_t *, int, int);
  373. void xfs_iunpin_wait(xfs_inode_t *);
  374. #define xfs_ipincount(ip) ((unsigned int) atomic_read(&ip->i_pincount))
  375. int xfs_iflush(struct xfs_inode *, struct xfs_buf **);
  376. void xfs_lock_two_inodes(xfs_inode_t *, xfs_inode_t *, uint);
  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, xfs_dev_t, prid_t, int,
  381. struct xfs_inode **, int *);
  382. /* from xfs_file.c */
  383. enum xfs_prealloc_flags {
  384. XFS_PREALLOC_SET = (1 << 1),
  385. XFS_PREALLOC_CLEAR = (1 << 2),
  386. XFS_PREALLOC_SYNC = (1 << 3),
  387. XFS_PREALLOC_INVISIBLE = (1 << 4),
  388. };
  389. int xfs_update_prealloc_flags(struct xfs_inode *ip,
  390. enum xfs_prealloc_flags flags);
  391. int xfs_zero_eof(struct xfs_inode *ip, xfs_off_t offset,
  392. xfs_fsize_t isize, bool *did_zeroing);
  393. int xfs_zero_range(struct xfs_inode *ip, xfs_off_t pos, xfs_off_t count,
  394. bool *did_zero);
  395. loff_t __xfs_seek_hole_data(struct inode *inode, loff_t start,
  396. loff_t eof, int whence);
  397. /* from xfs_iops.c */
  398. extern void xfs_setup_inode(struct xfs_inode *ip);
  399. extern void xfs_setup_iops(struct xfs_inode *ip);
  400. /*
  401. * When setting up a newly allocated inode, we need to call
  402. * xfs_finish_inode_setup() once the inode is fully instantiated at
  403. * the VFS level to prevent the rest of the world seeing the inode
  404. * before we've completed instantiation. Otherwise we can do it
  405. * the moment the inode lookup is complete.
  406. */
  407. static inline void xfs_finish_inode_setup(struct xfs_inode *ip)
  408. {
  409. xfs_iflags_clear(ip, XFS_INEW);
  410. barrier();
  411. unlock_new_inode(VFS_I(ip));
  412. wake_up_bit(&ip->i_flags, __XFS_INEW_BIT);
  413. }
  414. static inline void xfs_setup_existing_inode(struct xfs_inode *ip)
  415. {
  416. xfs_setup_inode(ip);
  417. xfs_setup_iops(ip);
  418. xfs_finish_inode_setup(ip);
  419. }
  420. #define IHOLD(ip) \
  421. do { \
  422. ASSERT(atomic_read(&VFS_I(ip)->i_count) > 0) ; \
  423. ihold(VFS_I(ip)); \
  424. trace_xfs_ihold(ip, _THIS_IP_); \
  425. } while (0)
  426. #define IRELE(ip) \
  427. do { \
  428. trace_xfs_irele(ip, _THIS_IP_); \
  429. iput(VFS_I(ip)); \
  430. } while (0)
  431. extern struct kmem_zone *xfs_inode_zone;
  432. /* The default CoW extent size hint. */
  433. #define XFS_DEFAULT_COWEXTSZ_HINT 32
  434. #endif /* __XFS_INODE_H__ */