]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/init.c
Fix libxfs device flush ioctl, sync with kernel source.
[thirdparty/xfsprogs-dev.git] / libxfs / init.c
CommitLineData
2bd0ea18 1/*
0d3e0b37 2 * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
2bd0ea18
NS
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
5000d01d 14 * or the like. Any license provided herein, whether implied or
2bd0ea18
NS
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32
33#define ustat __kernel_ustat
34#include <libxfs.h>
2bd0ea18
NS
35#include <mntent.h>
36#include <sys/stat.h>
37#undef ustat
38#include <sys/ustat.h>
39#include <sys/ioctl.h>
40#include <sys/mount.h>
41
29e62271
ES
42/* Until glibc catches up... */
43#ifndef BLKGETSIZE64
44#define BLKGETSIZE64 _IOR(0x12,114,sizeof(__uint64_t))
45#endif
46
2bd0ea18
NS
47#define findrawpath(x) x
48#define findblockpath(x) x
49
50char *progname = "libxfs"; /* default, changed by each tool */
51
52/*
53 * dev_map - map open devices to fd.
54 */
55#define MAX_DEVS 10 /* arbitary maximum */
56int nextfakedev = -1; /* device number to give to next fake device */
57static struct dev_to_fd {
58 dev_t dev;
59 int fd;
60} dev_map[MAX_DEVS]={{0}};
61
62static int
fc8202ba 63check_ismounted(char *name, char *block)
2bd0ea18
NS
64{
65 struct ustat ust;
66 struct stat64 st;
67
68 if (stat64(block, &st) < 0)
69 return 0;
70 if ((st.st_mode & S_IFMT) != S_IFBLK)
71 return 0;
72 if (ustat(st.st_rdev, &ust) >= 0) {
fc8202ba
NS
73 fprintf(stderr, "%s: %s contains a mounted filesystem\n",
74 progname, name);
2bd0ea18
NS
75 return 1;
76 }
77 return 0;
78}
79
80/*
81 * Checks whether a given device has a mounted, writable
82 * filesystem, returns 1 if it does & fatal (just warns
83 * if not fatal, but allows us to proceed).
5000d01d 84 *
2bd0ea18
NS
85 * Useful to tools which will produce uncertain results
86 * if the filesystem is active - repair, check, logprint.
87 */
88static int
89check_isactive(char *name, char *block, int fatal)
90{
fc8202ba 91#define PROC_MOUNTED "/proc/mounts"
2bd0ea18
NS
92 int sts = 0;
93 FILE *f;
94 struct mntent *mnt;
fc8202ba
NS
95 struct stat64 st, mst;
96 struct ustat ust;
97 char mounts[MAXPATHLEN];
2bd0ea18 98
fc8202ba
NS
99 if (stat64(block, &st) < 0)
100 return sts;
101 if ((st.st_mode & S_IFMT) != S_IFBLK)
102 return sts;
103 if (ustat(st.st_rdev, &ust) < 0)
104 return sts;
105
106 strcpy(mounts, access(PROC_MOUNTED, R_OK)? PROC_MOUNTED : MOUNTED);
107 if ((f = setmntent(mounts, "r")) == NULL) {
108 fprintf(stderr, "%s: %s contains a possibly writable, mounted "
2bd0ea18
NS
109 "filesystem\n", progname, name);
110 return fatal;
fc8202ba
NS
111 }
112 while ((mnt = getmntent(f)) != NULL) {
113 if (stat64(mnt->mnt_fsname, &mst) < 0)
114 continue;
115 if ((mst.st_mode & S_IFMT) != S_IFBLK)
116 continue;
117 if (mst.st_rdev == st.st_rdev
118 && hasmntopt(mnt, MNTOPT_RO) != NULL)
119 break;
120 }
121 if (mnt == NULL) {
122 fprintf(stderr, "%s: %s contains a writable, mounted "
2bd0ea18 123 "filesystem\n", progname, name);
fc8202ba 124 sts = fatal;
2bd0ea18 125 }
fc8202ba
NS
126 endmntent(f);
127
2bd0ea18
NS
128 return sts;
129}
130
131static __int64_t
132findsize(char *path)
133{
134 int fd;
135 int error;
29e62271 136 __uint64_t size;
557b0af8 137 struct stat64 st;
2bd0ea18
NS
138
139 /* Test to see if we are dealing with a regular file rather than a
140 * block device, if we are just use the size returned by stat64
141 */
142 if (stat64(path, &st) < 0) {
143 fprintf(stderr, "%s: "
144 "cannot stat the device special file \"%s\": %s\n",
145 progname, path, strerror(errno));
146 exit(1);
147 }
148 if ((st.st_mode & S_IFMT) == S_IFREG) {
149 return (__int64_t)(st.st_size >> 9);
150 }
151
152 if ((fd = open(path, 0)) < 0) {
153 fprintf(stderr, "%s: "
154 "error opening the device special file \"%s\": %s\n",
155 progname, path, strerror(errno));
156 exit(1);
157 }
29e62271 158 error = ioctl(fd, BLKGETSIZE64, &size);
58d3fbac
ES
159 if (error >= 0) {
160 /* BLKGETSIZE64 returns size in bytes not 512-byte blocks */
161 size = size >> 9;
162 } else {
163 /* If BLKGETSIZE64 fails, try BLKGETSIZE */
164 unsigned long tmpsize;
165 error = ioctl(fd, BLKGETSIZE, &tmpsize);
166 if (error < 0) {
167 fprintf(stderr, "%s: can't determine device size\n",
168 progname);
169 exit(1);
170 }
171 size = (__uint64_t)tmpsize;
2bd0ea18
NS
172 }
173
174 close(fd);
175
176 return (__int64_t)size;
177}
178
179
5000d01d 180/* libxfs_device_to_fd:
2bd0ea18
NS
181 * lookup a device number in the device map
182 * return the associated fd
183 */
184int
185libxfs_device_to_fd(dev_t device)
186{
187 int d;
5000d01d 188
2bd0ea18 189 for (d=0;d<MAX_DEVS;d++)
5000d01d 190 if (dev_map[d].dev == device)
2bd0ea18 191 return dev_map[d].fd;
5000d01d
SL
192
193 fprintf(stderr, "%s: device_to_fd: device %lld is not open\n",
5b64e00a 194 progname, (long long)device);
2bd0ea18
NS
195 exit(1);
196}
197
198/* libxfs_device_open:
199 * open a device and return its device number
200 */
201dev_t
c5907b96 202libxfs_device_open(char *path, int creat, int readonly, int setblksize)
2bd0ea18
NS
203{
204 int fd;
205 dev_t dev;
206 int d;
5000d01d 207 struct stat statb;
2bd0ea18
NS
208
209 if ((fd = open(path,
210 (readonly ? O_RDONLY : O_RDWR) |
211 (creat ? O_CREAT|O_TRUNC : 0),
212 0666)) < 0) {
213 fprintf(stderr, "%s: cannot open %s: %s\n",
214 progname, path, strerror(errno));
215 exit(1);
216 }
217
218 if (stat(path, &statb)<0) {
219 fprintf(stderr, "%s: cannot stat %s: %s\n",
220 progname, path, strerror(errno));
221 exit(1);
222 }
a33a9e62 223
d5dca43b 224 /*
a33a9e62 225 * Set device blocksize to 512 bytes (see bug #801063)
d5dca43b
NS
226 */
227#ifndef BLKBSZSET
a33a9e62 228#define BLKBSZSET _IOW(0x12,113,sizeof(int)) /* set device block size */
d5dca43b 229#endif
c5907b96 230 if (!readonly && setblksize && (statb.st_mode & S_IFMT) == S_IFBLK) {
d5dca43b 231 int blocksize = 512; /* bytes */
2075b82a 232 if (ioctl(fd, BLKBSZSET, &blocksize) < 0) {
2bd0ea18
NS
233 fprintf(stderr, "%s: warning - cannot set blocksize on "
234 "block device %s: %s\n",
235 progname, path, strerror(errno));
236 }
237 }
238
a33a9e62
NS
239 /*
240 * Get the device number from the stat buf - unless
2bd0ea18 241 * we're not opening a real device, in which case
a33a9e62 242 * choose a new fake device number.
2bd0ea18 243 */
32181a02 244 dev = (statb.st_rdev) ? (statb.st_rdev) : (nextfakedev--);
2bd0ea18 245
32181a02 246 for (d = 0; d < MAX_DEVS; d++)
2bd0ea18 247 if (dev_map[d].dev == dev) {
5000d01d 248 fprintf(stderr, "%s: device %lld is already open\n",
5b64e00a 249 progname, (long long)dev);
2bd0ea18
NS
250 exit(1);
251 }
252
32181a02 253 for (d = 0; d < MAX_DEVS; d++)
2bd0ea18 254 if (!dev_map[d].dev) {
32181a02
NS
255 dev_map[d].dev = dev;
256 dev_map[d].fd = fd;
5000d01d 257
2bd0ea18
NS
258 return dev;
259 }
260
261 fprintf(stderr, "%s: device_open: too many open devices\n", progname);
262 exit(1);
263}
264
265void
266libxfs_device_close(dev_t dev)
267{
5000d01d 268 int d;
2bd0ea18 269
32181a02 270 for (d = 0; d < MAX_DEVS; d++)
2bd0ea18 271 if (dev_map[d].dev == dev) {
32181a02 272 int fd;
5000d01d 273
32181a02
NS
274 fd = dev_map[d].fd;
275 dev_map[d].dev = dev_map[d].fd = 0;
5000d01d 276
2bd0ea18
NS
277 fsync(fd);
278 ioctl(fd, BLKFLSBUF, 0);
279 close(fd);
5000d01d 280
2bd0ea18
NS
281 return;
282 }
283
5b64e00a
NS
284 fprintf(stderr, "%s: device_close: device %lld is not open\n",
285 progname, (long long)dev);
2bd0ea18
NS
286 ASSERT(0);
287 exit(1);
288}
289
290
291/*
292 * libxfs initialization.
293 * Caller gets a 0 on failure (and we print a message), 1 on success.
294 */
295int
296libxfs_init(libxfs_init_t *a)
297{
298 char *blockfile;
299 char curdir[MAXPATHLEN];
300 char *dname;
301 char dpath[25];
302 int fd;
303 char *logname;
304 char logpath[25];
305 int needcd;
306 char *rawfile;
307 char *rtname;
308 char rtpath[25];
309 int rval = 0;
310 int readonly;
311 int inactive;
312 struct stat64 stbuf;
313
314 dpath[0] = logpath[0] = rtpath[0] = '\0';
315 dname = a->dname;
316 logname = a->logname;
317 rtname = a->rtname;
318 a->ddev = a->logdev = a->rtdev = 0;
319 a->dfd = a->logfd = a->rtfd = -1;
320 a->dsize = a->logBBsize = a->logBBstart = a->rtsize = 0;
321
322 (void)getcwd(curdir,MAXPATHLEN);
323 needcd = 0;
324 fd = -1;
325 readonly = (a->isreadonly & LIBXFS_ISREADONLY);
326 inactive = (a->isreadonly & LIBXFS_ISINACTIVE);
327 if (a->volname) {
328 if (stat64(a->volname, &stbuf) < 0) {
329 perror(a->volname);
330 goto done;
331 }
332 if (!(rawfile = findrawpath(a->volname))) {
333 fprintf(stderr, "%s: "
334 "can't find a character device matching %s\n",
335 progname, a->volname);
336 goto done;
337 }
338 if (!(blockfile = findblockpath(a->volname))) {
339 fprintf(stderr, "%s: "
340 "can't find a block device matching %s\n",
341 progname, a->volname);
342 goto done;
343 }
344 if (!readonly && !inactive && check_ismounted(
fc8202ba 345 a->volname, blockfile))
2bd0ea18
NS
346 goto done;
347 if (inactive && check_isactive(
348 a->volname, blockfile, readonly))
349 goto done;
350 needcd = 1;
351 fd = open(rawfile, O_RDONLY);
352#ifdef HAVE_VOLUME_MANAGER
353 xlv_getdev_t getdev;
354 if (ioctl(fd, DIOCGETVOLDEV, &getdev) < 0)
355#else
356 if (1)
357#endif
358 {
359 if (a->notvolok) {
360 dname = a->dname = a->volname;
361 a->volname = NULL;
362 goto voldone;
363 }
364 fprintf(stderr, "%s: "
365 "%s is not a volume device name\n",
366 progname, a->volname);
367 if (a->notvolmsg)
368 fprintf(stderr, a->notvolmsg, a->volname);
369 goto done;
370 }
371#ifdef HAVE_VOLUME_MANAGER
372 if (getdev.data_subvol_dev && dname) {
373 fprintf(stderr, "%s: "
374 "%s has a data subvolume, cannot specify %s\n",
375 progname, a->volname, dname);
376 goto done;
377 }
378 if (getdev.log_subvol_dev && logname) {
379 fprintf(stderr, "%s: "
380 "%s has a log subvolume, cannot specify %s\n",
381 progname, a->volname, logname);
382 goto done;
383 }
384 if (getdev.rt_subvol_dev && rtname) {
385 fprintf(stderr, "%s: %s has a realtime subvolume, "
386 "cannot specify %s\n",
387 progname, a->volname, rtname);
388 goto done;
389 }
390 if (!dname && getdev.data_subvol_dev) {
391 strcpy(dpath, "/tmp/libxfsdXXXXXX");
392 (void)mktemp(dpath);
393 if (mknod(dpath, S_IFCHR | 0600,
394 getdev.data_subvol_dev) < 0) {
395 fprintf(stderr, "%s: mknod failed: %s\n",
396 progname, strerror(errno));
397 goto done;
398 }
399 dname = dpath;
400 }
401 if (!logname && getdev.log_subvol_dev) {
402 strcpy(logpath, "/tmp/libxfslXXXXXX");
403 (void)mktemp(logpath);
404 if (mknod(logpath, S_IFCHR | 0600,
405 getdev.log_subvol_dev) < 0) {
406 fprintf(stderr, "%s: mknod failed: %s\n",
407 progname, strerror(errno));
408 goto done;
409 }
410 logname = logpath;
411 }
412 if (!rtname && getdev.rt_subvol_dev) {
413 strcpy(rtpath, "/tmp/libxfsrXXXXXX");
414 (void)mktemp(rtpath);
415 if (mknod(rtpath, S_IFCHR | 0600,
416 getdev.rt_subvol_dev) < 0) {
417 fprintf(stderr, "%s: mknod failed: %s\n",
418 progname, strerror(errno));
419 goto done;
420 }
421 rtname = rtpath;
422 }
423#endif
424 }
425voldone:
426 if (dname) {
427 if (dname[0] != '/' && needcd)
428 chdir(curdir);
429 if (a->disfile) {
5000d01d 430 a->ddev= libxfs_device_open(dname, a->dcreat, readonly,
c5907b96 431 a->setblksize);
2bd0ea18
NS
432 a->dfd = libxfs_device_to_fd(a->ddev);
433 } else {
434 if (stat64(dname, &stbuf) < 0) {
435 fprintf(stderr, "%s: stat64 failed on %s: %s\n",
436 progname, dname, strerror(errno));
437 goto done;
438 }
439 if (!(rawfile = findrawpath(dname))) {
440 fprintf(stderr, "%s: can't find a char device "
441 "matching %s\n", progname, dname);
442 goto done;
443 }
444 if (!(blockfile = findblockpath(dname))) {
445 fprintf(stderr, "%s: can't find a block device "
446 "matching %s\n", progname, dname);
447 goto done;
448 }
449 if (!readonly && !inactive && check_ismounted(
fc8202ba 450 dname, blockfile))
2bd0ea18
NS
451 goto done;
452 if (inactive && check_isactive(
453 dname, blockfile, readonly))
454 goto done;
455 a->ddev = libxfs_device_open(rawfile,
c5907b96 456 a->dcreat, readonly, a->setblksize);
2bd0ea18
NS
457 a->dfd = libxfs_device_to_fd(a->ddev);
458 a->dsize = findsize(rawfile);
459 }
460 needcd = 1;
461 } else
462 a->dsize = 0;
463 if (logname) {
464 if (logname[0] != '/' && needcd)
465 chdir(curdir);
466 if (a->lisfile) {
467 a->logdev = libxfs_device_open(logname,
c5907b96 468 a->lcreat, readonly, a->setblksize);
2bd0ea18
NS
469 a->logfd = libxfs_device_to_fd(a->logdev);
470 } else {
471 if (stat64(logname, &stbuf) < 0) {
472 fprintf(stderr, "%s: stat64 failed on %s: %s\n",
473 progname, logname, strerror(errno));
474 goto done;
475 }
476 if (!(rawfile = findrawpath(logname))) {
477 fprintf(stderr, "%s: can't find a char device "
478 "matching %s\n", progname, logname);
479 goto done;
480 }
481 if (!(blockfile = findblockpath(logname))) {
482 fprintf(stderr, "%s: can't find a block device "
483 "matching %s\n", progname, logname);
484 goto done;
485 }
486 if (!readonly && !inactive && check_ismounted(
fc8202ba 487 logname, blockfile))
2bd0ea18
NS
488 goto done;
489 else if (inactive && check_isactive(
490 logname, blockfile, readonly))
491 goto done;
492 a->logdev = libxfs_device_open(rawfile,
c5907b96 493 a->lcreat, readonly, a->setblksize);
2bd0ea18
NS
494 a->logfd = libxfs_device_to_fd(a->logdev);
495 a->logBBsize = findsize(rawfile);
496 }
497 needcd = 1;
498 } else
499 a->logBBsize = 0;
500 if (rtname) {
501 if (rtname[0] != '/' && needcd)
502 chdir(curdir);
503 if (a->risfile) {
504 a->rtdev = libxfs_device_open(rtname,
c5907b96 505 a->rcreat, readonly, a->setblksize);
2bd0ea18
NS
506 a->rtfd = libxfs_device_to_fd(a->rtdev);
507 } else {
508 if (stat64(rtname, &stbuf) < 0) {
509 fprintf(stderr, "%s: stat64 failed on %s: %s\n",
510 progname, rtname, strerror(errno));
511 goto done;
512 }
513 if (!(rawfile = findrawpath(rtname))) {
514 fprintf(stderr, "%s: can't find a char device "
515 "matching %s\n", progname, rtname);
516 goto done;
517 }
518 if (!(blockfile = findblockpath(rtname))) {
519 fprintf(stderr, "%s: can't find a block device "
520 "matching %s\n", progname, rtname);
521 goto done;
522 }
523 if (!readonly && !inactive && check_ismounted(
fc8202ba 524 rtname, blockfile))
2bd0ea18
NS
525 goto done;
526 if (inactive && check_isactive(
527 rtname, blockfile, readonly))
528 goto done;
529 a->rtdev = libxfs_device_open(rawfile,
c5907b96 530 a->rcreat, readonly, a->setblksize);
2bd0ea18
NS
531 a->rtfd = libxfs_device_to_fd(a->rtdev);
532 a->rtsize = findsize(rawfile);
533 }
534 needcd = 1;
535 } else
536 a->rtsize = 0;
537 if (a->dsize < 0) {
538 fprintf(stderr, "%s: can't get size for data subvolume\n",
539 progname);
540 goto done;
541 }
542 if (a->logBBsize < 0) {
543 fprintf(stderr, "%s: can't get size for log subvolume\n",
544 progname);
545 goto done;
546 }
547 if (a->rtsize < 0) {
548 fprintf(stderr, "%s: can't get size for realtime subvolume\n",
549 progname);
550 goto done;
551 }
552 if (needcd)
553 chdir(curdir);
554 rval = 1;
555done:
556 if (dpath[0])
557 unlink(dpath);
558 if (logpath[0])
559 unlink(logpath);
560 if (rtpath[0])
561 unlink(rtpath);
562 if (fd >= 0)
563 close(fd);
564 if (!rval && a->ddev)
565 libxfs_device_close(a->ddev);
566 if (!rval && a->logdev)
567 libxfs_device_close(a->logdev);
568 if (!rval && a->rtdev)
569 libxfs_device_close(a->rtdev);
570 return rval;
571}
572
573
574/*
575 * Initialize/destroy all of the zone allocators we use.
576 */
577static void
578manage_zones(int release)
579{
580 extern xfs_zone_t *xfs_ili_zone;
581 extern xfs_zone_t *xfs_inode_zone;
582 extern xfs_zone_t *xfs_ifork_zone;
583 extern xfs_zone_t *xfs_dabuf_zone;
584 extern xfs_zone_t *xfs_buf_item_zone;
585 extern xfs_zone_t *xfs_da_state_zone;
586 extern xfs_zone_t *xfs_btree_cur_zone;
587 extern xfs_zone_t *xfs_bmap_free_item_zone;
588 extern void xfs_dir_startup();
589
590 if (release) { /* free zone allocation */
591 libxfs_free(xfs_inode_zone);
592 libxfs_free(xfs_ifork_zone);
593 libxfs_free(xfs_dabuf_zone);
594 libxfs_free(xfs_buf_item_zone);
595 libxfs_free(xfs_da_state_zone);
596 libxfs_free(xfs_btree_cur_zone);
597 libxfs_free(xfs_bmap_free_item_zone);
598 return;
599 }
600 /* otherwise initialise zone allocation */
601 xfs_inode_zone = libxfs_zone_init(sizeof(xfs_inode_t), "xfs_inode");
602 xfs_ifork_zone = libxfs_zone_init(sizeof(xfs_ifork_t), "xfs_ifork");
603 xfs_dabuf_zone = libxfs_zone_init(sizeof(xfs_dabuf_t), "xfs_dabuf");
604 xfs_ili_zone = libxfs_zone_init(
605 sizeof(xfs_inode_log_item_t), "xfs_inode_log_item");
606 xfs_buf_item_zone = libxfs_zone_init(
607 sizeof(xfs_buf_log_item_t), "xfs_buf_log_item");
608 xfs_da_state_zone = libxfs_zone_init(
609 sizeof(xfs_da_state_t), "xfs_da_state");
610 xfs_btree_cur_zone = libxfs_zone_init(
611 sizeof(xfs_btree_cur_t), "xfs_btree_cur");
612 xfs_bmap_free_item_zone = libxfs_zone_init(
613 sizeof(xfs_bmap_free_item_t), "xfs_bmap_free_item");
614 xfs_dir_startup();
615}
616
617/*
618 * Get the bitmap and summary inodes into the mount structure
619 * at mount time.
620 */
621static int
622rtmount_inodes(xfs_mount_t *mp)
623{
624 int error;
625 xfs_sb_t *sbp;
626
627 sbp = &mp->m_sb;
628 if (sbp->sb_rbmino == NULLFSINO)
629 return 0;
630 error = libxfs_iread(mp, NULL, sbp->sb_rbmino, &mp->m_rbmip, 0);
631 if (error) {
632 fprintf(stderr, "%s: cannot read realtime bitmap inode (%d)\n",
633 progname, error);
634 return error;
635 }
636 ASSERT(mp->m_rbmip != NULL);
637 ASSERT(sbp->sb_rsumino != NULLFSINO);
638 error = libxfs_iread(mp, NULL, sbp->sb_rsumino, &mp->m_rsumip, 0);
639 if (error) {
640 fprintf(stderr, "%s: cannot read realtime summary inode (%d)\n",
641 progname, error);
642 return error;
643 }
644 ASSERT(mp->m_rsumip != NULL);
645 return 0;
646}
647
b391b7cd
NS
648/*
649 * Initialize realtime fields in the mount structure.
650 */
651static int
652rtmount_init(
653 xfs_mount_t *mp) /* file system mount structure */
654{
655 xfs_buf_t *bp; /* buffer for last block of subvolume */
656 xfs_daddr_t d; /* address of last block of subvolume */
657 xfs_sb_t *sbp; /* filesystem superblock copy in mount */
658
659 sbp = &mp->m_sb;
660 if (sbp->sb_rblocks == 0)
661 return 0;
662 if (mp->m_rtdev != 0) {
663 fprintf(stderr, "%s: filesystem has a realtime subvolume\n",
664 progname);
665 return -1;
666 }
667 mp->m_rsumlevels = sbp->sb_rextslog + 1;
668 mp->m_rsumsize =
669 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
670 sbp->sb_rbmblocks;
671 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
672 mp->m_rbmip = mp->m_rsumip = NULL;
673 /*
674 * Check that the realtime section is an ok size.
675 */
676 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
677 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
678 fprintf(stderr, "%s: realtime init - %llu != %llu\n", progname,
679 (unsigned long long) XFS_BB_TO_FSB(mp, d),
680 (unsigned long long) mp->m_sb.sb_rblocks);
681 return -1;
682 }
683 bp = libxfs_readbuf(mp->m_rtdev, d - 1, 1, 0);
684 if (bp == NULL) {
685 fprintf(stderr, "%s: realtime size check failed\n", progname);
686 return -1;
687 }
688 libxfs_putbuf(bp);
689 return 0;
690}
691
34317449 692#define XFS_MOUNT_32BITINODES 0x1
2bd0ea18
NS
693/*
694 * Mount structure initialization, provides a filled-in xfs_mount_t
695 * such that the numerous XFS_* macros can be used. If dev is zero,
696 * no IO will be performed (no size checks, read root inodes).
697 */
698xfs_mount_t *
699libxfs_mount(
700 xfs_mount_t *mp,
701 xfs_sb_t *sb,
702 dev_t dev,
703 dev_t logdev,
704 dev_t rtdev,
705 int rrootinos)
706{
707 xfs_daddr_t d;
708 xfs_buf_t *bp;
709 xfs_sb_t *sbp;
710 size_t size;
711 int error;
712
713 mp->m_dev = dev;
714 mp->m_rtdev = rtdev;
715 mp->m_logdev = logdev;
34317449 716 mp->m_flags = XFS_MOUNT_32BITINODES;
2bd0ea18
NS
717 mp->m_sb = *sb;
718 sbp = &(mp->m_sb);
719 manage_zones(0);
720
721 libxfs_mount_common(mp, sb);
722
723 libxfs_alloc_compute_maxlevels(mp);
724 libxfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
725 libxfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
726 libxfs_ialloc_compute_maxlevels(mp);
727
728 if (sbp->sb_imax_pct) {
729 /* Make sure the maximum inode count is a multiple of the
730 * units we allocate inodes in.
731 */
732 mp->m_maxicount = (sbp->sb_dblocks * sbp->sb_imax_pct) / 100;
733 mp->m_maxicount = ((mp->m_maxicount / mp->m_ialloc_blks) *
734 mp->m_ialloc_blks) << sbp->sb_inopblog;
735 } else
736 mp->m_maxicount = 0;
737
738 mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
739
740 /*
741 * Set whether we're using inode alignment.
742 */
743 if (XFS_SB_VERSION_HASALIGN(&mp->m_sb) &&
744 mp->m_sb.sb_inoalignmt >=
745 XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
746 mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
747 else
748 mp->m_inoalign_mask = 0;
749 /*
750 * If we are using stripe alignment, check whether
751 * the stripe unit is a multiple of the inode alignment
752 */
753 if ( mp->m_dalign
754 && mp->m_inoalign_mask && !(mp->m_dalign & mp->m_inoalign_mask))
755 mp->m_sinoalign = mp->m_dalign;
756 else
757 mp->m_sinoalign = 0;
758
759 /*
760 * Check that the data (and log if separate) are an ok size.
761 */
762 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
763 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
764 fprintf(stderr, "%s: size check failed\n", progname);
765 return NULL;
766 }
767
768 /* Initialize the appropriate directory manager */
769 if (XFS_SB_VERSION_HASDIRV2(sbp))
770 libxfs_dir2_mount(mp);
771 else
772 libxfs_dir_mount(mp);
773
774 /* Initialize the precomputed transaction reservations values */
775 libxfs_trans_init(mp);
776
777 if (dev == 0) /* maxtrres, we have no device so leave now */
778 return mp;
779
780 bp = libxfs_readbuf(mp->m_dev, d - 1, 1, 0);
781 if (bp == NULL) {
782 fprintf(stderr, "%s: data size check failed\n", progname);
783 return NULL;
784 }
785 libxfs_putbuf(bp);
786
787 if (mp->m_logdev && mp->m_logdev != mp->m_dev) {
788 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
789 if ( (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) ||
790 (!(bp = libxfs_readbuf(mp->m_logdev, d - 1, 1, 1)))) {
791 fprintf(stderr, "%s: log size checks failed\n",
792 progname);
793 return NULL;
794 }
795 libxfs_putbuf(bp);
796 }
797
798 /* Initialize realtime fields in the mount structure */
b391b7cd
NS
799 if (rtmount_init(mp)) {
800 fprintf(stderr, "%s: realtime device init failed\n", progname);
2bd0ea18
NS
801 return NULL;
802 }
803
804 /* Allocate and initialize the per-ag data */
805 size = sbp->sb_agcount * sizeof(xfs_perag_t);
806 if ((mp->m_perag = calloc(size, 1)) == NULL) {
5b64e00a
NS
807 fprintf(stderr, "%s: failed to alloc %ld bytes: %s\n",
808 progname, (long)size, strerror(errno));
2bd0ea18
NS
809 exit(1);
810 }
811
a33a9e62
NS
812 libxfs_initialize_perag(mp, sbp->sb_agcount);
813
2bd0ea18
NS
814 /*
815 * mkfs calls mount before the root inode is allocated.
816 */
817 if (rrootinos && sbp->sb_rootino != NULLFSINO) {
818 error = libxfs_iread(mp, NULL, sbp->sb_rootino,
819 &mp->m_rootip, 0);
820 if (error) {
821 fprintf(stderr, "%s: cannot read root inode (%d)\n",
822 progname, error);
823 return NULL;
824 }
825 ASSERT(mp->m_rootip != NULL);
826 }
827 if (rrootinos && rtmount_inodes(mp))
828 return NULL;
829 return mp;
830}
831
832/*
833 * Release any resourse obtained during a mount.
834 */
835void
836libxfs_umount(xfs_mount_t *mp)
837{
838 manage_zones(1);
839 free(mp->m_perag);
840}