]>
Commit | Line | Data |
---|---|---|
2bd0ea18 | 1 | /* |
93d9f139 | 2 | * Copyright (c) 2000-2003 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 | ||
1d7e80ee | 33 | #include <xfs/libxfs.h> |
2bd0ea18 | 34 | #include <sys/stat.h> |
9440d84d | 35 | #include "init.h" |
29e62271 | 36 | |
2bd0ea18 NS |
37 | #define findrawpath(x) x |
38 | #define findblockpath(x) x | |
39 | ||
40 | char *progname = "libxfs"; /* default, changed by each tool */ | |
41 | ||
42 | /* | |
43 | * dev_map - map open devices to fd. | |
44 | */ | |
45 | #define MAX_DEVS 10 /* arbitary maximum */ | |
46 | int nextfakedev = -1; /* device number to give to next fake device */ | |
47 | static struct dev_to_fd { | |
9440d84d NS |
48 | dev_t dev; |
49 | int fd; | |
2bd0ea18 NS |
50 | } dev_map[MAX_DEVS]={{0}}; |
51 | ||
2bd0ea18 NS |
52 | /* |
53 | * Checks whether a given device has a mounted, writable | |
54 | * filesystem, returns 1 if it does & fatal (just warns | |
55 | * if not fatal, but allows us to proceed). | |
5000d01d | 56 | * |
2bd0ea18 NS |
57 | * Useful to tools which will produce uncertain results |
58 | * if the filesystem is active - repair, check, logprint. | |
59 | */ | |
60 | static int | |
61 | check_isactive(char *name, char *block, int fatal) | |
62 | { | |
9440d84d | 63 | struct stat64 st; |
2bd0ea18 | 64 | |
fc8202ba | 65 | if (stat64(block, &st) < 0) |
9440d84d | 66 | return 0; |
fc8202ba | 67 | if ((st.st_mode & S_IFMT) != S_IFBLK) |
9440d84d | 68 | return 0; |
93d9f139 | 69 | if (platform_check_ismounted(name, block, &st, 0) == 0) |
9440d84d | 70 | return 0; |
93d9f139 | 71 | return platform_check_iswritable(name, block, &st, fatal); |
2bd0ea18 NS |
72 | } |
73 | ||
5000d01d | 74 | /* libxfs_device_to_fd: |
2bd0ea18 NS |
75 | * lookup a device number in the device map |
76 | * return the associated fd | |
77 | */ | |
78 | int | |
79 | libxfs_device_to_fd(dev_t device) | |
80 | { | |
9440d84d | 81 | int d; |
5000d01d | 82 | |
9440d84d | 83 | for (d = 0; d < MAX_DEVS; d++) |
5000d01d | 84 | if (dev_map[d].dev == device) |
2bd0ea18 | 85 | return dev_map[d].fd; |
5000d01d | 86 | |
9440d84d NS |
87 | fprintf(stderr, _("%s: %s: device %lld is not open\n"), |
88 | progname, __FUNCTION__, (long long)device); | |
2bd0ea18 NS |
89 | exit(1); |
90 | } | |
91 | ||
92 | /* libxfs_device_open: | |
93 | * open a device and return its device number | |
94 | */ | |
95 | dev_t | |
c5907b96 | 96 | libxfs_device_open(char *path, int creat, int readonly, int setblksize) |
2bd0ea18 NS |
97 | { |
98 | int fd; | |
99 | dev_t dev; | |
100 | int d; | |
5000d01d | 101 | struct stat statb; |
2bd0ea18 NS |
102 | |
103 | if ((fd = open(path, | |
104 | (readonly ? O_RDONLY : O_RDWR) | | |
105 | (creat ? O_CREAT|O_TRUNC : 0), | |
106 | 0666)) < 0) { | |
9440d84d | 107 | fprintf(stderr, _("%s: cannot open %s: %s\n"), |
2bd0ea18 NS |
108 | progname, path, strerror(errno)); |
109 | exit(1); | |
110 | } | |
111 | ||
112 | if (stat(path, &statb)<0) { | |
9440d84d | 113 | fprintf(stderr, _("%s: cannot stat %s: %s\n"), |
2bd0ea18 NS |
114 | progname, path, strerror(errno)); |
115 | exit(1); | |
116 | } | |
a33a9e62 | 117 | |
c5907b96 | 118 | if (!readonly && setblksize && (statb.st_mode & S_IFMT) == S_IFBLK) { |
93d9f139 | 119 | platform_set_blocksize(fd, path, 512); |
2bd0ea18 NS |
120 | } |
121 | ||
a33a9e62 NS |
122 | /* |
123 | * Get the device number from the stat buf - unless | |
2bd0ea18 | 124 | * we're not opening a real device, in which case |
a33a9e62 | 125 | * choose a new fake device number. |
2bd0ea18 | 126 | */ |
32181a02 | 127 | dev = (statb.st_rdev) ? (statb.st_rdev) : (nextfakedev--); |
2bd0ea18 | 128 | |
32181a02 | 129 | for (d = 0; d < MAX_DEVS; d++) |
2bd0ea18 | 130 | if (dev_map[d].dev == dev) { |
9440d84d | 131 | fprintf(stderr, _("%s: device %lld is already open\n"), |
5b64e00a | 132 | progname, (long long)dev); |
2bd0ea18 NS |
133 | exit(1); |
134 | } | |
135 | ||
32181a02 | 136 | for (d = 0; d < MAX_DEVS; d++) |
2bd0ea18 | 137 | if (!dev_map[d].dev) { |
32181a02 NS |
138 | dev_map[d].dev = dev; |
139 | dev_map[d].fd = fd; | |
5000d01d | 140 | |
2bd0ea18 NS |
141 | return dev; |
142 | } | |
143 | ||
9440d84d NS |
144 | fprintf(stderr, _("%s: %s: too many open devices\n"), |
145 | progname, __FUNCTION__); | |
2bd0ea18 NS |
146 | exit(1); |
147 | } | |
148 | ||
149 | void | |
150 | libxfs_device_close(dev_t dev) | |
151 | { | |
5000d01d | 152 | int d; |
2bd0ea18 | 153 | |
32181a02 | 154 | for (d = 0; d < MAX_DEVS; d++) |
2bd0ea18 | 155 | if (dev_map[d].dev == dev) { |
32181a02 | 156 | int fd; |
5000d01d | 157 | |
32181a02 NS |
158 | fd = dev_map[d].fd; |
159 | dev_map[d].dev = dev_map[d].fd = 0; | |
5000d01d | 160 | |
2bd0ea18 | 161 | fsync(fd); |
93d9f139 | 162 | platform_flush_device(fd); |
2bd0ea18 | 163 | close(fd); |
5000d01d | 164 | |
2bd0ea18 NS |
165 | return; |
166 | } | |
167 | ||
9440d84d NS |
168 | fprintf(stderr, _("%s: %s: device %lld is not open\n"), |
169 | progname, __FUNCTION__, (long long)dev); | |
2bd0ea18 NS |
170 | exit(1); |
171 | } | |
172 | ||
74668075 NS |
173 | static int |
174 | check_open(char *path, int flags, char **rawfile, char **blockfile) | |
175 | { | |
c781939c RC |
176 | int readonly = (flags & LIBXFS_ISREADONLY); |
177 | int inactive = (flags & LIBXFS_ISINACTIVE); | |
178 | int dangerously = (flags & LIBXFS_DANGEROUSLY); | |
179 | struct stat64 stbuf; | |
180 | ||
181 | if (stat64(path, &stbuf) < 0) { | |
182 | perror(path); | |
183 | return 0; | |
184 | } | |
185 | if (!(*rawfile = findrawpath(path))) { | |
186 | fprintf(stderr, _("%s: " | |
187 | "can't find a character device matching %s\n"), | |
188 | progname, path); | |
189 | return 0; | |
190 | } | |
191 | if (!(*blockfile = findblockpath(path))) { | |
192 | fprintf(stderr, _("%s: " | |
193 | "can't find a block device matching %s\n"), | |
194 | progname, path); | |
195 | return 0; | |
196 | } | |
197 | if (!readonly && !inactive && platform_check_ismounted(path, *blockfile, NULL, 1)) | |
198 | return 0; | |
199 | ||
200 | if (inactive && check_isactive(path, *blockfile, ((readonly|dangerously)?1:0))) | |
201 | return 0; | |
202 | ||
203 | return 1; | |
204 | } | |
205 | ||
2bd0ea18 NS |
206 | /* |
207 | * libxfs initialization. | |
208 | * Caller gets a 0 on failure (and we print a message), 1 on success. | |
209 | */ | |
210 | int | |
211 | libxfs_init(libxfs_init_t *a) | |
212 | { | |
213 | char *blockfile; | |
214 | char curdir[MAXPATHLEN]; | |
215 | char *dname; | |
216 | char dpath[25]; | |
217 | int fd; | |
218 | char *logname; | |
219 | char logpath[25]; | |
220 | int needcd; | |
221 | char *rawfile; | |
222 | char *rtname; | |
223 | char rtpath[25]; | |
224 | int rval = 0; | |
225 | int readonly; | |
226 | int inactive; | |
c781939c | 227 | int flags; |
2bd0ea18 NS |
228 | |
229 | dpath[0] = logpath[0] = rtpath[0] = '\0'; | |
230 | dname = a->dname; | |
231 | logname = a->logname; | |
232 | rtname = a->rtname; | |
2bd0ea18 | 233 | a->dfd = a->logfd = a->rtfd = -1; |
74668075 NS |
234 | a->ddev = a->logdev = a->rtdev = 0; |
235 | a->dbsize = a->lbsize = a->rtbsize = 0; | |
2bd0ea18 NS |
236 | a->dsize = a->logBBsize = a->logBBstart = a->rtsize = 0; |
237 | ||
238 | (void)getcwd(curdir,MAXPATHLEN); | |
239 | needcd = 0; | |
240 | fd = -1; | |
241 | readonly = (a->isreadonly & LIBXFS_ISREADONLY); | |
242 | inactive = (a->isreadonly & LIBXFS_ISINACTIVE); | |
c781939c RC |
243 | flags = a->isreadonly; |
244 | ||
2bd0ea18 | 245 | if (a->volname) { |
c781939c | 246 | if(!check_open(a->volname,flags,&rawfile,&blockfile)) |
2bd0ea18 NS |
247 | goto done; |
248 | needcd = 1; | |
249 | fd = open(rawfile, O_RDONLY); | |
250 | #ifdef HAVE_VOLUME_MANAGER | |
251 | xlv_getdev_t getdev; | |
252 | if (ioctl(fd, DIOCGETVOLDEV, &getdev) < 0) | |
253 | #else | |
254 | if (1) | |
255 | #endif | |
256 | { | |
257 | if (a->notvolok) { | |
258 | dname = a->dname = a->volname; | |
259 | a->volname = NULL; | |
260 | goto voldone; | |
261 | } | |
9440d84d NS |
262 | fprintf(stderr, _("%s: " |
263 | "%s is not a volume device name\n"), | |
2bd0ea18 NS |
264 | progname, a->volname); |
265 | if (a->notvolmsg) | |
266 | fprintf(stderr, a->notvolmsg, a->volname); | |
267 | goto done; | |
268 | } | |
269 | #ifdef HAVE_VOLUME_MANAGER | |
270 | if (getdev.data_subvol_dev && dname) { | |
9440d84d NS |
271 | fprintf(stderr, _("%s: " |
272 | "%s has a data subvolume, cannot specify %s\n"), | |
2bd0ea18 NS |
273 | progname, a->volname, dname); |
274 | goto done; | |
275 | } | |
276 | if (getdev.log_subvol_dev && logname) { | |
9440d84d NS |
277 | fprintf(stderr, _("%s: " |
278 | "%s has a log subvolume, cannot specify %s\n"), | |
2bd0ea18 NS |
279 | progname, a->volname, logname); |
280 | goto done; | |
281 | } | |
282 | if (getdev.rt_subvol_dev && rtname) { | |
9440d84d NS |
283 | fprintf(stderr, _("%s: %s has a realtime subvolume, " |
284 | "cannot specify %s\n"), | |
2bd0ea18 NS |
285 | progname, a->volname, rtname); |
286 | goto done; | |
287 | } | |
288 | if (!dname && getdev.data_subvol_dev) { | |
289 | strcpy(dpath, "/tmp/libxfsdXXXXXX"); | |
290 | (void)mktemp(dpath); | |
291 | if (mknod(dpath, S_IFCHR | 0600, | |
292 | getdev.data_subvol_dev) < 0) { | |
9440d84d | 293 | fprintf(stderr, _("%s: mknod failed: %s\n"), |
2bd0ea18 NS |
294 | progname, strerror(errno)); |
295 | goto done; | |
296 | } | |
297 | dname = dpath; | |
298 | } | |
299 | if (!logname && getdev.log_subvol_dev) { | |
300 | strcpy(logpath, "/tmp/libxfslXXXXXX"); | |
301 | (void)mktemp(logpath); | |
302 | if (mknod(logpath, S_IFCHR | 0600, | |
303 | getdev.log_subvol_dev) < 0) { | |
9440d84d | 304 | fprintf(stderr, _("%s: mknod failed: %s\n"), |
2bd0ea18 NS |
305 | progname, strerror(errno)); |
306 | goto done; | |
307 | } | |
308 | logname = logpath; | |
309 | } | |
310 | if (!rtname && getdev.rt_subvol_dev) { | |
311 | strcpy(rtpath, "/tmp/libxfsrXXXXXX"); | |
312 | (void)mktemp(rtpath); | |
313 | if (mknod(rtpath, S_IFCHR | 0600, | |
314 | getdev.rt_subvol_dev) < 0) { | |
9440d84d | 315 | fprintf(stderr, _("%s: mknod failed: %s\n"), |
2bd0ea18 NS |
316 | progname, strerror(errno)); |
317 | goto done; | |
318 | } | |
319 | rtname = rtpath; | |
320 | } | |
321 | #endif | |
322 | } | |
323 | voldone: | |
324 | if (dname) { | |
325 | if (dname[0] != '/' && needcd) | |
326 | chdir(curdir); | |
327 | if (a->disfile) { | |
5000d01d | 328 | a->ddev= libxfs_device_open(dname, a->dcreat, readonly, |
c5907b96 | 329 | a->setblksize); |
2bd0ea18 NS |
330 | a->dfd = libxfs_device_to_fd(a->ddev); |
331 | } else { | |
f02037ce | 332 | if (!check_open(dname, flags, &rawfile, &blockfile)) |
2bd0ea18 NS |
333 | goto done; |
334 | a->ddev = libxfs_device_open(rawfile, | |
c5907b96 | 335 | a->dcreat, readonly, a->setblksize); |
2bd0ea18 | 336 | a->dfd = libxfs_device_to_fd(a->ddev); |
f02037ce NS |
337 | platform_findsizes(rawfile, a->dfd, |
338 | &a->dsize, &a->dbsize); | |
2bd0ea18 NS |
339 | } |
340 | needcd = 1; | |
341 | } else | |
342 | a->dsize = 0; | |
343 | if (logname) { | |
344 | if (logname[0] != '/' && needcd) | |
345 | chdir(curdir); | |
346 | if (a->lisfile) { | |
347 | a->logdev = libxfs_device_open(logname, | |
c5907b96 | 348 | a->lcreat, readonly, a->setblksize); |
2bd0ea18 NS |
349 | a->logfd = libxfs_device_to_fd(a->logdev); |
350 | } else { | |
f02037ce | 351 | if (!check_open(logname, flags, &rawfile, &blockfile)) |
2bd0ea18 NS |
352 | goto done; |
353 | a->logdev = libxfs_device_open(rawfile, | |
c5907b96 | 354 | a->lcreat, readonly, a->setblksize); |
2bd0ea18 | 355 | a->logfd = libxfs_device_to_fd(a->logdev); |
f02037ce NS |
356 | platform_findsizes(rawfile, a->logfd, |
357 | &a->logBBsize, &a->lbsize); | |
2bd0ea18 NS |
358 | } |
359 | needcd = 1; | |
360 | } else | |
361 | a->logBBsize = 0; | |
362 | if (rtname) { | |
363 | if (rtname[0] != '/' && needcd) | |
364 | chdir(curdir); | |
365 | if (a->risfile) { | |
366 | a->rtdev = libxfs_device_open(rtname, | |
c5907b96 | 367 | a->rcreat, readonly, a->setblksize); |
2bd0ea18 NS |
368 | a->rtfd = libxfs_device_to_fd(a->rtdev); |
369 | } else { | |
f02037ce | 370 | if (!check_open(rtname, flags, &rawfile, &blockfile)) |
2bd0ea18 NS |
371 | goto done; |
372 | a->rtdev = libxfs_device_open(rawfile, | |
c5907b96 | 373 | a->rcreat, readonly, a->setblksize); |
2bd0ea18 | 374 | a->rtfd = libxfs_device_to_fd(a->rtdev); |
f02037ce NS |
375 | platform_findsizes(rawfile, a->rtfd, |
376 | &a->rtsize, &a->rtbsize); | |
2bd0ea18 NS |
377 | } |
378 | needcd = 1; | |
379 | } else | |
380 | a->rtsize = 0; | |
381 | if (a->dsize < 0) { | |
9440d84d | 382 | fprintf(stderr, _("%s: can't get size for data subvolume\n"), |
2bd0ea18 NS |
383 | progname); |
384 | goto done; | |
385 | } | |
386 | if (a->logBBsize < 0) { | |
9440d84d | 387 | fprintf(stderr, _("%s: can't get size for log subvolume\n"), |
2bd0ea18 NS |
388 | progname); |
389 | goto done; | |
390 | } | |
391 | if (a->rtsize < 0) { | |
9440d84d | 392 | fprintf(stderr, _("%s: can't get size for realtime subvolume\n"), |
2bd0ea18 NS |
393 | progname); |
394 | goto done; | |
395 | } | |
396 | if (needcd) | |
397 | chdir(curdir); | |
398 | rval = 1; | |
399 | done: | |
400 | if (dpath[0]) | |
401 | unlink(dpath); | |
402 | if (logpath[0]) | |
403 | unlink(logpath); | |
404 | if (rtpath[0]) | |
405 | unlink(rtpath); | |
406 | if (fd >= 0) | |
407 | close(fd); | |
408 | if (!rval && a->ddev) | |
409 | libxfs_device_close(a->ddev); | |
410 | if (!rval && a->logdev) | |
411 | libxfs_device_close(a->logdev); | |
412 | if (!rval && a->rtdev) | |
413 | libxfs_device_close(a->rtdev); | |
414 | return rval; | |
415 | } | |
416 | ||
417 | ||
418 | /* | |
419 | * Initialize/destroy all of the zone allocators we use. | |
420 | */ | |
421 | static void | |
422 | manage_zones(int release) | |
423 | { | |
424 | extern xfs_zone_t *xfs_ili_zone; | |
425 | extern xfs_zone_t *xfs_inode_zone; | |
426 | extern xfs_zone_t *xfs_ifork_zone; | |
427 | extern xfs_zone_t *xfs_dabuf_zone; | |
428 | extern xfs_zone_t *xfs_buf_item_zone; | |
429 | extern xfs_zone_t *xfs_da_state_zone; | |
430 | extern xfs_zone_t *xfs_btree_cur_zone; | |
431 | extern xfs_zone_t *xfs_bmap_free_item_zone; | |
432 | extern void xfs_dir_startup(); | |
433 | ||
434 | if (release) { /* free zone allocation */ | |
435 | libxfs_free(xfs_inode_zone); | |
436 | libxfs_free(xfs_ifork_zone); | |
437 | libxfs_free(xfs_dabuf_zone); | |
438 | libxfs_free(xfs_buf_item_zone); | |
439 | libxfs_free(xfs_da_state_zone); | |
440 | libxfs_free(xfs_btree_cur_zone); | |
441 | libxfs_free(xfs_bmap_free_item_zone); | |
442 | return; | |
443 | } | |
444 | /* otherwise initialise zone allocation */ | |
445 | xfs_inode_zone = libxfs_zone_init(sizeof(xfs_inode_t), "xfs_inode"); | |
446 | xfs_ifork_zone = libxfs_zone_init(sizeof(xfs_ifork_t), "xfs_ifork"); | |
447 | xfs_dabuf_zone = libxfs_zone_init(sizeof(xfs_dabuf_t), "xfs_dabuf"); | |
448 | xfs_ili_zone = libxfs_zone_init( | |
449 | sizeof(xfs_inode_log_item_t), "xfs_inode_log_item"); | |
450 | xfs_buf_item_zone = libxfs_zone_init( | |
451 | sizeof(xfs_buf_log_item_t), "xfs_buf_log_item"); | |
452 | xfs_da_state_zone = libxfs_zone_init( | |
453 | sizeof(xfs_da_state_t), "xfs_da_state"); | |
454 | xfs_btree_cur_zone = libxfs_zone_init( | |
455 | sizeof(xfs_btree_cur_t), "xfs_btree_cur"); | |
456 | xfs_bmap_free_item_zone = libxfs_zone_init( | |
457 | sizeof(xfs_bmap_free_item_t), "xfs_bmap_free_item"); | |
458 | xfs_dir_startup(); | |
459 | } | |
460 | ||
461 | /* | |
462 | * Get the bitmap and summary inodes into the mount structure | |
463 | * at mount time. | |
464 | */ | |
465 | static int | |
466 | rtmount_inodes(xfs_mount_t *mp) | |
467 | { | |
468 | int error; | |
469 | xfs_sb_t *sbp; | |
470 | ||
471 | sbp = &mp->m_sb; | |
472 | if (sbp->sb_rbmino == NULLFSINO) | |
473 | return 0; | |
474 | error = libxfs_iread(mp, NULL, sbp->sb_rbmino, &mp->m_rbmip, 0); | |
475 | if (error) { | |
9440d84d NS |
476 | fprintf(stderr, |
477 | _("%s: cannot read realtime bitmap inode (%d)\n"), | |
2bd0ea18 NS |
478 | progname, error); |
479 | return error; | |
480 | } | |
481 | ASSERT(mp->m_rbmip != NULL); | |
482 | ASSERT(sbp->sb_rsumino != NULLFSINO); | |
483 | error = libxfs_iread(mp, NULL, sbp->sb_rsumino, &mp->m_rsumip, 0); | |
484 | if (error) { | |
9440d84d NS |
485 | fprintf(stderr, |
486 | _("%s: cannot read realtime summary inode (%d)\n"), | |
2bd0ea18 NS |
487 | progname, error); |
488 | return error; | |
489 | } | |
490 | ASSERT(mp->m_rsumip != NULL); | |
491 | return 0; | |
492 | } | |
493 | ||
b391b7cd NS |
494 | /* |
495 | * Initialize realtime fields in the mount structure. | |
496 | */ | |
497 | static int | |
498 | rtmount_init( | |
499 | xfs_mount_t *mp) /* file system mount structure */ | |
500 | { | |
501 | xfs_buf_t *bp; /* buffer for last block of subvolume */ | |
502 | xfs_daddr_t d; /* address of last block of subvolume */ | |
503 | xfs_sb_t *sbp; /* filesystem superblock copy in mount */ | |
504 | ||
505 | sbp = &mp->m_sb; | |
506 | if (sbp->sb_rblocks == 0) | |
507 | return 0; | |
38c82117 | 508 | if (mp->m_rtdev == 0) { |
9440d84d | 509 | fprintf(stderr, _("%s: filesystem has a realtime subvolume\n"), |
b391b7cd NS |
510 | progname); |
511 | return -1; | |
512 | } | |
513 | mp->m_rsumlevels = sbp->sb_rextslog + 1; | |
514 | mp->m_rsumsize = | |
515 | (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels * | |
516 | sbp->sb_rbmblocks; | |
517 | mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize); | |
518 | mp->m_rbmip = mp->m_rsumip = NULL; | |
519 | /* | |
520 | * Check that the realtime section is an ok size. | |
521 | */ | |
522 | d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks); | |
523 | if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) { | |
9440d84d NS |
524 | fprintf(stderr, _("%s: realtime init - %llu != %llu\n"), |
525 | progname, (unsigned long long) XFS_BB_TO_FSB(mp, d), | |
b391b7cd NS |
526 | (unsigned long long) mp->m_sb.sb_rblocks); |
527 | return -1; | |
528 | } | |
9440d84d NS |
529 | bp = libxfs_readbuf(mp->m_rtdev, |
530 | d - XFS_FSB_TO_BB(mp, 1), XFS_FSB_TO_BB(mp, 1), 0); | |
b391b7cd | 531 | if (bp == NULL) { |
9440d84d NS |
532 | fprintf(stderr, _("%s: realtime size check failed\n"), |
533 | progname); | |
b391b7cd NS |
534 | return -1; |
535 | } | |
536 | libxfs_putbuf(bp); | |
537 | return 0; | |
538 | } | |
539 | ||
34317449 | 540 | #define XFS_MOUNT_32BITINODES 0x1 |
2b288ccf | 541 | #define XFS_MOUNT_32BITINOOPT 0x2 |
2bd0ea18 NS |
542 | /* |
543 | * Mount structure initialization, provides a filled-in xfs_mount_t | |
544 | * such that the numerous XFS_* macros can be used. If dev is zero, | |
545 | * no IO will be performed (no size checks, read root inodes). | |
546 | */ | |
547 | xfs_mount_t * | |
548 | libxfs_mount( | |
549 | xfs_mount_t *mp, | |
550 | xfs_sb_t *sb, | |
551 | dev_t dev, | |
552 | dev_t logdev, | |
553 | dev_t rtdev, | |
4ca431fc | 554 | int flags) |
2bd0ea18 NS |
555 | { |
556 | xfs_daddr_t d; | |
557 | xfs_buf_t *bp; | |
558 | xfs_sb_t *sbp; | |
559 | size_t size; | |
560 | int error; | |
561 | ||
562 | mp->m_dev = dev; | |
563 | mp->m_rtdev = rtdev; | |
564 | mp->m_logdev = logdev; | |
2b288ccf | 565 | mp->m_flags = (XFS_MOUNT_32BITINODES|XFS_MOUNT_32BITINOOPT); |
2bd0ea18 NS |
566 | mp->m_sb = *sb; |
567 | sbp = &(mp->m_sb); | |
568 | manage_zones(0); | |
569 | ||
570 | libxfs_mount_common(mp, sb); | |
571 | ||
572 | libxfs_alloc_compute_maxlevels(mp); | |
573 | libxfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK); | |
574 | libxfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK); | |
575 | libxfs_ialloc_compute_maxlevels(mp); | |
576 | ||
577 | if (sbp->sb_imax_pct) { | |
578 | /* Make sure the maximum inode count is a multiple of the | |
579 | * units we allocate inodes in. | |
580 | */ | |
581 | mp->m_maxicount = (sbp->sb_dblocks * sbp->sb_imax_pct) / 100; | |
582 | mp->m_maxicount = ((mp->m_maxicount / mp->m_ialloc_blks) * | |
583 | mp->m_ialloc_blks) << sbp->sb_inopblog; | |
584 | } else | |
585 | mp->m_maxicount = 0; | |
586 | ||
587 | mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE; | |
588 | ||
949c0f10 NS |
589 | /* |
590 | * Set whether we're using stripe alignment. | |
591 | */ | |
592 | if (XFS_SB_VERSION_HASDALIGN(&mp->m_sb)) { | |
593 | mp->m_dalign = sbp->sb_unit; | |
594 | mp->m_swidth = sbp->sb_width; | |
595 | } | |
596 | ||
2bd0ea18 NS |
597 | /* |
598 | * Set whether we're using inode alignment. | |
599 | */ | |
600 | if (XFS_SB_VERSION_HASALIGN(&mp->m_sb) && | |
601 | mp->m_sb.sb_inoalignmt >= | |
602 | XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size)) | |
603 | mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1; | |
604 | else | |
605 | mp->m_inoalign_mask = 0; | |
606 | /* | |
607 | * If we are using stripe alignment, check whether | |
608 | * the stripe unit is a multiple of the inode alignment | |
609 | */ | |
610 | if ( mp->m_dalign | |
611 | && mp->m_inoalign_mask && !(mp->m_dalign & mp->m_inoalign_mask)) | |
612 | mp->m_sinoalign = mp->m_dalign; | |
613 | else | |
614 | mp->m_sinoalign = 0; | |
615 | ||
616 | /* | |
617 | * Check that the data (and log if separate) are an ok size. | |
618 | */ | |
9440d84d | 619 | d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks); |
2bd0ea18 | 620 | if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) { |
9440d84d | 621 | fprintf(stderr, _("%s: size check failed\n"), progname); |
4ca431fc NS |
622 | if (!(flags & LIBXFS_MOUNT_DEBUGGER)) |
623 | return NULL; | |
2bd0ea18 NS |
624 | } |
625 | ||
626 | /* Initialize the appropriate directory manager */ | |
627 | if (XFS_SB_VERSION_HASDIRV2(sbp)) | |
628 | libxfs_dir2_mount(mp); | |
629 | else | |
630 | libxfs_dir_mount(mp); | |
631 | ||
632 | /* Initialize the precomputed transaction reservations values */ | |
633 | libxfs_trans_init(mp); | |
634 | ||
635 | if (dev == 0) /* maxtrres, we have no device so leave now */ | |
636 | return mp; | |
637 | ||
9440d84d | 638 | bp = libxfs_readbuf(mp->m_dev, |
4ca431fc NS |
639 | d - XFS_FSS_TO_BB(mp, 1), XFS_FSS_TO_BB(mp, 1), |
640 | !(flags & LIBXFS_MOUNT_DEBUGGER)); | |
9440d84d NS |
641 | if (!bp) { |
642 | fprintf(stderr, _("%s: data size check failed\n"), progname); | |
4ca431fc NS |
643 | if (!(flags & LIBXFS_MOUNT_DEBUGGER)) |
644 | return NULL; | |
2bd0ea18 NS |
645 | } |
646 | libxfs_putbuf(bp); | |
647 | ||
648 | if (mp->m_logdev && mp->m_logdev != mp->m_dev) { | |
9440d84d | 649 | d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks); |
2bd0ea18 | 650 | if ( (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) || |
9440d84d NS |
651 | (!(bp = libxfs_readbuf(mp->m_logdev, |
652 | d - XFS_FSB_TO_BB(mp, 1), | |
4ca431fc NS |
653 | XFS_FSB_TO_BB(mp, 1), |
654 | !(flags & LIBXFS_MOUNT_DEBUGGER)))) ) { | |
9440d84d | 655 | fprintf(stderr, _("%s: log size checks failed\n"), |
2bd0ea18 | 656 | progname); |
4ca431fc NS |
657 | if (!(flags & LIBXFS_MOUNT_DEBUGGER)) |
658 | return NULL; | |
2bd0ea18 NS |
659 | } |
660 | libxfs_putbuf(bp); | |
661 | } | |
662 | ||
663 | /* Initialize realtime fields in the mount structure */ | |
74668075 | 664 | if (!(flags & LIBXFS_MOUNT_DEBUGGER) && rtmount_init(mp)) { |
9440d84d NS |
665 | fprintf(stderr, _("%s: realtime device init failed\n"), |
666 | progname); | |
4ca431fc | 667 | return NULL; |
2bd0ea18 NS |
668 | } |
669 | ||
670 | /* Allocate and initialize the per-ag data */ | |
671 | size = sbp->sb_agcount * sizeof(xfs_perag_t); | |
8ebd0722 | 672 | if (size && (mp->m_perag = calloc(size, 1)) == NULL) { |
9440d84d | 673 | fprintf(stderr, _("%s: failed to alloc %ld bytes: %s\n"), |
5b64e00a | 674 | progname, (long)size, strerror(errno)); |
2bd0ea18 NS |
675 | exit(1); |
676 | } | |
677 | ||
a33a9e62 NS |
678 | libxfs_initialize_perag(mp, sbp->sb_agcount); |
679 | ||
2bd0ea18 NS |
680 | /* |
681 | * mkfs calls mount before the root inode is allocated. | |
682 | */ | |
4ca431fc | 683 | if ((flags & LIBXFS_MOUNT_ROOTINOS) && sbp->sb_rootino != NULLFSINO) { |
2bd0ea18 NS |
684 | error = libxfs_iread(mp, NULL, sbp->sb_rootino, |
685 | &mp->m_rootip, 0); | |
686 | if (error) { | |
9440d84d | 687 | fprintf(stderr, _("%s: cannot read root inode (%d)\n"), |
2bd0ea18 | 688 | progname, error); |
4ca431fc NS |
689 | if (!(flags & LIBXFS_MOUNT_DEBUGGER)) |
690 | return NULL; | |
2bd0ea18 NS |
691 | } |
692 | ASSERT(mp->m_rootip != NULL); | |
693 | } | |
74668075 NS |
694 | if ((flags & LIBXFS_MOUNT_ROOTINOS) && |
695 | !(flags & LIBXFS_MOUNT_DEBUGGER) && | |
696 | rtmount_inodes(mp)) | |
697 | return NULL; | |
2bd0ea18 NS |
698 | return mp; |
699 | } | |
700 | ||
701 | /* | |
9440d84d | 702 | * Release any resource obtained during a mount. |
2bd0ea18 NS |
703 | */ |
704 | void | |
705 | libxfs_umount(xfs_mount_t *mp) | |
706 | { | |
707 | manage_zones(1); | |
fceb0d99 ES |
708 | if (mp->m_perag) { |
709 | int agno; | |
710 | for (agno = 0; agno < mp->m_maxagi; agno++) { | |
711 | if (mp->m_perag[agno].pagb_list) | |
712 | free(mp->m_perag[agno].pagb_list); | |
713 | } | |
714 | free(mp->m_perag); | |
715 | } | |
2bd0ea18 | 716 | } |