]>
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 | ||
c781939c RC |
173 | int |
174 | check_open(char *path, int flags,char **rawfile,char **blockfile){ | |
175 | ||
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 | ||
206 | ||
2bd0ea18 NS |
207 | |
208 | /* | |
209 | * libxfs initialization. | |
210 | * Caller gets a 0 on failure (and we print a message), 1 on success. | |
211 | */ | |
212 | int | |
213 | libxfs_init(libxfs_init_t *a) | |
214 | { | |
215 | char *blockfile; | |
216 | char curdir[MAXPATHLEN]; | |
217 | char *dname; | |
218 | char dpath[25]; | |
219 | int fd; | |
220 | char *logname; | |
221 | char logpath[25]; | |
222 | int needcd; | |
223 | char *rawfile; | |
224 | char *rtname; | |
225 | char rtpath[25]; | |
226 | int rval = 0; | |
227 | int readonly; | |
228 | int inactive; | |
c781939c | 229 | int flags; |
2bd0ea18 NS |
230 | |
231 | dpath[0] = logpath[0] = rtpath[0] = '\0'; | |
232 | dname = a->dname; | |
233 | logname = a->logname; | |
234 | rtname = a->rtname; | |
235 | a->ddev = a->logdev = a->rtdev = 0; | |
236 | a->dfd = a->logfd = a->rtfd = -1; | |
237 | a->dsize = a->logBBsize = a->logBBstart = a->rtsize = 0; | |
238 | ||
239 | (void)getcwd(curdir,MAXPATHLEN); | |
240 | needcd = 0; | |
241 | fd = -1; | |
242 | readonly = (a->isreadonly & LIBXFS_ISREADONLY); | |
243 | inactive = (a->isreadonly & LIBXFS_ISINACTIVE); | |
c781939c RC |
244 | flags = a->isreadonly; |
245 | ||
2bd0ea18 | 246 | if (a->volname) { |
c781939c RC |
247 | |
248 | if(!check_open(a->volname,flags,&rawfile,&blockfile)) | |
2bd0ea18 NS |
249 | goto done; |
250 | needcd = 1; | |
251 | fd = open(rawfile, O_RDONLY); | |
252 | #ifdef HAVE_VOLUME_MANAGER | |
253 | xlv_getdev_t getdev; | |
254 | if (ioctl(fd, DIOCGETVOLDEV, &getdev) < 0) | |
255 | #else | |
256 | if (1) | |
257 | #endif | |
258 | { | |
259 | if (a->notvolok) { | |
260 | dname = a->dname = a->volname; | |
261 | a->volname = NULL; | |
262 | goto voldone; | |
263 | } | |
9440d84d NS |
264 | fprintf(stderr, _("%s: " |
265 | "%s is not a volume device name\n"), | |
2bd0ea18 NS |
266 | progname, a->volname); |
267 | if (a->notvolmsg) | |
268 | fprintf(stderr, a->notvolmsg, a->volname); | |
269 | goto done; | |
270 | } | |
271 | #ifdef HAVE_VOLUME_MANAGER | |
272 | if (getdev.data_subvol_dev && dname) { | |
9440d84d NS |
273 | fprintf(stderr, _("%s: " |
274 | "%s has a data subvolume, cannot specify %s\n"), | |
2bd0ea18 NS |
275 | progname, a->volname, dname); |
276 | goto done; | |
277 | } | |
278 | if (getdev.log_subvol_dev && logname) { | |
9440d84d NS |
279 | fprintf(stderr, _("%s: " |
280 | "%s has a log subvolume, cannot specify %s\n"), | |
2bd0ea18 NS |
281 | progname, a->volname, logname); |
282 | goto done; | |
283 | } | |
284 | if (getdev.rt_subvol_dev && rtname) { | |
9440d84d NS |
285 | fprintf(stderr, _("%s: %s has a realtime subvolume, " |
286 | "cannot specify %s\n"), | |
2bd0ea18 NS |
287 | progname, a->volname, rtname); |
288 | goto done; | |
289 | } | |
290 | if (!dname && getdev.data_subvol_dev) { | |
291 | strcpy(dpath, "/tmp/libxfsdXXXXXX"); | |
292 | (void)mktemp(dpath); | |
293 | if (mknod(dpath, S_IFCHR | 0600, | |
294 | getdev.data_subvol_dev) < 0) { | |
9440d84d | 295 | fprintf(stderr, _("%s: mknod failed: %s\n"), |
2bd0ea18 NS |
296 | progname, strerror(errno)); |
297 | goto done; | |
298 | } | |
299 | dname = dpath; | |
300 | } | |
301 | if (!logname && getdev.log_subvol_dev) { | |
302 | strcpy(logpath, "/tmp/libxfslXXXXXX"); | |
303 | (void)mktemp(logpath); | |
304 | if (mknod(logpath, S_IFCHR | 0600, | |
305 | getdev.log_subvol_dev) < 0) { | |
9440d84d | 306 | fprintf(stderr, _("%s: mknod failed: %s\n"), |
2bd0ea18 NS |
307 | progname, strerror(errno)); |
308 | goto done; | |
309 | } | |
310 | logname = logpath; | |
311 | } | |
312 | if (!rtname && getdev.rt_subvol_dev) { | |
313 | strcpy(rtpath, "/tmp/libxfsrXXXXXX"); | |
314 | (void)mktemp(rtpath); | |
315 | if (mknod(rtpath, S_IFCHR | 0600, | |
316 | getdev.rt_subvol_dev) < 0) { | |
9440d84d | 317 | fprintf(stderr, _("%s: mknod failed: %s\n"), |
2bd0ea18 NS |
318 | progname, strerror(errno)); |
319 | goto done; | |
320 | } | |
321 | rtname = rtpath; | |
322 | } | |
323 | #endif | |
324 | } | |
325 | voldone: | |
326 | if (dname) { | |
327 | if (dname[0] != '/' && needcd) | |
328 | chdir(curdir); | |
329 | if (a->disfile) { | |
5000d01d | 330 | a->ddev= libxfs_device_open(dname, a->dcreat, readonly, |
c5907b96 | 331 | a->setblksize); |
2bd0ea18 NS |
332 | a->dfd = libxfs_device_to_fd(a->ddev); |
333 | } else { | |
c781939c | 334 | if(!check_open(dname,flags,&rawfile,&blockfile)) |
2bd0ea18 NS |
335 | goto done; |
336 | a->ddev = libxfs_device_open(rawfile, | |
c5907b96 | 337 | a->dcreat, readonly, a->setblksize); |
2bd0ea18 | 338 | a->dfd = libxfs_device_to_fd(a->ddev); |
93d9f139 | 339 | a->dsize = platform_findsize(rawfile); |
2bd0ea18 NS |
340 | } |
341 | needcd = 1; | |
342 | } else | |
343 | a->dsize = 0; | |
344 | if (logname) { | |
345 | if (logname[0] != '/' && needcd) | |
346 | chdir(curdir); | |
347 | if (a->lisfile) { | |
348 | a->logdev = libxfs_device_open(logname, | |
c5907b96 | 349 | a->lcreat, readonly, a->setblksize); |
2bd0ea18 NS |
350 | a->logfd = libxfs_device_to_fd(a->logdev); |
351 | } else { | |
c781939c | 352 | if(!check_open(logname,flags,&rawfile,&blockfile)) |
2bd0ea18 NS |
353 | goto done; |
354 | a->logdev = libxfs_device_open(rawfile, | |
c5907b96 | 355 | a->lcreat, readonly, a->setblksize); |
2bd0ea18 | 356 | a->logfd = libxfs_device_to_fd(a->logdev); |
93d9f139 | 357 | a->logBBsize = platform_findsize(rawfile); |
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 { | |
c781939c | 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); |
93d9f139 | 375 | a->rtsize = platform_findsize(rawfile); |
2bd0ea18 NS |
376 | } |
377 | needcd = 1; | |
378 | } else | |
379 | a->rtsize = 0; | |
380 | if (a->dsize < 0) { | |
9440d84d | 381 | fprintf(stderr, _("%s: can't get size for data subvolume\n"), |
2bd0ea18 NS |
382 | progname); |
383 | goto done; | |
384 | } | |
385 | if (a->logBBsize < 0) { | |
9440d84d | 386 | fprintf(stderr, _("%s: can't get size for log subvolume\n"), |
2bd0ea18 NS |
387 | progname); |
388 | goto done; | |
389 | } | |
390 | if (a->rtsize < 0) { | |
9440d84d | 391 | fprintf(stderr, _("%s: can't get size for realtime subvolume\n"), |
2bd0ea18 NS |
392 | progname); |
393 | goto done; | |
394 | } | |
395 | if (needcd) | |
396 | chdir(curdir); | |
397 | rval = 1; | |
398 | done: | |
399 | if (dpath[0]) | |
400 | unlink(dpath); | |
401 | if (logpath[0]) | |
402 | unlink(logpath); | |
403 | if (rtpath[0]) | |
404 | unlink(rtpath); | |
405 | if (fd >= 0) | |
406 | close(fd); | |
407 | if (!rval && a->ddev) | |
408 | libxfs_device_close(a->ddev); | |
409 | if (!rval && a->logdev) | |
410 | libxfs_device_close(a->logdev); | |
411 | if (!rval && a->rtdev) | |
412 | libxfs_device_close(a->rtdev); | |
413 | return rval; | |
414 | } | |
415 | ||
416 | ||
417 | /* | |
418 | * Initialize/destroy all of the zone allocators we use. | |
419 | */ | |
420 | static void | |
421 | manage_zones(int release) | |
422 | { | |
423 | extern xfs_zone_t *xfs_ili_zone; | |
424 | extern xfs_zone_t *xfs_inode_zone; | |
425 | extern xfs_zone_t *xfs_ifork_zone; | |
426 | extern xfs_zone_t *xfs_dabuf_zone; | |
427 | extern xfs_zone_t *xfs_buf_item_zone; | |
428 | extern xfs_zone_t *xfs_da_state_zone; | |
429 | extern xfs_zone_t *xfs_btree_cur_zone; | |
430 | extern xfs_zone_t *xfs_bmap_free_item_zone; | |
431 | extern void xfs_dir_startup(); | |
432 | ||
433 | if (release) { /* free zone allocation */ | |
434 | libxfs_free(xfs_inode_zone); | |
435 | libxfs_free(xfs_ifork_zone); | |
436 | libxfs_free(xfs_dabuf_zone); | |
437 | libxfs_free(xfs_buf_item_zone); | |
438 | libxfs_free(xfs_da_state_zone); | |
439 | libxfs_free(xfs_btree_cur_zone); | |
440 | libxfs_free(xfs_bmap_free_item_zone); | |
441 | return; | |
442 | } | |
443 | /* otherwise initialise zone allocation */ | |
444 | xfs_inode_zone = libxfs_zone_init(sizeof(xfs_inode_t), "xfs_inode"); | |
445 | xfs_ifork_zone = libxfs_zone_init(sizeof(xfs_ifork_t), "xfs_ifork"); | |
446 | xfs_dabuf_zone = libxfs_zone_init(sizeof(xfs_dabuf_t), "xfs_dabuf"); | |
447 | xfs_ili_zone = libxfs_zone_init( | |
448 | sizeof(xfs_inode_log_item_t), "xfs_inode_log_item"); | |
449 | xfs_buf_item_zone = libxfs_zone_init( | |
450 | sizeof(xfs_buf_log_item_t), "xfs_buf_log_item"); | |
451 | xfs_da_state_zone = libxfs_zone_init( | |
452 | sizeof(xfs_da_state_t), "xfs_da_state"); | |
453 | xfs_btree_cur_zone = libxfs_zone_init( | |
454 | sizeof(xfs_btree_cur_t), "xfs_btree_cur"); | |
455 | xfs_bmap_free_item_zone = libxfs_zone_init( | |
456 | sizeof(xfs_bmap_free_item_t), "xfs_bmap_free_item"); | |
457 | xfs_dir_startup(); | |
458 | } | |
459 | ||
460 | /* | |
461 | * Get the bitmap and summary inodes into the mount structure | |
462 | * at mount time. | |
463 | */ | |
464 | static int | |
465 | rtmount_inodes(xfs_mount_t *mp) | |
466 | { | |
467 | int error; | |
468 | xfs_sb_t *sbp; | |
469 | ||
470 | sbp = &mp->m_sb; | |
471 | if (sbp->sb_rbmino == NULLFSINO) | |
472 | return 0; | |
473 | error = libxfs_iread(mp, NULL, sbp->sb_rbmino, &mp->m_rbmip, 0); | |
474 | if (error) { | |
9440d84d NS |
475 | fprintf(stderr, |
476 | _("%s: cannot read realtime bitmap inode (%d)\n"), | |
2bd0ea18 NS |
477 | progname, error); |
478 | return error; | |
479 | } | |
480 | ASSERT(mp->m_rbmip != NULL); | |
481 | ASSERT(sbp->sb_rsumino != NULLFSINO); | |
482 | error = libxfs_iread(mp, NULL, sbp->sb_rsumino, &mp->m_rsumip, 0); | |
483 | if (error) { | |
9440d84d NS |
484 | fprintf(stderr, |
485 | _("%s: cannot read realtime summary inode (%d)\n"), | |
2bd0ea18 NS |
486 | progname, error); |
487 | return error; | |
488 | } | |
489 | ASSERT(mp->m_rsumip != NULL); | |
490 | return 0; | |
491 | } | |
492 | ||
b391b7cd NS |
493 | /* |
494 | * Initialize realtime fields in the mount structure. | |
495 | */ | |
496 | static int | |
497 | rtmount_init( | |
498 | xfs_mount_t *mp) /* file system mount structure */ | |
499 | { | |
500 | xfs_buf_t *bp; /* buffer for last block of subvolume */ | |
501 | xfs_daddr_t d; /* address of last block of subvolume */ | |
502 | xfs_sb_t *sbp; /* filesystem superblock copy in mount */ | |
503 | ||
504 | sbp = &mp->m_sb; | |
505 | if (sbp->sb_rblocks == 0) | |
506 | return 0; | |
38c82117 | 507 | if (mp->m_rtdev == 0) { |
9440d84d | 508 | fprintf(stderr, _("%s: filesystem has a realtime subvolume\n"), |
b391b7cd NS |
509 | progname); |
510 | return -1; | |
511 | } | |
512 | mp->m_rsumlevels = sbp->sb_rextslog + 1; | |
513 | mp->m_rsumsize = | |
514 | (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels * | |
515 | sbp->sb_rbmblocks; | |
516 | mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize); | |
517 | mp->m_rbmip = mp->m_rsumip = NULL; | |
518 | /* | |
519 | * Check that the realtime section is an ok size. | |
520 | */ | |
521 | d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks); | |
522 | if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) { | |
9440d84d NS |
523 | fprintf(stderr, _("%s: realtime init - %llu != %llu\n"), |
524 | progname, (unsigned long long) XFS_BB_TO_FSB(mp, d), | |
b391b7cd NS |
525 | (unsigned long long) mp->m_sb.sb_rblocks); |
526 | return -1; | |
527 | } | |
9440d84d NS |
528 | bp = libxfs_readbuf(mp->m_rtdev, |
529 | d - XFS_FSB_TO_BB(mp, 1), XFS_FSB_TO_BB(mp, 1), 0); | |
b391b7cd | 530 | if (bp == NULL) { |
9440d84d NS |
531 | fprintf(stderr, _("%s: realtime size check failed\n"), |
532 | progname); | |
b391b7cd NS |
533 | return -1; |
534 | } | |
535 | libxfs_putbuf(bp); | |
536 | return 0; | |
537 | } | |
538 | ||
34317449 | 539 | #define XFS_MOUNT_32BITINODES 0x1 |
2bd0ea18 NS |
540 | /* |
541 | * Mount structure initialization, provides a filled-in xfs_mount_t | |
542 | * such that the numerous XFS_* macros can be used. If dev is zero, | |
543 | * no IO will be performed (no size checks, read root inodes). | |
544 | */ | |
545 | xfs_mount_t * | |
546 | libxfs_mount( | |
547 | xfs_mount_t *mp, | |
548 | xfs_sb_t *sb, | |
549 | dev_t dev, | |
550 | dev_t logdev, | |
551 | dev_t rtdev, | |
4ca431fc | 552 | int flags) |
2bd0ea18 NS |
553 | { |
554 | xfs_daddr_t d; | |
555 | xfs_buf_t *bp; | |
556 | xfs_sb_t *sbp; | |
557 | size_t size; | |
558 | int error; | |
559 | ||
560 | mp->m_dev = dev; | |
561 | mp->m_rtdev = rtdev; | |
562 | mp->m_logdev = logdev; | |
34317449 | 563 | mp->m_flags = XFS_MOUNT_32BITINODES; |
2bd0ea18 NS |
564 | mp->m_sb = *sb; |
565 | sbp = &(mp->m_sb); | |
566 | manage_zones(0); | |
567 | ||
568 | libxfs_mount_common(mp, sb); | |
569 | ||
570 | libxfs_alloc_compute_maxlevels(mp); | |
571 | libxfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK); | |
572 | libxfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK); | |
573 | libxfs_ialloc_compute_maxlevels(mp); | |
574 | ||
575 | if (sbp->sb_imax_pct) { | |
576 | /* Make sure the maximum inode count is a multiple of the | |
577 | * units we allocate inodes in. | |
578 | */ | |
579 | mp->m_maxicount = (sbp->sb_dblocks * sbp->sb_imax_pct) / 100; | |
580 | mp->m_maxicount = ((mp->m_maxicount / mp->m_ialloc_blks) * | |
581 | mp->m_ialloc_blks) << sbp->sb_inopblog; | |
582 | } else | |
583 | mp->m_maxicount = 0; | |
584 | ||
585 | mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE; | |
586 | ||
587 | /* | |
588 | * Set whether we're using inode alignment. | |
589 | */ | |
590 | if (XFS_SB_VERSION_HASALIGN(&mp->m_sb) && | |
591 | mp->m_sb.sb_inoalignmt >= | |
592 | XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size)) | |
593 | mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1; | |
594 | else | |
595 | mp->m_inoalign_mask = 0; | |
596 | /* | |
597 | * If we are using stripe alignment, check whether | |
598 | * the stripe unit is a multiple of the inode alignment | |
599 | */ | |
600 | if ( mp->m_dalign | |
601 | && mp->m_inoalign_mask && !(mp->m_dalign & mp->m_inoalign_mask)) | |
602 | mp->m_sinoalign = mp->m_dalign; | |
603 | else | |
604 | mp->m_sinoalign = 0; | |
605 | ||
606 | /* | |
607 | * Check that the data (and log if separate) are an ok size. | |
608 | */ | |
9440d84d | 609 | d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks); |
2bd0ea18 | 610 | if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) { |
9440d84d | 611 | fprintf(stderr, _("%s: size check failed\n"), progname); |
4ca431fc NS |
612 | if (!(flags & LIBXFS_MOUNT_DEBUGGER)) |
613 | return NULL; | |
2bd0ea18 NS |
614 | } |
615 | ||
616 | /* Initialize the appropriate directory manager */ | |
617 | if (XFS_SB_VERSION_HASDIRV2(sbp)) | |
618 | libxfs_dir2_mount(mp); | |
619 | else | |
620 | libxfs_dir_mount(mp); | |
621 | ||
622 | /* Initialize the precomputed transaction reservations values */ | |
623 | libxfs_trans_init(mp); | |
624 | ||
625 | if (dev == 0) /* maxtrres, we have no device so leave now */ | |
626 | return mp; | |
627 | ||
9440d84d | 628 | bp = libxfs_readbuf(mp->m_dev, |
4ca431fc NS |
629 | d - XFS_FSS_TO_BB(mp, 1), XFS_FSS_TO_BB(mp, 1), |
630 | !(flags & LIBXFS_MOUNT_DEBUGGER)); | |
9440d84d NS |
631 | if (!bp) { |
632 | fprintf(stderr, _("%s: data size check failed\n"), progname); | |
4ca431fc NS |
633 | if (!(flags & LIBXFS_MOUNT_DEBUGGER)) |
634 | return NULL; | |
2bd0ea18 NS |
635 | } |
636 | libxfs_putbuf(bp); | |
637 | ||
638 | if (mp->m_logdev && mp->m_logdev != mp->m_dev) { | |
9440d84d | 639 | d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks); |
2bd0ea18 | 640 | if ( (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) || |
9440d84d NS |
641 | (!(bp = libxfs_readbuf(mp->m_logdev, |
642 | d - XFS_FSB_TO_BB(mp, 1), | |
4ca431fc NS |
643 | XFS_FSB_TO_BB(mp, 1), |
644 | !(flags & LIBXFS_MOUNT_DEBUGGER)))) ) { | |
9440d84d | 645 | fprintf(stderr, _("%s: log size checks failed\n"), |
2bd0ea18 | 646 | progname); |
4ca431fc NS |
647 | if (!(flags & LIBXFS_MOUNT_DEBUGGER)) |
648 | return NULL; | |
2bd0ea18 NS |
649 | } |
650 | libxfs_putbuf(bp); | |
651 | } | |
652 | ||
653 | /* Initialize realtime fields in the mount structure */ | |
b391b7cd | 654 | if (rtmount_init(mp)) { |
9440d84d NS |
655 | fprintf(stderr, _("%s: realtime device init failed\n"), |
656 | progname); | |
4ca431fc NS |
657 | if (!(flags & LIBXFS_MOUNT_DEBUGGER)) |
658 | return NULL; | |
2bd0ea18 NS |
659 | } |
660 | ||
661 | /* Allocate and initialize the per-ag data */ | |
662 | size = sbp->sb_agcount * sizeof(xfs_perag_t); | |
663 | if ((mp->m_perag = calloc(size, 1)) == NULL) { | |
9440d84d | 664 | fprintf(stderr, _("%s: failed to alloc %ld bytes: %s\n"), |
5b64e00a | 665 | progname, (long)size, strerror(errno)); |
2bd0ea18 NS |
666 | exit(1); |
667 | } | |
668 | ||
a33a9e62 NS |
669 | libxfs_initialize_perag(mp, sbp->sb_agcount); |
670 | ||
2bd0ea18 NS |
671 | /* |
672 | * mkfs calls mount before the root inode is allocated. | |
673 | */ | |
4ca431fc | 674 | if ((flags & LIBXFS_MOUNT_ROOTINOS) && sbp->sb_rootino != NULLFSINO) { |
2bd0ea18 NS |
675 | error = libxfs_iread(mp, NULL, sbp->sb_rootino, |
676 | &mp->m_rootip, 0); | |
677 | if (error) { | |
9440d84d | 678 | fprintf(stderr, _("%s: cannot read root inode (%d)\n"), |
2bd0ea18 | 679 | progname, error); |
4ca431fc NS |
680 | if (!(flags & LIBXFS_MOUNT_DEBUGGER)) |
681 | return NULL; | |
2bd0ea18 NS |
682 | } |
683 | ASSERT(mp->m_rootip != NULL); | |
684 | } | |
4ca431fc NS |
685 | if ((flags & LIBXFS_MOUNT_ROOTINOS) && rtmount_inodes(mp)) |
686 | if (!(flags & LIBXFS_MOUNT_DEBUGGER)) | |
687 | return NULL; | |
2bd0ea18 NS |
688 | return mp; |
689 | } | |
690 | ||
691 | /* | |
9440d84d | 692 | * Release any resource obtained during a mount. |
2bd0ea18 NS |
693 | */ |
694 | void | |
695 | libxfs_umount(xfs_mount_t *mp) | |
696 | { | |
697 | manage_zones(1); | |
698 | free(mp->m_perag); | |
699 | } |