]>
Commit | Line | Data |
---|---|---|
959ef981 | 1 | // SPDX-License-Identifier: GPL-2.0 |
2bd0ea18 | 2 | /* |
da23017d NS |
3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
4 | * All Rights Reserved. | |
2bd0ea18 NS |
5 | */ |
6 | ||
9c799827 | 7 | #include "libxfs_priv.h" |
7ed3b9bd | 8 | #include "libxfs.h" |
9542ae13 | 9 | #include "libxfs_io.h" |
b626fb59 DC |
10 | #include "init.h" |
11 | #include "xfs_fs.h" | |
12 | #include "xfs_shared.h" | |
13 | #include "xfs_format.h" | |
14 | #include "xfs_log_format.h" | |
15 | #include "xfs_trans_resv.h" | |
16 | #include "xfs_mount.h" | |
f944d3d0 | 17 | #include "xfs_defer.h" |
b626fb59 DC |
18 | #include "xfs_inode_buf.h" |
19 | #include "xfs_inode_fork.h" | |
20 | #include "xfs_inode.h" | |
21 | #include "xfs_trans.h" | |
22 | #include "xfs_bmap.h" | |
23 | #include "xfs_bmap_btree.h" | |
24 | #include "xfs_trans_space.h" | |
25 | #include "xfs_ialloc.h" | |
26 | #include "xfs_alloc.h" | |
9542ae13 | 27 | #include "xfs_bit.h" |
d15188a1 DW |
28 | #include "xfs_da_format.h" |
29 | #include "xfs_da_btree.h" | |
30 | #include "xfs_dir2_priv.h" | |
2bd0ea18 | 31 | |
88cd79be DC |
32 | /* |
33 | * Calculate the worst case log unit reservation for a given superblock | |
34 | * configuration. Copied and munged from the kernel code, and assumes a | |
35 | * worse case header usage (maximum log buffer sizes) | |
36 | */ | |
37 | int | |
38 | xfs_log_calc_unit_res( | |
39 | struct xfs_mount *mp, | |
40 | int unit_bytes) | |
41 | { | |
42 | int iclog_space; | |
43 | int iclog_header_size; | |
44 | int iclog_size; | |
45 | uint num_headers; | |
46 | ||
47 | if (xfs_sb_version_haslogv2(&mp->m_sb)) { | |
48 | iclog_size = XLOG_MAX_RECORD_BSIZE; | |
49 | iclog_header_size = BBTOB(iclog_size / XLOG_HEADER_CYCLE_SIZE); | |
50 | } else { | |
51 | iclog_size = XLOG_BIG_RECORD_BSIZE; | |
52 | iclog_header_size = BBSIZE; | |
53 | } | |
54 | ||
55 | /* | |
56 | * Permanent reservations have up to 'cnt'-1 active log operations | |
57 | * in the log. A unit in this case is the amount of space for one | |
58 | * of these log operations. Normal reservations have a cnt of 1 | |
59 | * and their unit amount is the total amount of space required. | |
60 | * | |
61 | * The following lines of code account for non-transaction data | |
62 | * which occupy space in the on-disk log. | |
63 | * | |
64 | * Normal form of a transaction is: | |
65 | * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph> | |
66 | * and then there are LR hdrs, split-recs and roundoff at end of syncs. | |
67 | * | |
68 | * We need to account for all the leadup data and trailer data | |
69 | * around the transaction data. | |
70 | * And then we need to account for the worst case in terms of using | |
71 | * more space. | |
72 | * The worst case will happen if: | |
73 | * - the placement of the transaction happens to be such that the | |
74 | * roundoff is at its maximum | |
75 | * - the transaction data is synced before the commit record is synced | |
76 | * i.e. <transaction-data><roundoff> | <commit-rec><roundoff> | |
77 | * Therefore the commit record is in its own Log Record. | |
78 | * This can happen as the commit record is called with its | |
79 | * own region to xlog_write(). | |
80 | * This then means that in the worst case, roundoff can happen for | |
81 | * the commit-rec as well. | |
82 | * The commit-rec is smaller than padding in this scenario and so it is | |
83 | * not added separately. | |
84 | */ | |
85 | ||
86 | /* for trans header */ | |
87 | unit_bytes += sizeof(xlog_op_header_t); | |
88 | unit_bytes += sizeof(xfs_trans_header_t); | |
89 | ||
90 | /* for start-rec */ | |
91 | unit_bytes += sizeof(xlog_op_header_t); | |
92 | ||
93 | /* | |
94 | * for LR headers - the space for data in an iclog is the size minus | |
95 | * the space used for the headers. If we use the iclog size, then we | |
96 | * undercalculate the number of headers required. | |
97 | * | |
98 | * Furthermore - the addition of op headers for split-recs might | |
99 | * increase the space required enough to require more log and op | |
100 | * headers, so take that into account too. | |
101 | * | |
102 | * IMPORTANT: This reservation makes the assumption that if this | |
103 | * transaction is the first in an iclog and hence has the LR headers | |
104 | * accounted to it, then the remaining space in the iclog is | |
105 | * exclusively for this transaction. i.e. if the transaction is larger | |
106 | * than the iclog, it will be the only thing in that iclog. | |
107 | * Fundamentally, this means we must pass the entire log vector to | |
108 | * xlog_write to guarantee this. | |
109 | */ | |
110 | iclog_space = iclog_size - iclog_header_size; | |
111 | num_headers = howmany(unit_bytes, iclog_space); | |
112 | ||
113 | /* for split-recs - ophdrs added when data split over LRs */ | |
114 | unit_bytes += sizeof(xlog_op_header_t) * num_headers; | |
115 | ||
116 | /* add extra header reservations if we overrun */ | |
117 | while (!num_headers || | |
118 | howmany(unit_bytes, iclog_space) > num_headers) { | |
119 | unit_bytes += sizeof(xlog_op_header_t); | |
120 | num_headers++; | |
121 | } | |
122 | unit_bytes += iclog_header_size * num_headers; | |
123 | ||
124 | /* for commit-rec LR header - note: padding will subsume the ophdr */ | |
125 | unit_bytes += iclog_header_size; | |
126 | ||
127 | /* for roundoff padding for transaction data and one for commit record */ | |
128 | if (xfs_sb_version_haslogv2(&mp->m_sb) && mp->m_sb.sb_logsunit > 1) { | |
129 | /* log su roundoff */ | |
130 | unit_bytes += 2 * mp->m_sb.sb_logsunit; | |
131 | } else { | |
132 | /* BB roundoff */ | |
133 | unit_bytes += 2 * BBSIZE; | |
134 | } | |
135 | ||
136 | return unit_bytes; | |
137 | } | |
138 | ||
2bd0ea18 NS |
139 | /* |
140 | * Change the requested timestamp in the given inode. | |
5000d01d | 141 | * |
2bd0ea18 | 142 | * This was once shared with the kernel, but has diverged to the point |
ff1f79a7 | 143 | * where it's no longer worth the hassle of maintaining common code. |
2bd0ea18 NS |
144 | */ |
145 | void | |
56b2de80 DC |
146 | libxfs_trans_ichgtime( |
147 | struct xfs_trans *tp, | |
148 | struct xfs_inode *ip, | |
149 | int flags) | |
2bd0ea18 | 150 | { |
5000d01d | 151 | struct timespec tv; |
2bd0ea18 NS |
152 | struct timeval stv; |
153 | ||
154 | gettimeofday(&stv, (struct timezone *)0); | |
155 | tv.tv_sec = stv.tv_sec; | |
156 | tv.tv_nsec = stv.tv_usec * 1000; | |
1bc6cbe3 DC |
157 | if (flags & XFS_ICHGTIME_MOD) |
158 | VFS_I(ip)->i_mtime = tv; | |
159 | if (flags & XFS_ICHGTIME_CHG) | |
160 | VFS_I(ip)->i_ctime = tv; | |
41ce5f36 | 161 | if (flags & XFS_ICHGTIME_CREATE) { |
14f8b681 DW |
162 | ip->i_d.di_crtime.t_sec = (int32_t)tv.tv_sec; |
163 | ip->i_d.di_crtime.t_nsec = (int32_t)tv.tv_nsec; | |
41ce5f36 | 164 | } |
2bd0ea18 NS |
165 | } |
166 | ||
06b80354 DW |
167 | STATIC uint16_t |
168 | xfs_flags2diflags( | |
169 | struct xfs_inode *ip, | |
170 | unsigned int xflags) | |
171 | { | |
172 | /* can't set PREALLOC this way, just preserve it */ | |
173 | uint16_t di_flags = | |
174 | (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC); | |
175 | ||
176 | if (xflags & FS_XFLAG_IMMUTABLE) | |
177 | di_flags |= XFS_DIFLAG_IMMUTABLE; | |
178 | if (xflags & FS_XFLAG_APPEND) | |
179 | di_flags |= XFS_DIFLAG_APPEND; | |
180 | if (xflags & FS_XFLAG_SYNC) | |
181 | di_flags |= XFS_DIFLAG_SYNC; | |
182 | if (xflags & FS_XFLAG_NOATIME) | |
183 | di_flags |= XFS_DIFLAG_NOATIME; | |
184 | if (xflags & FS_XFLAG_NODUMP) | |
185 | di_flags |= XFS_DIFLAG_NODUMP; | |
186 | if (xflags & FS_XFLAG_NODEFRAG) | |
187 | di_flags |= XFS_DIFLAG_NODEFRAG; | |
188 | if (xflags & FS_XFLAG_FILESTREAM) | |
189 | di_flags |= XFS_DIFLAG_FILESTREAM; | |
190 | if (S_ISDIR(VFS_I(ip)->i_mode)) { | |
191 | if (xflags & FS_XFLAG_RTINHERIT) | |
192 | di_flags |= XFS_DIFLAG_RTINHERIT; | |
193 | if (xflags & FS_XFLAG_NOSYMLINKS) | |
194 | di_flags |= XFS_DIFLAG_NOSYMLINKS; | |
195 | if (xflags & FS_XFLAG_EXTSZINHERIT) | |
196 | di_flags |= XFS_DIFLAG_EXTSZINHERIT; | |
197 | if (xflags & FS_XFLAG_PROJINHERIT) | |
198 | di_flags |= XFS_DIFLAG_PROJINHERIT; | |
199 | } else if (S_ISREG(VFS_I(ip)->i_mode)) { | |
200 | if (xflags & FS_XFLAG_REALTIME) | |
201 | di_flags |= XFS_DIFLAG_REALTIME; | |
202 | if (xflags & FS_XFLAG_EXTSIZE) | |
203 | di_flags |= XFS_DIFLAG_EXTSIZE; | |
204 | } | |
205 | ||
206 | return di_flags; | |
207 | } | |
208 | ||
209 | STATIC uint64_t | |
210 | xfs_flags2diflags2( | |
211 | struct xfs_inode *ip, | |
212 | unsigned int xflags) | |
213 | { | |
214 | uint64_t di_flags2 = | |
215 | (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK); | |
216 | ||
217 | if (xflags & FS_XFLAG_DAX) | |
218 | di_flags2 |= XFS_DIFLAG2_DAX; | |
219 | if (xflags & FS_XFLAG_COWEXTSIZE) | |
220 | di_flags2 |= XFS_DIFLAG2_COWEXTSIZE; | |
221 | ||
222 | return di_flags2; | |
223 | } | |
224 | ||
2bd0ea18 | 225 | /* |
ff1f79a7 | 226 | * Allocate an inode on disk and return a copy of its in-core version. |
2bd0ea18 NS |
227 | * Set mode, nlink, and rdev appropriately within the inode. |
228 | * The uid and gid for the inode are set according to the contents of | |
229 | * the given cred structure. | |
230 | * | |
231 | * This was once shared with the kernel, but has diverged to the point | |
ff1f79a7 | 232 | * where it's no longer worth the hassle of maintaining common code. |
2bd0ea18 | 233 | */ |
00ff2b10 | 234 | static int |
2bd0ea18 NS |
235 | libxfs_ialloc( |
236 | xfs_trans_t *tp, | |
237 | xfs_inode_t *pip, | |
238 | mode_t mode, | |
239 | nlink_t nlink, | |
63899e27 | 240 | xfs_dev_t rdev, |
9f064b7e NS |
241 | struct cred *cr, |
242 | struct fsxattr *fsx, | |
2bd0ea18 | 243 | xfs_buf_t **ialloc_context, |
2bd0ea18 NS |
244 | xfs_inode_t **ipp) |
245 | { | |
246 | xfs_ino_t ino; | |
247 | xfs_inode_t *ip; | |
248 | uint flags; | |
249 | int error; | |
250 | ||
251 | /* | |
252 | * Call the space management code to pick | |
253 | * the on-disk inode to be allocated. | |
254 | */ | |
c1a29e92 | 255 | error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, |
a2ceac1f | 256 | ialloc_context, &ino); |
2bd0ea18 NS |
257 | if (error != 0) |
258 | return error; | |
a2ceac1f | 259 | if (*ialloc_context || ino == NULLFSINO) { |
2bd0ea18 NS |
260 | *ipp = NULL; |
261 | return 0; | |
262 | } | |
263 | ASSERT(*ialloc_context == NULL); | |
264 | ||
46eca962 | 265 | error = xfs_trans_iget(tp->t_mountp, tp, ino, 0, 0, &ip); |
2bd0ea18 NS |
266 | if (error != 0) |
267 | return error; | |
268 | ASSERT(ip != NULL); | |
269 | ||
e37bf53c | 270 | VFS_I(ip)->i_mode = mode; |
bcbe04c1 | 271 | set_nlink(VFS_I(ip), nlink); |
2bd0ea18 NS |
272 | ip->i_d.di_uid = cr->cr_uid; |
273 | ip->i_d.di_gid = cr->cr_gid; | |
22bc10ed | 274 | xfs_set_projid(&ip->i_d, pip ? 0 : fsx->fsx_projid); |
41ce5f36 | 275 | xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG | XFS_ICHGTIME_MOD); |
2bd0ea18 NS |
276 | |
277 | /* | |
ff105f75 DC |
278 | * We only support filesystems that understand v2 format inodes. So if |
279 | * this is currently an old format inode, then change the inode version | |
280 | * number now. This way we only do the conversion here rather than here | |
281 | * and in the flush/logging code. | |
2bd0ea18 | 282 | */ |
ff105f75 | 283 | if (ip->i_d.di_version == 1) { |
56b2de80 | 284 | ip->i_d.di_version = 2; |
22bc10ed AM |
285 | /* |
286 | * old link count, projid_lo/hi field, pad field | |
287 | * already zeroed | |
288 | */ | |
5000d01d | 289 | } |
2bd0ea18 | 290 | |
e37bf53c | 291 | if (pip && (VFS_I(pip)->i_mode & S_ISGID)) { |
9f064b7e | 292 | ip->i_d.di_gid = pip->i_d.di_gid; |
e37bf53c DC |
293 | if ((VFS_I(pip)->i_mode & S_ISGID) && (mode & S_IFMT) == S_IFDIR) |
294 | VFS_I(ip)->i_mode |= S_ISGID; | |
9f064b7e NS |
295 | } |
296 | ||
2bd0ea18 NS |
297 | ip->i_d.di_size = 0; |
298 | ip->i_d.di_nextents = 0; | |
299 | ASSERT(ip->i_d.di_nblocks == 0); | |
9f064b7e | 300 | ip->i_d.di_extsize = pip ? 0 : fsx->fsx_extsize; |
2bd0ea18 NS |
301 | ip->i_d.di_dmevmask = 0; |
302 | ip->i_d.di_dmstate = 0; | |
06b80354 | 303 | ip->i_d.di_flags = pip ? 0 : xfs_flags2diflags(ip, fsx->fsx_xflags); |
41ce5f36 DC |
304 | |
305 | if (ip->i_d.di_version == 3) { | |
306 | ASSERT(ip->i_d.di_ino == ino); | |
299c0388 | 307 | ASSERT(uuid_equal(&ip->i_d.di_uuid, &mp->m_sb.sb_meta_uuid)); |
9abcc5cd | 308 | VFS_I(ip)->i_version = 1; |
06b80354 DW |
309 | ip->i_d.di_flags2 = pip ? 0 : xfs_flags2diflags2(ip, |
310 | fsx->fsx_xflags); | |
14f8b681 DW |
311 | ip->i_d.di_crtime.t_sec = (int32_t)VFS_I(ip)->i_mtime.tv_sec; |
312 | ip->i_d.di_crtime.t_nsec = (int32_t)VFS_I(ip)->i_mtime.tv_nsec; | |
06b80354 | 313 | ip->i_d.di_cowextsize = pip ? 0 : fsx->fsx_cowextsize; |
41ce5f36 DC |
314 | } |
315 | ||
2bd0ea18 | 316 | flags = XFS_ILOG_CORE; |
322f2a29 SL |
317 | switch (mode & S_IFMT) { |
318 | case S_IFIFO: | |
63899e27 NS |
319 | case S_IFSOCK: |
320 | /* doesn't make sense to set an rdev for these */ | |
321 | rdev = 0; | |
90ec25ed | 322 | /* FALLTHROUGH */ |
322f2a29 SL |
323 | case S_IFCHR: |
324 | case S_IFBLK: | |
2bd0ea18 | 325 | ip->i_d.di_format = XFS_DINODE_FMT_DEV; |
2bd0ea18 | 326 | flags |= XFS_ILOG_DEV; |
551174eb | 327 | VFS_I(ip)->i_rdev = rdev; |
2bd0ea18 | 328 | break; |
322f2a29 SL |
329 | case S_IFREG: |
330 | case S_IFDIR: | |
9f064b7e NS |
331 | if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) { |
332 | uint di_flags = 0; | |
333 | ||
334 | if ((mode & S_IFMT) == S_IFDIR) { | |
335 | if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) | |
336 | di_flags |= XFS_DIFLAG_RTINHERIT; | |
337 | if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) { | |
338 | di_flags |= XFS_DIFLAG_EXTSZINHERIT; | |
339 | ip->i_d.di_extsize = pip->i_d.di_extsize; | |
340 | } | |
341 | } else { | |
342 | if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) { | |
343 | di_flags |= XFS_DIFLAG_REALTIME; | |
344 | } | |
345 | if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) { | |
346 | di_flags |= XFS_DIFLAG_EXTSIZE; | |
347 | ip->i_d.di_extsize = pip->i_d.di_extsize; | |
348 | } | |
349 | } | |
350 | if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) | |
351 | di_flags |= XFS_DIFLAG_PROJINHERIT; | |
352 | ip->i_d.di_flags |= di_flags; | |
353 | } | |
354 | /* FALLTHROUGH */ | |
322f2a29 | 355 | case S_IFLNK: |
2bd0ea18 NS |
356 | ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS; |
357 | ip->i_df.if_flags = XFS_IFEXTENTS; | |
9685aa3e | 358 | ip->i_df.if_bytes = 0; |
b37d753d | 359 | ip->i_df.if_u1.if_root = NULL; |
2bd0ea18 NS |
360 | break; |
361 | default: | |
362 | ASSERT(0); | |
363 | } | |
364 | /* Attribute fork settings for new inode. */ | |
365 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; | |
366 | ip->i_d.di_anextents = 0; | |
367 | ||
ff105f75 DC |
368 | /* |
369 | * set up the inode ops structure that the libxfs code relies on | |
370 | */ | |
e37bf53c | 371 | if (XFS_ISDIR(ip)) |
ff105f75 DC |
372 | ip->d_ops = ip->i_mount->m_dir_inode_ops; |
373 | else | |
374 | ip->d_ops = ip->i_mount->m_nondir_inode_ops; | |
375 | ||
2bd0ea18 NS |
376 | /* |
377 | * Log the new values stuffed into the inode. | |
378 | */ | |
379 | xfs_trans_log_inode(tp, ip, flags); | |
380 | *ipp = ip; | |
381 | return 0; | |
382 | } | |
383 | ||
2bd0ea18 NS |
384 | /* |
385 | * Writes a modified inode's changes out to the inode's on disk home. | |
386 | * Originally based on xfs_iflush_int() from xfs_inode.c in the kernel. | |
387 | */ | |
388 | int | |
389 | libxfs_iflush_int(xfs_inode_t *ip, xfs_buf_t *bp) | |
390 | { | |
391 | xfs_inode_log_item_t *iip; | |
392 | xfs_dinode_t *dip; | |
393 | xfs_mount_t *mp; | |
394 | ||
37d086ca | 395 | ASSERT(bp-b_log_item != NULL); |
2bd0ea18 NS |
396 | ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE || |
397 | ip->i_d.di_nextents > ip->i_df.if_ext_max); | |
ff105f75 | 398 | ASSERT(ip->i_d.di_version > 1); |
2bd0ea18 NS |
399 | |
400 | iip = ip->i_itemp; | |
401 | mp = ip->i_mount; | |
402 | ||
403 | /* set *dip = inode's place in the buffer */ | |
92acb899 | 404 | dip = xfs_buf_offset(bp, ip->i_imap.im_boffset); |
2bd0ea18 | 405 | |
2bd0ea18 | 406 | ASSERT(ip->i_d.di_magic == XFS_DINODE_MAGIC); |
e37bf53c | 407 | if (XFS_ISREG(ip)) { |
2bd0ea18 NS |
408 | ASSERT( (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS) || |
409 | (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) ); | |
ff0f39ea | 410 | } else if (XFS_ISDIR(ip)) { |
2bd0ea18 NS |
411 | ASSERT( (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS) || |
412 | (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) || | |
413 | (ip->i_d.di_format == XFS_DINODE_FMT_LOCAL) ); | |
414 | } | |
415 | ASSERT(ip->i_d.di_nextents+ip->i_d.di_anextents <= ip->i_d.di_nblocks); | |
416 | ASSERT(ip->i_d.di_forkoff <= mp->m_sb.sb_inodesize); | |
2bd0ea18 | 417 | |
41ce5f36 DC |
418 | /* bump the change count on v3 inodes */ |
419 | if (ip->i_d.di_version == 3) | |
9abcc5cd | 420 | VFS_I(ip)->i_version++; |
41ce5f36 | 421 | |
20e882d4 | 422 | /* Check the inline fork data before we write out. */ |
12ac6e04 | 423 | if (!libxfs_inode_verify_forks(ip, &xfs_default_ifork_ops)) |
d15188a1 DW |
424 | return -EFSCORRUPTED; |
425 | ||
2bd0ea18 NS |
426 | /* |
427 | * Copy the dirty parts of the inode into the on-disk | |
428 | * inode. We always copy out the core of the inode, | |
429 | * because if the inode is dirty at all the core must | |
430 | * be. | |
431 | */ | |
db17aebe | 432 | xfs_inode_to_disk(ip, dip, iip->ili_item.li_lsn); |
46eca962 | 433 | |
ff105f75 | 434 | xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK); |
f8149110 | 435 | if (XFS_IFORK_Q(ip)) |
ff105f75 | 436 | xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK); |
2bd0ea18 | 437 | |
41ce5f36 DC |
438 | /* generate the checksum. */ |
439 | xfs_dinode_calc_crc(mp, dip); | |
440 | ||
2bd0ea18 NS |
441 | return 0; |
442 | } | |
443 | ||
2bd0ea18 | 444 | int |
5e656dbb | 445 | libxfs_mod_incore_sb( |
19ebedcf DC |
446 | struct xfs_mount *mp, |
447 | int field, | |
5e656dbb BN |
448 | int64_t delta, |
449 | int rsvd) | |
2bd0ea18 NS |
450 | { |
451 | long long lcounter; /* long counter for 64 bit fields */ | |
452 | ||
453 | switch (field) { | |
19ebedcf | 454 | case XFS_TRANS_SB_FDBLOCKS: |
2bd0ea18 NS |
455 | lcounter = (long long)mp->m_sb.sb_fdblocks; |
456 | lcounter += delta; | |
457 | if (lcounter < 0) | |
12b53197 | 458 | return -ENOSPC; |
2bd0ea18 | 459 | mp->m_sb.sb_fdblocks = lcounter; |
5e656dbb | 460 | return 0; |
2bd0ea18 NS |
461 | default: |
462 | ASSERT(0); | |
12b53197 | 463 | return -EINVAL; |
2bd0ea18 | 464 | } |
2bd0ea18 NS |
465 | } |
466 | ||
2bd0ea18 NS |
467 | /* |
468 | * This routine allocates disk space for the given file. | |
469 | * Originally derived from xfs_alloc_file_space(). | |
470 | */ | |
471 | int | |
472 | libxfs_alloc_file_space( | |
473 | xfs_inode_t *ip, | |
474 | xfs_off_t offset, | |
475 | xfs_off_t len, | |
476 | int alloc_type, | |
477 | int attr_flags) | |
478 | { | |
479 | xfs_mount_t *mp; | |
480 | xfs_off_t count; | |
481 | xfs_filblks_t datablocks; | |
482 | xfs_filblks_t allocated_fsb; | |
483 | xfs_filblks_t allocatesize_fsb; | |
5000d01d SL |
484 | xfs_bmbt_irec_t *imapp; |
485 | xfs_bmbt_irec_t imaps[1]; | |
2bd0ea18 NS |
486 | int reccount; |
487 | uint resblks; | |
488 | xfs_fileoff_t startoffset_fsb; | |
489 | xfs_trans_t *tp; | |
490 | int xfs_bmapi_flags; | |
2bd0ea18 NS |
491 | int error; |
492 | ||
493 | if (len <= 0) | |
12b53197 | 494 | return -EINVAL; |
2bd0ea18 NS |
495 | |
496 | count = len; | |
497 | error = 0; | |
498 | imapp = &imaps[0]; | |
499 | reccount = 1; | |
a2ceac1f | 500 | xfs_bmapi_flags = alloc_type ? XFS_BMAPI_PREALLOC : 0; |
2bd0ea18 NS |
501 | mp = ip->i_mount; |
502 | startoffset_fsb = XFS_B_TO_FSBT(mp, offset); | |
503 | allocatesize_fsb = XFS_B_TO_FSB(mp, count); | |
504 | ||
505 | /* allocate file space until done or until there is an error */ | |
506 | while (allocatesize_fsb && !error) { | |
507 | datablocks = allocatesize_fsb; | |
508 | ||
2bd0ea18 | 509 | resblks = (uint)XFS_DIOSTRAT_SPACE_RES(mp, datablocks); |
9074815c CH |
510 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, |
511 | 0, 0, &tp); | |
682fa9e0 ES |
512 | /* |
513 | * Check for running out of space | |
514 | */ | |
515 | if (error) { | |
12b53197 | 516 | ASSERT(error == -ENOSPC); |
2bd0ea18 | 517 | break; |
682fa9e0 | 518 | } |
2bd0ea18 | 519 | xfs_trans_ijoin(tp, ip, 0); |
2bd0ea18 | 520 | |
a2ceac1f | 521 | error = xfs_bmapi_write(tp, ip, startoffset_fsb, allocatesize_fsb, |
b39f9fcb | 522 | xfs_bmapi_flags, 0, imapp, &reccount); |
56b2de80 | 523 | |
2bd0ea18 | 524 | if (error) |
682fa9e0 | 525 | goto error0; |
2bd0ea18 | 526 | |
5c33baee CH |
527 | /* |
528 | * Complete the transaction | |
529 | */ | |
de5a3f46 | 530 | error = xfs_trans_commit(tp); |
2bd0ea18 NS |
531 | if (error) |
532 | break; | |
533 | ||
534 | allocated_fsb = imapp->br_blockcount; | |
535 | if (reccount == 0) | |
12b53197 | 536 | return -ENOSPC; |
2bd0ea18 NS |
537 | |
538 | startoffset_fsb += allocated_fsb; | |
539 | allocatesize_fsb -= allocated_fsb; | |
540 | } | |
541 | return error; | |
682fa9e0 ES |
542 | |
543 | error0: /* Cancel bmap, cancel trans */ | |
3d7434fe | 544 | xfs_trans_cancel(tp); |
682fa9e0 | 545 | return error; |
2bd0ea18 NS |
546 | } |
547 | ||
9f064b7e NS |
548 | /* |
549 | * Wrapper around call to libxfs_ialloc. Takes care of committing and | |
550 | * allocating a new transaction as needed. | |
551 | * | |
552 | * Originally there were two copies of this code - one in mkfs, the | |
553 | * other in repair - now there is just the one. | |
554 | */ | |
555 | int | |
556 | libxfs_inode_alloc( | |
557 | xfs_trans_t **tp, | |
558 | xfs_inode_t *pip, | |
559 | mode_t mode, | |
560 | nlink_t nlink, | |
561 | xfs_dev_t rdev, | |
562 | struct cred *cr, | |
563 | struct fsxattr *fsx, | |
564 | xfs_inode_t **ipp) | |
565 | { | |
9f064b7e NS |
566 | xfs_buf_t *ialloc_context; |
567 | xfs_inode_t *ip; | |
9f064b7e NS |
568 | int error; |
569 | ||
9f064b7e NS |
570 | ialloc_context = (xfs_buf_t *)0; |
571 | error = libxfs_ialloc(*tp, pip, mode, nlink, rdev, cr, fsx, | |
c1a29e92 | 572 | &ialloc_context, &ip); |
a2ceac1f DC |
573 | if (error) { |
574 | *ipp = NULL; | |
9f064b7e | 575 | return error; |
a2ceac1f DC |
576 | } |
577 | if (!ialloc_context && !ip) { | |
578 | *ipp = NULL; | |
12b53197 | 579 | return -ENOSPC; |
a2ceac1f | 580 | } |
9f064b7e | 581 | |
a2ceac1f | 582 | if (ialloc_context) { |
48ea6cb9 | 583 | |
9f064b7e | 584 | xfs_trans_bhold(*tp, ialloc_context); |
d262295d | 585 | |
d67406c9 | 586 | error = xfs_trans_roll(tp); |
48ea6cb9 | 587 | if (error) { |
d262295d | 588 | fprintf(stderr, _("%s: cannot duplicate transaction: %s\n"), |
48ea6cb9 | 589 | progname, strerror(error)); |
9f064b7e NS |
590 | exit(1); |
591 | } | |
592 | xfs_trans_bjoin(*tp, ialloc_context); | |
593 | error = libxfs_ialloc(*tp, pip, mode, nlink, rdev, cr, | |
c1a29e92 | 594 | fsx, &ialloc_context, &ip); |
9f064b7e | 595 | if (!ip) |
12b53197 | 596 | error = -ENOSPC; |
9f064b7e NS |
597 | if (error) |
598 | return error; | |
599 | } | |
9f064b7e NS |
600 | |
601 | *ipp = ip; | |
602 | return error; | |
603 | } | |
1552a820 NS |
604 | |
605 | /* | |
606 | * Userspace versions of common diagnostic routines (varargs fun). | |
607 | */ | |
608 | void | |
5e656dbb | 609 | libxfs_fs_repair_cmn_err(int level, xfs_mount_t *mp, char *fmt, ...) |
1552a820 NS |
610 | { |
611 | va_list ap; | |
612 | ||
613 | va_start(ap, fmt); | |
614 | vfprintf(stderr, fmt, ap); | |
615 | fprintf(stderr, " This is a bug.\n"); | |
89c4bb8e | 616 | fprintf(stderr, "%s version %s\n", progname, VERSION); |
130093ab DC |
617 | fprintf(stderr, |
618 | "Please capture the filesystem metadata with xfs_metadump and\n" | |
619 | "report it to linux-xfs@vger.kernel.org\n"); | |
1552a820 NS |
620 | va_end(ap); |
621 | } | |
622 | ||
623 | void | |
5e656dbb | 624 | libxfs_fs_cmn_err(int level, xfs_mount_t *mp, char *fmt, ...) |
1552a820 NS |
625 | { |
626 | va_list ap; | |
627 | ||
628 | va_start(ap, fmt); | |
629 | vfprintf(stderr, fmt, ap); | |
630 | fputs("\n", stderr); | |
631 | va_end(ap); | |
632 | } | |
633 | ||
634 | void | |
635 | cmn_err(int level, char *fmt, ...) | |
636 | { | |
637 | va_list ap; | |
638 | ||
639 | va_start(ap, fmt); | |
640 | vfprintf(stderr, fmt, ap); | |
641 | fputs("\n", stderr); | |
642 | va_end(ap); | |
643 | } | |
99c1ec96 DC |
644 | |
645 | /* | |
646 | * Warnings specifically for verifier errors. Differentiate CRC vs. invalid | |
647 | * values, and omit the stack trace unless the error level is tuned high. | |
648 | */ | |
649 | void | |
650 | xfs_verifier_error( | |
7e6c95f1 | 651 | struct xfs_buf *bp, |
1e697959 DW |
652 | int error, |
653 | xfs_failaddr_t failaddr) | |
99c1ec96 | 654 | { |
7e6c95f1 DW |
655 | xfs_buf_ioerror(bp, error); |
656 | ||
1e697959 | 657 | xfs_alert(NULL, "Metadata %s detected at %p, %s block 0x%llx/0x%x", |
12b53197 | 658 | bp->b_error == -EFSBADCRC ? "CRC error" : "corruption", |
1e697959 | 659 | failaddr ? failaddr : __return_address, |
a3fac935 | 660 | bp->b_ops->name, bp->b_bn, BBTOB(bp->b_length)); |
99c1ec96 | 661 | } |
a65d8d29 | 662 | |
1d3bac1f DW |
663 | /* |
664 | * Warnings for inode corruption problems. Don't bother with the stack | |
665 | * trace unless the error level is turned up high. | |
666 | */ | |
667 | void | |
668 | xfs_inode_verifier_error( | |
669 | struct xfs_inode *ip, | |
670 | int error, | |
671 | const char *name, | |
672 | void *buf, | |
673 | size_t bufsz, | |
674 | xfs_failaddr_t failaddr) | |
675 | { | |
676 | xfs_alert(NULL, "Metadata %s detected at %p, inode 0x%llx %s", | |
677 | error == -EFSBADCRC ? "CRC error" : "corruption", | |
678 | failaddr ? failaddr : __return_address, | |
679 | ip->i_ino, name); | |
680 | } | |
681 | ||
7d77349c BF |
682 | /* |
683 | * This is called from I/O verifiers on v5 superblock filesystems. In the | |
684 | * kernel, it validates the metadata LSN parameter against the current LSN of | |
685 | * the active log. We don't have an active log in userspace so this kind of | |
686 | * validation is not required. Therefore, this function always returns true in | |
687 | * userspace. | |
688 | * | |
689 | * xfs_repair piggybacks off this mechanism to help track the largest metadata | |
690 | * LSN in use on a filesystem. Keep a record of the largest LSN seen such that | |
691 | * repair can validate it against the state of the log. | |
692 | */ | |
693 | xfs_lsn_t libxfs_max_lsn = 0; | |
00ff2b10 | 694 | static pthread_mutex_t libxfs_max_lsn_lock = PTHREAD_MUTEX_INITIALIZER; |
7d77349c | 695 | |
a65d8d29 BF |
696 | bool |
697 | xfs_log_check_lsn( | |
698 | struct xfs_mount *mp, | |
699 | xfs_lsn_t lsn) | |
700 | { | |
7d77349c BF |
701 | int cycle = CYCLE_LSN(lsn); |
702 | int block = BLOCK_LSN(lsn); | |
703 | int max_cycle; | |
704 | int max_block; | |
705 | ||
706 | if (lsn == NULLCOMMITLSN) | |
707 | return true; | |
708 | ||
709 | pthread_mutex_lock(&libxfs_max_lsn_lock); | |
710 | ||
711 | max_cycle = CYCLE_LSN(libxfs_max_lsn); | |
712 | max_block = BLOCK_LSN(libxfs_max_lsn); | |
713 | ||
714 | if ((cycle > max_cycle) || | |
715 | (cycle == max_cycle && block > max_block)) | |
716 | libxfs_max_lsn = lsn; | |
717 | ||
718 | pthread_mutex_unlock(&libxfs_max_lsn_lock); | |
719 | ||
a65d8d29 BF |
720 | return true; |
721 | } | |
9542ae13 DC |
722 | |
723 | static struct xfs_buftarg * | |
724 | xfs_find_bdev_for_inode( | |
725 | struct xfs_inode *ip) | |
726 | { | |
727 | struct xfs_mount *mp = ip->i_mount; | |
728 | ||
729 | if (XFS_IS_REALTIME_INODE(ip)) | |
730 | return mp->m_rtdev_targp; | |
731 | return mp->m_ddev_targp; | |
732 | } | |
733 | ||
734 | static xfs_daddr_t | |
735 | xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb) | |
736 | { | |
737 | if (XFS_IS_REALTIME_INODE(ip)) | |
738 | return XFS_FSB_TO_BB(ip->i_mount, fsb); | |
739 | return XFS_FSB_TO_DADDR(ip->i_mount, (fsb)); | |
740 | } | |
741 | ||
742 | int | |
743 | libxfs_zero_extent( | |
744 | struct xfs_inode *ip, | |
745 | xfs_fsblock_t start_fsb, | |
746 | xfs_off_t count_fsb) | |
747 | { | |
748 | xfs_daddr_t sector = xfs_fsb_to_db(ip, start_fsb); | |
749 | ssize_t size = XFS_FSB_TO_BB(ip->i_mount, count_fsb); | |
750 | ||
751 | return libxfs_device_zero(xfs_find_bdev_for_inode(ip), sector, size); | |
752 | } | |
0b90dda6 | 753 | |
c7a710b7 DW |
754 | unsigned int |
755 | hweight8(unsigned int w) | |
756 | { | |
757 | unsigned int res = w - ((w >> 1) & 0x55); | |
758 | res = (res & 0x33) + ((res >> 2) & 0x33); | |
759 | return (res + (res >> 4)) & 0x0F; | |
760 | } | |
ec291989 DC |
761 | |
762 | unsigned int | |
763 | hweight32(unsigned int w) | |
764 | { | |
765 | unsigned int res = w - ((w >> 1) & 0x55555555); | |
766 | res = (res & 0x33333333) + ((res >> 2) & 0x33333333); | |
767 | res = (res + (res >> 4)) & 0x0F0F0F0F; | |
768 | res = res + (res >> 8); | |
769 | return (res + (res >> 16)) & 0x000000FF; | |
770 | } | |
771 | ||
772 | unsigned int | |
773 | hweight64(__u64 w) | |
774 | { | |
775 | return hweight32((unsigned int)w) + | |
776 | hweight32((unsigned int)(w >> 32)); | |
777 | } | |
778 |