]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - mkfs/proto.c
9ff855482e78d09a6fd71f1d5bc728fd602cc8de
2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 * Prototypes for internal functions.
26 static long getnum(char **pp
);
27 static char *getstr(char **pp
);
28 static void fail(char *msg
, int i
);
29 static void getres(xfs_trans_t
*tp
, uint blocks
);
30 static void rsvfile(xfs_mount_t
*mp
, xfs_inode_t
*ip
, long long len
);
31 static int newfile(xfs_trans_t
*tp
, xfs_inode_t
*ip
, xfs_bmap_free_t
*flist
,
32 xfs_fsblock_t
*first
, int dolocal
, int logit
, char *buf
, int len
);
33 static char *newregfile(char **pp
, int *len
);
34 static void rtinit(xfs_mount_t
*mp
);
35 static long filesize(int fd
);
38 * Use this for block reservations needed for mkfs's conditions
39 * (basically no fragmentation).
41 #define MKFS_BLOCKRES_INODE \
42 ((uint)(mp->m_ialloc_blks + (mp->m_in_maxlevels - 1)))
43 #define MKFS_BLOCKRES(rb) \
44 ((uint)(MKFS_BLOCKRES_INODE + XFS_DA_NODE_MAXDEPTH + \
45 (XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - 1) + (rb)))
53 static char dflt
[] = "d--755 0 0 $";
59 if ((fd
= open(fname
, O_RDONLY
)) < 0 || (size
= filesize(fd
)) < 0) {
60 fprintf(stderr
, _("%s: failed to open %s: %s\n"),
61 progname
, fname
, strerror(errno
));
65 buf
= malloc(size
+ 1);
66 if (read(fd
, buf
, size
) < size
) {
67 fprintf(stderr
, _("%s: read failed on %s: %s\n"),
68 progname
, fname
, strerror(errno
));
71 if (buf
[size
- 1] != '\n') {
72 fprintf(stderr
, _("%s: proto file %s premature EOF\n"),
78 * Skip past the stuff there for compatibility, a string and 2 numbers.
80 (void)getstr(&buf
); /* boot image name */
81 (void)getnum(&buf
); /* block count */
82 (void)getnum(&buf
); /* inode count */
108 fprintf(stderr
, "%s: %s [%d - %s]\n", progname
, msg
, i
, strerror(i
));
116 fail(_("cannot reserve space"), i
);
129 for (i
= 0, r
= MKFS_BLOCKRES(blocks
); r
>= blocks
; r
--) {
130 struct xfs_trans_res tres
= {0};
132 i
= -libxfs_trans_reserve(tp
, &tres
, r
, 0);
163 while (c
!= ' ' && c
!= '\t' && c
!= '\n' && c
!= '\0')
171 fprintf(stderr
, _("%s: premature EOF in prototype file\n"),
187 error
= -libxfs_alloc_file_space(ip
, 0, llen
, 1, 0);
190 fail(_("error reserving space for a file"), error
);
195 * update the inode timestamp, mode, and prealloc flag bits
197 tp
= libxfs_trans_alloc(mp
, 0);
199 libxfs_trans_ijoin(tp
, ip
, 0);
201 ip
->i_d
.di_mode
&= ~S_ISUID
;
204 * Note that we don't have to worry about mandatory
205 * file locking being disabled here because we only
206 * clear the S_ISGID bit if the Group execute bit is
207 * on, but if it was on then mandatory locking wouldn't
210 if (ip
->i_d
.di_mode
& S_IXGRP
)
211 ip
->i_d
.di_mode
&= ~S_ISGID
;
213 libxfs_trans_ichgtime(tp
, ip
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
215 ip
->i_d
.di_flags
|= XFS_DIFLAG_PREALLOC
;
217 libxfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
218 libxfs_trans_commit(tp
);
225 xfs_bmap_free_t
*flist
,
226 xfs_fsblock_t
*first
,
243 if (dolocal
&& len
<= XFS_IFORK_DSIZE(ip
)) {
244 libxfs_idata_realloc(ip
, len
, XFS_DATA_FORK
);
246 memmove(ip
->i_df
.if_u1
.if_data
, buf
, len
);
247 ip
->i_d
.di_size
= len
;
248 ip
->i_df
.if_flags
&= ~XFS_IFEXTENTS
;
249 ip
->i_df
.if_flags
|= XFS_IFINLINE
;
250 ip
->i_d
.di_format
= XFS_DINODE_FMT_LOCAL
;
251 flags
= XFS_ILOG_DDATA
;
252 } else if (len
> 0) {
253 nb
= XFS_B_TO_FSB(mp
, len
);
255 error
= -libxfs_bmapi_write(tp
, ip
, 0, nb
, 0, first
, nb
,
258 fail(_("error allocating space for a file"), error
);
262 _("%s: cannot allocate space for file\n"),
266 d
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
);
267 bp
= libxfs_trans_get_buf(logit
? tp
: 0, mp
->m_dev
, d
,
268 nb
<< mp
->m_blkbb_log
, 0);
269 memmove(XFS_BUF_PTR(bp
), buf
, len
);
270 if (len
< XFS_BUF_COUNT(bp
))
271 memset(XFS_BUF_PTR(bp
) + len
, 0, XFS_BUF_COUNT(bp
) - len
);
273 libxfs_trans_log_buf(tp
, bp
, 0, XFS_BUF_COUNT(bp
) - 1);
275 libxfs_writebuf(bp
, LIBXFS_EXIT_ON_FAILURE
);
277 ip
->i_d
.di_size
= len
;
292 if ((fd
= open(fname
, O_RDONLY
)) < 0 || (size
= filesize(fd
)) < 0) {
293 fprintf(stderr
, _("%s: cannot open %s: %s\n"),
294 progname
, fname
, strerror(errno
));
297 if ((*len
= (int)size
)) {
299 if (read(fd
, buf
, size
) < size
) {
300 fprintf(stderr
, _("%s: read failed on %s: %s\n"),
301 progname
, fname
, strerror(errno
));
315 struct xfs_name
*name
,
317 xfs_fsblock_t
*first
,
318 xfs_bmap_free_t
*flist
)
323 rsv
= XFS_DIRENTER_SPACE_RES(mp
, name
->len
);
325 error
= -libxfs_dir_createname(tp
, pip
, name
, inum
, first
, flist
, rsv
);
327 fail(_("directory createname error"), error
);
339 error
= -libxfs_dir_init(tp
, dp
, pdp
);
341 fail(_("directory create error"), error
);
348 struct fsxattr
*fsxp
,
353 #define IF_RESERVED 1
356 #define IF_DIRECTORY 4
364 xfs_bmap_free_t flist
;
379 struct xfs_name xname
;
381 memset(&creds
, 0, sizeof(creds
));
406 fprintf(stderr
, _("%s: bad format string %s\n"),
418 fprintf(stderr
, _("%s: bad format string %s\n"),
429 fprintf(stderr
, _("%s: bad format string %s\n"),
434 for (i
= 3; i
< 6; i
++) {
435 if (mstr
[i
] < '0' || mstr
[i
] > '7') {
436 fprintf(stderr
, _("%s: bad format string %s\n"),
440 val
= val
* 8 + mstr
[i
] - '0';
443 creds
.cr_uid
= (int)getnum(pp
);
444 creds
.cr_gid
= (int)getnum(pp
);
445 xname
.name
= (unsigned char *)name
;
446 xname
.len
= name
? strlen(name
) : 0;
448 tp
= libxfs_trans_alloc(mp
, 0);
449 flags
= XFS_ILOG_CORE
;
450 xfs_bmap_init(&flist
, &first
);
453 buf
= newregfile(pp
, &len
);
454 getres(tp
, XFS_B_TO_FSB(mp
, len
));
455 error
= -libxfs_inode_alloc(&tp
, pip
, mode
|S_IFREG
, 1, 0,
458 fail(_("Inode allocation failed"), error
);
459 flags
|= newfile(tp
, ip
, &flist
, &first
, 0, 0, buf
, len
);
462 libxfs_trans_ijoin(tp
, pip
, 0);
463 xname
.type
= XFS_DIR3_FT_REG_FILE
;
464 newdirent(mp
, tp
, pip
, &xname
, ip
->i_ino
, &first
, &flist
);
467 case IF_RESERVED
: /* pre-allocated space only */
469 llen
= cvtnum(mp
->m_sb
.sb_blocksize
, mp
->m_sb
.sb_sectsize
, value
);
470 getres(tp
, XFS_B_TO_FSB(mp
, llen
));
472 error
= -libxfs_inode_alloc(&tp
, pip
, mode
|S_IFREG
, 1, 0,
475 fail(_("Inode pre-allocation failed"), error
);
477 libxfs_trans_ijoin(tp
, pip
, 0);
479 xname
.type
= XFS_DIR3_FT_REG_FILE
;
480 newdirent(mp
, tp
, pip
, &xname
, ip
->i_ino
, &first
, &flist
);
481 libxfs_trans_log_inode(tp
, ip
, flags
);
483 error
= -libxfs_bmap_finish(&tp
, &flist
, ip
);
485 fail(_("Pre-allocated file creation failed"), error
);
486 libxfs_trans_commit(tp
);
487 rsvfile(mp
, ip
, llen
);
493 majdev
= (int)getnum(pp
);
494 mindev
= (int)getnum(pp
);
495 error
= -libxfs_inode_alloc(&tp
, pip
, mode
|S_IFBLK
, 1,
496 IRIX_MKDEV(majdev
, mindev
), &creds
, fsxp
, &ip
);
498 fail(_("Inode allocation failed"), error
);
500 libxfs_trans_ijoin(tp
, pip
, 0);
501 xname
.type
= XFS_DIR3_FT_BLKDEV
;
502 newdirent(mp
, tp
, pip
, &xname
, ip
->i_ino
, &first
, &flist
);
503 flags
|= XFS_ILOG_DEV
;
508 majdev
= (int)getnum(pp
);
509 mindev
= (int)getnum(pp
);
510 error
= -libxfs_inode_alloc(&tp
, pip
, mode
|S_IFCHR
, 1,
511 IRIX_MKDEV(majdev
, mindev
), &creds
, fsxp
, &ip
);
513 fail(_("Inode allocation failed"), error
);
514 libxfs_trans_ijoin(tp
, pip
, 0);
515 xname
.type
= XFS_DIR3_FT_CHRDEV
;
516 newdirent(mp
, tp
, pip
, &xname
, ip
->i_ino
, &first
, &flist
);
517 flags
|= XFS_ILOG_DEV
;
522 error
= -libxfs_inode_alloc(&tp
, pip
, mode
|S_IFIFO
, 1, 0,
525 fail(_("Inode allocation failed"), error
);
526 libxfs_trans_ijoin(tp
, pip
, 0);
527 xname
.type
= XFS_DIR3_FT_FIFO
;
528 newdirent(mp
, tp
, pip
, &xname
, ip
->i_ino
, &first
, &flist
);
532 len
= (int)strlen(buf
);
533 getres(tp
, XFS_B_TO_FSB(mp
, len
));
534 error
= -libxfs_inode_alloc(&tp
, pip
, mode
|S_IFLNK
, 1, 0,
537 fail(_("Inode allocation failed"), error
);
538 flags
|= newfile(tp
, ip
, &flist
, &first
, 1, 1, buf
, len
);
539 libxfs_trans_ijoin(tp
, pip
, 0);
540 xname
.type
= XFS_DIR3_FT_SYMLINK
;
541 newdirent(mp
, tp
, pip
, &xname
, ip
->i_ino
, &first
, &flist
);
545 error
= -libxfs_inode_alloc(&tp
, pip
, mode
|S_IFDIR
, 1, 0,
548 fail(_("Inode allocation failed"), error
);
549 inc_nlink(VFS_I(ip
)); /* account for . */
552 mp
->m_sb
.sb_rootino
= ip
->i_ino
;
556 libxfs_trans_ijoin(tp
, pip
, 0);
557 xname
.type
= XFS_DIR3_FT_DIR
;
558 newdirent(mp
, tp
, pip
, &xname
, ip
->i_ino
,
560 inc_nlink(VFS_I(pip
));
561 libxfs_trans_log_inode(tp
, pip
, XFS_ILOG_CORE
);
563 newdirectory(mp
, tp
, ip
, pip
);
564 libxfs_trans_log_inode(tp
, ip
, flags
);
565 error
= -libxfs_bmap_finish(&tp
, &flist
, ip
);
567 fail(_("Directory creation failed"), error
);
568 libxfs_trans_commit(tp
);
570 * RT initialization. Do this here to ensure that
571 * the RT inodes get placed after the root inode.
580 if (strcmp(name
, "$") == 0)
582 parseproto(mp
, ip
, fsxp
, pp
, name
);
588 fail(_("Unknown format"), EINVAL
);
590 libxfs_trans_log_inode(tp
, ip
, flags
);
591 error
= -libxfs_bmap_finish(&tp
, &flist
, ip
);
593 fail(_("Error encountered creating file from prototype file"),
596 libxfs_trans_commit(tp
);
606 parseproto(mp
, NULL
, fsx
, pp
, NULL
);
610 * Allocate the realtime bitmap and summary inodes, and fill in data if any.
621 xfs_bmap_free_t flist
;
623 xfs_bmbt_irec_t map
[XFS_BMAP_MAX_NMAP
];
624 xfs_extlen_t nsumblocks
;
630 struct fsxattr fsxattrs
;
631 struct xfs_trans_res tres
= {0};
634 * First, allocate the inodes.
636 tp
= libxfs_trans_alloc(mp
, 0);
637 i
= -libxfs_trans_reserve(tp
, &tres
, MKFS_BLOCKRES_INODE
, 0);
641 memset(&creds
, 0, sizeof(creds
));
642 memset(&fsxattrs
, 0, sizeof(fsxattrs
));
643 error
= -libxfs_inode_alloc(&tp
, NULL
, S_IFREG
, 1, 0,
644 &creds
, &fsxattrs
, &rbmip
);
646 fail(_("Realtime bitmap inode allocation failed"), error
);
649 * Do our thing with rbmip before allocating rsumip,
650 * because the next call to ialloc() may
651 * commit the transaction in which rbmip was allocated.
653 mp
->m_sb
.sb_rbmino
= rbmip
->i_ino
;
654 rbmip
->i_d
.di_size
= mp
->m_sb
.sb_rbmblocks
* mp
->m_sb
.sb_blocksize
;
655 rbmip
->i_d
.di_flags
= XFS_DIFLAG_NEWRTBM
;
656 *(__uint64_t
*)&VFS_I(mp
->m_rbmip
)->i_atime
= 0;
657 libxfs_trans_log_inode(tp
, rbmip
, XFS_ILOG_CORE
);
660 error
= -libxfs_inode_alloc(&tp
, NULL
, S_IFREG
, 1, 0,
661 &creds
, &fsxattrs
, &rsumip
);
663 fail(_("Realtime summary inode allocation failed"), error
);
665 mp
->m_sb
.sb_rsumino
= rsumip
->i_ino
;
666 rsumip
->i_d
.di_size
= mp
->m_rsumsize
;
667 libxfs_trans_log_inode(tp
, rsumip
, XFS_ILOG_CORE
);
669 libxfs_trans_commit(tp
);
670 mp
->m_rsumip
= rsumip
;
672 * Next, give the bitmap file some zero-filled blocks.
674 tp
= libxfs_trans_alloc(mp
, 0);
675 i
= -libxfs_trans_reserve(tp
, &tres
, mp
->m_sb
.sb_rbmblocks
+
676 (XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) - 1), 0);
680 libxfs_trans_ijoin(tp
, rbmip
, 0);
682 xfs_bmap_init(&flist
, &first
);
683 while (bno
< mp
->m_sb
.sb_rbmblocks
) {
684 nmap
= XFS_BMAP_MAX_NMAP
;
685 error
= -libxfs_bmapi_write(tp
, rbmip
, bno
,
686 (xfs_extlen_t
)(mp
->m_sb
.sb_rbmblocks
- bno
),
687 0, &first
, mp
->m_sb
.sb_rbmblocks
,
690 fail(_("Allocation of the realtime bitmap failed"),
693 for (i
= 0, ep
= map
; i
< nmap
; i
++, ep
++) {
694 libxfs_device_zero(mp
->m_ddev_targp
,
695 XFS_FSB_TO_DADDR(mp
, ep
->br_startblock
),
696 XFS_FSB_TO_BB(mp
, ep
->br_blockcount
));
697 bno
+= ep
->br_blockcount
;
701 error
= -libxfs_bmap_finish(&tp
, &flist
, rbmip
);
703 fail(_("Completion of the realtime bitmap failed"), error
);
705 libxfs_trans_commit(tp
);
708 * Give the summary file some zero-filled blocks.
710 tp
= libxfs_trans_alloc(mp
, 0);
711 nsumblocks
= mp
->m_rsumsize
>> mp
->m_sb
.sb_blocklog
;
712 i
= -libxfs_trans_reserve(tp
, &tres
, nsumblocks
+
713 (XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) - 1), 0);
716 libxfs_trans_ijoin(tp
, rsumip
, 0);
718 xfs_bmap_init(&flist
, &first
);
719 while (bno
< nsumblocks
) {
720 nmap
= XFS_BMAP_MAX_NMAP
;
721 error
= -libxfs_bmapi_write(tp
, rsumip
, bno
,
722 (xfs_extlen_t
)(nsumblocks
- bno
),
723 0, &first
, nsumblocks
,
726 fail(_("Allocation of the realtime summary failed"),
729 for (i
= 0, ep
= map
; i
< nmap
; i
++, ep
++) {
730 libxfs_device_zero(mp
->m_ddev_targp
,
731 XFS_FSB_TO_DADDR(mp
, ep
->br_startblock
),
732 XFS_FSB_TO_BB(mp
, ep
->br_blockcount
));
733 bno
+= ep
->br_blockcount
;
736 error
= -libxfs_bmap_finish(&tp
, &flist
, rsumip
);
738 fail(_("Completion of the realtime summary failed"), error
);
740 libxfs_trans_commit(tp
);
743 * Free the whole area using transactions.
744 * Do one transaction per bitmap block.
746 for (bno
= 0; bno
< mp
->m_sb
.sb_rextents
; bno
= ebno
) {
747 tp
= libxfs_trans_alloc(mp
, 0);
748 i
= -libxfs_trans_reserve(tp
, &tres
, 0, 0);
751 libxfs_trans_ijoin(tp
, rbmip
, 0);
752 xfs_bmap_init(&flist
, &first
);
753 ebno
= XFS_RTMIN(mp
->m_sb
.sb_rextents
,
754 bno
+ NBBY
* mp
->m_sb
.sb_blocksize
);
755 error
= -libxfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)(ebno
-bno
));
757 fail(_("Error initializing the realtime space"),
760 error
= -libxfs_bmap_finish(&tp
, &flist
, rbmip
);
762 fail(_("Error completing the realtime space"), error
);
764 libxfs_trans_commit(tp
);
774 if (fstat64(fd
, &stb
) < 0)
776 return (long)stb
.st_size
;