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