]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/util.c
xfs: use a b+tree for the in-core extent list
[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 172 if (flags & XFS_ICHGTIME_CREATE) {
14f8b681
DW
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;
41ce5f36 175 }
2bd0ea18
NS
176}
177
06b80354
DW
178STATIC uint16_t
179xfs_flags2diflags(
180 struct xfs_inode *ip,
181 unsigned int xflags)
182{
183 /* can't set PREALLOC this way, just preserve it */
184 uint16_t di_flags =
185 (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
186
187 if (xflags & FS_XFLAG_IMMUTABLE)
188 di_flags |= XFS_DIFLAG_IMMUTABLE;
189 if (xflags & FS_XFLAG_APPEND)
190 di_flags |= XFS_DIFLAG_APPEND;
191 if (xflags & FS_XFLAG_SYNC)
192 di_flags |= XFS_DIFLAG_SYNC;
193 if (xflags & FS_XFLAG_NOATIME)
194 di_flags |= XFS_DIFLAG_NOATIME;
195 if (xflags & FS_XFLAG_NODUMP)
196 di_flags |= XFS_DIFLAG_NODUMP;
197 if (xflags & FS_XFLAG_NODEFRAG)
198 di_flags |= XFS_DIFLAG_NODEFRAG;
199 if (xflags & FS_XFLAG_FILESTREAM)
200 di_flags |= XFS_DIFLAG_FILESTREAM;
201 if (S_ISDIR(VFS_I(ip)->i_mode)) {
202 if (xflags & FS_XFLAG_RTINHERIT)
203 di_flags |= XFS_DIFLAG_RTINHERIT;
204 if (xflags & FS_XFLAG_NOSYMLINKS)
205 di_flags |= XFS_DIFLAG_NOSYMLINKS;
206 if (xflags & FS_XFLAG_EXTSZINHERIT)
207 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
208 if (xflags & FS_XFLAG_PROJINHERIT)
209 di_flags |= XFS_DIFLAG_PROJINHERIT;
210 } else if (S_ISREG(VFS_I(ip)->i_mode)) {
211 if (xflags & FS_XFLAG_REALTIME)
212 di_flags |= XFS_DIFLAG_REALTIME;
213 if (xflags & FS_XFLAG_EXTSIZE)
214 di_flags |= XFS_DIFLAG_EXTSIZE;
215 }
216
217 return di_flags;
218}
219
220STATIC uint64_t
221xfs_flags2diflags2(
222 struct xfs_inode *ip,
223 unsigned int xflags)
224{
225 uint64_t di_flags2 =
226 (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK);
227
228 if (xflags & FS_XFLAG_DAX)
229 di_flags2 |= XFS_DIFLAG2_DAX;
230 if (xflags & FS_XFLAG_COWEXTSIZE)
231 di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
232
233 return di_flags2;
234}
235
2bd0ea18 236/*
ff1f79a7 237 * Allocate an inode on disk and return a copy of its in-core version.
2bd0ea18
NS
238 * Set mode, nlink, and rdev appropriately within the inode.
239 * The uid and gid for the inode are set according to the contents of
240 * the given cred structure.
241 *
242 * This was once shared with the kernel, but has diverged to the point
ff1f79a7 243 * where it's no longer worth the hassle of maintaining common code.
2bd0ea18 244 */
5e656dbb 245int
2bd0ea18
NS
246libxfs_ialloc(
247 xfs_trans_t *tp,
248 xfs_inode_t *pip,
249 mode_t mode,
250 nlink_t nlink,
63899e27 251 xfs_dev_t rdev,
9f064b7e
NS
252 struct cred *cr,
253 struct fsxattr *fsx,
2bd0ea18
NS
254 int okalloc,
255 xfs_buf_t **ialloc_context,
2bd0ea18
NS
256 xfs_inode_t **ipp)
257{
258 xfs_ino_t ino;
259 xfs_inode_t *ip;
260 uint flags;
261 int error;
262
263 /*
264 * Call the space management code to pick
265 * the on-disk inode to be allocated.
266 */
267 error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
a2ceac1f 268 ialloc_context, &ino);
2bd0ea18
NS
269 if (error != 0)
270 return error;
a2ceac1f 271 if (*ialloc_context || ino == NULLFSINO) {
2bd0ea18
NS
272 *ipp = NULL;
273 return 0;
274 }
275 ASSERT(*ialloc_context == NULL);
276
46eca962 277 error = xfs_trans_iget(tp->t_mountp, tp, ino, 0, 0, &ip);
2bd0ea18
NS
278 if (error != 0)
279 return error;
280 ASSERT(ip != NULL);
281
e37bf53c 282 VFS_I(ip)->i_mode = mode;
bcbe04c1 283 set_nlink(VFS_I(ip), nlink);
2bd0ea18
NS
284 ip->i_d.di_uid = cr->cr_uid;
285 ip->i_d.di_gid = cr->cr_gid;
22bc10ed 286 xfs_set_projid(&ip->i_d, pip ? 0 : fsx->fsx_projid);
41ce5f36 287 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG | XFS_ICHGTIME_MOD);
2bd0ea18
NS
288
289 /*
ff105f75
DC
290 * We only support filesystems that understand v2 format inodes. So if
291 * this is currently an old format inode, then change the inode version
292 * number now. This way we only do the conversion here rather than here
293 * and in the flush/logging code.
2bd0ea18 294 */
ff105f75 295 if (ip->i_d.di_version == 1) {
56b2de80 296 ip->i_d.di_version = 2;
22bc10ed
AM
297 /*
298 * old link count, projid_lo/hi field, pad field
299 * already zeroed
300 */
5000d01d 301 }
2bd0ea18 302
e37bf53c 303 if (pip && (VFS_I(pip)->i_mode & S_ISGID)) {
9f064b7e 304 ip->i_d.di_gid = pip->i_d.di_gid;
e37bf53c
DC
305 if ((VFS_I(pip)->i_mode & S_ISGID) && (mode & S_IFMT) == S_IFDIR)
306 VFS_I(ip)->i_mode |= S_ISGID;
9f064b7e
NS
307 }
308
2bd0ea18
NS
309 ip->i_d.di_size = 0;
310 ip->i_d.di_nextents = 0;
311 ASSERT(ip->i_d.di_nblocks == 0);
9f064b7e 312 ip->i_d.di_extsize = pip ? 0 : fsx->fsx_extsize;
2bd0ea18
NS
313 ip->i_d.di_dmevmask = 0;
314 ip->i_d.di_dmstate = 0;
06b80354 315 ip->i_d.di_flags = pip ? 0 : xfs_flags2diflags(ip, fsx->fsx_xflags);
41ce5f36
DC
316
317 if (ip->i_d.di_version == 3) {
318 ASSERT(ip->i_d.di_ino == ino);
299c0388 319 ASSERT(uuid_equal(&ip->i_d.di_uuid, &mp->m_sb.sb_meta_uuid));
9abcc5cd 320 VFS_I(ip)->i_version = 1;
06b80354
DW
321 ip->i_d.di_flags2 = pip ? 0 : xfs_flags2diflags2(ip,
322 fsx->fsx_xflags);
14f8b681
DW
323 ip->i_d.di_crtime.t_sec = (int32_t)VFS_I(ip)->i_mtime.tv_sec;
324 ip->i_d.di_crtime.t_nsec = (int32_t)VFS_I(ip)->i_mtime.tv_nsec;
06b80354 325 ip->i_d.di_cowextsize = pip ? 0 : fsx->fsx_cowextsize;
41ce5f36
DC
326 }
327
2bd0ea18 328 flags = XFS_ILOG_CORE;
322f2a29
SL
329 switch (mode & S_IFMT) {
330 case S_IFIFO:
63899e27
NS
331 case S_IFSOCK:
332 /* doesn't make sense to set an rdev for these */
333 rdev = 0;
90ec25ed 334 /* FALLTHROUGH */
322f2a29
SL
335 case S_IFCHR:
336 case S_IFBLK:
2bd0ea18 337 ip->i_d.di_format = XFS_DINODE_FMT_DEV;
2bd0ea18 338 flags |= XFS_ILOG_DEV;
551174eb 339 VFS_I(ip)->i_rdev = rdev;
2bd0ea18 340 break;
322f2a29
SL
341 case S_IFREG:
342 case S_IFDIR:
9f064b7e
NS
343 if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
344 uint di_flags = 0;
345
346 if ((mode & S_IFMT) == S_IFDIR) {
347 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
348 di_flags |= XFS_DIFLAG_RTINHERIT;
349 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
350 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
351 ip->i_d.di_extsize = pip->i_d.di_extsize;
352 }
353 } else {
354 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) {
355 di_flags |= XFS_DIFLAG_REALTIME;
356 }
357 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
358 di_flags |= XFS_DIFLAG_EXTSIZE;
359 ip->i_d.di_extsize = pip->i_d.di_extsize;
360 }
361 }
362 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
363 di_flags |= XFS_DIFLAG_PROJINHERIT;
364 ip->i_d.di_flags |= di_flags;
365 }
366 /* FALLTHROUGH */
322f2a29 367 case S_IFLNK:
2bd0ea18
NS
368 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
369 ip->i_df.if_flags = XFS_IFEXTENTS;
370 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
b37d753d 371 ip->i_df.if_u1.if_root = NULL;
2bd0ea18
NS
372 break;
373 default:
374 ASSERT(0);
375 }
376 /* Attribute fork settings for new inode. */
377 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
378 ip->i_d.di_anextents = 0;
379
ff105f75
DC
380 /*
381 * set up the inode ops structure that the libxfs code relies on
382 */
e37bf53c 383 if (XFS_ISDIR(ip))
ff105f75
DC
384 ip->d_ops = ip->i_mount->m_dir_inode_ops;
385 else
386 ip->d_ops = ip->i_mount->m_nondir_inode_ops;
387
2bd0ea18
NS
388 /*
389 * Log the new values stuffed into the inode.
390 */
391 xfs_trans_log_inode(tp, ip, flags);
392 *ipp = ip;
393 return 0;
394}
395
396void
5e656dbb
BN
397libxfs_iprint(
398 xfs_inode_t *ip)
2bd0ea18 399{
138659f1 400 struct xfs_icdinode *dip;
b37d753d
CH
401 xfs_extnum_t i = 0;
402 xfs_ifork_t *ifp; /* inode fork pointer */
403 struct xfs_iext_cursor icur;
404 xfs_bmbt_irec_t rec;
2bd0ea18 405
5b64e00a 406 printf("Inode %lx\n", (unsigned long)ip);
5b64e00a 407 printf(" i_ino %llx\n", (unsigned long long)ip->i_ino);
2bd0ea18
NS
408
409 if (ip->i_df.if_flags & XFS_IFEXTENTS)
410 printf("EXTENTS ");
411 printf("\n");
412 printf(" i_df.if_bytes %d\n", ip->i_df.if_bytes);
b37d753d
CH
413 printf(" i_df.if_u1.if_root/if_data %lx\n",
414 (unsigned long)ip->i_df.if_u1.if_root);
2bd0ea18 415 if (ip->i_df.if_flags & XFS_IFEXTENTS) {
b37d753d
CH
416 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
417 for_each_xfs_iext(ifp, &icur, &rec) {
5b64e00a
NS
418 printf("\t%d: startoff %llu, startblock 0x%llx,"
419 " blockcount %llu, state %d\n",
420 i, (unsigned long long)rec.br_startoff,
421 (unsigned long long)rec.br_startblock,
422 (unsigned long long)rec.br_blockcount,
2bd0ea18 423 (int)rec.br_state);
b37d753d 424 i++;
2bd0ea18
NS
425 }
426 }
5b64e00a 427 printf(" i_df.if_broot %lx\n", (unsigned long)ip->i_df.if_broot);
2bd0ea18
NS
428 printf(" i_df.if_broot_bytes %x\n", ip->i_df.if_broot_bytes);
429
5e656dbb 430 dip = &ip->i_d;
2bd0ea18 431 printf("\nOn disk portion\n");
e37bf53c 432 printf(" di_mode %o\n", VFS_I(ip)->i_mode);
2bd0ea18
NS
433 printf(" di_version %x\n", (uint)dip->di_version);
434 switch (ip->i_d.di_format) {
435 case XFS_DINODE_FMT_LOCAL:
436 printf(" Inline inode\n");
437 break;
438 case XFS_DINODE_FMT_EXTENTS:
439 printf(" Extents inode\n");
440 break;
441 case XFS_DINODE_FMT_BTREE:
442 printf(" B-tree inode\n");
443 break;
444 default:
445 printf(" Other inode\n");
446 break;
447 }
bcbe04c1 448 printf(" di_nlink %x\n", VFS_I(ip)->i_nlink);
2bd0ea18
NS
449 printf(" di_uid %d\n", dip->di_uid);
450 printf(" di_gid %d\n", dip->di_gid);
451 printf(" di_nextents %d\n", dip->di_nextents);
5b64e00a 452 printf(" di_size %llu\n", (unsigned long long)dip->di_size);
6652c253 453 printf(" di_gen %x\n", VFS_I(ip)->i_generation);
2bd0ea18
NS
454 printf(" di_extsize %d\n", dip->di_extsize);
455 printf(" di_flags %x\n", dip->di_flags);
5b64e00a 456 printf(" di_nblocks %llu\n", (unsigned long long)dip->di_nblocks);
2bd0ea18
NS
457}
458
459/*
460 * Writes a modified inode's changes out to the inode's on disk home.
461 * Originally based on xfs_iflush_int() from xfs_inode.c in the kernel.
462 */
463int
464libxfs_iflush_int(xfs_inode_t *ip, xfs_buf_t *bp)
465{
466 xfs_inode_log_item_t *iip;
467 xfs_dinode_t *dip;
468 xfs_mount_t *mp;
469
470 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
471 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
472 ip->i_d.di_nextents > ip->i_df.if_ext_max);
ff105f75 473 ASSERT(ip->i_d.di_version > 1);
2bd0ea18
NS
474
475 iip = ip->i_itemp;
476 mp = ip->i_mount;
477
478 /* set *dip = inode's place in the buffer */
92acb899 479 dip = xfs_buf_offset(bp, ip->i_imap.im_boffset);
2bd0ea18 480
2bd0ea18 481 ASSERT(ip->i_d.di_magic == XFS_DINODE_MAGIC);
e37bf53c 482 if (XFS_ISREG(ip)) {
2bd0ea18
NS
483 ASSERT( (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS) ||
484 (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) );
ff0f39ea 485 } else if (XFS_ISDIR(ip)) {
2bd0ea18
NS
486 ASSERT( (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS) ||
487 (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) ||
488 (ip->i_d.di_format == XFS_DINODE_FMT_LOCAL) );
489 }
490 ASSERT(ip->i_d.di_nextents+ip->i_d.di_anextents <= ip->i_d.di_nblocks);
491 ASSERT(ip->i_d.di_forkoff <= mp->m_sb.sb_inodesize);
2bd0ea18 492
41ce5f36
DC
493 /* bump the change count on v3 inodes */
494 if (ip->i_d.di_version == 3)
9abcc5cd 495 VFS_I(ip)->i_version++;
41ce5f36 496
d15188a1
DW
497 /* Check the inline directory data. */
498 if (S_ISDIR(VFS_I(ip)->i_mode) &&
499 ip->i_d.di_format == XFS_DINODE_FMT_LOCAL &&
500 xfs_dir2_sf_verify(ip))
501 return -EFSCORRUPTED;
502
2bd0ea18
NS
503 /*
504 * Copy the dirty parts of the inode into the on-disk
505 * inode. We always copy out the core of the inode,
506 * because if the inode is dirty at all the core must
507 * be.
508 */
db17aebe 509 xfs_inode_to_disk(ip, dip, iip->ili_item.li_lsn);
46eca962 510
ff105f75 511 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK);
f8149110 512 if (XFS_IFORK_Q(ip))
ff105f75 513 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK);
2bd0ea18 514
41ce5f36
DC
515 /* generate the checksum. */
516 xfs_dinode_calc_crc(mp, dip);
517
2bd0ea18
NS
518 return 0;
519}
520
2bd0ea18 521int
5e656dbb 522libxfs_mod_incore_sb(
19ebedcf
DC
523 struct xfs_mount *mp,
524 int field,
5e656dbb
BN
525 int64_t delta,
526 int rsvd)
2bd0ea18
NS
527{
528 long long lcounter; /* long counter for 64 bit fields */
529
530 switch (field) {
19ebedcf 531 case XFS_TRANS_SB_FDBLOCKS:
2bd0ea18
NS
532 lcounter = (long long)mp->m_sb.sb_fdblocks;
533 lcounter += delta;
534 if (lcounter < 0)
12b53197 535 return -ENOSPC;
2bd0ea18 536 mp->m_sb.sb_fdblocks = lcounter;
5e656dbb 537 return 0;
2bd0ea18
NS
538 default:
539 ASSERT(0);
12b53197 540 return -EINVAL;
2bd0ea18 541 }
2bd0ea18
NS
542}
543
2bd0ea18
NS
544/*
545 * This routine allocates disk space for the given file.
546 * Originally derived from xfs_alloc_file_space().
547 */
548int
549libxfs_alloc_file_space(
550 xfs_inode_t *ip,
551 xfs_off_t offset,
552 xfs_off_t len,
553 int alloc_type,
554 int attr_flags)
555{
556 xfs_mount_t *mp;
557 xfs_off_t count;
558 xfs_filblks_t datablocks;
559 xfs_filblks_t allocated_fsb;
560 xfs_filblks_t allocatesize_fsb;
561 xfs_fsblock_t firstfsb;
6f530e9a 562 struct xfs_defer_ops free_list;
5000d01d
SL
563 xfs_bmbt_irec_t *imapp;
564 xfs_bmbt_irec_t imaps[1];
2bd0ea18
NS
565 int reccount;
566 uint resblks;
567 xfs_fileoff_t startoffset_fsb;
568 xfs_trans_t *tp;
569 int xfs_bmapi_flags;
2bd0ea18
NS
570 int error;
571
572 if (len <= 0)
12b53197 573 return -EINVAL;
2bd0ea18
NS
574
575 count = len;
576 error = 0;
577 imapp = &imaps[0];
578 reccount = 1;
a2ceac1f 579 xfs_bmapi_flags = alloc_type ? XFS_BMAPI_PREALLOC : 0;
2bd0ea18
NS
580 mp = ip->i_mount;
581 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
582 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
583
584 /* allocate file space until done or until there is an error */
585 while (allocatesize_fsb && !error) {
586 datablocks = allocatesize_fsb;
587
2bd0ea18 588 resblks = (uint)XFS_DIOSTRAT_SPACE_RES(mp, datablocks);
9074815c
CH
589 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks,
590 0, 0, &tp);
682fa9e0
ES
591 /*
592 * Check for running out of space
593 */
594 if (error) {
12b53197 595 ASSERT(error == -ENOSPC);
2bd0ea18 596 break;
682fa9e0 597 }
2bd0ea18 598 xfs_trans_ijoin(tp, ip, 0);
2bd0ea18 599
6f530e9a 600 xfs_defer_init(&free_list, &firstfsb);
a2ceac1f 601 error = xfs_bmapi_write(tp, ip, startoffset_fsb, allocatesize_fsb,
2bd0ea18 602 xfs_bmapi_flags, &firstfsb, 0, imapp,
56b2de80
DC
603 &reccount, &free_list);
604
2bd0ea18 605 if (error)
682fa9e0 606 goto error0;
2bd0ea18 607
5c33baee
CH
608 /*
609 * Complete the transaction
610 */
611 error = xfs_defer_finish(&tp, &free_list);
2bd0ea18 612 if (error)
682fa9e0 613 goto error0;
2bd0ea18 614
de5a3f46 615 error = xfs_trans_commit(tp);
2bd0ea18
NS
616 if (error)
617 break;
618
619 allocated_fsb = imapp->br_blockcount;
620 if (reccount == 0)
12b53197 621 return -ENOSPC;
2bd0ea18
NS
622
623 startoffset_fsb += allocated_fsb;
624 allocatesize_fsb -= allocated_fsb;
625 }
626 return error;
682fa9e0
ES
627
628error0: /* Cancel bmap, cancel trans */
6f530e9a 629 xfs_defer_cancel(&free_list);
3d7434fe 630 xfs_trans_cancel(tp);
682fa9e0 631 return error;
2bd0ea18
NS
632}
633
634unsigned int
635libxfs_log2_roundup(unsigned int i)
636{
637 unsigned int rval;
638
639 for (rval = 0; rval < NBBY * sizeof(i); rval++) {
640 if ((1 << rval) >= i)
641 break;
642 }
643 return rval;
644}
645
9f064b7e
NS
646/*
647 * Wrapper around call to libxfs_ialloc. Takes care of committing and
648 * allocating a new transaction as needed.
649 *
650 * Originally there were two copies of this code - one in mkfs, the
651 * other in repair - now there is just the one.
652 */
653int
654libxfs_inode_alloc(
655 xfs_trans_t **tp,
656 xfs_inode_t *pip,
657 mode_t mode,
658 nlink_t nlink,
659 xfs_dev_t rdev,
660 struct cred *cr,
661 struct fsxattr *fsx,
662 xfs_inode_t **ipp)
663{
9f064b7e
NS
664 xfs_buf_t *ialloc_context;
665 xfs_inode_t *ip;
9f064b7e
NS
666 int error;
667
9f064b7e
NS
668 ialloc_context = (xfs_buf_t *)0;
669 error = libxfs_ialloc(*tp, pip, mode, nlink, rdev, cr, fsx,
a2ceac1f
DC
670 1, &ialloc_context, &ip);
671 if (error) {
672 *ipp = NULL;
9f064b7e 673 return error;
a2ceac1f
DC
674 }
675 if (!ialloc_context && !ip) {
676 *ipp = NULL;
12b53197 677 return -ENOSPC;
a2ceac1f 678 }
9f064b7e 679
a2ceac1f 680 if (ialloc_context) {
48ea6cb9 681
9f064b7e 682 xfs_trans_bhold(*tp, ialloc_context);
d262295d 683
d67406c9 684 error = xfs_trans_roll(tp);
48ea6cb9 685 if (error) {
d262295d 686 fprintf(stderr, _("%s: cannot duplicate transaction: %s\n"),
48ea6cb9 687 progname, strerror(error));
9f064b7e
NS
688 exit(1);
689 }
690 xfs_trans_bjoin(*tp, ialloc_context);
691 error = libxfs_ialloc(*tp, pip, mode, nlink, rdev, cr,
a2ceac1f 692 fsx, 1, &ialloc_context, &ip);
9f064b7e 693 if (!ip)
12b53197 694 error = -ENOSPC;
9f064b7e
NS
695 if (error)
696 return error;
697 }
9f064b7e
NS
698
699 *ipp = ip;
700 return error;
701}
1552a820
NS
702
703/*
704 * Userspace versions of common diagnostic routines (varargs fun).
705 */
706void
5e656dbb 707libxfs_fs_repair_cmn_err(int level, xfs_mount_t *mp, char *fmt, ...)
1552a820
NS
708{
709 va_list ap;
710
711 va_start(ap, fmt);
712 vfprintf(stderr, fmt, ap);
713 fprintf(stderr, " This is a bug.\n");
89c4bb8e 714 fprintf(stderr, "%s version %s\n", progname, VERSION);
130093ab
DC
715 fprintf(stderr,
716 "Please capture the filesystem metadata with xfs_metadump and\n"
717 "report it to linux-xfs@vger.kernel.org\n");
1552a820
NS
718 va_end(ap);
719}
720
721void
5e656dbb 722libxfs_fs_cmn_err(int level, xfs_mount_t *mp, char *fmt, ...)
1552a820
NS
723{
724 va_list ap;
725
726 va_start(ap, fmt);
727 vfprintf(stderr, fmt, ap);
728 fputs("\n", stderr);
729 va_end(ap);
730}
731
732void
733cmn_err(int level, char *fmt, ...)
734{
735 va_list ap;
736
737 va_start(ap, fmt);
738 vfprintf(stderr, fmt, ap);
739 fputs("\n", stderr);
740 va_end(ap);
741}
99c1ec96
DC
742
743/*
744 * Warnings specifically for verifier errors. Differentiate CRC vs. invalid
745 * values, and omit the stack trace unless the error level is tuned high.
746 */
747void
748xfs_verifier_error(
749 struct xfs_buf *bp)
750{
a3fac935 751 xfs_alert(NULL, "Metadata %s detected at %s block 0x%llx/0x%x",
12b53197 752 bp->b_error == -EFSBADCRC ? "CRC error" : "corruption",
a3fac935 753 bp->b_ops->name, bp->b_bn, BBTOB(bp->b_length));
99c1ec96 754}
a65d8d29 755
7d77349c
BF
756/*
757 * This is called from I/O verifiers on v5 superblock filesystems. In the
758 * kernel, it validates the metadata LSN parameter against the current LSN of
759 * the active log. We don't have an active log in userspace so this kind of
760 * validation is not required. Therefore, this function always returns true in
761 * userspace.
762 *
763 * xfs_repair piggybacks off this mechanism to help track the largest metadata
764 * LSN in use on a filesystem. Keep a record of the largest LSN seen such that
765 * repair can validate it against the state of the log.
766 */
767xfs_lsn_t libxfs_max_lsn = 0;
768pthread_mutex_t libxfs_max_lsn_lock = PTHREAD_MUTEX_INITIALIZER;
769
a65d8d29
BF
770bool
771xfs_log_check_lsn(
772 struct xfs_mount *mp,
773 xfs_lsn_t lsn)
774{
7d77349c
BF
775 int cycle = CYCLE_LSN(lsn);
776 int block = BLOCK_LSN(lsn);
777 int max_cycle;
778 int max_block;
779
780 if (lsn == NULLCOMMITLSN)
781 return true;
782
783 pthread_mutex_lock(&libxfs_max_lsn_lock);
784
785 max_cycle = CYCLE_LSN(libxfs_max_lsn);
786 max_block = BLOCK_LSN(libxfs_max_lsn);
787
788 if ((cycle > max_cycle) ||
789 (cycle == max_cycle && block > max_block))
790 libxfs_max_lsn = lsn;
791
792 pthread_mutex_unlock(&libxfs_max_lsn_lock);
793
a65d8d29
BF
794 return true;
795}
9542ae13
DC
796
797static struct xfs_buftarg *
798xfs_find_bdev_for_inode(
799 struct xfs_inode *ip)
800{
801 struct xfs_mount *mp = ip->i_mount;
802
803 if (XFS_IS_REALTIME_INODE(ip))
804 return mp->m_rtdev_targp;
805 return mp->m_ddev_targp;
806}
807
808static xfs_daddr_t
809xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
810{
811 if (XFS_IS_REALTIME_INODE(ip))
812 return XFS_FSB_TO_BB(ip->i_mount, fsb);
813 return XFS_FSB_TO_DADDR(ip->i_mount, (fsb));
814}
815
816int
817libxfs_zero_extent(
818 struct xfs_inode *ip,
819 xfs_fsblock_t start_fsb,
820 xfs_off_t count_fsb)
821{
822 xfs_daddr_t sector = xfs_fsb_to_db(ip, start_fsb);
823 ssize_t size = XFS_FSB_TO_BB(ip->i_mount, count_fsb);
824
825 return libxfs_device_zero(xfs_find_bdev_for_inode(ip), sector, size);
826}
0b90dda6 827