]>
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 | |
bacd44a5 AE |
251 | radix_tree_init(); |
252 | ||
2bd0ea18 | 253 | if (a->volname) { |
c781939c | 254 | if(!check_open(a->volname,flags,&rawfile,&blockfile)) |
2bd0ea18 | 255 | goto done; |
2bd0ea18 | 256 | fd = open(rawfile, O_RDONLY); |
eb37fca5 NS |
257 | dname = a->dname = a->volname; |
258 | a->volname = NULL; | |
2bd0ea18 | 259 | } |
2bd0ea18 | 260 | if (dname) { |
2bd0ea18 | 261 | if (a->disfile) { |
7eb6693f | 262 | a->ddev= libxfs_device_open(dname, a->dcreat, flags, |
c5907b96 | 263 | a->setblksize); |
2bd0ea18 | 264 | a->dfd = libxfs_device_to_fd(a->ddev); |
06ac92fd DC |
265 | platform_findsizes(dname, a->dfd, &a->dsize, |
266 | &a->dbsize); | |
2bd0ea18 | 267 | } else { |
f02037ce | 268 | if (!check_open(dname, flags, &rawfile, &blockfile)) |
2bd0ea18 NS |
269 | goto done; |
270 | a->ddev = libxfs_device_open(rawfile, | |
7eb6693f | 271 | a->dcreat, flags, a->setblksize); |
2bd0ea18 | 272 | a->dfd = libxfs_device_to_fd(a->ddev); |
f02037ce | 273 | platform_findsizes(rawfile, a->dfd, |
06ac92fd | 274 | &a->dsize, &a->dbsize); |
2bd0ea18 | 275 | } |
2bd0ea18 NS |
276 | } else |
277 | a->dsize = 0; | |
278 | if (logname) { | |
2bd0ea18 NS |
279 | if (a->lisfile) { |
280 | a->logdev = libxfs_device_open(logname, | |
7eb6693f | 281 | a->lcreat, flags, a->setblksize); |
2bd0ea18 | 282 | a->logfd = libxfs_device_to_fd(a->logdev); |
06ac92fd DC |
283 | platform_findsizes(dname, a->logfd, &a->logBBsize, |
284 | &a->lbsize); | |
2bd0ea18 | 285 | } else { |
f02037ce | 286 | if (!check_open(logname, flags, &rawfile, &blockfile)) |
2bd0ea18 NS |
287 | goto done; |
288 | a->logdev = libxfs_device_open(rawfile, | |
7eb6693f | 289 | a->lcreat, flags, a->setblksize); |
2bd0ea18 | 290 | a->logfd = libxfs_device_to_fd(a->logdev); |
f02037ce | 291 | platform_findsizes(rawfile, a->logfd, |
06ac92fd | 292 | &a->logBBsize, &a->lbsize); |
2bd0ea18 | 293 | } |
2bd0ea18 NS |
294 | } else |
295 | a->logBBsize = 0; | |
296 | if (rtname) { | |
2bd0ea18 NS |
297 | if (a->risfile) { |
298 | a->rtdev = libxfs_device_open(rtname, | |
7eb6693f | 299 | a->rcreat, flags, a->setblksize); |
2bd0ea18 | 300 | a->rtfd = libxfs_device_to_fd(a->rtdev); |
06ac92fd DC |
301 | platform_findsizes(dname, a->rtfd, &a->rtsize, |
302 | &a->rtbsize); | |
2bd0ea18 | 303 | } else { |
f02037ce | 304 | if (!check_open(rtname, flags, &rawfile, &blockfile)) |
2bd0ea18 NS |
305 | goto done; |
306 | a->rtdev = libxfs_device_open(rawfile, | |
7eb6693f | 307 | a->rcreat, flags, a->setblksize); |
2bd0ea18 | 308 | a->rtfd = libxfs_device_to_fd(a->rtdev); |
f02037ce | 309 | platform_findsizes(rawfile, a->rtfd, |
06ac92fd | 310 | &a->rtsize, &a->rtbsize); |
2bd0ea18 | 311 | } |
2bd0ea18 NS |
312 | } else |
313 | a->rtsize = 0; | |
314 | if (a->dsize < 0) { | |
9440d84d | 315 | fprintf(stderr, _("%s: can't get size for data subvolume\n"), |
2bd0ea18 NS |
316 | progname); |
317 | goto done; | |
318 | } | |
319 | if (a->logBBsize < 0) { | |
9440d84d | 320 | fprintf(stderr, _("%s: can't get size for log subvolume\n"), |
2bd0ea18 NS |
321 | progname); |
322 | goto done; | |
323 | } | |
324 | if (a->rtsize < 0) { | |
9440d84d | 325 | fprintf(stderr, _("%s: can't get size for realtime subvolume\n"), |
2bd0ea18 NS |
326 | progname); |
327 | goto done; | |
328 | } | |
9f38f08d MV |
329 | if (!libxfs_bhash_size) |
330 | libxfs_bhash_size = LIBXFS_BHASHSIZE(sbp); | |
ba9ecd40 DC |
331 | libxfs_bcache = cache_init(a->bcache_flags, libxfs_bhash_size, |
332 | &libxfs_bcache_operations); | |
d0572de5 | 333 | use_xfs_buf_lock = a->usebuflock; |
f1b058f9 | 334 | manage_zones(0); |
2bd0ea18 NS |
335 | rval = 1; |
336 | done: | |
337 | if (dpath[0]) | |
338 | unlink(dpath); | |
339 | if (logpath[0]) | |
340 | unlink(logpath); | |
341 | if (rtpath[0]) | |
342 | unlink(rtpath); | |
343 | if (fd >= 0) | |
344 | close(fd); | |
345 | if (!rval && a->ddev) | |
346 | libxfs_device_close(a->ddev); | |
347 | if (!rval && a->logdev) | |
348 | libxfs_device_close(a->logdev); | |
349 | if (!rval && a->rtdev) | |
350 | libxfs_device_close(a->rtdev); | |
351 | return rval; | |
352 | } | |
353 | ||
354 | ||
355 | /* | |
356 | * Initialize/destroy all of the zone allocators we use. | |
357 | */ | |
44488491 | 358 | static int |
2bd0ea18 NS |
359 | manage_zones(int release) |
360 | { | |
2bd0ea18 NS |
361 | extern void xfs_dir_startup(); |
362 | ||
363 | if (release) { /* free zone allocation */ | |
44488491 ES |
364 | int leaked = 0; |
365 | ||
366 | leaked += kmem_zone_destroy(xfs_buf_zone); | |
367 | leaked += kmem_zone_destroy(xfs_ili_zone); | |
368 | leaked += kmem_zone_destroy(xfs_inode_zone); | |
369 | leaked += kmem_zone_destroy(xfs_ifork_zone); | |
370 | leaked += kmem_zone_destroy(xfs_buf_item_zone); | |
371 | leaked += kmem_zone_destroy(xfs_da_state_zone); | |
372 | leaked += kmem_zone_destroy(xfs_btree_cur_zone); | |
373 | leaked += kmem_zone_destroy(xfs_bmap_free_item_zone); | |
374 | leaked += kmem_zone_destroy(xfs_trans_zone); | |
44488491 ES |
375 | |
376 | return leaked; | |
2bd0ea18 NS |
377 | } |
378 | /* otherwise initialise zone allocation */ | |
5e656dbb | 379 | xfs_buf_zone = kmem_zone_init(sizeof(xfs_buf_t), "xfs_buffer"); |
b626fb59 | 380 | xfs_inode_zone = kmem_zone_init(sizeof(struct xfs_inode), "xfs_inode"); |
e07055b8 | 381 | xfs_ifork_zone = kmem_zone_init(sizeof(struct xfs_ifork), "xfs_ifork"); |
5e656dbb | 382 | xfs_ili_zone = kmem_zone_init( |
2bd0ea18 | 383 | sizeof(xfs_inode_log_item_t), "xfs_inode_log_item"); |
5e656dbb | 384 | xfs_buf_item_zone = kmem_zone_init( |
2bd0ea18 | 385 | sizeof(xfs_buf_log_item_t), "xfs_buf_log_item"); |
5e656dbb | 386 | xfs_da_state_zone = kmem_zone_init( |
2bd0ea18 | 387 | sizeof(xfs_da_state_t), "xfs_da_state"); |
5e656dbb | 388 | xfs_btree_cur_zone = kmem_zone_init( |
2bd0ea18 | 389 | sizeof(xfs_btree_cur_t), "xfs_btree_cur"); |
5e656dbb | 390 | xfs_bmap_free_item_zone = kmem_zone_init( |
6f530e9a DW |
391 | sizeof(struct xfs_extent_free_item), |
392 | "xfs_bmap_free_item"); | |
67d72279 ES |
393 | xfs_trans_zone = kmem_zone_init( |
394 | sizeof(struct xfs_trans), "xfs_trans"); | |
2bd0ea18 | 395 | xfs_dir_startup(); |
44488491 ES |
396 | |
397 | return 0; | |
2bd0ea18 NS |
398 | } |
399 | ||
b391b7cd NS |
400 | /* |
401 | * Initialize realtime fields in the mount structure. | |
402 | */ | |
403 | static int | |
404 | rtmount_init( | |
39798eb5 NS |
405 | xfs_mount_t *mp, /* file system mount structure */ |
406 | int flags) | |
b391b7cd NS |
407 | { |
408 | xfs_buf_t *bp; /* buffer for last block of subvolume */ | |
409 | xfs_daddr_t d; /* address of last block of subvolume */ | |
410 | xfs_sb_t *sbp; /* filesystem superblock copy in mount */ | |
411 | ||
412 | sbp = &mp->m_sb; | |
413 | if (sbp->sb_rblocks == 0) | |
414 | return 0; | |
75c8b434 | 415 | if (mp->m_rtdev_targp->dev == 0 && !(flags & LIBXFS_MOUNT_DEBUGGER)) { |
9440d84d | 416 | fprintf(stderr, _("%s: filesystem has a realtime subvolume\n"), |
b391b7cd NS |
417 | progname); |
418 | return -1; | |
419 | } | |
420 | mp->m_rsumlevels = sbp->sb_rextslog + 1; | |
421 | mp->m_rsumsize = | |
422 | (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels * | |
423 | sbp->sb_rbmblocks; | |
424 | mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize); | |
425 | mp->m_rbmip = mp->m_rsumip = NULL; | |
39798eb5 NS |
426 | |
427 | /* | |
428 | * Allow debugger to be run without the realtime device present. | |
429 | */ | |
430 | if (flags & LIBXFS_MOUNT_DEBUGGER) | |
431 | return 0; | |
432 | ||
b391b7cd NS |
433 | /* |
434 | * Check that the realtime section is an ok size. | |
435 | */ | |
436 | d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks); | |
437 | if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) { | |
9440d84d NS |
438 | fprintf(stderr, _("%s: realtime init - %llu != %llu\n"), |
439 | progname, (unsigned long long) XFS_BB_TO_FSB(mp, d), | |
b391b7cd NS |
440 | (unsigned long long) mp->m_sb.sb_rblocks); |
441 | return -1; | |
442 | } | |
9440d84d | 443 | bp = libxfs_readbuf(mp->m_rtdev, |
75c8b434 | 444 | d - XFS_FSB_TO_BB(mp, 1), XFS_FSB_TO_BB(mp, 1), 0, NULL); |
b391b7cd | 445 | if (bp == NULL) { |
9440d84d NS |
446 | fprintf(stderr, _("%s: realtime size check failed\n"), |
447 | progname); | |
b391b7cd NS |
448 | return -1; |
449 | } | |
450 | libxfs_putbuf(bp); | |
451 | return 0; | |
452 | } | |
453 | ||
56b2de80 DC |
454 | static int |
455 | libxfs_initialize_perag( | |
456 | xfs_mount_t *mp, | |
457 | xfs_agnumber_t agcount, | |
458 | xfs_agnumber_t *maxagi) | |
459 | { | |
460 | xfs_agnumber_t index, max_metadata; | |
461 | xfs_agnumber_t first_initialised = 0; | |
462 | xfs_perag_t *pag; | |
463 | xfs_agino_t agino; | |
464 | xfs_ino_t ino; | |
465 | xfs_sb_t *sbp = &mp->m_sb; | |
466 | int error = -ENOMEM; | |
467 | ||
468 | /* | |
469 | * Walk the current per-ag tree so we don't try to initialise AGs | |
470 | * that already exist (growfs case). Allocate and insert all the | |
471 | * AGs we don't find ready for initialisation. | |
472 | */ | |
473 | for (index = 0; index < agcount; index++) { | |
474 | pag = xfs_perag_get(mp, index); | |
475 | if (pag) { | |
476 | xfs_perag_put(pag); | |
477 | continue; | |
478 | } | |
479 | if (!first_initialised) | |
480 | first_initialised = index; | |
481 | ||
482 | pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL); | |
483 | if (!pag) | |
484 | goto out_unwind; | |
485 | pag->pag_agno = index; | |
486 | pag->pag_mount = mp; | |
487 | ||
488 | if (radix_tree_insert(&mp->m_perag_tree, index, pag)) { | |
489 | error = -EEXIST; | |
490 | goto out_unwind; | |
491 | } | |
492 | } | |
493 | ||
494 | /* | |
495 | * If we mount with the inode64 option, or no inode overflows | |
496 | * the legacy 32-bit address space clear the inode32 option. | |
497 | */ | |
7516da71 | 498 | agino = XFS_AGB_TO_AGINO(mp, sbp->sb_agblocks - 1); |
56b2de80 DC |
499 | ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino); |
500 | ||
501 | if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > XFS_MAXINUMBER_32) | |
502 | mp->m_flags |= XFS_MOUNT_32BITINODES; | |
503 | else | |
504 | mp->m_flags &= ~XFS_MOUNT_32BITINODES; | |
505 | ||
506 | if (mp->m_flags & XFS_MOUNT_32BITINODES) { | |
507 | /* | |
508 | * Calculate how much should be reserved for inodes to meet | |
509 | * the max inode percentage. | |
510 | */ | |
511 | if (mp->m_maxicount) { | |
14f8b681 | 512 | uint64_t icount; |
56b2de80 DC |
513 | |
514 | icount = sbp->sb_dblocks * sbp->sb_imax_pct; | |
515 | do_div(icount, 100); | |
516 | icount += sbp->sb_agblocks - 1; | |
517 | do_div(icount, sbp->sb_agblocks); | |
518 | max_metadata = icount; | |
519 | } else { | |
520 | max_metadata = agcount; | |
521 | } | |
522 | ||
523 | for (index = 0; index < agcount; index++) { | |
524 | ino = XFS_AGINO_TO_INO(mp, index, agino); | |
525 | if (ino > XFS_MAXINUMBER_32) { | |
526 | index++; | |
527 | break; | |
528 | } | |
529 | ||
530 | pag = xfs_perag_get(mp, index); | |
531 | pag->pagi_inodeok = 1; | |
532 | if (index < max_metadata) | |
533 | pag->pagf_metadata = 1; | |
534 | xfs_perag_put(pag); | |
535 | } | |
536 | } else { | |
537 | for (index = 0; index < agcount; index++) { | |
538 | pag = xfs_perag_get(mp, index); | |
539 | pag->pagi_inodeok = 1; | |
540 | xfs_perag_put(pag); | |
541 | } | |
542 | } | |
543 | ||
544 | if (maxagi) | |
545 | *maxagi = index; | |
ef5340cd DW |
546 | |
547 | mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp); | |
56b2de80 DC |
548 | return 0; |
549 | ||
550 | out_unwind: | |
551 | kmem_free(pag); | |
552 | for (; index > first_initialised; index--) { | |
553 | pag = radix_tree_delete(&mp->m_perag_tree, index); | |
554 | kmem_free(pag); | |
555 | } | |
556 | return error; | |
557 | } | |
558 | ||
75c8b434 DC |
559 | static struct xfs_buftarg * |
560 | libxfs_buftarg_alloc( | |
561 | struct xfs_mount *mp, | |
562 | dev_t dev) | |
563 | { | |
564 | struct xfs_buftarg *btp; | |
565 | ||
566 | btp = malloc(sizeof(*btp)); | |
567 | if (!btp) { | |
568 | fprintf(stderr, _("%s: buftarg init failed\n"), | |
569 | progname); | |
570 | exit(1); | |
571 | } | |
572 | btp->bt_mount = mp; | |
573 | btp->dev = dev; | |
574 | return btp; | |
575 | } | |
576 | ||
577 | void | |
578 | libxfs_buftarg_init( | |
579 | struct xfs_mount *mp, | |
580 | dev_t dev, | |
581 | dev_t logdev, | |
582 | dev_t rtdev) | |
583 | { | |
584 | if (mp->m_ddev_targp) { | |
585 | /* should already have all buftargs initialised */ | |
586 | if (mp->m_ddev_targp->dev != dev || | |
587 | mp->m_ddev_targp->bt_mount != mp) { | |
588 | fprintf(stderr, | |
589 | _("%s: bad buftarg reinit, ddev\n"), | |
590 | progname); | |
591 | exit(1); | |
592 | } | |
593 | if (!logdev || logdev == dev) { | |
594 | if (mp->m_logdev_targp != mp->m_ddev_targp) { | |
595 | fprintf(stderr, | |
596 | _("%s: bad buftarg reinit, ldev mismatch\n"), | |
597 | progname); | |
598 | exit(1); | |
599 | } | |
600 | } else if (mp->m_logdev_targp->dev != logdev || | |
601 | mp->m_logdev_targp->bt_mount != mp) { | |
602 | fprintf(stderr, | |
603 | _("%s: bad buftarg reinit, logdev\n"), | |
604 | progname); | |
605 | exit(1); | |
606 | } | |
607 | if (rtdev && (mp->m_rtdev_targp->dev != rtdev || | |
608 | mp->m_rtdev_targp->bt_mount != mp)) { | |
609 | fprintf(stderr, | |
610 | _("%s: bad buftarg reinit, rtdev\n"), | |
611 | progname); | |
612 | exit(1); | |
613 | } | |
614 | return; | |
615 | } | |
616 | ||
617 | mp->m_ddev_targp = libxfs_buftarg_alloc(mp, dev); | |
618 | if (!logdev || logdev == dev) | |
619 | mp->m_logdev_targp = mp->m_ddev_targp; | |
620 | else | |
621 | mp->m_logdev_targp = libxfs_buftarg_alloc(mp, logdev); | |
622 | mp->m_rtdev_targp = libxfs_buftarg_alloc(mp, rtdev); | |
623 | } | |
624 | ||
2bd0ea18 NS |
625 | /* |
626 | * Mount structure initialization, provides a filled-in xfs_mount_t | |
627 | * such that the numerous XFS_* macros can be used. If dev is zero, | |
628 | * no IO will be performed (no size checks, read root inodes). | |
629 | */ | |
630 | xfs_mount_t * | |
631 | libxfs_mount( | |
632 | xfs_mount_t *mp, | |
633 | xfs_sb_t *sb, | |
634 | dev_t dev, | |
635 | dev_t logdev, | |
636 | dev_t rtdev, | |
4ca431fc | 637 | int flags) |
2bd0ea18 NS |
638 | { |
639 | xfs_daddr_t d; | |
640 | xfs_buf_t *bp; | |
641 | xfs_sb_t *sbp; | |
2bd0ea18 NS |
642 | int error; |
643 | ||
75c8b434 DC |
644 | libxfs_buftarg_init(mp, dev, logdev, rtdev); |
645 | ||
6239071d | 646 | mp->m_flags = (LIBXFS_MOUNT_32BITINODES|LIBXFS_MOUNT_32BITINOOPT); |
2bd0ea18 | 647 | mp->m_sb = *sb; |
56b2de80 | 648 | INIT_RADIX_TREE(&mp->m_perag_tree, GFP_KERNEL); |
2bd0ea18 | 649 | sbp = &(mp->m_sb); |
2bd0ea18 | 650 | |
4896e6c8 | 651 | xfs_sb_mount_common(mp, sb); |
2bd0ea18 | 652 | |
5e656dbb BN |
653 | xfs_alloc_compute_maxlevels(mp); |
654 | xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK); | |
655 | xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK); | |
656 | xfs_ialloc_compute_maxlevels(mp); | |
b3a96b46 | 657 | xfs_rmapbt_compute_maxlevels(mp); |
e7be6330 | 658 | xfs_refcountbt_compute_maxlevels(mp); |
2bd0ea18 NS |
659 | |
660 | if (sbp->sb_imax_pct) { | |
661 | /* Make sure the maximum inode count is a multiple of the | |
662 | * units we allocate inodes in. | |
663 | */ | |
664 | mp->m_maxicount = (sbp->sb_dblocks * sbp->sb_imax_pct) / 100; | |
7516da71 DW |
665 | mp->m_maxicount = XFS_FSB_TO_INO(mp, |
666 | (mp->m_maxicount / mp->m_ialloc_blks) * | |
667 | mp->m_ialloc_blks); | |
2bd0ea18 NS |
668 | } else |
669 | mp->m_maxicount = 0; | |
670 | ||
671 | mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE; | |
2a3145a8 DW |
672 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
673 | int new_size = mp->m_inode_cluster_size; | |
674 | ||
675 | new_size *= mp->m_sb.sb_inodesize / XFS_DINODE_MIN_SIZE; | |
676 | if (mp->m_sb.sb_inoalignmt >= XFS_B_TO_FSBT(mp, new_size)) | |
677 | mp->m_inode_cluster_size = new_size; | |
678 | } | |
42d896cf DW |
679 | mp->m_blocks_per_cluster = xfs_icluster_size_fsb(mp); |
680 | mp->m_inodes_per_cluster = XFS_FSB_TO_INO(mp, mp->m_blocks_per_cluster); | |
bafa1332 DW |
681 | mp->m_cluster_align = xfs_ialloc_cluster_alignment(mp); |
682 | mp->m_cluster_align_inodes = XFS_FSB_TO_INO(mp, mp->m_cluster_align); | |
2bd0ea18 | 683 | |
949c0f10 NS |
684 | /* |
685 | * Set whether we're using stripe alignment. | |
686 | */ | |
5e656dbb | 687 | if (xfs_sb_version_hasdalign(&mp->m_sb)) { |
949c0f10 NS |
688 | mp->m_dalign = sbp->sb_unit; |
689 | mp->m_swidth = sbp->sb_width; | |
690 | } | |
691 | ||
2bd0ea18 NS |
692 | /* |
693 | * Set whether we're using inode alignment. | |
694 | */ | |
5e656dbb | 695 | if (xfs_sb_version_hasalign(&mp->m_sb) && |
2bd0ea18 NS |
696 | mp->m_sb.sb_inoalignmt >= |
697 | XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size)) | |
698 | mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1; | |
699 | else | |
700 | mp->m_inoalign_mask = 0; | |
701 | /* | |
702 | * If we are using stripe alignment, check whether | |
703 | * the stripe unit is a multiple of the inode alignment | |
704 | */ | |
b3563c19 | 705 | if (mp->m_dalign && mp->m_inoalign_mask && |
5e656dbb | 706 | !(mp->m_dalign & mp->m_inoalign_mask)) |
2bd0ea18 NS |
707 | mp->m_sinoalign = mp->m_dalign; |
708 | else | |
709 | mp->m_sinoalign = 0; | |
710 | ||
711 | /* | |
712 | * Check that the data (and log if separate) are an ok size. | |
713 | */ | |
9440d84d | 714 | d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks); |
2bd0ea18 | 715 | if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) { |
9440d84d | 716 | fprintf(stderr, _("%s: size check failed\n"), progname); |
4ca431fc NS |
717 | if (!(flags & LIBXFS_MOUNT_DEBUGGER)) |
718 | return NULL; | |
2bd0ea18 NS |
719 | } |
720 | ||
ff105f75 DC |
721 | /* |
722 | * We automatically convert v1 inodes to v2 inodes now, so if | |
723 | * the NLINK bit is not set we can't operate on the filesystem. | |
724 | */ | |
725 | if (!(sbp->sb_versionnum & XFS_SB_VERSION_NLINKBIT)) { | |
726 | ||
727 | fprintf(stderr, _( | |
728 | "%s: V1 inodes unsupported. Please try an older xfsprogs.\n"), | |
729 | progname); | |
730 | exit(1); | |
731 | } | |
732 | ||
733 | /* Check for supported directory formats */ | |
734 | if (!(sbp->sb_versionnum & XFS_SB_VERSION_DIRV2BIT)) { | |
9a048535 DC |
735 | |
736 | fprintf(stderr, _( | |
737 | "%s: V1 directories unsupported. Please try an older xfsprogs.\n"), | |
738 | progname); | |
739 | exit(1); | |
5e656dbb | 740 | } |
2bd0ea18 | 741 | |
ff105f75 DC |
742 | /* check for unsupported other features */ |
743 | if (!xfs_sb_good_version(sbp)) { | |
744 | fprintf(stderr, _( | |
745 | "%s: Unsupported features detected. Please try a newer xfsprogs.\n"), | |
746 | progname); | |
747 | exit(1); | |
748 | } | |
749 | ||
750 | xfs_da_mount(mp); | |
751 | ||
5e656dbb BN |
752 | if (xfs_sb_version_hasattr2(&mp->m_sb)) |
753 | mp->m_flags |= LIBXFS_MOUNT_ATTR2; | |
57c9fccb | 754 | |
2bd0ea18 | 755 | /* Initialize the precomputed transaction reservations values */ |
5e656dbb | 756 | xfs_trans_init(mp); |
2bd0ea18 NS |
757 | |
758 | if (dev == 0) /* maxtrres, we have no device so leave now */ | |
759 | return mp; | |
760 | ||
9440d84d | 761 | bp = libxfs_readbuf(mp->m_dev, |
4ca431fc | 762 | d - XFS_FSS_TO_BB(mp, 1), XFS_FSS_TO_BB(mp, 1), |
75c8b434 | 763 | !(flags & LIBXFS_MOUNT_DEBUGGER), NULL); |
9440d84d NS |
764 | if (!bp) { |
765 | fprintf(stderr, _("%s: data size check failed\n"), progname); | |
4ca431fc NS |
766 | if (!(flags & LIBXFS_MOUNT_DEBUGGER)) |
767 | return NULL; | |
32244196 BN |
768 | } else |
769 | libxfs_putbuf(bp); | |
2bd0ea18 | 770 | |
75c8b434 DC |
771 | if (mp->m_logdev_targp->dev && |
772 | mp->m_logdev_targp->dev != mp->m_ddev_targp->dev) { | |
9440d84d | 773 | d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks); |
2bd0ea18 | 774 | if ( (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) || |
75c8b434 | 775 | (!(bp = libxfs_readbuf(mp->m_logdev_targp, |
9440d84d | 776 | d - XFS_FSB_TO_BB(mp, 1), |
4ca431fc | 777 | XFS_FSB_TO_BB(mp, 1), |
75c8b434 | 778 | !(flags & LIBXFS_MOUNT_DEBUGGER), NULL))) ) { |
9440d84d | 779 | fprintf(stderr, _("%s: log size checks failed\n"), |
2bd0ea18 | 780 | progname); |
4ca431fc NS |
781 | if (!(flags & LIBXFS_MOUNT_DEBUGGER)) |
782 | return NULL; | |
2bd0ea18 | 783 | } |
32244196 BN |
784 | if (bp) |
785 | libxfs_putbuf(bp); | |
2bd0ea18 NS |
786 | } |
787 | ||
788 | /* Initialize realtime fields in the mount structure */ | |
39798eb5 | 789 | if (rtmount_init(mp, flags)) { |
9440d84d NS |
790 | fprintf(stderr, _("%s: realtime device init failed\n"), |
791 | progname); | |
4ca431fc | 792 | return NULL; |
2bd0ea18 NS |
793 | } |
794 | ||
a547152d ES |
795 | /* |
796 | * libxfs_initialize_perag will allocate a perag structure for each ag. | |
797 | * If agcount is corrupted and insanely high, this will OOM the box. | |
798 | * If the agount seems (arbitrarily) high, try to read what would be | |
799 | * the last AG, and if that fails for a relatively high agcount, just | |
800 | * read the first one and let the user know to check the geometry. | |
801 | */ | |
802 | if (sbp->sb_agcount > 1000000) { | |
803 | bp = libxfs_readbuf(mp->m_dev, | |
804 | XFS_AG_DADDR(mp, sbp->sb_agcount - 1, 0), 1, | |
805 | !(flags & LIBXFS_MOUNT_DEBUGGER), NULL); | |
806 | if (bp->b_error) { | |
807 | fprintf(stderr, _("%s: read of AG %u failed\n"), | |
808 | progname, sbp->sb_agcount); | |
809 | if (!(flags & LIBXFS_MOUNT_DEBUGGER)) | |
810 | return NULL; | |
811 | fprintf(stderr, _("%s: limiting reads to AG 0\n"), | |
812 | progname); | |
813 | sbp->sb_agcount = 1; | |
814 | } | |
815 | libxfs_putbuf(bp); | |
816 | } | |
817 | ||
56b2de80 DC |
818 | error = libxfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi); |
819 | if (error) { | |
820 | fprintf(stderr, _("%s: perag init failed\n"), | |
821 | progname); | |
2bd0ea18 NS |
822 | exit(1); |
823 | } | |
824 | ||
2bd0ea18 NS |
825 | return mp; |
826 | } | |
827 | ||
f1b058f9 NS |
828 | void |
829 | libxfs_rtmount_destroy(xfs_mount_t *mp) | |
830 | { | |
831 | if (mp->m_rsumip) | |
31845e4c | 832 | libxfs_irele(mp->m_rsumip); |
f1b058f9 | 833 | if (mp->m_rbmip) |
31845e4c | 834 | libxfs_irele(mp->m_rbmip); |
f1b058f9 NS |
835 | mp->m_rsumip = mp->m_rbmip = NULL; |
836 | } | |
837 | ||
2bd0ea18 | 838 | /* |
9440d84d | 839 | * Release any resource obtained during a mount. |
2bd0ea18 NS |
840 | */ |
841 | void | |
842 | libxfs_umount(xfs_mount_t *mp) | |
843 | { | |
56b2de80 DC |
844 | struct xfs_perag *pag; |
845 | int agno; | |
846 | ||
f1b058f9 | 847 | libxfs_rtmount_destroy(mp); |
f1b058f9 NS |
848 | libxfs_bcache_purge(); |
849 | ||
56b2de80 DC |
850 | for (agno = 0; agno < mp->m_maxagi; agno++) { |
851 | pag = radix_tree_delete(&mp->m_perag_tree, agno); | |
852 | kmem_free(pag); | |
fceb0d99 | 853 | } |
4334e2e8 ES |
854 | |
855 | kmem_free(mp->m_attr_geo); | |
856 | kmem_free(mp->m_dir_geo); | |
857 | ||
858 | kmem_free(mp->m_rtdev_targp); | |
859 | if (mp->m_logdev_targp != mp->m_ddev_targp) | |
860 | kmem_free(mp->m_logdev_targp); | |
861 | kmem_free(mp->m_ddev_targp); | |
f8149110 | 862 | |
2bd0ea18 | 863 | } |
f1b058f9 NS |
864 | |
865 | /* | |
866 | * Release any global resources used by libxfs. | |
867 | */ | |
868 | void | |
869 | libxfs_destroy(void) | |
870 | { | |
44488491 ES |
871 | int leaked; |
872 | ||
864028ed ES |
873 | /* Free everything from the buffer cache before freeing buffer zone */ |
874 | libxfs_bcache_purge(); | |
875 | libxfs_bcache_free(); | |
f1b058f9 | 876 | cache_destroy(libxfs_bcache); |
44488491 ES |
877 | leaked = manage_zones(1); |
878 | if (getenv("LIBXFS_LEAK_CHECK") && leaked) | |
879 | exit(1); | |
f1b058f9 | 880 | } |
9f38f08d | 881 | |
b74a1f6a NS |
882 | int |
883 | libxfs_device_alignment(void) | |
884 | { | |
885 | return platform_align_blockdev(); | |
886 | } | |
887 | ||
9f38f08d | 888 | void |
b6281496 | 889 | libxfs_report(FILE *fp) |
9f38f08d | 890 | { |
cb5b3ef4 MV |
891 | time_t t; |
892 | char *c; | |
893 | ||
b6281496 | 894 | cache_report(fp, "libxfs_bcache", libxfs_bcache); |
cb5b3ef4 MV |
895 | |
896 | t = time(NULL); | |
897 | c = asctime(localtime(&t)); | |
898 | fprintf(fp, "%s", c); | |
899 | } | |
3b6ac903 MV |
900 | |
901 | int | |
902 | libxfs_nproc(void) | |
903 | { | |
30a757b7 DW |
904 | int nr; |
905 | ||
906 | nr = platform_nproc(); | |
907 | if (nr < 1) | |
908 | nr = 1; | |
909 | return nr; | |
3b6ac903 | 910 | } |
2556c98b BN |
911 | |
912 | unsigned long | |
913 | libxfs_physmem(void) | |
914 | { | |
915 | return platform_physmem(); | |
e08f5594 | 916 | } |