1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/fs/ufs/ufs_dir.c 4 * 5 * Copyright (C) 1996 6 * Adrian Rodriguez (adrian@franklins-tower.rutgers.edu) 7 * Laboratory for Computer Science Research Computing Facility 8 * Rutgers, The State University of New Jersey 9 * 10 * swab support by Francois-Rene Rideau <fare@tunes.org> 19970406 11 * 12 * 4.4BSD (FreeBSD) support added on February 1st 1998 by 13 * Niels Kristian Bech Jensen <nkbj@image.dk> partially based 14 * on code by Martin von Loewis <martin@mira.isdn.cs.tu-berlin.de>. 15 * 16 * Migration to usage of "page cache" on May 2006 by 17 * Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base. 18 */ 19 20 #include <linux/time.h> 21 #include <linux/fs.h> 22 #include <linux/swap.h> 23 24 #include "ufs_fs.h" 25 #include "ufs.h" 26 #include "swab.h" 27 #include "util.h" 28 29 /* 30 * NOTE! unlike strncmp, ufs_match returns 1 for success, 0 for failure. 31 * 32 * len <= UFS_MAXNAMLEN and de != NULL are guaranteed by caller. 33 */ 34 static inline int ufs_match(struct super_block *sb, int len, 35 const unsigned char *name, struct ufs_dir_entry *de) 36 { 37 if (len != ufs_get_de_namlen(sb, de)) 38 return 0; 39 if (!de->d_ino) 40 return 0; 41 return !memcmp(name, de->d_name, len); 42 } 43 44 static int ufs_commit_chunk(struct page *page, loff_t pos, unsigned len) 45 { 46 struct address_space *mapping = page->mapping; 47 struct inode *dir = mapping->host; 48 int err = 0; 49 50 dir->i_version++; 51 block_write_end(NULL, mapping, pos, len, len, page, NULL); 52 if (pos+len > dir->i_size) { 53 i_size_write(dir, pos+len); 54 mark_inode_dirty(dir); 55 } 56 if (IS_DIRSYNC(dir)) 57 err = write_one_page(page); 58 else 59 unlock_page(page); 60 return err; 61 } 62 63 static inline void ufs_put_page(struct page *page) 64 { 65 kunmap(page); 66 put_page(page); 67 } 68 69 ino_t ufs_inode_by_name(struct inode *dir, const struct qstr *qstr) 70 { 71 ino_t res = 0; 72 struct ufs_dir_entry *de; 73 struct page *page; 74 75 de = ufs_find_entry(dir, qstr, &page); 76 if (de) { 77 res = fs32_to_cpu(dir->i_sb, de->d_ino); 78 ufs_put_page(page); 79 } 80 return res; 81 } 82 83 84 /* Releases the page */ 85 void ufs_set_link(struct inode *dir, struct ufs_dir_entry *de, 86 struct page *page, struct inode *inode, 87 bool update_times) 88 { 89 loff_t pos = page_offset(page) + 90 (char *) de - (char *) page_address(page); 91 unsigned len = fs16_to_cpu(dir->i_sb, de->d_reclen); 92 int err; 93 94 lock_page(page); 95 err = ufs_prepare_chunk(page, pos, len); 96 BUG_ON(err); 97 98 de->d_ino = cpu_to_fs32(dir->i_sb, inode->i_ino); 99 ufs_set_de_type(dir->i_sb, de, inode->i_mode); 100 101 err = ufs_commit_chunk(page, pos, len); 102 ufs_put_page(page); 103 if (update_times) 104 dir->i_mtime = dir->i_ctime = current_time(dir); 105 mark_inode_dirty(dir); 106 } 107 108 109 static bool ufs_check_page(struct page *page) 110 { 111 struct inode *dir = page->mapping->host; 112 struct super_block *sb = dir->i_sb; 113 char *kaddr = page_address(page); 114 unsigned offs, rec_len; 115 unsigned limit = PAGE_SIZE; 116 const unsigned chunk_mask = UFS_SB(sb)->s_uspi->s_dirblksize - 1; 117 struct ufs_dir_entry *p; 118 char *error; 119 120 if ((dir->i_size >> PAGE_SHIFT) == page->index) { 121 limit = dir->i_size & ~PAGE_MASK; 122 if (limit & chunk_mask) 123 goto Ebadsize; 124 if (!limit) 125 goto out; 126 } 127 for (offs = 0; offs <= limit - UFS_DIR_REC_LEN(1); offs += rec_len) { 128 p = (struct ufs_dir_entry *)(kaddr + offs); 129 rec_len = fs16_to_cpu(sb, p->d_reclen); 130 131 if (rec_len < UFS_DIR_REC_LEN(1)) 132 goto Eshort; 133 if (rec_len & 3) 134 goto Ealign; 135 if (rec_len < UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, p))) 136 goto Enamelen; 137 if (((offs + rec_len - 1) ^ offs) & ~chunk_mask) 138 goto Espan; 139 if (fs32_to_cpu(sb, p->d_ino) > (UFS_SB(sb)->s_uspi->s_ipg * 140 UFS_SB(sb)->s_uspi->s_ncg)) 141 goto Einumber; 142 } 143 if (offs != limit) 144 goto Eend; 145 out: 146 SetPageChecked(page); 147 return true; 148 149 /* Too bad, we had an error */ 150 151 Ebadsize: 152 ufs_error(sb, "ufs_check_page", 153 "size of directory #%lu is not a multiple of chunk size", 154 dir->i_ino 155 ); 156 goto fail; 157 Eshort: 158 error = "rec_len is smaller than minimal"; 159 goto bad_entry; 160 Ealign: 161 error = "unaligned directory entry"; 162 goto bad_entry; 163 Enamelen: 164 error = "rec_len is too small for name_len"; 165 goto bad_entry; 166 Espan: 167 error = "directory entry across blocks"; 168 goto bad_entry; 169 Einumber: 170 error = "inode out of bounds"; 171 bad_entry: 172 ufs_error (sb, "ufs_check_page", "bad entry in directory #%lu: %s - " 173 "offset=%lu, rec_len=%d, name_len=%d", 174 dir->i_ino, error, (page->index<<PAGE_SHIFT)+offs, 175 rec_len, ufs_get_de_namlen(sb, p)); 176 goto fail; 177 Eend: 178 p = (struct ufs_dir_entry *)(kaddr + offs); 179 ufs_error(sb, __func__, 180 "entry in directory #%lu spans the page boundary" 181 "offset=%lu", 182 dir->i_ino, (page->index<<PAGE_SHIFT)+offs); 183 fail: 184 SetPageError(page); 185 return false; 186 } 187 188 static struct page *ufs_get_page(struct inode *dir, unsigned long n) 189 { 190 struct address_space *mapping = dir->i_mapping; 191 struct page *page = read_mapping_page(mapping, n, NULL); 192 if (!IS_ERR(page)) { 193 kmap(page); 194 if (unlikely(!PageChecked(page))) { 195 if (PageError(page) || !ufs_check_page(page)) 196 goto fail; 197 } 198 } 199 return page; 200 201 fail: 202 ufs_put_page(page); 203 return ERR_PTR(-EIO); 204 } 205 206 /* 207 * Return the offset into page `page_nr' of the last valid 208 * byte in that page, plus one. 209 */ 210 static unsigned 211 ufs_last_byte(struct inode *inode, unsigned long page_nr) 212 { 213 unsigned last_byte = inode->i_size; 214 215 last_byte -= page_nr << PAGE_SHIFT; 216 if (last_byte > PAGE_SIZE) 217 last_byte = PAGE_SIZE; 218 return last_byte; 219 } 220 221 static inline struct ufs_dir_entry * 222 ufs_next_entry(struct super_block *sb, struct ufs_dir_entry *p) 223 { 224 return (struct ufs_dir_entry *)((char *)p + 225 fs16_to_cpu(sb, p->d_reclen)); 226 } 227 228 struct ufs_dir_entry *ufs_dotdot(struct inode *dir, struct page **p) 229 { 230 struct page *page = ufs_get_page(dir, 0); 231 struct ufs_dir_entry *de = NULL; 232 233 if (!IS_ERR(page)) { 234 de = ufs_next_entry(dir->i_sb, 235 (struct ufs_dir_entry *)page_address(page)); 236 *p = page; 237 } 238 return de; 239 } 240 241 /* 242 * ufs_find_entry() 243 * 244 * finds an entry in the specified directory with the wanted name. It 245 * returns the page in which the entry was found, and the entry itself 246 * (as a parameter - res_dir). Page is returned mapped and unlocked. 247 * Entry is guaranteed to be valid. 248 */ 249 struct ufs_dir_entry *ufs_find_entry(struct inode *dir, const struct qstr *qstr, 250 struct page **res_page) 251 { 252 struct super_block *sb = dir->i_sb; 253 const unsigned char *name = qstr->name; 254 int namelen = qstr->len; 255 unsigned reclen = UFS_DIR_REC_LEN(namelen); 256 unsigned long start, n; 257 unsigned long npages = dir_pages(dir); 258 struct page *page = NULL; 259 struct ufs_inode_info *ui = UFS_I(dir); 260 struct ufs_dir_entry *de; 261 262 UFSD("ENTER, dir_ino %lu, name %s, namlen %u\n", dir->i_ino, name, namelen); 263 264 if (npages == 0 || namelen > UFS_MAXNAMLEN) 265 goto out; 266 267 /* OFFSET_CACHE */ 268 *res_page = NULL; 269 270 start = ui->i_dir_start_lookup; 271 272 if (start >= npages) 273 start = 0; 274 n = start; 275 do { 276 char *kaddr; 277 page = ufs_get_page(dir, n); 278 if (!IS_ERR(page)) { 279 kaddr = page_address(page); 280 de = (struct ufs_dir_entry *) kaddr; 281 kaddr += ufs_last_byte(dir, n) - reclen; 282 while ((char *) de <= kaddr) { 283 if (ufs_match(sb, namelen, name, de)) 284 goto found; 285 de = ufs_next_entry(sb, de); 286 } 287 ufs_put_page(page); 288 } 289 if (++n >= npages) 290 n = 0; 291 } while (n != start); 292 out: 293 return NULL; 294 295 found: 296 *res_page = page; 297 ui->i_dir_start_lookup = n; 298 return de; 299 } 300 301 /* 302 * Parent is locked. 303 */ 304 int ufs_add_link(struct dentry *dentry, struct inode *inode) 305 { 306 struct inode *dir = d_inode(dentry->d_parent); 307 const unsigned char *name = dentry->d_name.name; 308 int namelen = dentry->d_name.len; 309 struct super_block *sb = dir->i_sb; 310 unsigned reclen = UFS_DIR_REC_LEN(namelen); 311 const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize; 312 unsigned short rec_len, name_len; 313 struct page *page = NULL; 314 struct ufs_dir_entry *de; 315 unsigned long npages = dir_pages(dir); 316 unsigned long n; 317 char *kaddr; 318 loff_t pos; 319 int err; 320 321 UFSD("ENTER, name %s, namelen %u\n", name, namelen); 322 323 /* 324 * We take care of directory expansion in the same loop. 325 * This code plays outside i_size, so it locks the page 326 * to protect that region. 327 */ 328 for (n = 0; n <= npages; n++) { 329 char *dir_end; 330 331 page = ufs_get_page(dir, n); 332 err = PTR_ERR(page); 333 if (IS_ERR(page)) 334 goto out; 335 lock_page(page); 336 kaddr = page_address(page); 337 dir_end = kaddr + ufs_last_byte(dir, n); 338 de = (struct ufs_dir_entry *)kaddr; 339 kaddr += PAGE_SIZE - reclen; 340 while ((char *)de <= kaddr) { 341 if ((char *)de == dir_end) { 342 /* We hit i_size */ 343 name_len = 0; 344 rec_len = chunk_size; 345 de->d_reclen = cpu_to_fs16(sb, chunk_size); 346 de->d_ino = 0; 347 goto got_it; 348 } 349 if (de->d_reclen == 0) { 350 ufs_error(dir->i_sb, __func__, 351 "zero-length directory entry"); 352 err = -EIO; 353 goto out_unlock; 354 } 355 err = -EEXIST; 356 if (ufs_match(sb, namelen, name, de)) 357 goto out_unlock; 358 name_len = UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, de)); 359 rec_len = fs16_to_cpu(sb, de->d_reclen); 360 if (!de->d_ino && rec_len >= reclen) 361 goto got_it; 362 if (rec_len >= name_len + reclen) 363 goto got_it; 364 de = (struct ufs_dir_entry *) ((char *) de + rec_len); 365 } 366 unlock_page(page); 367 ufs_put_page(page); 368 } 369 BUG(); 370 return -EINVAL; 371 372 got_it: 373 pos = page_offset(page) + 374 (char*)de - (char*)page_address(page); 375 err = ufs_prepare_chunk(page, pos, rec_len); 376 if (err) 377 goto out_unlock; 378 if (de->d_ino) { 379 struct ufs_dir_entry *de1 = 380 (struct ufs_dir_entry *) ((char *) de + name_len); 381 de1->d_reclen = cpu_to_fs16(sb, rec_len - name_len); 382 de->d_reclen = cpu_to_fs16(sb, name_len); 383 384 de = de1; 385 } 386 387 ufs_set_de_namlen(sb, de, namelen); 388 memcpy(de->d_name, name, namelen + 1); 389 de->d_ino = cpu_to_fs32(sb, inode->i_ino); 390 ufs_set_de_type(sb, de, inode->i_mode); 391 392 err = ufs_commit_chunk(page, pos, rec_len); 393 dir->i_mtime = dir->i_ctime = current_time(dir); 394 395 mark_inode_dirty(dir); 396 /* OFFSET_CACHE */ 397 out_put: 398 ufs_put_page(page); 399 out: 400 return err; 401 out_unlock: 402 unlock_page(page); 403 goto out_put; 404 } 405 406 static inline unsigned 407 ufs_validate_entry(struct super_block *sb, char *base, 408 unsigned offset, unsigned mask) 409 { 410 struct ufs_dir_entry *de = (struct ufs_dir_entry*)(base + offset); 411 struct ufs_dir_entry *p = (struct ufs_dir_entry*)(base + (offset&mask)); 412 while ((char*)p < (char*)de) 413 p = ufs_next_entry(sb, p); 414 return (char *)p - base; 415 } 416 417 418 /* 419 * This is blatantly stolen from ext2fs 420 */ 421 static int 422 ufs_readdir(struct file *file, struct dir_context *ctx) 423 { 424 loff_t pos = ctx->pos; 425 struct inode *inode = file_inode(file); 426 struct super_block *sb = inode->i_sb; 427 unsigned int offset = pos & ~PAGE_MASK; 428 unsigned long n = pos >> PAGE_SHIFT; 429 unsigned long npages = dir_pages(inode); 430 unsigned chunk_mask = ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1); 431 int need_revalidate = file->f_version != inode->i_version; 432 unsigned flags = UFS_SB(sb)->s_flags; 433 434 UFSD("BEGIN\n"); 435 436 if (pos > inode->i_size - UFS_DIR_REC_LEN(1)) 437 return 0; 438 439 for ( ; n < npages; n++, offset = 0) { 440 char *kaddr, *limit; 441 struct ufs_dir_entry *de; 442 443 struct page *page = ufs_get_page(inode, n); 444 445 if (IS_ERR(page)) { 446 ufs_error(sb, __func__, 447 "bad page in #%lu", 448 inode->i_ino); 449 ctx->pos += PAGE_SIZE - offset; 450 return -EIO; 451 } 452 kaddr = page_address(page); 453 if (unlikely(need_revalidate)) { 454 if (offset) { 455 offset = ufs_validate_entry(sb, kaddr, offset, chunk_mask); 456 ctx->pos = (n<<PAGE_SHIFT) + offset; 457 } 458 file->f_version = inode->i_version; 459 need_revalidate = 0; 460 } 461 de = (struct ufs_dir_entry *)(kaddr+offset); 462 limit = kaddr + ufs_last_byte(inode, n) - UFS_DIR_REC_LEN(1); 463 for ( ;(char*)de <= limit; de = ufs_next_entry(sb, de)) { 464 if (de->d_ino) { 465 unsigned char d_type = DT_UNKNOWN; 466 467 UFSD("filldir(%s,%u)\n", de->d_name, 468 fs32_to_cpu(sb, de->d_ino)); 469 UFSD("namlen %u\n", ufs_get_de_namlen(sb, de)); 470 471 if ((flags & UFS_DE_MASK) == UFS_DE_44BSD) 472 d_type = de->d_u.d_44.d_type; 473 474 if (!dir_emit(ctx, de->d_name, 475 ufs_get_de_namlen(sb, de), 476 fs32_to_cpu(sb, de->d_ino), 477 d_type)) { 478 ufs_put_page(page); 479 return 0; 480 } 481 } 482 ctx->pos += fs16_to_cpu(sb, de->d_reclen); 483 } 484 ufs_put_page(page); 485 } 486 return 0; 487 } 488 489 490 /* 491 * ufs_delete_entry deletes a directory entry by merging it with the 492 * previous entry. 493 */ 494 int ufs_delete_entry(struct inode *inode, struct ufs_dir_entry *dir, 495 struct page * page) 496 { 497 struct super_block *sb = inode->i_sb; 498 char *kaddr = page_address(page); 499 unsigned from = ((char*)dir - kaddr) & ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1); 500 unsigned to = ((char*)dir - kaddr) + fs16_to_cpu(sb, dir->d_reclen); 501 loff_t pos; 502 struct ufs_dir_entry *pde = NULL; 503 struct ufs_dir_entry *de = (struct ufs_dir_entry *) (kaddr + from); 504 int err; 505 506 UFSD("ENTER\n"); 507 508 UFSD("ino %u, reclen %u, namlen %u, name %s\n", 509 fs32_to_cpu(sb, de->d_ino), 510 fs16_to_cpu(sb, de->d_reclen), 511 ufs_get_de_namlen(sb, de), de->d_name); 512 513 while ((char*)de < (char*)dir) { 514 if (de->d_reclen == 0) { 515 ufs_error(inode->i_sb, __func__, 516 "zero-length directory entry"); 517 err = -EIO; 518 goto out; 519 } 520 pde = de; 521 de = ufs_next_entry(sb, de); 522 } 523 if (pde) 524 from = (char*)pde - (char*)page_address(page); 525 526 pos = page_offset(page) + from; 527 lock_page(page); 528 err = ufs_prepare_chunk(page, pos, to - from); 529 BUG_ON(err); 530 if (pde) 531 pde->d_reclen = cpu_to_fs16(sb, to - from); 532 dir->d_ino = 0; 533 err = ufs_commit_chunk(page, pos, to - from); 534 inode->i_ctime = inode->i_mtime = current_time(inode); 535 mark_inode_dirty(inode); 536 out: 537 ufs_put_page(page); 538 UFSD("EXIT\n"); 539 return err; 540 } 541 542 int ufs_make_empty(struct inode * inode, struct inode *dir) 543 { 544 struct super_block * sb = dir->i_sb; 545 struct address_space *mapping = inode->i_mapping; 546 struct page *page = grab_cache_page(mapping, 0); 547 const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize; 548 struct ufs_dir_entry * de; 549 char *base; 550 int err; 551 552 if (!page) 553 return -ENOMEM; 554 555 err = ufs_prepare_chunk(page, 0, chunk_size); 556 if (err) { 557 unlock_page(page); 558 goto fail; 559 } 560 561 kmap(page); 562 base = (char*)page_address(page); 563 memset(base, 0, PAGE_SIZE); 564 565 de = (struct ufs_dir_entry *) base; 566 567 de->d_ino = cpu_to_fs32(sb, inode->i_ino); 568 ufs_set_de_type(sb, de, inode->i_mode); 569 ufs_set_de_namlen(sb, de, 1); 570 de->d_reclen = cpu_to_fs16(sb, UFS_DIR_REC_LEN(1)); 571 strcpy (de->d_name, "."); 572 de = (struct ufs_dir_entry *) 573 ((char *)de + fs16_to_cpu(sb, de->d_reclen)); 574 de->d_ino = cpu_to_fs32(sb, dir->i_ino); 575 ufs_set_de_type(sb, de, dir->i_mode); 576 de->d_reclen = cpu_to_fs16(sb, chunk_size - UFS_DIR_REC_LEN(1)); 577 ufs_set_de_namlen(sb, de, 2); 578 strcpy (de->d_name, ".."); 579 kunmap(page); 580 581 err = ufs_commit_chunk(page, 0, chunk_size); 582 fail: 583 put_page(page); 584 return err; 585 } 586 587 /* 588 * routine to check that the specified directory is empty (for rmdir) 589 */ 590 int ufs_empty_dir(struct inode * inode) 591 { 592 struct super_block *sb = inode->i_sb; 593 struct page *page = NULL; 594 unsigned long i, npages = dir_pages(inode); 595 596 for (i = 0; i < npages; i++) { 597 char *kaddr; 598 struct ufs_dir_entry *de; 599 page = ufs_get_page(inode, i); 600 601 if (IS_ERR(page)) 602 continue; 603 604 kaddr = page_address(page); 605 de = (struct ufs_dir_entry *)kaddr; 606 kaddr += ufs_last_byte(inode, i) - UFS_DIR_REC_LEN(1); 607 608 while ((char *)de <= kaddr) { 609 if (de->d_reclen == 0) { 610 ufs_error(inode->i_sb, __func__, 611 "zero-length directory entry: " 612 "kaddr=%p, de=%p\n", kaddr, de); 613 goto not_empty; 614 } 615 if (de->d_ino) { 616 u16 namelen=ufs_get_de_namlen(sb, de); 617 /* check for . and .. */ 618 if (de->d_name[0] != '.') 619 goto not_empty; 620 if (namelen > 2) 621 goto not_empty; 622 if (namelen < 2) { 623 if (inode->i_ino != 624 fs32_to_cpu(sb, de->d_ino)) 625 goto not_empty; 626 } else if (de->d_name[1] != '.') 627 goto not_empty; 628 } 629 de = ufs_next_entry(sb, de); 630 } 631 ufs_put_page(page); 632 } 633 return 1; 634 635 not_empty: 636 ufs_put_page(page); 637 return 0; 638 } 639 640 const struct file_operations ufs_dir_operations = { 641 .read = generic_read_dir, 642 .iterate_shared = ufs_readdir, 643 .fsync = generic_file_fsync, 644 .llseek = generic_file_llseek, 645 }; 646