2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
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.
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.
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
18 #ifndef __XFS_INODE_H__
19 #define __XFS_INODE_H__
26 * The following xfs_ext_irec_t struct introduces a second (top) level
27 * to the in-core extent allocation scheme. These structs are allocated
28 * in a contiguous block, creating an indirection array where each entry
29 * (irec) contains a pointer to a buffer of in-core extent records which
30 * it manages. Each extent buffer is 4k in size, since 4k is the system
31 * page size on Linux i386 and systems with larger page sizes don't seem
32 * to gain much, if anything, by using their native page size as the
33 * extent buffer size. Also, using 4k extent buffers everywhere provides
34 * a consistent interface for CXFS across different platforms.
36 * There is currently no limit on the number of irec's (extent lists)
37 * allowed, so heavily fragmented files may require an indirection array
38 * which spans multiple system pages of memory. The number of extents
39 * which would require this amount of contiguous memory is very large
40 * and should not cause problems in the foreseeable future. However,
41 * if the memory needed for the contiguous array ever becomes a problem,
42 * it is possible that a third level of indirection may be required.
44 typedef struct xfs_ext_irec
{
45 xfs_bmbt_rec_host_t
*er_extbuf
; /* block of extent records */
46 xfs_extnum_t er_extoff
; /* extent offset in file */
47 xfs_extnum_t er_extcount
; /* number of extents in page/block */
51 * File incore extent information, present for each of data & attr forks.
53 #define XFS_IEXT_BUFSZ 4096
54 #define XFS_LINEAR_EXTS (XFS_IEXT_BUFSZ / (uint)sizeof(xfs_bmbt_rec_t))
55 #define XFS_INLINE_EXTS 2
56 #define XFS_INLINE_DATA 32
57 typedef struct xfs_ifork
{
58 int if_bytes
; /* bytes in if_u1 */
59 int if_real_bytes
; /* bytes allocated in if_u1 */
60 struct xfs_btree_block
*if_broot
; /* file's incore btree root */
61 short if_broot_bytes
; /* bytes allocated for root */
62 unsigned char if_flags
; /* per-fork flags */
64 xfs_bmbt_rec_host_t
*if_extents
;/* linear map file exts */
65 xfs_ext_irec_t
*if_ext_irec
; /* irec map file exts */
66 char *if_data
; /* inline file data */
69 xfs_bmbt_rec_host_t if_inline_ext
[XFS_INLINE_EXTS
];
70 /* very small file extents */
71 char if_inline_data
[XFS_INLINE_DATA
];
72 /* very small file data */
73 xfs_dev_t if_rdev
; /* dev number if special */
74 uuid_t if_uuid
; /* mount point value */
79 * Inode location information. Stored in the inode and passed to
80 * xfs_imap_to_bp() to get a buffer and dinode for a given inode.
83 xfs_daddr_t im_blkno
; /* starting BB of inode chunk */
84 ushort im_len
; /* length in BBs of inode chunk */
85 ushort im_boffset
; /* inode offset in block in bytes */
89 * This is the xfs in-core inode structure.
90 * Most of the on-disk inode is embedded in the i_d field.
92 * The extent pointers/inline file space, however, are managed
93 * separately. The memory for this information is pointed to by
94 * the if_u1 unions depending on the type of the data.
95 * This is used to linearize the array of extents for fast in-core
96 * access. This is used until the file's number of extents
97 * surpasses XFS_MAX_INCORE_EXTENTS, at which point all extent pointers
98 * are accessed through the buffer cache.
100 * Other state kept in the in-core inode is used for identification,
101 * locking, transactional updating, etc of the inode.
103 * Generally, we do not want to hold the i_rlock while holding the
104 * i_ilock. Hierarchy is i_iolock followed by i_rlock.
106 * xfs_iptr_t contains all the inode fields up to and including the
107 * i_mnext and i_mprev fields, it is used as a marker in the inode
108 * chain off the mount structure by xfs_sync calls.
112 * Flags for xfs_ichgtime().
114 #define XFS_ICHGTIME_MOD 0x1 /* data fork modification timestamp */
115 #define XFS_ICHGTIME_CHG 0x2 /* inode field change timestamp */
116 #define XFS_ICHGTIME_CREATE 0x4 /* inode create timestamp */
119 * Per-fork incore inode flags.
121 #define XFS_IFINLINE 0x01 /* Inline data is read in */
122 #define XFS_IFEXTENTS 0x02 /* All extent pointers are read in */
123 #define XFS_IFBROOT 0x04 /* i_broot points to the bmap b-tree root */
124 #define XFS_IFEXTIREC 0x08 /* Indirection array of extent blocks */
130 #define XFS_IFORK_Q(ip) ((ip)->i_d.di_forkoff != 0)
131 #define XFS_IFORK_BOFF(ip) ((int)((ip)->i_d.di_forkoff << 3))
133 #define XFS_IFORK_PTR(ip,w) \
134 ((w) == XFS_DATA_FORK ? \
137 #define XFS_IFORK_DSIZE(ip) \
139 XFS_IFORK_BOFF(ip) : \
140 XFS_LITINO((ip)->i_mount, (ip)->i_d.di_version))
141 #define XFS_IFORK_ASIZE(ip) \
143 XFS_LITINO((ip)->i_mount, (ip)->i_d.di_version) - \
144 XFS_IFORK_BOFF(ip) : \
146 #define XFS_IFORK_SIZE(ip,w) \
147 ((w) == XFS_DATA_FORK ? \
148 XFS_IFORK_DSIZE(ip) : \
150 #define XFS_IFORK_FORMAT(ip,w) \
151 ((w) == XFS_DATA_FORK ? \
152 (ip)->i_d.di_format : \
153 (ip)->i_d.di_aformat)
154 #define XFS_IFORK_FMT_SET(ip,w,n) \
155 ((w) == XFS_DATA_FORK ? \
156 ((ip)->i_d.di_format = (n)) : \
157 ((ip)->i_d.di_aformat = (n)))
158 #define XFS_IFORK_NEXTENTS(ip,w) \
159 ((w) == XFS_DATA_FORK ? \
160 (ip)->i_d.di_nextents : \
161 (ip)->i_d.di_anextents)
162 #define XFS_IFORK_NEXT_SET(ip,w,n) \
163 ((w) == XFS_DATA_FORK ? \
164 ((ip)->i_d.di_nextents = (n)) : \
165 ((ip)->i_d.di_anextents = (n)))
166 #define XFS_IFORK_MAXEXT(ip, w) \
167 (XFS_IFORK_SIZE(ip, w) / sizeof(xfs_bmbt_rec_t))
173 struct xfs_bmap_free
;
174 struct xfs_bmbt_irec
;
175 struct xfs_inode_log_item
;
180 typedef struct xfs_inode
{
181 /* Inode linking and identification information. */
182 struct xfs_mount
*i_mount
; /* fs mount struct ptr */
183 struct xfs_dquot
*i_udquot
; /* user dquot */
184 struct xfs_dquot
*i_gdquot
; /* group dquot */
185 struct xfs_dquot
*i_pdquot
; /* project dquot */
187 /* Inode location stuff */
188 xfs_ino_t i_ino
; /* inode number (agno/agino)*/
189 struct xfs_imap i_imap
; /* location for xfs_imap() */
191 /* Extent information. */
192 xfs_ifork_t
*i_afp
; /* attribute fork pointer */
193 xfs_ifork_t i_df
; /* data fork */
195 /* Transaction and locking information. */
196 struct xfs_inode_log_item
*i_itemp
; /* logging information */
197 mrlock_t i_lock
; /* inode lock */
198 mrlock_t i_iolock
; /* inode IO lock */
199 atomic_t i_pincount
; /* inode pin count */
200 spinlock_t i_flags_lock
; /* inode i_flags lock */
201 /* Miscellaneous state. */
202 unsigned long i_flags
; /* see defined flags below */
203 unsigned int i_delayed_blks
; /* count of delay alloc blks */
205 xfs_icdinode_t i_d
; /* most of ondisk inode */
208 struct inode i_vnode
; /* embedded VFS inode */
211 /* Convert from vfs inode to xfs inode */
212 static inline struct xfs_inode
*XFS_I(struct inode
*inode
)
214 return container_of(inode
, struct xfs_inode
, i_vnode
);
217 /* convert from xfs inode to vfs inode */
218 static inline struct inode
*VFS_I(struct xfs_inode
*ip
)
224 * For regular files we only update the on-disk filesize when actually
225 * writing data back to disk. Until then only the copy in the VFS inode
228 static inline xfs_fsize_t
XFS_ISIZE(struct xfs_inode
*ip
)
230 if (S_ISREG(ip
->i_d
.di_mode
))
231 return i_size_read(VFS_I(ip
));
232 return ip
->i_d
.di_size
;
236 * If this I/O goes past the on-disk inode size update it unless it would
237 * be past the current in-core inode size.
239 static inline xfs_fsize_t
240 xfs_new_eof(struct xfs_inode
*ip
, xfs_fsize_t new_size
)
242 xfs_fsize_t i_size
= i_size_read(VFS_I(ip
));
244 if (new_size
> i_size
)
246 return new_size
> ip
->i_d
.di_size
? new_size
: 0;
250 * i_flags helper functions
253 __xfs_iflags_set(xfs_inode_t
*ip
, unsigned short flags
)
255 ip
->i_flags
|= flags
;
259 xfs_iflags_set(xfs_inode_t
*ip
, unsigned short flags
)
261 spin_lock(&ip
->i_flags_lock
);
262 __xfs_iflags_set(ip
, flags
);
263 spin_unlock(&ip
->i_flags_lock
);
267 xfs_iflags_clear(xfs_inode_t
*ip
, unsigned short flags
)
269 spin_lock(&ip
->i_flags_lock
);
270 ip
->i_flags
&= ~flags
;
271 spin_unlock(&ip
->i_flags_lock
);
275 __xfs_iflags_test(xfs_inode_t
*ip
, unsigned short flags
)
277 return (ip
->i_flags
& flags
);
281 xfs_iflags_test(xfs_inode_t
*ip
, unsigned short flags
)
284 spin_lock(&ip
->i_flags_lock
);
285 ret
= __xfs_iflags_test(ip
, flags
);
286 spin_unlock(&ip
->i_flags_lock
);
291 xfs_iflags_test_and_clear(xfs_inode_t
*ip
, unsigned short flags
)
295 spin_lock(&ip
->i_flags_lock
);
296 ret
= ip
->i_flags
& flags
;
298 ip
->i_flags
&= ~flags
;
299 spin_unlock(&ip
->i_flags_lock
);
304 xfs_iflags_test_and_set(xfs_inode_t
*ip
, unsigned short flags
)
308 spin_lock(&ip
->i_flags_lock
);
309 ret
= ip
->i_flags
& flags
;
311 ip
->i_flags
|= flags
;
312 spin_unlock(&ip
->i_flags_lock
);
317 * Project quota id helpers (previously projid was 16bit only
318 * and using two 16bit values to hold new 32bit projid was chosen
319 * to retain compatibility with "old" filesystems).
322 xfs_get_projid(struct xfs_inode
*ip
)
324 return (prid_t
)ip
->i_d
.di_projid_hi
<< 16 | ip
->i_d
.di_projid_lo
;
328 xfs_set_projid(struct xfs_inode
*ip
,
331 ip
->i_d
.di_projid_hi
= (__uint16_t
) (projid
>> 16);
332 ip
->i_d
.di_projid_lo
= (__uint16_t
) (projid
& 0xffff);
336 * In-core inode flags.
338 #define XFS_IRECLAIM (1 << 0) /* started reclaiming this inode */
339 #define XFS_ISTALE (1 << 1) /* inode has been staled */
340 #define XFS_IRECLAIMABLE (1 << 2) /* inode can be reclaimed */
341 #define XFS_INEW (1 << 3) /* inode has just been allocated */
342 #define XFS_IFILESTREAM (1 << 4) /* inode is in a filestream dir. */
343 #define XFS_ITRUNCATED (1 << 5) /* truncated down so flush-on-close */
344 #define XFS_IDIRTY_RELEASE (1 << 6) /* dirty release already seen */
345 #define __XFS_IFLOCK_BIT 7 /* inode is being flushed right now */
346 #define XFS_IFLOCK (1 << __XFS_IFLOCK_BIT)
347 #define __XFS_IPINNED_BIT 8 /* wakeup key for zero pin count */
348 #define XFS_IPINNED (1 << __XFS_IPINNED_BIT)
349 #define XFS_IDONTCACHE (1 << 9) /* don't cache the inode long term */
352 * Per-lifetime flags need to be reset when re-using a reclaimable inode during
353 * inode lookup. This prevents unintended behaviour on the new inode from
356 #define XFS_IRECLAIM_RESET_FLAGS \
357 (XFS_IRECLAIMABLE | XFS_IRECLAIM | \
358 XFS_IDIRTY_RELEASE | XFS_ITRUNCATED | \
362 * Synchronize processes attempting to flush the in-core inode back to disk.
365 extern void __xfs_iflock(struct xfs_inode
*ip
);
367 static inline int xfs_iflock_nowait(struct xfs_inode
*ip
)
369 return !xfs_iflags_test_and_set(ip
, XFS_IFLOCK
);
372 static inline void xfs_iflock(struct xfs_inode
*ip
)
374 if (!xfs_iflock_nowait(ip
))
378 static inline void xfs_ifunlock(struct xfs_inode
*ip
)
380 xfs_iflags_clear(ip
, XFS_IFLOCK
);
382 wake_up_bit(&ip
->i_flags
, __XFS_IFLOCK_BIT
);
385 static inline int xfs_isiflocked(struct xfs_inode
*ip
)
387 return xfs_iflags_test(ip
, XFS_IFLOCK
);
391 * Flags for inode locking.
392 * Bit ranges: 1<<1 - 1<<16-1 -- iolock/ilock modes (bitfield)
393 * 1<<16 - 1<<32-1 -- lockdep annotation (integers)
395 #define XFS_IOLOCK_EXCL (1<<0)
396 #define XFS_IOLOCK_SHARED (1<<1)
397 #define XFS_ILOCK_EXCL (1<<2)
398 #define XFS_ILOCK_SHARED (1<<3)
400 #define XFS_LOCK_MASK (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED \
401 | XFS_ILOCK_EXCL | XFS_ILOCK_SHARED)
403 #define XFS_LOCK_FLAGS \
404 { XFS_IOLOCK_EXCL, "IOLOCK_EXCL" }, \
405 { XFS_IOLOCK_SHARED, "IOLOCK_SHARED" }, \
406 { XFS_ILOCK_EXCL, "ILOCK_EXCL" }, \
407 { XFS_ILOCK_SHARED, "ILOCK_SHARED" }
411 * Flags for lockdep annotations.
413 * XFS_LOCK_PARENT - for directory operations that require locking a
414 * parent directory inode and a child entry inode. The parent gets locked
415 * with this flag so it gets a lockdep subclass of 1 and the child entry
416 * lock will have a lockdep subclass of 0.
418 * XFS_LOCK_RTBITMAP/XFS_LOCK_RTSUM - the realtime device bitmap and summary
419 * inodes do not participate in the normal lock order, and thus have their
422 * XFS_LOCK_INUMORDER - for locking several inodes at the some time
423 * with xfs_lock_inodes(). This flag is used as the starting subclass
424 * and each subsequent lock acquired will increment the subclass by one.
425 * So the first lock acquired will have a lockdep subclass of 4, the
426 * second lock will have a lockdep subclass of 5, and so on. It is
427 * the responsibility of the class builder to shift this to the correct
428 * portion of the lock_mode lockdep mask.
430 #define XFS_LOCK_PARENT 1
431 #define XFS_LOCK_RTBITMAP 2
432 #define XFS_LOCK_RTSUM 3
433 #define XFS_LOCK_INUMORDER 4
435 #define XFS_IOLOCK_SHIFT 16
436 #define XFS_IOLOCK_PARENT (XFS_LOCK_PARENT << XFS_IOLOCK_SHIFT)
438 #define XFS_ILOCK_SHIFT 24
439 #define XFS_ILOCK_PARENT (XFS_LOCK_PARENT << XFS_ILOCK_SHIFT)
440 #define XFS_ILOCK_RTBITMAP (XFS_LOCK_RTBITMAP << XFS_ILOCK_SHIFT)
441 #define XFS_ILOCK_RTSUM (XFS_LOCK_RTSUM << XFS_ILOCK_SHIFT)
443 #define XFS_IOLOCK_DEP_MASK 0x00ff0000
444 #define XFS_ILOCK_DEP_MASK 0xff000000
445 #define XFS_LOCK_DEP_MASK (XFS_IOLOCK_DEP_MASK | XFS_ILOCK_DEP_MASK)
447 #define XFS_IOLOCK_DEP(flags) (((flags) & XFS_IOLOCK_DEP_MASK) >> XFS_IOLOCK_SHIFT)
448 #define XFS_ILOCK_DEP(flags) (((flags) & XFS_ILOCK_DEP_MASK) >> XFS_ILOCK_SHIFT)
451 * For multiple groups support: if S_ISGID bit is set in the parent
452 * directory, group of new file is set to that of the parent, and
453 * new subdirectory gets S_ISGID bit from parent.
455 #define XFS_INHERIT_GID(pip) \
456 (((pip)->i_mount->m_flags & XFS_MOUNT_GRPID) || \
457 ((pip)->i_d.di_mode & S_ISGID))
461 * xfs_inode.c prototypes.
463 void xfs_ilock(xfs_inode_t
*, uint
);
464 int xfs_ilock_nowait(xfs_inode_t
*, uint
);
465 void xfs_iunlock(xfs_inode_t
*, uint
);
466 void xfs_ilock_demote(xfs_inode_t
*, uint
);
467 int xfs_isilocked(xfs_inode_t
*, uint
);
468 uint
xfs_ilock_map_shared(xfs_inode_t
*);
469 void xfs_iunlock_map_shared(xfs_inode_t
*, uint
);
470 int xfs_ialloc(struct xfs_trans
*, xfs_inode_t
*, umode_t
,
471 xfs_nlink_t
, xfs_dev_t
, prid_t
, int,
472 struct xfs_buf
**, xfs_inode_t
**);
474 uint
xfs_ip2xflags(struct xfs_inode
*);
475 uint
xfs_dic2xflags(struct xfs_dinode
*);
476 int xfs_ifree(struct xfs_trans
*, xfs_inode_t
*,
477 struct xfs_bmap_free
*);
478 int xfs_itruncate_extents(struct xfs_trans
**, struct xfs_inode
*,
480 int xfs_iunlink(struct xfs_trans
*, xfs_inode_t
*);
482 void xfs_iext_realloc(xfs_inode_t
*, int, int);
483 void xfs_iunpin_wait(xfs_inode_t
*);
484 int xfs_iflush(struct xfs_inode
*, struct xfs_buf
**);
485 void xfs_lock_inodes(xfs_inode_t
**, int, uint
);
486 void xfs_lock_two_inodes(xfs_inode_t
*, xfs_inode_t
*, uint
);
488 xfs_extlen_t
xfs_get_extsz_hint(struct xfs_inode
*ip
);
492 ASSERT(atomic_read(&VFS_I(ip)->i_count) > 0) ; \
494 trace_xfs_ihold(ip, _THIS_IP_); \
499 trace_xfs_irele(ip, _THIS_IP_); \
503 #endif /* __KERNEL__ */
506 * Flags for xfs_iget()
508 #define XFS_IGET_CREATE 0x1
509 #define XFS_IGET_UNTRUSTED 0x2
510 #define XFS_IGET_DONTCACHE 0x4
512 int xfs_imap_to_bp(struct xfs_mount
*, struct xfs_trans
*,
513 struct xfs_imap
*, struct xfs_dinode
**,
514 struct xfs_buf
**, uint
, uint
);
515 int xfs_iread(struct xfs_mount
*, struct xfs_trans
*,
516 struct xfs_inode
*, uint
);
517 void xfs_dinode_calc_crc(struct xfs_mount
*, struct xfs_dinode
*);
518 void xfs_dinode_to_disk(struct xfs_dinode
*,
519 struct xfs_icdinode
*);
520 void xfs_idestroy_fork(struct xfs_inode
*, int);
521 void xfs_idata_realloc(struct xfs_inode
*, int, int);
522 void xfs_iroot_realloc(struct xfs_inode
*, int, int);
523 int xfs_iread_extents(struct xfs_trans
*, struct xfs_inode
*, int);
524 int xfs_iextents_copy(struct xfs_inode
*, xfs_bmbt_rec_t
*, int);
526 xfs_bmbt_rec_host_t
*xfs_iext_get_ext(xfs_ifork_t
*, xfs_extnum_t
);
527 void xfs_iext_insert(xfs_inode_t
*, xfs_extnum_t
, xfs_extnum_t
,
528 xfs_bmbt_irec_t
*, int);
529 void xfs_iext_add(xfs_ifork_t
*, xfs_extnum_t
, int);
530 void xfs_iext_add_indirect_multi(xfs_ifork_t
*, int, xfs_extnum_t
, int);
531 void xfs_iext_remove(xfs_inode_t
*, xfs_extnum_t
, int, int);
532 void xfs_iext_remove_inline(xfs_ifork_t
*, xfs_extnum_t
, int);
533 void xfs_iext_remove_direct(xfs_ifork_t
*, xfs_extnum_t
, int);
534 void xfs_iext_remove_indirect(xfs_ifork_t
*, xfs_extnum_t
, int);
535 void xfs_iext_realloc_direct(xfs_ifork_t
*, int);
536 void xfs_iext_direct_to_inline(xfs_ifork_t
*, xfs_extnum_t
);
537 void xfs_iext_inline_to_direct(xfs_ifork_t
*, int);
538 void xfs_iext_destroy(xfs_ifork_t
*);
539 xfs_bmbt_rec_host_t
*xfs_iext_bno_to_ext(xfs_ifork_t
*, xfs_fileoff_t
, int *);
540 xfs_ext_irec_t
*xfs_iext_bno_to_irec(xfs_ifork_t
*, xfs_fileoff_t
, int *);
541 xfs_ext_irec_t
*xfs_iext_idx_to_irec(xfs_ifork_t
*, xfs_extnum_t
*, int *, int);
542 void xfs_iext_irec_init(xfs_ifork_t
*);
543 xfs_ext_irec_t
*xfs_iext_irec_new(xfs_ifork_t
*, int);
544 void xfs_iext_irec_remove(xfs_ifork_t
*, int);
545 void xfs_iext_irec_compact(xfs_ifork_t
*);
546 void xfs_iext_irec_compact_pages(xfs_ifork_t
*);
547 void xfs_iext_irec_compact_full(xfs_ifork_t
*);
548 void xfs_iext_irec_update_extoffs(xfs_ifork_t
*, int, int);
549 bool xfs_can_free_eofblocks(struct xfs_inode
*, bool);
551 #define xfs_ipincount(ip) ((unsigned int) atomic_read(&ip->i_pincount))
554 void xfs_inobp_check(struct xfs_mount
*, struct xfs_buf
*);
556 #define xfs_inobp_check(mp, bp)
559 extern struct kmem_zone
*xfs_ifork_zone
;
560 extern struct kmem_zone
*xfs_inode_zone
;
561 extern const struct xfs_buf_ops xfs_inode_buf_ops
;
563 #endif /* __XFS_INODE_H__ */