blob: d454161793e2e4ab70883335dfacd5bb16726a9c [file] [log] [blame]
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001/*
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_FORK_H__
19#define __XFS_INODE_FORK_H__
20
21struct xfs_inode_log_item;
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110022struct xfs_dinode;
Dave Chinner5c4d97d2013-08-12 20:49:33 +100023
24/*
25 * The following xfs_ext_irec_t struct introduces a second (top) level
26 * to the in-core extent allocation scheme. These structs are allocated
27 * in a contiguous block, creating an indirection array where each entry
28 * (irec) contains a pointer to a buffer of in-core extent records which
29 * it manages. Each extent buffer is 4k in size, since 4k is the system
30 * page size on Linux i386 and systems with larger page sizes don't seem
31 * to gain much, if anything, by using their native page size as the
32 * extent buffer size. Also, using 4k extent buffers everywhere provides
33 * a consistent interface for CXFS across different platforms.
34 *
35 * There is currently no limit on the number of irec's (extent lists)
36 * allowed, so heavily fragmented files may require an indirection array
37 * which spans multiple system pages of memory. The number of extents
38 * which would require this amount of contiguous memory is very large
39 * and should not cause problems in the foreseeable future. However,
40 * if the memory needed for the contiguous array ever becomes a problem,
41 * it is possible that a third level of indirection may be required.
42 */
43typedef struct xfs_ext_irec {
44 xfs_bmbt_rec_host_t *er_extbuf; /* block of extent records */
45 xfs_extnum_t er_extoff; /* extent offset in file */
46 xfs_extnum_t er_extcount; /* number of extents in page/block */
47} xfs_ext_irec_t;
48
49/*
50 * File incore extent information, present for each of data & attr forks.
51 */
52#define XFS_IEXT_BUFSZ 4096
53#define XFS_LINEAR_EXTS (XFS_IEXT_BUFSZ / (uint)sizeof(xfs_bmbt_rec_t))
54#define XFS_INLINE_EXTS 2
55#define XFS_INLINE_DATA 32
56typedef struct xfs_ifork {
57 int if_bytes; /* bytes in if_u1 */
58 int if_real_bytes; /* bytes allocated in if_u1 */
59 struct xfs_btree_block *if_broot; /* file's incore btree root */
60 short if_broot_bytes; /* bytes allocated for root */
61 unsigned char if_flags; /* per-fork flags */
62 union {
63 xfs_bmbt_rec_host_t *if_extents;/* linear map file exts */
64 xfs_ext_irec_t *if_ext_irec; /* irec map file exts */
65 char *if_data; /* inline file data */
66 } if_u1;
67 union {
68 xfs_bmbt_rec_host_t if_inline_ext[XFS_INLINE_EXTS];
69 /* very small file extents */
70 char if_inline_data[XFS_INLINE_DATA];
71 /* very small file data */
Dave Chinner5c4d97d2013-08-12 20:49:33 +100072 } if_u2;
73} xfs_ifork_t;
74
75/*
76 * Per-fork incore inode flags.
77 */
78#define XFS_IFINLINE 0x01 /* Inline data is read in */
79#define XFS_IFEXTENTS 0x02 /* All extent pointers are read in */
80#define XFS_IFBROOT 0x04 /* i_broot points to the bmap b-tree root */
81#define XFS_IFEXTIREC 0x08 /* Indirection array of extent blocks */
82
83/*
84 * Fork handling.
85 */
86
87#define XFS_IFORK_Q(ip) ((ip)->i_d.di_forkoff != 0)
88#define XFS_IFORK_BOFF(ip) ((int)((ip)->i_d.di_forkoff << 3))
89
90#define XFS_IFORK_PTR(ip,w) \
91 ((w) == XFS_DATA_FORK ? \
92 &(ip)->i_df : \
Darrick J. Wong3993bae2016-10-03 09:11:32 -070093 ((w) == XFS_ATTR_FORK ? \
94 (ip)->i_afp : \
95 (ip)->i_cowfp))
Dave Chinner5c4d97d2013-08-12 20:49:33 +100096#define XFS_IFORK_DSIZE(ip) \
97 (XFS_IFORK_Q(ip) ? \
98 XFS_IFORK_BOFF(ip) : \
99 XFS_LITINO((ip)->i_mount, (ip)->i_d.di_version))
100#define XFS_IFORK_ASIZE(ip) \
101 (XFS_IFORK_Q(ip) ? \
102 XFS_LITINO((ip)->i_mount, (ip)->i_d.di_version) - \
103 XFS_IFORK_BOFF(ip) : \
104 0)
105#define XFS_IFORK_SIZE(ip,w) \
106 ((w) == XFS_DATA_FORK ? \
107 XFS_IFORK_DSIZE(ip) : \
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700108 ((w) == XFS_ATTR_FORK ? \
109 XFS_IFORK_ASIZE(ip) : \
110 0))
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000111#define XFS_IFORK_FORMAT(ip,w) \
112 ((w) == XFS_DATA_FORK ? \
113 (ip)->i_d.di_format : \
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700114 ((w) == XFS_ATTR_FORK ? \
115 (ip)->i_d.di_aformat : \
116 (ip)->i_cformat))
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000117#define XFS_IFORK_FMT_SET(ip,w,n) \
118 ((w) == XFS_DATA_FORK ? \
119 ((ip)->i_d.di_format = (n)) : \
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700120 ((w) == XFS_ATTR_FORK ? \
121 ((ip)->i_d.di_aformat = (n)) : \
122 ((ip)->i_cformat = (n))))
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000123#define XFS_IFORK_NEXTENTS(ip,w) \
124 ((w) == XFS_DATA_FORK ? \
125 (ip)->i_d.di_nextents : \
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700126 ((w) == XFS_ATTR_FORK ? \
127 (ip)->i_d.di_anextents : \
128 (ip)->i_cnextents))
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000129#define XFS_IFORK_NEXT_SET(ip,w,n) \
130 ((w) == XFS_DATA_FORK ? \
131 ((ip)->i_d.di_nextents = (n)) : \
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700132 ((w) == XFS_ATTR_FORK ? \
133 ((ip)->i_d.di_anextents = (n)) : \
134 ((ip)->i_cnextents = (n))))
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000135#define XFS_IFORK_MAXEXT(ip, w) \
136 (XFS_IFORK_SIZE(ip, w) / sizeof(xfs_bmbt_rec_t))
137
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700138struct xfs_ifork *xfs_iext_state_to_fork(struct xfs_inode *ip, int state);
139
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000140int xfs_iformat_fork(struct xfs_inode *, struct xfs_dinode *);
Darrick J. Wong78420282017-04-03 12:22:20 -0700141void xfs_iflush_fork(struct xfs_inode *, struct xfs_dinode *,
Eric Sandeenfd9fdba2014-04-14 19:04:46 +1000142 struct xfs_inode_log_item *, int);
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000143void xfs_idestroy_fork(struct xfs_inode *, int);
144void xfs_idata_realloc(struct xfs_inode *, int, int);
145void xfs_iroot_realloc(struct xfs_inode *, int, int);
146int xfs_iread_extents(struct xfs_trans *, struct xfs_inode *, int);
147int xfs_iextents_copy(struct xfs_inode *, struct xfs_bmbt_rec *,
148 int);
Christoph Hellwig143f4ae2016-04-06 07:41:43 +1000149void xfs_init_local_fork(struct xfs_inode *, int, const void *, int);
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000150
151struct xfs_bmbt_rec_host *
152 xfs_iext_get_ext(struct xfs_ifork *, xfs_extnum_t);
Eric Sandeen5d829302016-11-08 12:59:42 +1100153xfs_extnum_t xfs_iext_count(struct xfs_ifork *);
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700154void xfs_iext_insert(struct xfs_inode *, struct xfs_iext_cursor *cur,
155 xfs_extnum_t, struct xfs_bmbt_irec *, int);
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000156void xfs_iext_add(struct xfs_ifork *, xfs_extnum_t, int);
157void xfs_iext_add_indirect_multi(struct xfs_ifork *, int,
158 xfs_extnum_t, int);
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700159void xfs_iext_remove(struct xfs_inode *, struct xfs_iext_cursor *,
160 int, int);
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000161void xfs_iext_remove_inline(struct xfs_ifork *, xfs_extnum_t, int);
162void xfs_iext_remove_direct(struct xfs_ifork *, xfs_extnum_t, int);
163void xfs_iext_remove_indirect(struct xfs_ifork *, xfs_extnum_t, int);
164void xfs_iext_realloc_direct(struct xfs_ifork *, int);
165void xfs_iext_direct_to_inline(struct xfs_ifork *, xfs_extnum_t);
166void xfs_iext_inline_to_direct(struct xfs_ifork *, int);
167void xfs_iext_destroy(struct xfs_ifork *);
168struct xfs_bmbt_rec_host *
169 xfs_iext_bno_to_ext(struct xfs_ifork *, xfs_fileoff_t, int *);
170struct xfs_ext_irec *
171 xfs_iext_bno_to_irec(struct xfs_ifork *, xfs_fileoff_t, int *);
172struct xfs_ext_irec *
173 xfs_iext_idx_to_irec(struct xfs_ifork *, xfs_extnum_t *, int *,
174 int);
175void xfs_iext_irec_init(struct xfs_ifork *);
176struct xfs_ext_irec *
177 xfs_iext_irec_new(struct xfs_ifork *, int);
178void xfs_iext_irec_remove(struct xfs_ifork *, int);
179void xfs_iext_irec_compact(struct xfs_ifork *);
180void xfs_iext_irec_compact_pages(struct xfs_ifork *);
181void xfs_iext_irec_compact_full(struct xfs_ifork *);
182void xfs_iext_irec_update_extoffs(struct xfs_ifork *, int, int);
183
Christoph Hellwig93533c72016-11-24 11:39:32 +1100184bool xfs_iext_lookup_extent(struct xfs_inode *ip,
185 struct xfs_ifork *ifp, xfs_fileoff_t bno,
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700186 struct xfs_iext_cursor *cur,
187 struct xfs_bmbt_irec *gotp);
Christoph Hellwigdc560152017-10-23 16:32:39 -0700188bool xfs_iext_lookup_extent_before(struct xfs_inode *ip,
189 struct xfs_ifork *ifp, xfs_fileoff_t *end,
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700190 struct xfs_iext_cursor *cur,
191 struct xfs_bmbt_irec *gotp);
192bool xfs_iext_get_extent(struct xfs_ifork *ifp,
193 struct xfs_iext_cursor *cur,
Christoph Hellwig93533c72016-11-24 11:39:32 +1100194 struct xfs_bmbt_irec *gotp);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -0700195void xfs_iext_update_extent(struct xfs_inode *ip, int state,
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700196 struct xfs_iext_cursor *cur,
197 struct xfs_bmbt_irec *gotp);
198
199static inline void xfs_iext_first(struct xfs_ifork *ifp,
200 struct xfs_iext_cursor *cur)
201{
202 cur->idx = 0;
203}
204
205static inline void xfs_iext_last(struct xfs_ifork *ifp,
206 struct xfs_iext_cursor *cur)
207{
208 cur->idx = xfs_iext_count(ifp) - 1;
209}
210
211static inline void xfs_iext_next(struct xfs_ifork *ifp,
212 struct xfs_iext_cursor *cur)
213{
214 cur->idx++;
215}
216
217static inline void xfs_iext_prev(struct xfs_ifork *ifp,
218 struct xfs_iext_cursor *cur)
219{
220 cur->idx--;
221}
222
223static inline bool xfs_iext_next_extent(struct xfs_ifork *ifp,
224 struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
225{
226 xfs_iext_next(ifp, cur);
227 return xfs_iext_get_extent(ifp, cur, gotp);
228}
229
230static inline bool xfs_iext_prev_extent(struct xfs_ifork *ifp,
231 struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
232{
233 xfs_iext_prev(ifp, cur);
234 return xfs_iext_get_extent(ifp, cur, gotp);
235}
236
237/*
238 * Return the extent after cur in gotp without updating the cursor.
239 */
240static inline bool xfs_iext_peek_next_extent(struct xfs_ifork *ifp,
241 struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
242{
243 struct xfs_iext_cursor ncur = *cur;
244
245 xfs_iext_next(ifp, &ncur);
246 return xfs_iext_get_extent(ifp, &ncur, gotp);
247}
248
249/*
250 * Return the extent before cur in gotp without updating the cursor.
251 */
252static inline bool xfs_iext_peek_prev_extent(struct xfs_ifork *ifp,
253 struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
254{
255 struct xfs_iext_cursor ncur = *cur;
256
257 xfs_iext_prev(ifp, &ncur);
258 return xfs_iext_get_extent(ifp, &ncur, gotp);
259}
260
261#define for_each_xfs_iext(ifp, ext, got) \
262 for (xfs_iext_first((ifp), (ext)); \
263 xfs_iext_get_extent((ifp), (ext), (got)); \
264 xfs_iext_next((ifp), (ext)))
Christoph Hellwig93533c72016-11-24 11:39:32 +1100265
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000266extern struct kmem_zone *xfs_ifork_zone;
267
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700268extern void xfs_ifork_init_cow(struct xfs_inode *ip);
269
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000270#endif /* __XFS_INODE_FORK_H__ */