]>
Commit | Line | Data |
---|---|---|
959ef981 | 1 | // SPDX-License-Identifier: GPL-2.0 |
2bd0ea18 | 2 | /* |
da23017d NS |
3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
4 | * All Rights Reserved. | |
2bd0ea18 NS |
5 | */ |
6 | ||
2bd0ea18 | 7 | #include <sys/stat.h> |
9440d84d | 8 | #include "init.h" |
29e62271 | 9 | |
9c799827 | 10 | #include "libxfs_priv.h" |
b626fb59 DC |
11 | #include "xfs_fs.h" |
12 | #include "xfs_shared.h" | |
13 | #include "xfs_format.h" | |
14 | #include "xfs_log_format.h" | |
15 | #include "xfs_trans_resv.h" | |
16 | #include "xfs_mount.h" | |
794a5604 | 17 | #include "xfs_defer.h" |
b626fb59 DC |
18 | #include "xfs_inode_buf.h" |
19 | #include "xfs_inode_fork.h" | |
20 | #include "xfs_inode.h" | |
21 | #include "xfs_trans.h" | |
b3a96b46 | 22 | #include "xfs_rmap_btree.h" |
e7be6330 | 23 | #include "xfs_refcount_btree.h" |
b626fb59 | 24 | |
6b803e5a | 25 | #include "libxfs.h" /* for now */ |
b626fb59 | 26 | |
2bd0ea18 NS |
27 | char *progname = "libxfs"; /* default, changed by each tool */ |
28 | ||
f1b058f9 | 29 | struct cache *libxfs_bcache; /* global buffer cache */ |
9f38f08d | 30 | int libxfs_bhash_size; /* #buckets in bcache */ |
f1b058f9 | 31 | |
d0572de5 BN |
32 | int use_xfs_buf_lock; /* global flag: use xfs_buf_t locks for MT */ |
33 | ||
44488491 | 34 | static int manage_zones(int); /* setup/teardown global zones */ |
f1b058f9 | 35 | |
2bd0ea18 NS |
36 | /* |
37 | * dev_map - map open devices to fd. | |
38 | */ | |
39 | #define MAX_DEVS 10 /* arbitary maximum */ | |
00ff2b10 | 40 | static int nextfakedev = -1; /* device number to give to next fake device */ |
2bd0ea18 | 41 | static struct dev_to_fd { |
9440d84d NS |
42 | dev_t dev; |
43 | int fd; | |
2bd0ea18 NS |
44 | } dev_map[MAX_DEVS]={{0}}; |
45 | ||
2bd0ea18 NS |
46 | /* |
47 | * Checks whether a given device has a mounted, writable | |
48 | * filesystem, returns 1 if it does & fatal (just warns | |
49 | * if not fatal, but allows us to proceed). | |
5000d01d | 50 | * |
2bd0ea18 NS |
51 | * Useful to tools which will produce uncertain results |
52 | * if the filesystem is active - repair, check, logprint. | |
53 | */ | |
54 | static int | |
55 | check_isactive(char *name, char *block, int fatal) | |
56 | { | |
f594a0d1 | 57 | struct stat st; |
2bd0ea18 | 58 | |
f594a0d1 | 59 | if (stat(block, &st) < 0) |
9440d84d | 60 | return 0; |
fc8202ba | 61 | if ((st.st_mode & S_IFMT) != S_IFBLK) |
9440d84d | 62 | return 0; |
93d9f139 | 63 | if (platform_check_ismounted(name, block, &st, 0) == 0) |
9440d84d | 64 | return 0; |
7f510afb ES |
65 | if (platform_check_iswritable(name, block, &st)) |
66 | return fatal ? 1 : 0; | |
67 | return 0; | |
2bd0ea18 NS |
68 | } |
69 | ||
5000d01d | 70 | /* libxfs_device_to_fd: |
2bd0ea18 NS |
71 | * lookup a device number in the device map |
72 | * return the associated fd | |
73 | */ | |
74 | int | |
75 | libxfs_device_to_fd(dev_t device) | |
76 | { | |
9440d84d | 77 | int d; |
5000d01d | 78 | |
9440d84d | 79 | for (d = 0; d < MAX_DEVS; d++) |
5000d01d | 80 | if (dev_map[d].dev == device) |
2bd0ea18 | 81 | return dev_map[d].fd; |
5000d01d | 82 | |
9440d84d NS |
83 | fprintf(stderr, _("%s: %s: device %lld is not open\n"), |
84 | progname, __FUNCTION__, (long long)device); | |
2bd0ea18 | 85 | exit(1); |
25d246df | 86 | /* NOTREACHED */ |
2bd0ea18 NS |
87 | } |
88 | ||
89 | /* libxfs_device_open: | |
90 | * open a device and return its device number | |
91 | */ | |
92 | dev_t | |
7eb6693f | 93 | libxfs_device_open(char *path, int creat, int xflags, int setblksize) |
2bd0ea18 | 94 | { |
2bd0ea18 | 95 | dev_t dev; |
7eb6693f | 96 | int fd, d, flags; |
b74a1f6a | 97 | int readonly, dio, excl; |
f594a0d1 | 98 | struct stat statb; |
7eb6693f NS |
99 | |
100 | readonly = (xflags & LIBXFS_ISREADONLY); | |
101 | excl = (xflags & LIBXFS_EXCLUSIVELY) && !creat; | |
b74a1f6a | 102 | dio = (xflags & LIBXFS_DIRECT) && !creat && platform_direct_blockdev(); |
2bd0ea18 | 103 | |
b74a1f6a | 104 | retry: |
7eb6693f NS |
105 | flags = (readonly ? O_RDONLY : O_RDWR) | \ |
106 | (creat ? (O_CREAT|O_TRUNC) : 0) | \ | |
b74a1f6a | 107 | (dio ? O_DIRECT : 0) | \ |
7eb6693f | 108 | (excl ? O_EXCL : 0); |
b74a1f6a | 109 | |
7eb6693f | 110 | if ((fd = open(path, flags, 0666)) < 0) { |
b74a1f6a NS |
111 | if (errno == EINVAL && --dio == 0) |
112 | goto retry; | |
9440d84d | 113 | fprintf(stderr, _("%s: cannot open %s: %s\n"), |
2bd0ea18 NS |
114 | progname, path, strerror(errno)); |
115 | exit(1); | |
116 | } | |
117 | ||
f594a0d1 | 118 | if (fstat(fd, &statb) < 0) { |
9440d84d | 119 | fprintf(stderr, _("%s: cannot stat %s: %s\n"), |
2bd0ea18 NS |
120 | progname, path, strerror(errno)); |
121 | exit(1); | |
122 | } | |
a33a9e62 | 123 | |
edd45774 TS |
124 | if (!readonly && setblksize && (statb.st_mode & S_IFMT) == S_IFBLK) { |
125 | if (setblksize == 1) | |
126 | /* use the default blocksize */ | |
127 | (void)platform_set_blocksize(fd, path, statb.st_rdev, XFS_MIN_SECTORSIZE, 0); | |
128 | else { | |
129 | /* given an explicit blocksize to use */ | |
130 | if (platform_set_blocksize(fd, path, statb.st_rdev, setblksize, 1)) | |
131 | exit(1); | |
132 | } | |
133 | } | |
2bd0ea18 | 134 | |
a33a9e62 NS |
135 | /* |
136 | * Get the device number from the stat buf - unless | |
2bd0ea18 | 137 | * we're not opening a real device, in which case |
a33a9e62 | 138 | * choose a new fake device number. |
2bd0ea18 | 139 | */ |
32181a02 | 140 | dev = (statb.st_rdev) ? (statb.st_rdev) : (nextfakedev--); |
2bd0ea18 | 141 | |
32181a02 | 142 | for (d = 0; d < MAX_DEVS; d++) |
2bd0ea18 | 143 | if (dev_map[d].dev == dev) { |
9440d84d | 144 | fprintf(stderr, _("%s: device %lld is already open\n"), |
5b64e00a | 145 | progname, (long long)dev); |
2bd0ea18 NS |
146 | exit(1); |
147 | } | |
148 | ||
32181a02 | 149 | for (d = 0; d < MAX_DEVS; d++) |
2bd0ea18 | 150 | if (!dev_map[d].dev) { |
32181a02 NS |
151 | dev_map[d].dev = dev; |
152 | dev_map[d].fd = fd; | |
5000d01d | 153 | |
2bd0ea18 NS |
154 | return dev; |
155 | } | |
156 | ||
9440d84d NS |
157 | fprintf(stderr, _("%s: %s: too many open devices\n"), |
158 | progname, __FUNCTION__); | |
2bd0ea18 | 159 | exit(1); |
25d246df | 160 | /* NOTREACHED */ |
2bd0ea18 NS |
161 | } |
162 | ||
163 | void | |
164 | libxfs_device_close(dev_t dev) | |
165 | { | |
5000d01d | 166 | int d; |
2bd0ea18 | 167 | |
32181a02 | 168 | for (d = 0; d < MAX_DEVS; d++) |
2bd0ea18 | 169 | if (dev_map[d].dev == dev) { |
32181a02 | 170 | int fd; |
5000d01d | 171 | |
32181a02 NS |
172 | fd = dev_map[d].fd; |
173 | dev_map[d].dev = dev_map[d].fd = 0; | |
5000d01d | 174 | |
2bd0ea18 | 175 | fsync(fd); |
ac419944 | 176 | platform_flush_device(fd, dev); |
2bd0ea18 | 177 | close(fd); |
5000d01d | 178 | |
2bd0ea18 NS |
179 | return; |
180 | } | |
181 | ||
9440d84d NS |
182 | fprintf(stderr, _("%s: %s: device %lld is not open\n"), |
183 | progname, __FUNCTION__, (long long)dev); | |
2bd0ea18 NS |
184 | exit(1); |
185 | } | |
186 | ||
74668075 NS |
187 | static int |
188 | check_open(char *path, int flags, char **rawfile, char **blockfile) | |
189 | { | |
c781939c RC |
190 | int readonly = (flags & LIBXFS_ISREADONLY); |
191 | int inactive = (flags & LIBXFS_ISINACTIVE); | |
192 | int dangerously = (flags & LIBXFS_DANGEROUSLY); | |
f594a0d1 | 193 | struct stat stbuf; |
c781939c | 194 | |
f594a0d1 | 195 | if (stat(path, &stbuf) < 0) { |
c781939c RC |
196 | perror(path); |
197 | return 0; | |
198 | } | |
b74a1f6a | 199 | if (!(*rawfile = platform_findrawpath(path))) { |
c781939c RC |
200 | fprintf(stderr, _("%s: " |
201 | "can't find a character device matching %s\n"), | |
202 | progname, path); | |
203 | return 0; | |
204 | } | |
b74a1f6a | 205 | if (!(*blockfile = platform_findblockpath(path))) { |
c781939c RC |
206 | fprintf(stderr, _("%s: " |
207 | "can't find a block device matching %s\n"), | |
208 | progname, path); | |
209 | return 0; | |
210 | } | |
211 | if (!readonly && !inactive && platform_check_ismounted(path, *blockfile, NULL, 1)) | |
212 | return 0; | |
e08f5594 | 213 | |
c781939c RC |
214 | if (inactive && check_isactive(path, *blockfile, ((readonly|dangerously)?1:0))) |
215 | return 0; | |
216 | ||
217 | return 1; | |
218 | } | |
219 | ||
2bd0ea18 NS |
220 | /* |
221 | * libxfs initialization. | |
222 | * Caller gets a 0 on failure (and we print a message), 1 on success. | |
223 | */ | |
224 | int | |
225 | libxfs_init(libxfs_init_t *a) | |
226 | { | |
227 | char *blockfile; | |
2bd0ea18 NS |
228 | char *dname; |
229 | char dpath[25]; | |
230 | int fd; | |
231 | char *logname; | |
232 | char logpath[25]; | |
2bd0ea18 NS |
233 | char *rawfile; |
234 | char *rtname; | |
235 | char rtpath[25]; | |
236 | int rval = 0; | |
c781939c | 237 | int flags; |
2bd0ea18 NS |
238 | |
239 | dpath[0] = logpath[0] = rtpath[0] = '\0'; | |
240 | dname = a->dname; | |
241 | logname = a->logname; | |
242 | rtname = a->rtname; | |
2bd0ea18 | 243 | a->dfd = a->logfd = a->rtfd = -1; |
74668075 | 244 | a->ddev = a->logdev = a->rtdev = 0; |
06ac92fd DC |
245 | a->dsize = a->lbsize = a->rtbsize = 0; |
246 | a->dbsize = a->logBBsize = a->logBBstart = a->rtsize = 0; | |
2bd0ea18 | 247 | |
2bd0ea18 | 248 | fd = -1; |
b74a1f6a | 249 | flags = (a->isreadonly | a->isdirect); |
c781939c | 250 | |
dc3bce02 DW |
251 | xfs_extent_free_init_defer_op(); |
252 | xfs_rmap_update_init_defer_op(); | |
cdc9cf25 | 253 | xfs_refcount_update_init_defer_op(); |
f8f8c8a0 | 254 | xfs_bmap_update_init_defer_op(); |
dc3bce02 | 255 | |
bacd44a5 AE |
256 | radix_tree_init(); |
257 | ||
2bd0ea18 | 258 | if (a->volname) { |
c781939c | 259 | if(!check_open(a->volname,flags,&rawfile,&blockfile)) |
2bd0ea18 | 260 | goto done; |
2bd0ea18 | 261 | fd = open(rawfile, O_RDONLY); |
eb37fca5 NS |
262 | dname = a->dname = a->volname; |
263 | a->volname = NULL; | |
2bd0ea18 | 264 | } |
2bd0ea18 | 265 | if (dname) { |
2bd0ea18 | 266 | if (a->disfile) { |
7eb6693f | 267 | a->ddev= libxfs_device_open(dname, a->dcreat, flags, |
c5907b96 | 268 | a->setblksize); |
2bd0ea18 | 269 | a->dfd = libxfs_device_to_fd(a->ddev); |
06ac92fd DC |
270 | platform_findsizes(dname, a->dfd, &a->dsize, |
271 | &a->dbsize); | |
2bd0ea18 | 272 | } else { |
f02037ce | 273 | if (!check_open(dname, flags, &rawfile, &blockfile)) |
2bd0ea18 NS |
274 | goto done; |
275 | a->ddev = libxfs_device_open(rawfile, | |
7eb6693f | 276 | a->dcreat, flags, a->setblksize); |
2bd0ea18 | 277 | a->dfd = libxfs_device_to_fd(a->ddev); |
f02037ce | 278 | platform_findsizes(rawfile, a->dfd, |
06ac92fd | 279 | &a->dsize, &a->dbsize); |
2bd0ea18 | 280 | } |
2bd0ea18 NS |
281 | } else |
282 | a->dsize = 0; | |
283 | if (logname) { | |
2bd0ea18 NS |
284 | if (a->lisfile) { |
285 | a->logdev = libxfs_device_open(logname, | |
7eb6693f | 286 | a->lcreat, flags, a->setblksize); |
2bd0ea18 | 287 | a->logfd = libxfs_device_to_fd(a->logdev); |
06ac92fd DC |
288 | platform_findsizes(dname, a->logfd, &a->logBBsize, |
289 | &a->lbsize); | |
2bd0ea18 | 290 | } else { |
f02037ce | 291 | if (!check_open(logname, flags, &rawfile, &blockfile)) |
2bd0ea18 NS |
292 | goto done; |
293 | a->logdev = libxfs_device_open(rawfile, | |
7eb6693f | 294 | a->lcreat, flags, a->setblksize); |
2bd0ea18 | 295 | a->logfd = libxfs_device_to_fd(a->logdev); |
f02037ce | 296 | platform_findsizes(rawfile, a->logfd, |
06ac92fd | 297 | &a->logBBsize, &a->lbsize); |
2bd0ea18 | 298 | } |
2bd0ea18 NS |
299 | } else |
300 | a->logBBsize = 0; | |
301 | if (rtname) { | |
2bd0ea18 NS |
302 | if (a->risfile) { |
303 | a->rtdev = libxfs_device_open(rtname, | |
7eb6693f | 304 | a->rcreat, flags, a->setblksize); |
2bd0ea18 | 305 | a->rtfd = libxfs_device_to_fd(a->rtdev); |
06ac92fd DC |
306 | platform_findsizes(dname, a->rtfd, &a->rtsize, |
307 | &a->rtbsize); | |
2bd0ea18 | 308 | } else { |
f02037ce | 309 | if (!check_open(rtname, flags, &rawfile, &blockfile)) |
2bd0ea18 NS |
310 | goto done; |
311 | a->rtdev = libxfs_device_open(rawfile, | |
7eb6693f | 312 | a->rcreat, flags, a->setblksize); |
2bd0ea18 | 313 | a->rtfd = libxfs_device_to_fd(a->rtdev); |
f02037ce | 314 | platform_findsizes(rawfile, a->rtfd, |
06ac92fd | 315 | &a->rtsize, &a->rtbsize); |
2bd0ea18 | 316 | } |
2bd0ea18 NS |
317 | } else |
318 | a->rtsize = 0; | |
319 | if (a->dsize < 0) { | |
9440d84d | 320 | fprintf(stderr, _("%s: can't get size for data subvolume\n"), |
2bd0ea18 NS |
321 | progname); |
322 | goto done; | |
323 | } | |
324 | if (a->logBBsize < 0) { | |
9440d84d | 325 | fprintf(stderr, _("%s: can't get size for log subvolume\n"), |
2bd0ea18 NS |
326 | progname); |
327 | goto done; | |
328 | } | |
329 | if (a->rtsize < 0) { | |
9440d84d | 330 | fprintf(stderr, _("%s: can't get size for realtime subvolume\n"), |
2bd0ea18 NS |
331 | progname); |
332 | goto done; | |
333 | } | |
9f38f08d MV |
334 | if (!libxfs_bhash_size) |
335 | libxfs_bhash_size = LIBXFS_BHASHSIZE(sbp); | |
ba9ecd40 DC |
336 | libxfs_bcache = cache_init(a->bcache_flags, libxfs_bhash_size, |
337 | &libxfs_bcache_operations); | |
d0572de5 | 338 | use_xfs_buf_lock = a->usebuflock; |
f1b058f9 | 339 | manage_zones(0); |
2bd0ea18 NS |
340 | rval = 1; |
341 | done: | |
342 | if (dpath[0]) | |
343 | unlink(dpath); | |
344 | if (logpath[0]) | |
345 | unlink(logpath); | |
346 | if (rtpath[0]) | |
347 | unlink(rtpath); | |
348 | if (fd >= 0) | |
349 | close(fd); | |
350 | if (!rval && a->ddev) | |
351 | libxfs_device_close(a->ddev); | |
352 | if (!rval && a->logdev) | |
353 | libxfs_device_close(a->logdev); | |
354 | if (!rval && a->rtdev) | |
355 | libxfs_device_close(a->rtdev); | |
356 | return rval; | |
357 | } | |
358 | ||
359 | ||
360 | /* | |
361 | * Initialize/destroy all of the zone allocators we use. | |
362 | */ | |
44488491 | 363 | static int |
2bd0ea18 NS |
364 | manage_zones(int release) |
365 | { | |
5e656dbb BN |
366 | extern kmem_zone_t *xfs_buf_zone; |
367 | extern kmem_zone_t *xfs_ili_zone; | |
bf0e024f | 368 | extern kmem_zone_t *xfs_inode_zone; |
5e656dbb | 369 | extern kmem_zone_t *xfs_ifork_zone; |
5e656dbb BN |
370 | extern kmem_zone_t *xfs_buf_item_zone; |
371 | extern kmem_zone_t *xfs_da_state_zone; | |
372 | extern kmem_zone_t *xfs_btree_cur_zone; | |
373 | extern kmem_zone_t *xfs_bmap_free_item_zone; | |
67d72279 | 374 | extern kmem_zone_t *xfs_trans_zone; |
2bd0ea18 NS |
375 | extern void xfs_dir_startup(); |
376 | ||
377 | if (release) { /* free zone allocation */ | |
44488491 ES |
378 | int leaked = 0; |
379 | ||
380 | leaked += kmem_zone_destroy(xfs_buf_zone); | |
381 | leaked += kmem_zone_destroy(xfs_ili_zone); | |
382 | leaked += kmem_zone_destroy(xfs_inode_zone); | |
383 | leaked += kmem_zone_destroy(xfs_ifork_zone); | |
384 | leaked += kmem_zone_destroy(xfs_buf_item_zone); | |
385 | leaked += kmem_zone_destroy(xfs_da_state_zone); | |
386 | leaked += kmem_zone_destroy(xfs_btree_cur_zone); | |
387 | leaked += kmem_zone_destroy(xfs_bmap_free_item_zone); | |
388 | leaked += kmem_zone_destroy(xfs_trans_zone); | |
44488491 ES |
389 | |
390 | return leaked; | |
2bd0ea18 NS |
391 | } |
392 | /* otherwise initialise zone allocation */ | |
5e656dbb | 393 | xfs_buf_zone = kmem_zone_init(sizeof(xfs_buf_t), "xfs_buffer"); |
b626fb59 | 394 | xfs_inode_zone = kmem_zone_init(sizeof(struct xfs_inode), "xfs_inode"); |
e07055b8 | 395 | xfs_ifork_zone = kmem_zone_init(sizeof(struct xfs_ifork), "xfs_ifork"); |
5e656dbb | 396 | xfs_ili_zone = kmem_zone_init( |
2bd0ea18 | 397 | sizeof(xfs_inode_log_item_t), "xfs_inode_log_item"); |
5e656dbb | 398 | xfs_buf_item_zone = kmem_zone_init( |
2bd0ea18 | 399 | sizeof(xfs_buf_log_item_t), "xfs_buf_log_item"); |
5e656dbb | 400 | xfs_da_state_zone = kmem_zone_init( |
2bd0ea18 | 401 | sizeof(xfs_da_state_t), "xfs_da_state"); |
5e656dbb | 402 | xfs_btree_cur_zone = kmem_zone_init( |
2bd0ea18 | 403 | sizeof(xfs_btree_cur_t), "xfs_btree_cur"); |
5e656dbb | 404 | xfs_bmap_free_item_zone = kmem_zone_init( |
6f530e9a DW |
405 | sizeof(struct xfs_extent_free_item), |
406 | "xfs_bmap_free_item"); | |
67d72279 ES |
407 | xfs_trans_zone = kmem_zone_init( |
408 | sizeof(struct xfs_trans), "xfs_trans"); | |
2bd0ea18 | 409 | xfs_dir_startup(); |
44488491 ES |
410 | |
411 | return 0; | |
2bd0ea18 NS |
412 | } |
413 | ||
b391b7cd NS |
414 | /* |
415 | * Initialize realtime fields in the mount structure. | |
416 | */ | |
417 | static int | |
418 | rtmount_init( | |
39798eb5 NS |
419 | xfs_mount_t *mp, /* file system mount structure */ |
420 | int flags) | |
b391b7cd NS |
421 | { |
422 | xfs_buf_t *bp; /* buffer for last block of subvolume */ | |
423 | xfs_daddr_t d; /* address of last block of subvolume */ | |
424 | xfs_sb_t *sbp; /* filesystem superblock copy in mount */ | |
425 | ||
426 | sbp = &mp->m_sb; | |
427 | if (sbp->sb_rblocks == 0) | |
428 | return 0; | |
75c8b434 | 429 | if (mp->m_rtdev_targp->dev == 0 && !(flags & LIBXFS_MOUNT_DEBUGGER)) { |
9440d84d | 430 | fprintf(stderr, _("%s: filesystem has a realtime subvolume\n"), |
b391b7cd NS |
431 | progname); |
432 | return -1; | |
433 | } | |
434 | mp->m_rsumlevels = sbp->sb_rextslog + 1; | |
435 | mp->m_rsumsize = | |
436 | (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels * | |
437 | sbp->sb_rbmblocks; | |
438 | mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize); | |
439 | mp->m_rbmip = mp->m_rsumip = NULL; | |
39798eb5 NS |
440 | |
441 | /* | |
442 | * Allow debugger to be run without the realtime device present. | |
443 | */ | |
444 | if (flags & LIBXFS_MOUNT_DEBUGGER) | |
445 | return 0; | |
446 | ||
b391b7cd NS |
447 | /* |
448 | * Check that the realtime section is an ok size. | |
449 | */ | |
450 | d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks); | |
451 | if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) { | |
9440d84d NS |
452 | fprintf(stderr, _("%s: realtime init - %llu != %llu\n"), |
453 | progname, (unsigned long long) XFS_BB_TO_FSB(mp, d), | |
b391b7cd NS |
454 | (unsigned long long) mp->m_sb.sb_rblocks); |
455 | return -1; | |
456 | } | |
9440d84d | 457 | bp = libxfs_readbuf(mp->m_rtdev, |
75c8b434 | 458 | d - XFS_FSB_TO_BB(mp, 1), XFS_FSB_TO_BB(mp, 1), 0, NULL); |
b391b7cd | 459 | if (bp == NULL) { |
9440d84d NS |
460 | fprintf(stderr, _("%s: realtime size check failed\n"), |
461 | progname); | |
b391b7cd NS |
462 | return -1; |
463 | } | |
464 | libxfs_putbuf(bp); | |
465 | return 0; | |
466 | } | |
467 | ||
56b2de80 DC |
468 | static int |
469 | libxfs_initialize_perag( | |
470 | xfs_mount_t *mp, | |
471 | xfs_agnumber_t agcount, | |
472 | xfs_agnumber_t *maxagi) | |
473 | { | |
474 | xfs_agnumber_t index, max_metadata; | |
475 | xfs_agnumber_t first_initialised = 0; | |
476 | xfs_perag_t *pag; | |
477 | xfs_agino_t agino; | |
478 | xfs_ino_t ino; | |
479 | xfs_sb_t *sbp = &mp->m_sb; | |
480 | int error = -ENOMEM; | |
481 | ||
482 | /* | |
483 | * Walk the current per-ag tree so we don't try to initialise AGs | |
484 | * that already exist (growfs case). Allocate and insert all the | |
485 | * AGs we don't find ready for initialisation. | |
486 | */ | |
487 | for (index = 0; index < agcount; index++) { | |
488 | pag = xfs_perag_get(mp, index); | |
489 | if (pag) { | |
490 | xfs_perag_put(pag); | |
491 | continue; | |
492 | } | |
493 | if (!first_initialised) | |
494 | first_initialised = index; | |
495 | ||
496 | pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL); | |
497 | if (!pag) | |
498 | goto out_unwind; | |
499 | pag->pag_agno = index; | |
500 | pag->pag_mount = mp; | |
501 | ||
502 | if (radix_tree_insert(&mp->m_perag_tree, index, pag)) { | |
503 | error = -EEXIST; | |
504 | goto out_unwind; | |
505 | } | |
506 | } | |
507 | ||
508 | /* | |
509 | * If we mount with the inode64 option, or no inode overflows | |
510 | * the legacy 32-bit address space clear the inode32 option. | |
511 | */ | |
512 | agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0); | |
513 | ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino); | |
514 | ||
515 | if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > XFS_MAXINUMBER_32) | |
516 | mp->m_flags |= XFS_MOUNT_32BITINODES; | |
517 | else | |
518 | mp->m_flags &= ~XFS_MOUNT_32BITINODES; | |
519 | ||
520 | if (mp->m_flags & XFS_MOUNT_32BITINODES) { | |
521 | /* | |
522 | * Calculate how much should be reserved for inodes to meet | |
523 | * the max inode percentage. | |
524 | */ | |
525 | if (mp->m_maxicount) { | |
14f8b681 | 526 | uint64_t icount; |
56b2de80 DC |
527 | |
528 | icount = sbp->sb_dblocks * sbp->sb_imax_pct; | |
529 | do_div(icount, 100); | |
530 | icount += sbp->sb_agblocks - 1; | |
531 | do_div(icount, sbp->sb_agblocks); | |
532 | max_metadata = icount; | |
533 | } else { | |
534 | max_metadata = agcount; | |
535 | } | |
536 | ||
537 | for (index = 0; index < agcount; index++) { | |
538 | ino = XFS_AGINO_TO_INO(mp, index, agino); | |
539 | if (ino > XFS_MAXINUMBER_32) { | |
540 | index++; | |
541 | break; | |
542 | } | |
543 | ||
544 | pag = xfs_perag_get(mp, index); | |
545 | pag->pagi_inodeok = 1; | |
546 | if (index < max_metadata) | |
547 | pag->pagf_metadata = 1; | |
548 | xfs_perag_put(pag); | |
549 | } | |
550 | } else { | |
551 | for (index = 0; index < agcount; index++) { | |
552 | pag = xfs_perag_get(mp, index); | |
553 | pag->pagi_inodeok = 1; | |
554 | xfs_perag_put(pag); | |
555 | } | |
556 | } | |
557 | ||
558 | if (maxagi) | |
559 | *maxagi = index; | |
ef5340cd DW |
560 | |
561 | mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp); | |
56b2de80 DC |
562 | return 0; |
563 | ||
564 | out_unwind: | |
565 | kmem_free(pag); | |
566 | for (; index > first_initialised; index--) { | |
567 | pag = radix_tree_delete(&mp->m_perag_tree, index); | |
568 | kmem_free(pag); | |
569 | } | |
570 | return error; | |
571 | } | |
572 | ||
75c8b434 DC |
573 | static struct xfs_buftarg * |
574 | libxfs_buftarg_alloc( | |
575 | struct xfs_mount *mp, | |
576 | dev_t dev) | |
577 | { | |
578 | struct xfs_buftarg *btp; | |
579 | ||
580 | btp = malloc(sizeof(*btp)); | |
581 | if (!btp) { | |
582 | fprintf(stderr, _("%s: buftarg init failed\n"), | |
583 | progname); | |
584 | exit(1); | |
585 | } | |
586 | btp->bt_mount = mp; | |
587 | btp->dev = dev; | |
588 | return btp; | |
589 | } | |
590 | ||
591 | void | |
592 | libxfs_buftarg_init( | |
593 | struct xfs_mount *mp, | |
594 | dev_t dev, | |
595 | dev_t logdev, | |
596 | dev_t rtdev) | |
597 | { | |
598 | if (mp->m_ddev_targp) { | |
599 | /* should already have all buftargs initialised */ | |
600 | if (mp->m_ddev_targp->dev != dev || | |
601 | mp->m_ddev_targp->bt_mount != mp) { | |
602 | fprintf(stderr, | |
603 | _("%s: bad buftarg reinit, ddev\n"), | |
604 | progname); | |
605 | exit(1); | |
606 | } | |
607 | if (!logdev || logdev == dev) { | |
608 | if (mp->m_logdev_targp != mp->m_ddev_targp) { | |
609 | fprintf(stderr, | |
610 | _("%s: bad buftarg reinit, ldev mismatch\n"), | |
611 | progname); | |
612 | exit(1); | |
613 | } | |
614 | } else if (mp->m_logdev_targp->dev != logdev || | |
615 | mp->m_logdev_targp->bt_mount != mp) { | |
616 | fprintf(stderr, | |
617 | _("%s: bad buftarg reinit, logdev\n"), | |
618 | progname); | |
619 | exit(1); | |
620 | } | |
621 | if (rtdev && (mp->m_rtdev_targp->dev != rtdev || | |
622 | mp->m_rtdev_targp->bt_mount != mp)) { | |
623 | fprintf(stderr, | |
624 | _("%s: bad buftarg reinit, rtdev\n"), | |
625 | progname); | |
626 | exit(1); | |
627 | } | |
628 | return; | |
629 | } | |
630 | ||
631 | mp->m_ddev_targp = libxfs_buftarg_alloc(mp, dev); | |
632 | if (!logdev || logdev == dev) | |
633 | mp->m_logdev_targp = mp->m_ddev_targp; | |
634 | else | |
635 | mp->m_logdev_targp = libxfs_buftarg_alloc(mp, logdev); | |
636 | mp->m_rtdev_targp = libxfs_buftarg_alloc(mp, rtdev); | |
637 | } | |
638 | ||
2bd0ea18 NS |
639 | /* |
640 | * Mount structure initialization, provides a filled-in xfs_mount_t | |
641 | * such that the numerous XFS_* macros can be used. If dev is zero, | |
642 | * no IO will be performed (no size checks, read root inodes). | |
643 | */ | |
644 | xfs_mount_t * | |
645 | libxfs_mount( | |
646 | xfs_mount_t *mp, | |
647 | xfs_sb_t *sb, | |
648 | dev_t dev, | |
649 | dev_t logdev, | |
650 | dev_t rtdev, | |
4ca431fc | 651 | int flags) |
2bd0ea18 NS |
652 | { |
653 | xfs_daddr_t d; | |
654 | xfs_buf_t *bp; | |
655 | xfs_sb_t *sbp; | |
2bd0ea18 NS |
656 | int error; |
657 | ||
75c8b434 DC |
658 | libxfs_buftarg_init(mp, dev, logdev, rtdev); |
659 | ||
6239071d | 660 | mp->m_flags = (LIBXFS_MOUNT_32BITINODES|LIBXFS_MOUNT_32BITINOOPT); |
2bd0ea18 | 661 | mp->m_sb = *sb; |
56b2de80 | 662 | INIT_RADIX_TREE(&mp->m_perag_tree, GFP_KERNEL); |
2bd0ea18 | 663 | sbp = &(mp->m_sb); |
2bd0ea18 | 664 | |
4896e6c8 | 665 | xfs_sb_mount_common(mp, sb); |
2bd0ea18 | 666 | |
5e656dbb BN |
667 | xfs_alloc_compute_maxlevels(mp); |
668 | xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK); | |
669 | xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK); | |
670 | xfs_ialloc_compute_maxlevels(mp); | |
b3a96b46 | 671 | xfs_rmapbt_compute_maxlevels(mp); |
e7be6330 | 672 | xfs_refcountbt_compute_maxlevels(mp); |
2bd0ea18 NS |
673 | |
674 | if (sbp->sb_imax_pct) { | |
675 | /* Make sure the maximum inode count is a multiple of the | |
676 | * units we allocate inodes in. | |
677 | */ | |
678 | mp->m_maxicount = (sbp->sb_dblocks * sbp->sb_imax_pct) / 100; | |
679 | mp->m_maxicount = ((mp->m_maxicount / mp->m_ialloc_blks) * | |
680 | mp->m_ialloc_blks) << sbp->sb_inopblog; | |
681 | } else | |
682 | mp->m_maxicount = 0; | |
683 | ||
684 | mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE; | |
2a3145a8 DW |
685 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
686 | int new_size = mp->m_inode_cluster_size; | |
687 | ||
688 | new_size *= mp->m_sb.sb_inodesize / XFS_DINODE_MIN_SIZE; | |
689 | if (mp->m_sb.sb_inoalignmt >= XFS_B_TO_FSBT(mp, new_size)) | |
690 | mp->m_inode_cluster_size = new_size; | |
691 | } | |
2bd0ea18 | 692 | |
949c0f10 NS |
693 | /* |
694 | * Set whether we're using stripe alignment. | |
695 | */ | |
5e656dbb | 696 | if (xfs_sb_version_hasdalign(&mp->m_sb)) { |
949c0f10 NS |
697 | mp->m_dalign = sbp->sb_unit; |
698 | mp->m_swidth = sbp->sb_width; | |
699 | } | |
700 | ||
2bd0ea18 NS |
701 | /* |
702 | * Set whether we're using inode alignment. | |
703 | */ | |
5e656dbb | 704 | if (xfs_sb_version_hasalign(&mp->m_sb) && |
2bd0ea18 NS |
705 | mp->m_sb.sb_inoalignmt >= |
706 | XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size)) | |
707 | mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1; | |
708 | else | |
709 | mp->m_inoalign_mask = 0; | |
710 | /* | |
711 | * If we are using stripe alignment, check whether | |
712 | * the stripe unit is a multiple of the inode alignment | |
713 | */ | |
b3563c19 | 714 | if (mp->m_dalign && mp->m_inoalign_mask && |
5e656dbb | 715 | !(mp->m_dalign & mp->m_inoalign_mask)) |
2bd0ea18 NS |
716 | mp->m_sinoalign = mp->m_dalign; |
717 | else | |
718 | mp->m_sinoalign = 0; | |
719 | ||
720 | /* | |
721 | * Check that the data (and log if separate) are an ok size. | |
722 | */ | |
9440d84d | 723 | d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks); |
2bd0ea18 | 724 | if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) { |
9440d84d | 725 | fprintf(stderr, _("%s: size check failed\n"), progname); |
4ca431fc NS |
726 | if (!(flags & LIBXFS_MOUNT_DEBUGGER)) |
727 | return NULL; | |
2bd0ea18 NS |
728 | } |
729 | ||
ff105f75 DC |
730 | /* |
731 | * We automatically convert v1 inodes to v2 inodes now, so if | |
732 | * the NLINK bit is not set we can't operate on the filesystem. | |
733 | */ | |
734 | if (!(sbp->sb_versionnum & XFS_SB_VERSION_NLINKBIT)) { | |
735 | ||
736 | fprintf(stderr, _( | |
737 | "%s: V1 inodes unsupported. Please try an older xfsprogs.\n"), | |
738 | progname); | |
739 | exit(1); | |
740 | } | |
741 | ||
742 | /* Check for supported directory formats */ | |
743 | if (!(sbp->sb_versionnum & XFS_SB_VERSION_DIRV2BIT)) { | |
9a048535 DC |
744 | |
745 | fprintf(stderr, _( | |
746 | "%s: V1 directories unsupported. Please try an older xfsprogs.\n"), | |
747 | progname); | |
748 | exit(1); | |
5e656dbb | 749 | } |
2bd0ea18 | 750 | |
ff105f75 DC |
751 | /* check for unsupported other features */ |
752 | if (!xfs_sb_good_version(sbp)) { | |
753 | fprintf(stderr, _( | |
754 | "%s: Unsupported features detected. Please try a newer xfsprogs.\n"), | |
755 | progname); | |
756 | exit(1); | |
757 | } | |
758 | ||
759 | xfs_da_mount(mp); | |
760 | ||
5e656dbb BN |
761 | if (xfs_sb_version_hasattr2(&mp->m_sb)) |
762 | mp->m_flags |= LIBXFS_MOUNT_ATTR2; | |
57c9fccb | 763 | |
2bd0ea18 | 764 | /* Initialize the precomputed transaction reservations values */ |
5e656dbb | 765 | xfs_trans_init(mp); |
2bd0ea18 NS |
766 | |
767 | if (dev == 0) /* maxtrres, we have no device so leave now */ | |
768 | return mp; | |
769 | ||
9440d84d | 770 | bp = libxfs_readbuf(mp->m_dev, |
4ca431fc | 771 | d - XFS_FSS_TO_BB(mp, 1), XFS_FSS_TO_BB(mp, 1), |
75c8b434 | 772 | !(flags & LIBXFS_MOUNT_DEBUGGER), NULL); |
9440d84d NS |
773 | if (!bp) { |
774 | fprintf(stderr, _("%s: data size check failed\n"), progname); | |
4ca431fc NS |
775 | if (!(flags & LIBXFS_MOUNT_DEBUGGER)) |
776 | return NULL; | |
32244196 BN |
777 | } else |
778 | libxfs_putbuf(bp); | |
2bd0ea18 | 779 | |
75c8b434 DC |
780 | if (mp->m_logdev_targp->dev && |
781 | mp->m_logdev_targp->dev != mp->m_ddev_targp->dev) { | |
9440d84d | 782 | d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks); |
2bd0ea18 | 783 | if ( (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) || |
75c8b434 | 784 | (!(bp = libxfs_readbuf(mp->m_logdev_targp, |
9440d84d | 785 | d - XFS_FSB_TO_BB(mp, 1), |
4ca431fc | 786 | XFS_FSB_TO_BB(mp, 1), |
75c8b434 | 787 | !(flags & LIBXFS_MOUNT_DEBUGGER), NULL))) ) { |
9440d84d | 788 | fprintf(stderr, _("%s: log size checks failed\n"), |
2bd0ea18 | 789 | progname); |
4ca431fc NS |
790 | if (!(flags & LIBXFS_MOUNT_DEBUGGER)) |
791 | return NULL; | |
2bd0ea18 | 792 | } |
32244196 BN |
793 | if (bp) |
794 | libxfs_putbuf(bp); | |
2bd0ea18 NS |
795 | } |
796 | ||
797 | /* Initialize realtime fields in the mount structure */ | |
39798eb5 | 798 | if (rtmount_init(mp, flags)) { |
9440d84d NS |
799 | fprintf(stderr, _("%s: realtime device init failed\n"), |
800 | progname); | |
4ca431fc | 801 | return NULL; |
2bd0ea18 NS |
802 | } |
803 | ||
a547152d ES |
804 | /* |
805 | * libxfs_initialize_perag will allocate a perag structure for each ag. | |
806 | * If agcount is corrupted and insanely high, this will OOM the box. | |
807 | * If the agount seems (arbitrarily) high, try to read what would be | |
808 | * the last AG, and if that fails for a relatively high agcount, just | |
809 | * read the first one and let the user know to check the geometry. | |
810 | */ | |
811 | if (sbp->sb_agcount > 1000000) { | |
812 | bp = libxfs_readbuf(mp->m_dev, | |
813 | XFS_AG_DADDR(mp, sbp->sb_agcount - 1, 0), 1, | |
814 | !(flags & LIBXFS_MOUNT_DEBUGGER), NULL); | |
815 | if (bp->b_error) { | |
816 | fprintf(stderr, _("%s: read of AG %u failed\n"), | |
817 | progname, sbp->sb_agcount); | |
818 | if (!(flags & LIBXFS_MOUNT_DEBUGGER)) | |
819 | return NULL; | |
820 | fprintf(stderr, _("%s: limiting reads to AG 0\n"), | |
821 | progname); | |
822 | sbp->sb_agcount = 1; | |
823 | } | |
824 | libxfs_putbuf(bp); | |
825 | } | |
826 | ||
56b2de80 DC |
827 | error = libxfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi); |
828 | if (error) { | |
829 | fprintf(stderr, _("%s: perag init failed\n"), | |
830 | progname); | |
2bd0ea18 NS |
831 | exit(1); |
832 | } | |
833 | ||
2bd0ea18 NS |
834 | return mp; |
835 | } | |
836 | ||
f1b058f9 NS |
837 | void |
838 | libxfs_rtmount_destroy(xfs_mount_t *mp) | |
839 | { | |
840 | if (mp->m_rsumip) | |
31845e4c | 841 | libxfs_irele(mp->m_rsumip); |
f1b058f9 | 842 | if (mp->m_rbmip) |
31845e4c | 843 | libxfs_irele(mp->m_rbmip); |
f1b058f9 NS |
844 | mp->m_rsumip = mp->m_rbmip = NULL; |
845 | } | |
846 | ||
2bd0ea18 | 847 | /* |
9440d84d | 848 | * Release any resource obtained during a mount. |
2bd0ea18 NS |
849 | */ |
850 | void | |
851 | libxfs_umount(xfs_mount_t *mp) | |
852 | { | |
56b2de80 DC |
853 | struct xfs_perag *pag; |
854 | int agno; | |
855 | ||
f1b058f9 | 856 | libxfs_rtmount_destroy(mp); |
f1b058f9 NS |
857 | libxfs_bcache_purge(); |
858 | ||
56b2de80 DC |
859 | for (agno = 0; agno < mp->m_maxagi; agno++) { |
860 | pag = radix_tree_delete(&mp->m_perag_tree, agno); | |
861 | kmem_free(pag); | |
fceb0d99 | 862 | } |
4334e2e8 ES |
863 | |
864 | kmem_free(mp->m_attr_geo); | |
865 | kmem_free(mp->m_dir_geo); | |
866 | ||
867 | kmem_free(mp->m_rtdev_targp); | |
868 | if (mp->m_logdev_targp != mp->m_ddev_targp) | |
869 | kmem_free(mp->m_logdev_targp); | |
870 | kmem_free(mp->m_ddev_targp); | |
f8149110 | 871 | |
2bd0ea18 | 872 | } |
f1b058f9 NS |
873 | |
874 | /* | |
875 | * Release any global resources used by libxfs. | |
876 | */ | |
877 | void | |
878 | libxfs_destroy(void) | |
879 | { | |
44488491 ES |
880 | int leaked; |
881 | ||
864028ed ES |
882 | /* Free everything from the buffer cache before freeing buffer zone */ |
883 | libxfs_bcache_purge(); | |
884 | libxfs_bcache_free(); | |
f1b058f9 | 885 | cache_destroy(libxfs_bcache); |
44488491 ES |
886 | leaked = manage_zones(1); |
887 | if (getenv("LIBXFS_LEAK_CHECK") && leaked) | |
888 | exit(1); | |
f1b058f9 | 889 | } |
9f38f08d | 890 | |
b74a1f6a NS |
891 | int |
892 | libxfs_device_alignment(void) | |
893 | { | |
894 | return platform_align_blockdev(); | |
895 | } | |
896 | ||
9f38f08d | 897 | void |
b6281496 | 898 | libxfs_report(FILE *fp) |
9f38f08d | 899 | { |
cb5b3ef4 MV |
900 | time_t t; |
901 | char *c; | |
902 | ||
b6281496 | 903 | cache_report(fp, "libxfs_bcache", libxfs_bcache); |
cb5b3ef4 MV |
904 | |
905 | t = time(NULL); | |
906 | c = asctime(localtime(&t)); | |
907 | fprintf(fp, "%s", c); | |
908 | } | |
3b6ac903 MV |
909 | |
910 | int | |
911 | libxfs_nproc(void) | |
912 | { | |
30a757b7 DW |
913 | int nr; |
914 | ||
915 | nr = platform_nproc(); | |
916 | if (nr < 1) | |
917 | nr = 1; | |
918 | return nr; | |
3b6ac903 | 919 | } |
2556c98b BN |
920 | |
921 | unsigned long | |
922 | libxfs_physmem(void) | |
923 | { | |
924 | return platform_physmem(); | |
e08f5594 | 925 | } |