]>
Commit | Line | Data |
---|---|---|
2bd0ea18 | 1 | /* |
9911e5dc | 2 | * Copyright (c) 2000 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 | |
14 | * or the like. Any license provided herein, whether implied or | |
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 | ||
50 | char *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 */ | |
56 | int nextfakedev = -1; /* device number to give to next fake device */ | |
57 | static struct dev_to_fd { | |
58 | dev_t dev; | |
59 | int fd; | |
60 | } dev_map[MAX_DEVS]={{0}}; | |
61 | ||
62 | static int | |
fc8202ba | 63 | check_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). | |
84 | * | |
85 | * Useful to tools which will produce uncertain results | |
86 | * if the filesystem is active - repair, check, logprint. | |
87 | */ | |
88 | static int | |
89 | check_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 | ||
131 | static __int64_t | |
132 | findsize(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 | ||
180 | /* libxfs_device_to_fd: | |
181 | * lookup a device number in the device map | |
182 | * return the associated fd | |
183 | */ | |
184 | int | |
185 | libxfs_device_to_fd(dev_t device) | |
186 | { | |
187 | int d; | |
188 | ||
189 | for (d=0;d<MAX_DEVS;d++) | |
190 | if (dev_map[d].dev == device) | |
191 | return dev_map[d].fd; | |
192 | ||
5b64e00a NS |
193 | fprintf(stderr, "%s: device_to_fd: device %lld is not open\n", |
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 | */ | |
201 | dev_t | |
c5907b96 | 202 | libxfs_device_open(char *path, int creat, int readonly, int setblksize) |
2bd0ea18 NS |
203 | { |
204 | int fd; | |
205 | dev_t dev; | |
206 | int d; | |
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 NS |
243 | */ |
244 | dev=(statb.st_rdev)?(statb.st_rdev):(nextfakedev--); | |
245 | ||
246 | for (d=0;d<MAX_DEVS;d++) | |
247 | if (dev_map[d].dev == dev) { | |
5b64e00a NS |
248 | fprintf(stderr, "%s: device %lld is already open\n", |
249 | progname, (long long)dev); | |
2bd0ea18 NS |
250 | exit(1); |
251 | } | |
252 | ||
253 | for (d=0;d<MAX_DEVS;d++) | |
254 | if (!dev_map[d].dev) { | |
255 | dev_map[d].dev=dev; | |
256 | dev_map[d].fd=fd; | |
257 | ||
258 | return dev; | |
259 | } | |
260 | ||
261 | fprintf(stderr, "%s: device_open: too many open devices\n", progname); | |
262 | exit(1); | |
263 | } | |
264 | ||
265 | void | |
266 | libxfs_device_close(dev_t dev) | |
267 | { | |
268 | int d; | |
269 | ||
270 | for (d=0;d<MAX_DEVS;d++) | |
271 | if (dev_map[d].dev == dev) { | |
272 | int fd; | |
273 | ||
274 | fd=dev_map[d].dev; | |
275 | dev_map[d].dev=dev_map[d].fd=0; | |
276 | ||
277 | fsync(fd); | |
278 | ioctl(fd, BLKFLSBUF, 0); | |
279 | close(fd); | |
280 | ||
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 | */ | |
295 | int | |
296 | libxfs_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 | } | |
425 | voldone: | |
426 | if (dname) { | |
427 | if (dname[0] != '/' && needcd) | |
428 | chdir(curdir); | |
429 | if (a->disfile) { | |
c5907b96 MP |
430 | a->ddev= libxfs_device_open(dname, a->dcreat, readonly, |
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; | |
555 | done: | |
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 | */ | |
577 | static void | |
578 | manage_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 | */ | |
621 | static int | |
622 | rtmount_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 | ||
34317449 | 648 | #define XFS_MOUNT_32BITINODES 0x1 |
2bd0ea18 NS |
649 | /* |
650 | * Mount structure initialization, provides a filled-in xfs_mount_t | |
651 | * such that the numerous XFS_* macros can be used. If dev is zero, | |
652 | * no IO will be performed (no size checks, read root inodes). | |
653 | */ | |
654 | xfs_mount_t * | |
655 | libxfs_mount( | |
656 | xfs_mount_t *mp, | |
657 | xfs_sb_t *sb, | |
658 | dev_t dev, | |
659 | dev_t logdev, | |
660 | dev_t rtdev, | |
661 | int rrootinos) | |
662 | { | |
663 | xfs_daddr_t d; | |
664 | xfs_buf_t *bp; | |
665 | xfs_sb_t *sbp; | |
666 | size_t size; | |
667 | int error; | |
668 | ||
669 | mp->m_dev = dev; | |
670 | mp->m_rtdev = rtdev; | |
671 | mp->m_logdev = logdev; | |
34317449 | 672 | mp->m_flags = XFS_MOUNT_32BITINODES; |
2bd0ea18 NS |
673 | mp->m_sb = *sb; |
674 | sbp = &(mp->m_sb); | |
675 | manage_zones(0); | |
676 | ||
677 | libxfs_mount_common(mp, sb); | |
678 | ||
679 | libxfs_alloc_compute_maxlevels(mp); | |
680 | libxfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK); | |
681 | libxfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK); | |
682 | libxfs_ialloc_compute_maxlevels(mp); | |
683 | ||
684 | if (sbp->sb_imax_pct) { | |
685 | /* Make sure the maximum inode count is a multiple of the | |
686 | * units we allocate inodes in. | |
687 | */ | |
688 | mp->m_maxicount = (sbp->sb_dblocks * sbp->sb_imax_pct) / 100; | |
689 | mp->m_maxicount = ((mp->m_maxicount / mp->m_ialloc_blks) * | |
690 | mp->m_ialloc_blks) << sbp->sb_inopblog; | |
691 | } else | |
692 | mp->m_maxicount = 0; | |
693 | ||
694 | mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE; | |
695 | ||
696 | /* | |
697 | * Set whether we're using inode alignment. | |
698 | */ | |
699 | if (XFS_SB_VERSION_HASALIGN(&mp->m_sb) && | |
700 | mp->m_sb.sb_inoalignmt >= | |
701 | XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size)) | |
702 | mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1; | |
703 | else | |
704 | mp->m_inoalign_mask = 0; | |
705 | /* | |
706 | * If we are using stripe alignment, check whether | |
707 | * the stripe unit is a multiple of the inode alignment | |
708 | */ | |
709 | if ( mp->m_dalign | |
710 | && mp->m_inoalign_mask && !(mp->m_dalign & mp->m_inoalign_mask)) | |
711 | mp->m_sinoalign = mp->m_dalign; | |
712 | else | |
713 | mp->m_sinoalign = 0; | |
714 | ||
715 | /* | |
716 | * Check that the data (and log if separate) are an ok size. | |
717 | */ | |
718 | d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks); | |
719 | if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) { | |
720 | fprintf(stderr, "%s: size check failed\n", progname); | |
721 | return NULL; | |
722 | } | |
723 | ||
724 | /* Initialize the appropriate directory manager */ | |
725 | if (XFS_SB_VERSION_HASDIRV2(sbp)) | |
726 | libxfs_dir2_mount(mp); | |
727 | else | |
728 | libxfs_dir_mount(mp); | |
729 | ||
730 | /* Initialize the precomputed transaction reservations values */ | |
731 | libxfs_trans_init(mp); | |
732 | ||
733 | if (dev == 0) /* maxtrres, we have no device so leave now */ | |
734 | return mp; | |
735 | ||
736 | bp = libxfs_readbuf(mp->m_dev, d - 1, 1, 0); | |
737 | if (bp == NULL) { | |
738 | fprintf(stderr, "%s: data size check failed\n", progname); | |
739 | return NULL; | |
740 | } | |
741 | libxfs_putbuf(bp); | |
742 | ||
743 | if (mp->m_logdev && mp->m_logdev != mp->m_dev) { | |
744 | d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks); | |
745 | if ( (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) || | |
746 | (!(bp = libxfs_readbuf(mp->m_logdev, d - 1, 1, 1)))) { | |
747 | fprintf(stderr, "%s: log size checks failed\n", | |
748 | progname); | |
749 | return NULL; | |
750 | } | |
751 | libxfs_putbuf(bp); | |
752 | } | |
753 | ||
754 | /* Initialize realtime fields in the mount structure */ | |
755 | if (libxfs_rtmount_init(mp)) { | |
756 | fprintf(stderr, "%s: real-time device init failed\n", progname); | |
757 | return NULL; | |
758 | } | |
759 | ||
760 | /* Allocate and initialize the per-ag data */ | |
761 | size = sbp->sb_agcount * sizeof(xfs_perag_t); | |
762 | if ((mp->m_perag = calloc(size, 1)) == NULL) { | |
5b64e00a NS |
763 | fprintf(stderr, "%s: failed to alloc %ld bytes: %s\n", |
764 | progname, (long)size, strerror(errno)); | |
2bd0ea18 NS |
765 | exit(1); |
766 | } | |
767 | ||
a33a9e62 NS |
768 | libxfs_initialize_perag(mp, sbp->sb_agcount); |
769 | ||
2bd0ea18 NS |
770 | /* |
771 | * mkfs calls mount before the root inode is allocated. | |
772 | */ | |
773 | if (rrootinos && sbp->sb_rootino != NULLFSINO) { | |
774 | error = libxfs_iread(mp, NULL, sbp->sb_rootino, | |
775 | &mp->m_rootip, 0); | |
776 | if (error) { | |
777 | fprintf(stderr, "%s: cannot read root inode (%d)\n", | |
778 | progname, error); | |
779 | return NULL; | |
780 | } | |
781 | ASSERT(mp->m_rootip != NULL); | |
782 | } | |
783 | if (rrootinos && rtmount_inodes(mp)) | |
784 | return NULL; | |
785 | return mp; | |
786 | } | |
787 | ||
788 | /* | |
789 | * Release any resourse obtained during a mount. | |
790 | */ | |
791 | void | |
792 | libxfs_umount(xfs_mount_t *mp) | |
793 | { | |
794 | manage_zones(1); | |
795 | free(mp->m_perag); | |
796 | } |