]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - mkfs/proto.c
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
19 #include <xfs/libxfs.h>
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)(XFS_IALLOC_BLOCKS(mp) + ((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
));
64 buf
= malloc(size
+ 1);
65 if (read(fd
, buf
, size
) < size
) {
66 fprintf(stderr
, _("%s: read failed on %s: %s\n"),
67 progname
, fname
, strerror(errno
));
70 if (buf
[size
- 1] != '\n') {
71 fprintf(stderr
, _("%s: proto file %s premature EOF\n"),
77 * Skip past the stuff there for compatibility, a string and 2 numbers.
79 (void)getstr(&buf
); /* boot image name */
80 (void)getnum(&buf
); /* block count */
81 (void)getnum(&buf
); /* inode count */
100 fprintf(stderr
, "%s: %s [%d - %s]\n", progname
, msg
, i
, strerror(i
));
108 fail(_("cannot reserve space"), i
);
121 for (i
= 0, r
= MKFS_BLOCKRES(blocks
); r
>= blocks
; r
--) {
122 struct xfs_trans_res tres
= {0};
124 i
= libxfs_trans_reserve(tp
, &tres
, r
, 0);
155 while (c
!= ' ' && c
!= '\t' && c
!= '\n' && c
!= '\0')
163 fprintf(stderr
, _("%s: premature EOF in prototype file\n"),
179 error
= libxfs_alloc_file_space(ip
, 0, llen
, 1, 0);
182 fail(_("error reserving space for a file"), error
);
187 * update the inode timestamp, mode, and prealloc flag bits
189 tp
= libxfs_trans_alloc(mp
, 0);
191 libxfs_trans_ijoin(tp
, ip
, 0);
192 libxfs_trans_ihold(tp
, ip
);
194 ip
->i_d
.di_mode
&= ~S_ISUID
;
197 * Note that we don't have to worry about mandatory
198 * file locking being disabled here because we only
199 * clear the S_ISGID bit if the Group execute bit is
200 * on, but if it was on then mandatory locking wouldn't
203 if (ip
->i_d
.di_mode
& S_IXGRP
)
204 ip
->i_d
.di_mode
&= ~S_ISGID
;
206 libxfs_trans_ichgtime(tp
, ip
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
208 ip
->i_d
.di_flags
|= XFS_DIFLAG_PREALLOC
;
210 libxfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
211 libxfs_trans_commit(tp
, 0);
218 xfs_bmap_free_t
*flist
,
219 xfs_fsblock_t
*first
,
236 if (dolocal
&& len
<= XFS_IFORK_DSIZE(ip
)) {
237 xfs_idata_realloc(ip
, len
, XFS_DATA_FORK
);
239 memmove(ip
->i_df
.if_u1
.if_data
, buf
, len
);
240 ip
->i_d
.di_size
= len
;
241 ip
->i_df
.if_flags
&= ~XFS_IFEXTENTS
;
242 ip
->i_df
.if_flags
|= XFS_IFINLINE
;
243 ip
->i_d
.di_format
= XFS_DINODE_FMT_LOCAL
;
244 flags
= XFS_ILOG_DDATA
;
245 } else if (len
> 0) {
246 nb
= XFS_B_TO_FSB(mp
, len
);
248 error
= libxfs_bmapi_write(tp
, ip
, 0, nb
, 0, first
, nb
,
251 fail(_("error allocating space for a file"), error
);
255 _("%s: cannot allocate space for file\n"),
259 d
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
);
260 bp
= libxfs_trans_get_buf(logit
? tp
: 0, mp
->m_dev
, d
,
261 nb
<< mp
->m_blkbb_log
, 0);
262 memmove(XFS_BUF_PTR(bp
), buf
, len
);
263 if (len
< XFS_BUF_COUNT(bp
))
264 memset(XFS_BUF_PTR(bp
) + len
, 0, XFS_BUF_COUNT(bp
) - len
);
266 libxfs_trans_log_buf(tp
, bp
, 0, XFS_BUF_COUNT(bp
) - 1);
268 libxfs_writebuf(bp
, LIBXFS_EXIT_ON_FAILURE
);
270 ip
->i_d
.di_size
= len
;
285 if ((fd
= open(fname
, O_RDONLY
)) < 0 || (size
= filesize(fd
)) < 0) {
286 fprintf(stderr
, _("%s: cannot open %s: %s\n"),
287 progname
, fname
, strerror(errno
));
290 if ((*len
= (int)size
)) {
292 if (read(fd
, buf
, size
) < size
) {
293 fprintf(stderr
, _("%s: read failed on %s: %s\n"),
294 progname
, fname
, strerror(errno
));
308 struct xfs_name
*name
,
310 xfs_fsblock_t
*first
,
311 xfs_bmap_free_t
*flist
)
316 rsv
= XFS_DIRENTER_SPACE_RES(mp
, name
->len
);
318 error
= libxfs_dir_createname(tp
, pip
, name
, inum
, first
, flist
, rsv
);
320 fail(_("directory createname error"), error
);
332 error
= libxfs_dir_init(tp
, dp
, pdp
);
334 fail(_("directory create error"), error
);
341 struct fsxattr
*fsxp
,
346 #define IF_RESERVED 1
349 #define IF_DIRECTORY 4
358 xfs_bmap_free_t flist
;
373 struct xfs_name xname
;
375 memset(&creds
, 0, sizeof(creds
));
400 fprintf(stderr
, _("%s: bad format string %s\n"),
412 fprintf(stderr
, _("%s: bad format string %s\n"),
423 fprintf(stderr
, _("%s: bad format string %s\n"),
428 for (i
= 3; i
< 6; i
++) {
429 if (mstr
[i
] < '0' || mstr
[i
] > '7') {
430 fprintf(stderr
, _("%s: bad format string %s\n"),
434 val
= val
* 8 + mstr
[i
] - '0';
437 creds
.cr_uid
= (int)getnum(pp
);
438 creds
.cr_gid
= (int)getnum(pp
);
439 xname
.name
= (uchar_t
*)name
;
440 xname
.len
= name
? strlen(name
) : 0;
441 tp
= libxfs_trans_alloc(mp
, 0);
442 flags
= XFS_ILOG_CORE
;
443 xfs_bmap_init(&flist
, &first
);
446 buf
= newregfile(pp
, &len
);
447 getres(tp
, XFS_B_TO_FSB(mp
, len
));
448 error
= libxfs_inode_alloc(&tp
, pip
, mode
|S_IFREG
, 1, 0,
451 fail(_("Inode allocation failed"), error
);
452 flags
|= newfile(tp
, ip
, &flist
, &first
, 0, 0, buf
, len
);
455 libxfs_trans_ijoin(tp
, pip
, 0);
456 newdirent(mp
, tp
, pip
, &xname
, ip
->i_ino
, &first
, &flist
);
457 libxfs_trans_ihold(tp
, pip
);
460 case IF_RESERVED
: /* pre-allocated space only */
462 llen
= cvtnum(mp
->m_sb
.sb_blocksize
, mp
->m_sb
.sb_sectsize
, value
);
463 getres(tp
, XFS_B_TO_FSB(mp
, llen
));
465 error
= libxfs_inode_alloc(&tp
, pip
, mode
|S_IFREG
, 1, 0,
468 fail(_("Inode pre-allocation failed"), error
);
470 libxfs_trans_ijoin(tp
, pip
, 0);
472 newdirent(mp
, tp
, pip
, &xname
, ip
->i_ino
, &first
, &flist
);
473 libxfs_trans_ihold(tp
, pip
);
474 libxfs_trans_log_inode(tp
, ip
, flags
);
476 error
= libxfs_bmap_finish(&tp
, &flist
, &committed
);
478 fail(_("Pre-allocated file creation failed"), error
);
479 libxfs_trans_commit(tp
, 0);
480 rsvfile(mp
, ip
, llen
);
485 majdev
= (int)getnum(pp
);
486 mindev
= (int)getnum(pp
);
487 error
= libxfs_inode_alloc(&tp
, pip
, mode
|S_IFBLK
, 1,
488 IRIX_MKDEV(majdev
, mindev
), &creds
, fsxp
, &ip
);
490 fail(_("Inode allocation failed"), error
);
492 libxfs_trans_ijoin(tp
, pip
, 0);
493 newdirent(mp
, tp
, pip
, &xname
, ip
->i_ino
, &first
, &flist
);
494 libxfs_trans_ihold(tp
, pip
);
495 flags
|= XFS_ILOG_DEV
;
500 majdev
= (int)getnum(pp
);
501 mindev
= (int)getnum(pp
);
502 error
= libxfs_inode_alloc(&tp
, pip
, mode
|S_IFCHR
, 1,
503 IRIX_MKDEV(majdev
, mindev
), &creds
, fsxp
, &ip
);
505 fail(_("Inode allocation failed"), error
);
506 libxfs_trans_ijoin(tp
, pip
, 0);
507 newdirent(mp
, tp
, pip
, &xname
, ip
->i_ino
, &first
, &flist
);
508 libxfs_trans_ihold(tp
, pip
);
509 flags
|= XFS_ILOG_DEV
;
514 error
= libxfs_inode_alloc(&tp
, pip
, mode
|S_IFIFO
, 1, 0,
517 fail(_("Inode allocation failed"), error
);
518 libxfs_trans_ijoin(tp
, pip
, 0);
519 newdirent(mp
, tp
, pip
, &xname
, ip
->i_ino
, &first
, &flist
);
520 libxfs_trans_ihold(tp
, pip
);
524 len
= (int)strlen(buf
);
525 getres(tp
, XFS_B_TO_FSB(mp
, len
));
526 error
= libxfs_inode_alloc(&tp
, pip
, mode
|S_IFLNK
, 1, 0,
529 fail(_("Inode allocation failed"), error
);
530 flags
|= newfile(tp
, ip
, &flist
, &first
, 1, 1, buf
, len
);
531 libxfs_trans_ijoin(tp
, pip
, 0);
532 newdirent(mp
, tp
, pip
, &xname
, ip
->i_ino
, &first
, &flist
);
533 libxfs_trans_ihold(tp
, pip
);
537 error
= libxfs_inode_alloc(&tp
, pip
, mode
|S_IFDIR
, 1, 0,
540 fail(_("Inode allocation failed"), error
);
541 ip
->i_d
.di_nlink
++; /* account for . */
544 mp
->m_sb
.sb_rootino
= ip
->i_ino
;
545 libxfs_mod_sb(tp
, XFS_SB_ROOTINO
);
549 libxfs_trans_ijoin(tp
, pip
, 0);
550 newdirent(mp
, tp
, pip
, &xname
, ip
->i_ino
,
553 libxfs_trans_ihold(tp
, pip
);
554 libxfs_trans_log_inode(tp
, pip
, XFS_ILOG_CORE
);
556 newdirectory(mp
, tp
, ip
, pip
);
557 libxfs_trans_log_inode(tp
, ip
, flags
);
558 error
= libxfs_bmap_finish(&tp
, &flist
, &committed
);
560 fail(_("Directory creation failed"), error
);
561 libxfs_trans_ihold(tp
, ip
);
562 libxfs_trans_commit(tp
, 0);
564 * RT initialization. Do this here to ensure that
565 * the RT inodes get placed after the root inode.
574 if (strcmp(name
, "$") == 0)
576 parseproto(mp
, ip
, fsxp
, pp
, name
);
581 libxfs_trans_log_inode(tp
, ip
, flags
);
582 error
= libxfs_bmap_finish(&tp
, &flist
, &committed
);
584 fail(_("Error encountered creating file from prototype file"),
587 libxfs_trans_commit(tp
, 0);
596 parseproto(mp
, NULL
, fsx
, pp
, NULL
);
600 * Allocate the realtime bitmap and summary inodes, and fill in data if any.
612 xfs_bmap_free_t flist
;
614 xfs_bmbt_irec_t map
[XFS_BMAP_MAX_NMAP
];
615 xfs_extlen_t nsumblocks
;
621 struct fsxattr fsxattrs
;
622 struct xfs_trans_res tres
= {0};
625 * First, allocate the inodes.
627 tp
= libxfs_trans_alloc(mp
, 0);
628 i
= libxfs_trans_reserve(tp
, &tres
, MKFS_BLOCKRES_INODE
, 0);
632 memset(&creds
, 0, sizeof(creds
));
633 memset(&fsxattrs
, 0, sizeof(fsxattrs
));
634 error
= libxfs_inode_alloc(&tp
, NULL
, S_IFREG
, 1, 0,
635 &creds
, &fsxattrs
, &rbmip
);
637 fail(_("Realtime bitmap inode allocation failed"), error
);
640 * Do our thing with rbmip before allocating rsumip,
641 * because the next call to ialloc() may
642 * commit the transaction in which rbmip was allocated.
644 mp
->m_sb
.sb_rbmino
= rbmip
->i_ino
;
645 rbmip
->i_d
.di_size
= mp
->m_sb
.sb_rbmblocks
* mp
->m_sb
.sb_blocksize
;
646 rbmip
->i_d
.di_flags
= XFS_DIFLAG_NEWRTBM
;
647 *(__uint64_t
*)&rbmip
->i_d
.di_atime
= 0;
648 libxfs_trans_log_inode(tp
, rbmip
, XFS_ILOG_CORE
);
649 libxfs_mod_sb(tp
, XFS_SB_RBMINO
);
650 libxfs_trans_ihold(tp
, rbmip
);
652 error
= libxfs_inode_alloc(&tp
, NULL
, S_IFREG
, 1, 0,
653 &creds
, &fsxattrs
, &rsumip
);
655 fail(_("Realtime summary inode allocation failed"), error
);
657 mp
->m_sb
.sb_rsumino
= rsumip
->i_ino
;
658 rsumip
->i_d
.di_size
= mp
->m_rsumsize
;
659 libxfs_trans_log_inode(tp
, rsumip
, XFS_ILOG_CORE
);
660 libxfs_mod_sb(tp
, XFS_SB_RSUMINO
);
661 libxfs_trans_ihold(tp
, rsumip
);
662 libxfs_trans_commit(tp
, 0);
663 mp
->m_rsumip
= rsumip
;
665 * Next, give the bitmap file some zero-filled blocks.
667 tp
= libxfs_trans_alloc(mp
, 0);
668 i
= libxfs_trans_reserve(tp
, &tres
, mp
->m_sb
.sb_rbmblocks
+
669 (XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) - 1), 0);
673 libxfs_trans_ijoin(tp
, rbmip
, 0);
674 libxfs_trans_ihold(tp
, rbmip
);
676 xfs_bmap_init(&flist
, &first
);
677 while (bno
< mp
->m_sb
.sb_rbmblocks
) {
678 nmap
= XFS_BMAP_MAX_NMAP
;
679 error
= libxfs_bmapi_write(tp
, rbmip
, bno
,
680 (xfs_extlen_t
)(mp
->m_sb
.sb_rbmblocks
- bno
),
681 0, &first
, mp
->m_sb
.sb_rbmblocks
,
684 fail(_("Allocation of the realtime bitmap failed"),
687 for (i
= 0, ep
= map
; i
< nmap
; i
++, ep
++) {
688 libxfs_device_zero(mp
->m_ddev_targp
,
689 XFS_FSB_TO_DADDR(mp
, ep
->br_startblock
),
690 XFS_FSB_TO_BB(mp
, ep
->br_blockcount
));
691 bno
+= ep
->br_blockcount
;
695 error
= libxfs_bmap_finish(&tp
, &flist
, &committed
);
697 fail(_("Completion of the realtime bitmap failed"), error
);
699 libxfs_trans_commit(tp
, 0);
702 * Give the summary file some zero-filled blocks.
704 tp
= libxfs_trans_alloc(mp
, 0);
705 nsumblocks
= mp
->m_rsumsize
>> mp
->m_sb
.sb_blocklog
;
706 i
= libxfs_trans_reserve(tp
, &tres
, nsumblocks
+
707 (XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) - 1), 0);
710 libxfs_trans_ijoin(tp
, rsumip
, 0);
711 libxfs_trans_ihold(tp
, rsumip
);
713 xfs_bmap_init(&flist
, &first
);
714 while (bno
< nsumblocks
) {
715 nmap
= XFS_BMAP_MAX_NMAP
;
716 error
= libxfs_bmapi_write(tp
, rsumip
, bno
,
717 (xfs_extlen_t
)(nsumblocks
- bno
),
718 0, &first
, nsumblocks
,
721 fail(_("Allocation of the realtime summary failed"),
724 for (i
= 0, ep
= map
; i
< nmap
; i
++, ep
++) {
725 libxfs_device_zero(mp
->m_ddev_targp
,
726 XFS_FSB_TO_DADDR(mp
, ep
->br_startblock
),
727 XFS_FSB_TO_BB(mp
, ep
->br_blockcount
));
728 bno
+= ep
->br_blockcount
;
731 error
= libxfs_bmap_finish(&tp
, &flist
, &committed
);
733 fail(_("Completion of the realtime summary failed"), error
);
735 libxfs_trans_commit(tp
, 0);
738 * Free the whole area using transactions.
739 * Do one transaction per bitmap block.
741 for (bno
= 0; bno
< mp
->m_sb
.sb_rextents
; bno
= ebno
) {
742 tp
= libxfs_trans_alloc(mp
, 0);
743 i
= libxfs_trans_reserve(tp
, &tres
, 0, 0);
746 libxfs_trans_ijoin(tp
, rbmip
, 0);
747 libxfs_trans_ihold(tp
, rbmip
);
748 xfs_bmap_init(&flist
, &first
);
749 ebno
= XFS_RTMIN(mp
->m_sb
.sb_rextents
,
750 bno
+ NBBY
* mp
->m_sb
.sb_blocksize
);
751 error
= libxfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)(ebno
-bno
));
753 fail(_("Error initializing the realtime space"),
756 error
= libxfs_bmap_finish(&tp
, &flist
, &committed
);
758 fail(_("Error completing the realtime space"), error
);
760 libxfs_trans_commit(tp
, 0);
770 if (fstat64(fd
, &stb
) < 0)
772 return (long)stb
.st_size
;