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