1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. 4 * All Rights Reserved. 5 */ 6 #include "xfs.h" 7 #include "xfs_fs.h" 8 #include "xfs_shared.h" 9 #include "xfs_format.h" 10 #include "xfs_log_format.h" 11 #include "xfs_trans_resv.h" 12 #include "xfs_mount.h" 13 #include "xfs_inode.h" 14 #include "xfs_btree.h" 15 #include "xfs_ialloc.h" 16 #include "xfs_ialloc_btree.h" 17 #include "xfs_itable.h" 18 #include "xfs_error.h" 19 #include "xfs_trace.h" 20 #include "xfs_icache.h" 21 22 /* 23 * Return stat information for one inode. 24 * Return 0 if ok, else errno. 25 */ 26 int 27 xfs_bulkstat_one_int( 28 struct xfs_mount *mp, /* mount point for filesystem */ 29 xfs_ino_t ino, /* inode to get data for */ 30 void __user *buffer, /* buffer to place output in */ 31 int ubsize, /* size of buffer */ 32 bulkstat_one_fmt_pf formatter, /* formatter, copy to user */ 33 int *ubused, /* bytes used by me */ 34 int *stat) /* BULKSTAT_RV_... */ 35 { 36 struct xfs_icdinode *dic; /* dinode core info pointer */ 37 struct xfs_inode *ip; /* incore inode pointer */ 38 struct inode *inode; 39 struct xfs_bstat *buf; /* return buffer */ 40 int error = 0; /* error value */ 41 42 *stat = BULKSTAT_RV_NOTHING; 43 44 if (!buffer || xfs_internal_inum(mp, ino)) 45 return -EINVAL; 46 47 buf = kmem_zalloc(sizeof(*buf), KM_SLEEP | KM_MAYFAIL); 48 if (!buf) 49 return -ENOMEM; 50 51 error = xfs_iget(mp, NULL, ino, 52 (XFS_IGET_DONTCACHE | XFS_IGET_UNTRUSTED), 53 XFS_ILOCK_SHARED, &ip); 54 if (error) 55 goto out_free; 56 57 ASSERT(ip != NULL); 58 ASSERT(ip->i_imap.im_blkno != 0); 59 inode = VFS_I(ip); 60 61 dic = &ip->i_d; 62 63 /* xfs_iget returns the following without needing 64 * further change. 65 */ 66 buf->bs_projid_lo = dic->di_projid_lo; 67 buf->bs_projid_hi = dic->di_projid_hi; 68 buf->bs_ino = ino; 69 buf->bs_uid = dic->di_uid; 70 buf->bs_gid = dic->di_gid; 71 buf->bs_size = dic->di_size; 72 73 buf->bs_nlink = inode->i_nlink; 74 buf->bs_atime.tv_sec = inode->i_atime.tv_sec; 75 buf->bs_atime.tv_nsec = inode->i_atime.tv_nsec; 76 buf->bs_mtime.tv_sec = inode->i_mtime.tv_sec; 77 buf->bs_mtime.tv_nsec = inode->i_mtime.tv_nsec; 78 buf->bs_ctime.tv_sec = inode->i_ctime.tv_sec; 79 buf->bs_ctime.tv_nsec = inode->i_ctime.tv_nsec; 80 buf->bs_gen = inode->i_generation; 81 buf->bs_mode = inode->i_mode; 82 83 buf->bs_xflags = xfs_ip2xflags(ip); 84 buf->bs_extsize = dic->di_extsize << mp->m_sb.sb_blocklog; 85 buf->bs_extents = dic->di_nextents; 86 memset(buf->bs_pad, 0, sizeof(buf->bs_pad)); 87 buf->bs_dmevmask = dic->di_dmevmask; 88 buf->bs_dmstate = dic->di_dmstate; 89 buf->bs_aextents = dic->di_anextents; 90 buf->bs_forkoff = XFS_IFORK_BOFF(ip); 91 92 if (dic->di_version == 3) { 93 if (dic->di_flags2 & XFS_DIFLAG2_COWEXTSIZE) 94 buf->bs_cowextsize = dic->di_cowextsize << 95 mp->m_sb.sb_blocklog; 96 } 97 98 switch (dic->di_format) { 99 case XFS_DINODE_FMT_DEV: 100 buf->bs_rdev = sysv_encode_dev(inode->i_rdev); 101 buf->bs_blksize = BLKDEV_IOSIZE; 102 buf->bs_blocks = 0; 103 break; 104 case XFS_DINODE_FMT_LOCAL: 105 buf->bs_rdev = 0; 106 buf->bs_blksize = mp->m_sb.sb_blocksize; 107 buf->bs_blocks = 0; 108 break; 109 case XFS_DINODE_FMT_EXTENTS: 110 case XFS_DINODE_FMT_BTREE: 111 buf->bs_rdev = 0; 112 buf->bs_blksize = mp->m_sb.sb_blocksize; 113 buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks; 114 break; 115 } 116 xfs_iunlock(ip, XFS_ILOCK_SHARED); 117 xfs_irele(ip); 118 119 error = formatter(buffer, ubsize, ubused, buf); 120 if (!error) 121 *stat = BULKSTAT_RV_DIDONE; 122 123 out_free: 124 kmem_free(buf); 125 return error; 126 } 127 128 /* Return 0 on success or positive error */ 129 STATIC int 130 xfs_bulkstat_one_fmt( 131 void __user *ubuffer, 132 int ubsize, 133 int *ubused, 134 const xfs_bstat_t *buffer) 135 { 136 if (ubsize < sizeof(*buffer)) 137 return -ENOMEM; 138 if (copy_to_user(ubuffer, buffer, sizeof(*buffer))) 139 return -EFAULT; 140 if (ubused) 141 *ubused = sizeof(*buffer); 142 return 0; 143 } 144 145 int 146 xfs_bulkstat_one( 147 xfs_mount_t *mp, /* mount point for filesystem */ 148 xfs_ino_t ino, /* inode number to get data for */ 149 void __user *buffer, /* buffer to place output in */ 150 int ubsize, /* size of buffer */ 151 int *ubused, /* bytes used by me */ 152 int *stat) /* BULKSTAT_RV_... */ 153 { 154 return xfs_bulkstat_one_int(mp, ino, buffer, ubsize, 155 xfs_bulkstat_one_fmt, ubused, stat); 156 } 157 158 /* 159 * Loop over all clusters in a chunk for a given incore inode allocation btree 160 * record. Do a readahead if there are any allocated inodes in that cluster. 161 */ 162 STATIC void 163 xfs_bulkstat_ichunk_ra( 164 struct xfs_mount *mp, 165 xfs_agnumber_t agno, 166 struct xfs_inobt_rec_incore *irec) 167 { 168 xfs_agblock_t agbno; 169 struct blk_plug plug; 170 int i; /* inode chunk index */ 171 172 agbno = XFS_AGINO_TO_AGBNO(mp, irec->ir_startino); 173 174 blk_start_plug(&plug); 175 for (i = 0; i < XFS_INODES_PER_CHUNK; 176 i += mp->m_inodes_per_cluster, agbno += mp->m_blocks_per_cluster) { 177 if (xfs_inobt_maskn(i, mp->m_inodes_per_cluster) & 178 ~irec->ir_free) { 179 xfs_btree_reada_bufs(mp, agno, agbno, 180 mp->m_blocks_per_cluster, 181 &xfs_inode_buf_ops); 182 } 183 } 184 blk_finish_plug(&plug); 185 } 186 187 /* 188 * Lookup the inode chunk that the given inode lives in and then get the record 189 * if we found the chunk. If the inode was not the last in the chunk and there 190 * are some left allocated, update the data for the pointed-to record as well as 191 * return the count of grabbed inodes. 192 */ 193 STATIC int 194 xfs_bulkstat_grab_ichunk( 195 struct xfs_btree_cur *cur, /* btree cursor */ 196 xfs_agino_t agino, /* starting inode of chunk */ 197 int *icount,/* return # of inodes grabbed */ 198 struct xfs_inobt_rec_incore *irec) /* btree record */ 199 { 200 int idx; /* index into inode chunk */ 201 int stat; 202 int error = 0; 203 204 /* Lookup the inode chunk that this inode lives in */ 205 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &stat); 206 if (error) 207 return error; 208 if (!stat) { 209 *icount = 0; 210 return error; 211 } 212 213 /* Get the record, should always work */ 214 error = xfs_inobt_get_rec(cur, irec, &stat); 215 if (error) 216 return error; 217 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, stat == 1); 218 219 /* Check if the record contains the inode in request */ 220 if (irec->ir_startino + XFS_INODES_PER_CHUNK <= agino) { 221 *icount = 0; 222 return 0; 223 } 224 225 idx = agino - irec->ir_startino + 1; 226 if (idx < XFS_INODES_PER_CHUNK && 227 (xfs_inobt_maskn(idx, XFS_INODES_PER_CHUNK - idx) & ~irec->ir_free)) { 228 int i; 229 230 /* We got a right chunk with some left inodes allocated at it. 231 * Grab the chunk record. Mark all the uninteresting inodes 232 * free -- because they're before our start point. 233 */ 234 for (i = 0; i < idx; i++) { 235 if (XFS_INOBT_MASK(i) & ~irec->ir_free) 236 irec->ir_freecount++; 237 } 238 239 irec->ir_free |= xfs_inobt_maskn(0, idx); 240 *icount = irec->ir_count - irec->ir_freecount; 241 } 242 243 return 0; 244 } 245 246 #define XFS_BULKSTAT_UBLEFT(ubleft) ((ubleft) >= statstruct_size) 247 248 struct xfs_bulkstat_agichunk { 249 char __user **ac_ubuffer;/* pointer into user's buffer */ 250 int ac_ubleft; /* bytes left in user's buffer */ 251 int ac_ubelem; /* spaces used in user's buffer */ 252 }; 253 254 /* 255 * Process inodes in chunk with a pointer to a formatter function 256 * that will iget the inode and fill in the appropriate structure. 257 */ 258 static int 259 xfs_bulkstat_ag_ichunk( 260 struct xfs_mount *mp, 261 xfs_agnumber_t agno, 262 struct xfs_inobt_rec_incore *irbp, 263 bulkstat_one_pf formatter, 264 size_t statstruct_size, 265 struct xfs_bulkstat_agichunk *acp, 266 xfs_agino_t *last_agino) 267 { 268 char __user **ubufp = acp->ac_ubuffer; 269 int chunkidx; 270 int error = 0; 271 xfs_agino_t agino = irbp->ir_startino; 272 273 for (chunkidx = 0; chunkidx < XFS_INODES_PER_CHUNK; 274 chunkidx++, agino++) { 275 int fmterror; 276 int ubused; 277 278 /* inode won't fit in buffer, we are done */ 279 if (acp->ac_ubleft < statstruct_size) 280 break; 281 282 /* Skip if this inode is free */ 283 if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free) 284 continue; 285 286 /* Get the inode and fill in a single buffer */ 287 ubused = statstruct_size; 288 error = formatter(mp, XFS_AGINO_TO_INO(mp, agno, agino), 289 *ubufp, acp->ac_ubleft, &ubused, &fmterror); 290 291 if (fmterror == BULKSTAT_RV_GIVEUP || 292 (error && error != -ENOENT && error != -EINVAL)) { 293 acp->ac_ubleft = 0; 294 ASSERT(error); 295 break; 296 } 297 298 /* be careful not to leak error if at end of chunk */ 299 if (fmterror == BULKSTAT_RV_NOTHING || error) { 300 error = 0; 301 continue; 302 } 303 304 *ubufp += ubused; 305 acp->ac_ubleft -= ubused; 306 acp->ac_ubelem++; 307 } 308 309 /* 310 * Post-update *last_agino. At this point, agino will always point one 311 * inode past the last inode we processed successfully. Hence we 312 * substract that inode when setting the *last_agino cursor so that we 313 * return the correct cookie to userspace. On the next bulkstat call, 314 * the inode under the lastino cookie will be skipped as we have already 315 * processed it here. 316 */ 317 *last_agino = agino - 1; 318 319 return error; 320 } 321 322 /* 323 * Return stat information in bulk (by-inode) for the filesystem. 324 */ 325 int /* error status */ 326 xfs_bulkstat( 327 xfs_mount_t *mp, /* mount point for filesystem */ 328 xfs_ino_t *lastinop, /* last inode returned */ 329 int *ubcountp, /* size of buffer/count returned */ 330 bulkstat_one_pf formatter, /* func that'd fill a single buf */ 331 size_t statstruct_size, /* sizeof struct filling */ 332 char __user *ubuffer, /* buffer with inode stats */ 333 int *done) /* 1 if there are more stats to get */ 334 { 335 xfs_buf_t *agbp; /* agi header buffer */ 336 xfs_agino_t agino; /* inode # in allocation group */ 337 xfs_agnumber_t agno; /* allocation group number */ 338 xfs_btree_cur_t *cur; /* btree cursor for ialloc btree */ 339 xfs_inobt_rec_incore_t *irbuf; /* start of irec buffer */ 340 int nirbuf; /* size of irbuf */ 341 int ubcount; /* size of user's buffer */ 342 struct xfs_bulkstat_agichunk ac; 343 int error = 0; 344 345 /* 346 * Get the last inode value, see if there's nothing to do. 347 */ 348 agno = XFS_INO_TO_AGNO(mp, *lastinop); 349 agino = XFS_INO_TO_AGINO(mp, *lastinop); 350 if (agno >= mp->m_sb.sb_agcount || 351 *lastinop != XFS_AGINO_TO_INO(mp, agno, agino)) { 352 *done = 1; 353 *ubcountp = 0; 354 return 0; 355 } 356 357 ubcount = *ubcountp; /* statstruct's */ 358 ac.ac_ubuffer = &ubuffer; 359 ac.ac_ubleft = ubcount * statstruct_size; /* bytes */; 360 ac.ac_ubelem = 0; 361 362 *ubcountp = 0; 363 *done = 0; 364 365 irbuf = kmem_zalloc_large(PAGE_SIZE * 4, KM_SLEEP); 366 if (!irbuf) 367 return -ENOMEM; 368 nirbuf = (PAGE_SIZE * 4) / sizeof(*irbuf); 369 370 /* 371 * Loop over the allocation groups, starting from the last 372 * inode returned; 0 means start of the allocation group. 373 */ 374 while (agno < mp->m_sb.sb_agcount) { 375 struct xfs_inobt_rec_incore *irbp = irbuf; 376 struct xfs_inobt_rec_incore *irbufend = irbuf + nirbuf; 377 bool end_of_ag = false; 378 int icount = 0; 379 int stat; 380 381 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp); 382 if (error) 383 break; 384 /* 385 * Allocate and initialize a btree cursor for ialloc btree. 386 */ 387 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno, 388 XFS_BTNUM_INO); 389 if (agino > 0) { 390 /* 391 * In the middle of an allocation group, we need to get 392 * the remainder of the chunk we're in. 393 */ 394 struct xfs_inobt_rec_incore r; 395 396 error = xfs_bulkstat_grab_ichunk(cur, agino, &icount, &r); 397 if (error) 398 goto del_cursor; 399 if (icount) { 400 irbp->ir_startino = r.ir_startino; 401 irbp->ir_holemask = r.ir_holemask; 402 irbp->ir_count = r.ir_count; 403 irbp->ir_freecount = r.ir_freecount; 404 irbp->ir_free = r.ir_free; 405 irbp++; 406 } 407 /* Increment to the next record */ 408 error = xfs_btree_increment(cur, 0, &stat); 409 } else { 410 /* Start of ag. Lookup the first inode chunk */ 411 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &stat); 412 } 413 if (error || stat == 0) { 414 end_of_ag = true; 415 goto del_cursor; 416 } 417 418 /* 419 * Loop through inode btree records in this ag, 420 * until we run out of inodes or space in the buffer. 421 */ 422 while (irbp < irbufend && icount < ubcount) { 423 struct xfs_inobt_rec_incore r; 424 425 error = xfs_inobt_get_rec(cur, &r, &stat); 426 if (error || stat == 0) { 427 end_of_ag = true; 428 goto del_cursor; 429 } 430 431 /* 432 * If this chunk has any allocated inodes, save it. 433 * Also start read-ahead now for this chunk. 434 */ 435 if (r.ir_freecount < r.ir_count) { 436 xfs_bulkstat_ichunk_ra(mp, agno, &r); 437 irbp->ir_startino = r.ir_startino; 438 irbp->ir_holemask = r.ir_holemask; 439 irbp->ir_count = r.ir_count; 440 irbp->ir_freecount = r.ir_freecount; 441 irbp->ir_free = r.ir_free; 442 irbp++; 443 icount += r.ir_count - r.ir_freecount; 444 } 445 error = xfs_btree_increment(cur, 0, &stat); 446 if (error || stat == 0) { 447 end_of_ag = true; 448 goto del_cursor; 449 } 450 cond_resched(); 451 } 452 453 /* 454 * Drop the btree buffers and the agi buffer as we can't hold any 455 * of the locks these represent when calling iget. If there is a 456 * pending error, then we are done. 457 */ 458 del_cursor: 459 xfs_btree_del_cursor(cur, error); 460 xfs_buf_relse(agbp); 461 if (error) 462 break; 463 /* 464 * Now format all the good inodes into the user's buffer. The 465 * call to xfs_bulkstat_ag_ichunk() sets up the agino pointer 466 * for the next loop iteration. 467 */ 468 irbufend = irbp; 469 for (irbp = irbuf; 470 irbp < irbufend && ac.ac_ubleft >= statstruct_size; 471 irbp++) { 472 error = xfs_bulkstat_ag_ichunk(mp, agno, irbp, 473 formatter, statstruct_size, &ac, 474 &agino); 475 if (error) 476 break; 477 478 cond_resched(); 479 } 480 481 /* 482 * If we've run out of space or had a formatting error, we 483 * are now done 484 */ 485 if (ac.ac_ubleft < statstruct_size || error) 486 break; 487 488 if (end_of_ag) { 489 agno++; 490 agino = 0; 491 } 492 } 493 /* 494 * Done, we're either out of filesystem or space to put the data. 495 */ 496 kmem_free(irbuf); 497 *ubcountp = ac.ac_ubelem; 498 499 /* 500 * We found some inodes, so clear the error status and return them. 501 * The lastino pointer will point directly at the inode that triggered 502 * any error that occurred, so on the next call the error will be 503 * triggered again and propagated to userspace as there will be no 504 * formatted inodes in the buffer. 505 */ 506 if (ac.ac_ubelem) 507 error = 0; 508 509 /* 510 * If we ran out of filesystem, lastino will point off the end of 511 * the filesystem so the next call will return immediately. 512 */ 513 *lastinop = XFS_AGINO_TO_INO(mp, agno, agino); 514 if (agno >= mp->m_sb.sb_agcount) 515 *done = 1; 516 517 return error; 518 } 519 520 int 521 xfs_inumbers_fmt( 522 void __user *ubuffer, /* buffer to write to */ 523 const struct xfs_inogrp *buffer, /* buffer to read from */ 524 long count, /* # of elements to read */ 525 long *written) /* # of bytes written */ 526 { 527 if (copy_to_user(ubuffer, buffer, count * sizeof(*buffer))) 528 return -EFAULT; 529 *written = count * sizeof(*buffer); 530 return 0; 531 } 532 533 /* 534 * Return inode number table for the filesystem. 535 */ 536 int /* error status */ 537 xfs_inumbers( 538 struct xfs_mount *mp,/* mount point for filesystem */ 539 xfs_ino_t *lastino,/* last inode returned */ 540 int *count,/* size of buffer/count returned */ 541 void __user *ubuffer,/* buffer with inode descriptions */ 542 inumbers_fmt_pf formatter) 543 { 544 xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, *lastino); 545 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, *lastino); 546 struct xfs_btree_cur *cur = NULL; 547 struct xfs_buf *agbp = NULL; 548 struct xfs_inogrp *buffer; 549 int bcount; 550 int left = *count; 551 int bufidx = 0; 552 int error = 0; 553 554 *count = 0; 555 if (agno >= mp->m_sb.sb_agcount || 556 *lastino != XFS_AGINO_TO_INO(mp, agno, agino)) 557 return error; 558 559 bcount = min(left, (int)(PAGE_SIZE / sizeof(*buffer))); 560 buffer = kmem_zalloc(bcount * sizeof(*buffer), KM_SLEEP); 561 do { 562 struct xfs_inobt_rec_incore r; 563 int stat; 564 565 if (!agbp) { 566 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp); 567 if (error) 568 break; 569 570 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno, 571 XFS_BTNUM_INO); 572 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_GE, 573 &stat); 574 if (error) 575 break; 576 if (!stat) 577 goto next_ag; 578 } 579 580 error = xfs_inobt_get_rec(cur, &r, &stat); 581 if (error) 582 break; 583 if (!stat) 584 goto next_ag; 585 586 agino = r.ir_startino + XFS_INODES_PER_CHUNK - 1; 587 buffer[bufidx].xi_startino = 588 XFS_AGINO_TO_INO(mp, agno, r.ir_startino); 589 buffer[bufidx].xi_alloccount = r.ir_count - r.ir_freecount; 590 buffer[bufidx].xi_allocmask = ~r.ir_free; 591 if (++bufidx == bcount) { 592 long written; 593 594 error = formatter(ubuffer, buffer, bufidx, &written); 595 if (error) 596 break; 597 ubuffer += written; 598 *count += bufidx; 599 bufidx = 0; 600 } 601 if (!--left) 602 break; 603 604 error = xfs_btree_increment(cur, 0, &stat); 605 if (error) 606 break; 607 if (stat) 608 continue; 609 610 next_ag: 611 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 612 cur = NULL; 613 xfs_buf_relse(agbp); 614 agbp = NULL; 615 agino = 0; 616 agno++; 617 } while (agno < mp->m_sb.sb_agcount); 618 619 if (!error) { 620 if (bufidx) { 621 long written; 622 623 error = formatter(ubuffer, buffer, bufidx, &written); 624 if (!error) 625 *count += bufidx; 626 } 627 *lastino = XFS_AGINO_TO_INO(mp, agno, agino); 628 } 629 630 kmem_free(buffer); 631 if (cur) 632 xfs_btree_del_cursor(cur, error); 633 if (agbp) 634 xfs_buf_relse(agbp); 635 636 return error; 637 } 638