1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/fs/ext2/dir.c 4 * 5 * Copyright (C) 1992, 1993, 1994, 1995 6 * Remy Card (card@masi.ibp.fr) 7 * Laboratoire MASI - Institut Blaise Pascal 8 * Universite Pierre et Marie Curie (Paris VI) 9 * 10 * from 11 * 12 * linux/fs/minix/dir.c 13 * 14 * Copyright (C) 1991, 1992 Linus Torvalds 15 * 16 * ext2 directory handling functions 17 * 18 * Big-endian to little-endian byte-swapping/bitmaps by 19 * David S. Miller (davem@caip.rutgers.edu), 1995 20 * 21 * All code that works with directory layout had been switched to pagecache 22 * and moved here. AV 23 */ 24 25 #include "ext2.h" 26 #include <linux/buffer_head.h> 27 #include <linux/filelock.h> 28 #include <linux/pagemap.h> 29 #include <linux/swap.h> 30 #include <linux/iversion.h> 31 32 typedef struct ext2_dir_entry_2 ext2_dirent; 33 34 /* 35 * Tests against MAX_REC_LEN etc were put in place for 64k block 36 * sizes; if that is not possible on this arch, we can skip 37 * those tests and speed things up. 38 */ 39 static inline unsigned ext2_rec_len_from_disk(__le16 dlen) 40 { 41 unsigned len = le16_to_cpu(dlen); 42 43 #if (PAGE_SIZE >= 65536) 44 if (len == EXT2_MAX_REC_LEN) 45 return 1 << 16; 46 #endif 47 return len; 48 } 49 50 static inline __le16 ext2_rec_len_to_disk(unsigned len) 51 { 52 #if (PAGE_SIZE >= 65536) 53 if (len == (1 << 16)) 54 return cpu_to_le16(EXT2_MAX_REC_LEN); 55 else 56 BUG_ON(len > (1 << 16)); 57 #endif 58 return cpu_to_le16(len); 59 } 60 61 /* 62 * ext2 uses block-sized chunks. Arguably, sector-sized ones would be 63 * more robust, but we have what we have 64 */ 65 static inline unsigned ext2_chunk_size(struct inode *inode) 66 { 67 return inode->i_sb->s_blocksize; 68 } 69 70 /* 71 * Return the offset into page `page_nr' of the last valid 72 * byte in that page, plus one. 73 */ 74 static unsigned 75 ext2_last_byte(struct inode *inode, unsigned long page_nr) 76 { 77 unsigned last_byte = inode->i_size; 78 79 last_byte -= page_nr << PAGE_SHIFT; 80 if (last_byte > PAGE_SIZE) 81 last_byte = PAGE_SIZE; 82 return last_byte; 83 } 84 85 static void ext2_commit_chunk(struct folio *folio, loff_t pos, unsigned len) 86 { 87 struct address_space *mapping = folio->mapping; 88 struct inode *dir = mapping->host; 89 90 inode_inc_iversion(dir); 91 block_write_end(pos, len, len, folio); 92 93 if (pos+len > dir->i_size) { 94 i_size_write(dir, pos+len); 95 mark_inode_dirty(dir); 96 } 97 folio_unlock(folio); 98 } 99 100 static bool ext2_check_folio(struct folio *folio, int quiet, char *kaddr) 101 { 102 struct inode *dir = folio->mapping->host; 103 struct super_block *sb = dir->i_sb; 104 unsigned chunk_size = ext2_chunk_size(dir); 105 u32 max_inumber = le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count); 106 unsigned offs, rec_len; 107 unsigned limit = folio_size(folio); 108 ext2_dirent *p; 109 char *error; 110 111 if (dir->i_size < folio_pos(folio) + limit) { 112 limit = offset_in_folio(folio, dir->i_size); 113 if (limit & (chunk_size - 1)) 114 goto Ebadsize; 115 if (!limit) 116 goto out; 117 } 118 for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) { 119 p = (ext2_dirent *)(kaddr + offs); 120 rec_len = ext2_rec_len_from_disk(p->rec_len); 121 122 if (unlikely(rec_len < EXT2_DIR_REC_LEN(1))) 123 goto Eshort; 124 if (unlikely(rec_len & 3)) 125 goto Ealign; 126 if (unlikely(rec_len < EXT2_DIR_REC_LEN(p->name_len))) 127 goto Enamelen; 128 if (unlikely(((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))) 129 goto Espan; 130 if (unlikely(le32_to_cpu(p->inode) > max_inumber)) 131 goto Einumber; 132 } 133 if (offs != limit) 134 goto Eend; 135 out: 136 folio_set_checked(folio); 137 return true; 138 139 /* Too bad, we had an error */ 140 141 Ebadsize: 142 if (!quiet) 143 ext2_error(sb, __func__, 144 "size of directory #%llu is not a multiple " 145 "of chunk size", dir->i_ino); 146 goto fail; 147 Eshort: 148 error = "rec_len is smaller than minimal"; 149 goto bad_entry; 150 Ealign: 151 error = "unaligned directory entry"; 152 goto bad_entry; 153 Enamelen: 154 error = "rec_len is too small for name_len"; 155 goto bad_entry; 156 Espan: 157 error = "directory entry across blocks"; 158 goto bad_entry; 159 Einumber: 160 error = "inode out of bounds"; 161 bad_entry: 162 if (!quiet) 163 ext2_error(sb, __func__, "bad entry in directory #%llu: : %s - " 164 "offset=%llu, inode=%lu, rec_len=%d, name_len=%d", 165 dir->i_ino, error, folio_pos(folio) + offs, 166 (unsigned long) le32_to_cpu(p->inode), 167 rec_len, p->name_len); 168 goto fail; 169 Eend: 170 if (!quiet) { 171 p = (ext2_dirent *)(kaddr + offs); 172 ext2_error(sb, "ext2_check_folio", 173 "entry in directory #%llu spans the page boundary" 174 "offset=%llu, inode=%lu", 175 dir->i_ino, folio_pos(folio) + offs, 176 (unsigned long) le32_to_cpu(p->inode)); 177 } 178 fail: 179 return false; 180 } 181 182 /* 183 * Calls to ext2_get_folio()/folio_release_kmap() must be nested according 184 * to the rules documented in kmap_local_folio()/kunmap_local(). 185 * 186 * NOTE: ext2_find_entry() and ext2_dotdot() act as a call 187 * to folio_release_kmap() and should be treated as a call to 188 * folio_release_kmap() for nesting purposes. 189 */ 190 static void *ext2_get_folio(struct inode *dir, unsigned long n, 191 int quiet, struct folio **foliop) 192 { 193 struct address_space *mapping = dir->i_mapping; 194 struct folio *folio = read_mapping_folio(mapping, n, NULL); 195 void *kaddr; 196 197 if (IS_ERR(folio)) 198 return ERR_CAST(folio); 199 kaddr = kmap_local_folio(folio, 0); 200 if (unlikely(!folio_test_checked(folio))) { 201 if (!ext2_check_folio(folio, quiet, kaddr)) 202 goto fail; 203 } 204 *foliop = folio; 205 return kaddr; 206 207 fail: 208 folio_release_kmap(folio, kaddr); 209 return ERR_PTR(-EIO); 210 } 211 212 /* 213 * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure. 214 * 215 * len <= EXT2_NAME_LEN and de != NULL are guaranteed by caller. 216 */ 217 static inline int ext2_match (int len, const char * const name, 218 struct ext2_dir_entry_2 * de) 219 { 220 if (len != de->name_len) 221 return 0; 222 if (!de->inode) 223 return 0; 224 return !memcmp(name, de->name, len); 225 } 226 227 /* 228 * p is at least 6 bytes before the end of page 229 */ 230 static inline ext2_dirent *ext2_next_entry(ext2_dirent *p) 231 { 232 return (ext2_dirent *)((char *)p + 233 ext2_rec_len_from_disk(p->rec_len)); 234 } 235 236 static inline unsigned 237 ext2_validate_entry(char *base, unsigned offset, unsigned mask) 238 { 239 ext2_dirent *de = (ext2_dirent*)(base + offset); 240 ext2_dirent *p = (ext2_dirent*)(base + (offset&mask)); 241 while ((char*)p < (char*)de) { 242 if (p->rec_len == 0) 243 break; 244 p = ext2_next_entry(p); 245 } 246 return offset_in_page(p); 247 } 248 249 static inline void ext2_set_de_type(ext2_dirent *de, struct inode *inode) 250 { 251 if (EXT2_HAS_INCOMPAT_FEATURE(inode->i_sb, EXT2_FEATURE_INCOMPAT_FILETYPE)) 252 de->file_type = fs_umode_to_ftype(inode->i_mode); 253 else 254 de->file_type = 0; 255 } 256 257 static int 258 ext2_readdir(struct file *file, struct dir_context *ctx) 259 { 260 loff_t pos = ctx->pos; 261 struct inode *inode = file_inode(file); 262 struct super_block *sb = inode->i_sb; 263 unsigned int offset = pos & ~PAGE_MASK; 264 unsigned long n = pos >> PAGE_SHIFT; 265 unsigned long npages = dir_pages(inode); 266 unsigned chunk_mask = ~(ext2_chunk_size(inode)-1); 267 bool need_revalidate = !inode_eq_iversion(inode, *(u64 *)file->private_data); 268 bool has_filetype; 269 270 if (pos > inode->i_size - EXT2_DIR_REC_LEN(1)) 271 return 0; 272 273 has_filetype = 274 EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE); 275 276 for ( ; n < npages; n++, offset = 0) { 277 ext2_dirent *de; 278 struct folio *folio; 279 char *kaddr = ext2_get_folio(inode, n, 0, &folio); 280 char *limit; 281 282 if (IS_ERR(kaddr)) { 283 ext2_error(sb, __func__, 284 "bad page in #%llu", 285 inode->i_ino); 286 ctx->pos += PAGE_SIZE - offset; 287 return PTR_ERR(kaddr); 288 } 289 if (unlikely(need_revalidate)) { 290 if (offset) { 291 offset = ext2_validate_entry(kaddr, offset, chunk_mask); 292 ctx->pos = (n<<PAGE_SHIFT) + offset; 293 } 294 *(u64 *)file->private_data = inode_query_iversion(inode); 295 need_revalidate = false; 296 } 297 de = (ext2_dirent *)(kaddr+offset); 298 limit = kaddr + ext2_last_byte(inode, n) - EXT2_DIR_REC_LEN(1); 299 for ( ;(char*)de <= limit; de = ext2_next_entry(de)) { 300 if (de->rec_len == 0) { 301 ext2_error(sb, __func__, 302 "zero-length directory entry"); 303 folio_release_kmap(folio, de); 304 return -EIO; 305 } 306 if (de->inode) { 307 unsigned char d_type = DT_UNKNOWN; 308 309 if (has_filetype) 310 d_type = fs_ftype_to_dtype(de->file_type); 311 312 if (!dir_emit(ctx, de->name, de->name_len, 313 le32_to_cpu(de->inode), 314 d_type)) { 315 folio_release_kmap(folio, de); 316 return 0; 317 } 318 } 319 ctx->pos += ext2_rec_len_from_disk(de->rec_len); 320 } 321 folio_release_kmap(folio, kaddr); 322 } 323 return 0; 324 } 325 326 /* 327 * ext2_find_entry() 328 * 329 * finds an entry in the specified directory with the wanted name. It 330 * returns the page in which the entry was found (as a parameter - res_page), 331 * and the entry itself. Page is returned mapped and unlocked. 332 * Entry is guaranteed to be valid. 333 * 334 * On Success folio_release_kmap() should be called on *foliop. 335 * 336 * NOTE: Calls to ext2_get_folio()/folio_release_kmap() must be nested 337 * according to the rules documented in kmap_local_folio()/kunmap_local(). 338 * 339 * ext2_find_entry() and ext2_dotdot() act as a call to ext2_get_folio() 340 * and should be treated as a call to ext2_get_folio() for nesting 341 * purposes. 342 */ 343 struct ext2_dir_entry_2 *ext2_find_entry (struct inode *dir, 344 const struct qstr *child, struct folio **foliop) 345 { 346 const char *name = child->name; 347 int namelen = child->len; 348 unsigned reclen = EXT2_DIR_REC_LEN(namelen); 349 unsigned long start, n; 350 unsigned long npages = dir_pages(dir); 351 struct ext2_inode_info *ei = EXT2_I(dir); 352 ext2_dirent * de; 353 354 if (npages == 0) 355 goto out; 356 357 start = ei->i_dir_start_lookup; 358 if (start >= npages) 359 start = 0; 360 n = start; 361 do { 362 char *kaddr = ext2_get_folio(dir, n, 0, foliop); 363 if (IS_ERR(kaddr)) 364 return ERR_CAST(kaddr); 365 366 de = (ext2_dirent *) kaddr; 367 kaddr += ext2_last_byte(dir, n) - reclen; 368 while ((char *) de <= kaddr) { 369 if (de->rec_len == 0) { 370 ext2_error(dir->i_sb, __func__, 371 "zero-length directory entry"); 372 folio_release_kmap(*foliop, de); 373 goto out; 374 } 375 if (ext2_match(namelen, name, de)) 376 goto found; 377 de = ext2_next_entry(de); 378 } 379 folio_release_kmap(*foliop, kaddr); 380 381 if (++n >= npages) 382 n = 0; 383 /* next folio is past the blocks we've got */ 384 if (unlikely(n > (dir->i_blocks >> (PAGE_SHIFT - 9)))) { 385 ext2_error(dir->i_sb, __func__, 386 "dir %llu size %lld exceeds block count %llu", 387 dir->i_ino, dir->i_size, 388 (unsigned long long)dir->i_blocks); 389 goto out; 390 } 391 } while (n != start); 392 out: 393 return ERR_PTR(-ENOENT); 394 395 found: 396 ei->i_dir_start_lookup = n; 397 return de; 398 } 399 400 /* 401 * Return the '..' directory entry and the page in which the entry was found 402 * (as a parameter - p). 403 * 404 * On Success folio_release_kmap() should be called on *foliop. 405 * 406 * NOTE: Calls to ext2_get_folio()/folio_release_kmap() must be nested 407 * according to the rules documented in kmap_local_folio()/kunmap_local(). 408 * 409 * ext2_find_entry() and ext2_dotdot() act as a call to ext2_get_folio() 410 * and should be treated as a call to ext2_get_folio() for nesting 411 * purposes. 412 */ 413 struct ext2_dir_entry_2 *ext2_dotdot(struct inode *dir, struct folio **foliop) 414 { 415 ext2_dirent *de = ext2_get_folio(dir, 0, 0, foliop); 416 417 if (!IS_ERR(de)) 418 return ext2_next_entry(de); 419 return NULL; 420 } 421 422 int ext2_inode_by_name(struct inode *dir, const struct qstr *child, ino_t *ino) 423 { 424 struct ext2_dir_entry_2 *de; 425 struct folio *folio; 426 427 de = ext2_find_entry(dir, child, &folio); 428 if (IS_ERR(de)) 429 return PTR_ERR(de); 430 431 *ino = le32_to_cpu(de->inode); 432 folio_release_kmap(folio, de); 433 return 0; 434 } 435 436 static int ext2_prepare_chunk(struct folio *folio, loff_t pos, unsigned len) 437 { 438 return __block_write_begin(folio, pos, len, ext2_get_block); 439 } 440 441 static int ext2_handle_dirsync(struct inode *dir) 442 { 443 int err; 444 445 if (!IS_DIRSYNC(dir)) 446 return 0; 447 448 err = filemap_write_and_wait(dir->i_mapping); 449 if (!err) 450 err = sync_inode_metadata(dir, 1); 451 return err; 452 } 453 454 int ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de, 455 struct folio *folio, struct inode *inode, bool update_times) 456 { 457 loff_t pos = folio_pos(folio) + offset_in_folio(folio, de); 458 unsigned len = ext2_rec_len_from_disk(de->rec_len); 459 int err; 460 461 folio_lock(folio); 462 err = ext2_prepare_chunk(folio, pos, len); 463 if (err) { 464 folio_unlock(folio); 465 return err; 466 } 467 de->inode = cpu_to_le32(inode->i_ino); 468 ext2_set_de_type(de, inode); 469 ext2_commit_chunk(folio, pos, len); 470 if (update_times) 471 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); 472 EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL; 473 mark_inode_dirty(dir); 474 return ext2_handle_dirsync(dir); 475 } 476 477 /* 478 * Parent is locked. 479 */ 480 int ext2_add_link (struct dentry *dentry, struct inode *inode) 481 { 482 struct inode *dir = d_inode(dentry->d_parent); 483 const char *name = dentry->d_name.name; 484 int namelen = dentry->d_name.len; 485 unsigned chunk_size = ext2_chunk_size(dir); 486 unsigned reclen = EXT2_DIR_REC_LEN(namelen); 487 unsigned short rec_len, name_len; 488 struct folio *folio = NULL; 489 ext2_dirent * de; 490 unsigned long npages = dir_pages(dir); 491 unsigned long n; 492 loff_t pos; 493 int err; 494 495 /* 496 * We take care of directory expansion in the same loop. 497 * This code plays outside i_size, so it locks the folio 498 * to protect that region. 499 */ 500 for (n = 0; n <= npages; n++) { 501 char *kaddr = ext2_get_folio(dir, n, 0, &folio); 502 char *dir_end; 503 504 if (IS_ERR(kaddr)) 505 return PTR_ERR(kaddr); 506 folio_lock(folio); 507 dir_end = kaddr + ext2_last_byte(dir, n); 508 de = (ext2_dirent *)kaddr; 509 kaddr += folio_size(folio) - reclen; 510 while ((char *)de <= kaddr) { 511 if ((char *)de == dir_end) { 512 /* We hit i_size */ 513 name_len = 0; 514 rec_len = chunk_size; 515 de->rec_len = ext2_rec_len_to_disk(chunk_size); 516 de->inode = 0; 517 goto got_it; 518 } 519 if (de->rec_len == 0) { 520 ext2_error(dir->i_sb, __func__, 521 "zero-length directory entry"); 522 err = -EIO; 523 goto out_unlock; 524 } 525 err = -EEXIST; 526 if (ext2_match (namelen, name, de)) 527 goto out_unlock; 528 name_len = EXT2_DIR_REC_LEN(de->name_len); 529 rec_len = ext2_rec_len_from_disk(de->rec_len); 530 if (!de->inode && rec_len >= reclen) 531 goto got_it; 532 if (rec_len >= name_len + reclen) 533 goto got_it; 534 de = (ext2_dirent *) ((char *) de + rec_len); 535 } 536 folio_unlock(folio); 537 folio_release_kmap(folio, kaddr); 538 } 539 BUG(); 540 return -EINVAL; 541 542 got_it: 543 pos = folio_pos(folio) + offset_in_folio(folio, de); 544 err = ext2_prepare_chunk(folio, pos, rec_len); 545 if (err) 546 goto out_unlock; 547 if (de->inode) { 548 ext2_dirent *de1 = (ext2_dirent *) ((char *) de + name_len); 549 de1->rec_len = ext2_rec_len_to_disk(rec_len - name_len); 550 de->rec_len = ext2_rec_len_to_disk(name_len); 551 de = de1; 552 } 553 de->name_len = namelen; 554 memcpy(de->name, name, namelen); 555 de->inode = cpu_to_le32(inode->i_ino); 556 ext2_set_de_type (de, inode); 557 ext2_commit_chunk(folio, pos, rec_len); 558 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); 559 EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL; 560 mark_inode_dirty(dir); 561 err = ext2_handle_dirsync(dir); 562 /* OFFSET_CACHE */ 563 out_put: 564 folio_release_kmap(folio, de); 565 return err; 566 out_unlock: 567 folio_unlock(folio); 568 goto out_put; 569 } 570 571 /* 572 * ext2_delete_entry deletes a directory entry by merging it with the 573 * previous entry. Page is up-to-date. 574 */ 575 int ext2_delete_entry(struct ext2_dir_entry_2 *dir, struct folio *folio) 576 { 577 struct inode *inode = folio->mapping->host; 578 size_t from, to; 579 char *kaddr; 580 loff_t pos; 581 ext2_dirent *de, *pde = NULL; 582 int err; 583 584 from = offset_in_folio(folio, dir); 585 to = from + ext2_rec_len_from_disk(dir->rec_len); 586 kaddr = (char *)dir - from; 587 from &= ~(ext2_chunk_size(inode)-1); 588 de = (ext2_dirent *)(kaddr + from); 589 590 while ((char*)de < (char*)dir) { 591 if (de->rec_len == 0) { 592 ext2_error(inode->i_sb, __func__, 593 "zero-length directory entry"); 594 return -EIO; 595 } 596 pde = de; 597 de = ext2_next_entry(de); 598 } 599 if (pde) 600 from = offset_in_folio(folio, pde); 601 pos = folio_pos(folio) + from; 602 folio_lock(folio); 603 err = ext2_prepare_chunk(folio, pos, to - from); 604 if (err) { 605 folio_unlock(folio); 606 return err; 607 } 608 if (pde) 609 pde->rec_len = ext2_rec_len_to_disk(to - from); 610 dir->inode = 0; 611 ext2_commit_chunk(folio, pos, to - from); 612 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); 613 EXT2_I(inode)->i_flags &= ~EXT2_BTREE_FL; 614 mark_inode_dirty(inode); 615 return ext2_handle_dirsync(inode); 616 } 617 618 /* 619 * Set the first fragment of directory. 620 */ 621 int ext2_make_empty(struct inode *inode, struct inode *parent) 622 { 623 struct folio *folio = filemap_grab_folio(inode->i_mapping, 0); 624 unsigned chunk_size = ext2_chunk_size(inode); 625 struct ext2_dir_entry_2 * de; 626 int err; 627 void *kaddr; 628 629 if (IS_ERR(folio)) 630 return PTR_ERR(folio); 631 632 err = ext2_prepare_chunk(folio, 0, chunk_size); 633 if (err) { 634 folio_unlock(folio); 635 goto fail; 636 } 637 kaddr = kmap_local_folio(folio, 0); 638 memset(kaddr, 0, chunk_size); 639 de = (struct ext2_dir_entry_2 *)kaddr; 640 de->name_len = 1; 641 de->rec_len = ext2_rec_len_to_disk(EXT2_DIR_REC_LEN(1)); 642 memcpy (de->name, ".\0\0", 4); 643 de->inode = cpu_to_le32(inode->i_ino); 644 ext2_set_de_type (de, inode); 645 646 de = (struct ext2_dir_entry_2 *)(kaddr + EXT2_DIR_REC_LEN(1)); 647 de->name_len = 2; 648 de->rec_len = ext2_rec_len_to_disk(chunk_size - EXT2_DIR_REC_LEN(1)); 649 de->inode = cpu_to_le32(parent->i_ino); 650 memcpy (de->name, "..\0", 4); 651 ext2_set_de_type (de, inode); 652 kunmap_local(kaddr); 653 ext2_commit_chunk(folio, 0, chunk_size); 654 err = ext2_handle_dirsync(inode); 655 fail: 656 folio_put(folio); 657 return err; 658 } 659 660 /* 661 * routine to check that the specified directory is empty (for rmdir) 662 */ 663 int ext2_empty_dir(struct inode *inode) 664 { 665 struct folio *folio; 666 char *kaddr; 667 unsigned long i, npages = dir_pages(inode); 668 669 for (i = 0; i < npages; i++) { 670 ext2_dirent *de; 671 672 kaddr = ext2_get_folio(inode, i, 0, &folio); 673 if (IS_ERR(kaddr)) 674 return 0; 675 676 de = (ext2_dirent *)kaddr; 677 kaddr += ext2_last_byte(inode, i) - EXT2_DIR_REC_LEN(1); 678 679 while ((char *)de <= kaddr) { 680 if (de->rec_len == 0) { 681 ext2_error(inode->i_sb, __func__, 682 "zero-length directory entry"); 683 printk("kaddr=%p, de=%p\n", kaddr, de); 684 goto not_empty; 685 } 686 if (de->inode != 0) { 687 /* check for . and .. */ 688 if (de->name[0] != '.') 689 goto not_empty; 690 if (de->name_len > 2) 691 goto not_empty; 692 if (de->name_len < 2) { 693 if (de->inode != 694 cpu_to_le32(inode->i_ino)) 695 goto not_empty; 696 } else if (de->name[1] != '.') 697 goto not_empty; 698 } 699 de = ext2_next_entry(de); 700 } 701 folio_release_kmap(folio, kaddr); 702 } 703 return 1; 704 705 not_empty: 706 folio_release_kmap(folio, kaddr); 707 return 0; 708 } 709 710 static int ext2_dir_open(struct inode *inode, struct file *file) 711 { 712 file->private_data = kzalloc(sizeof(u64), GFP_KERNEL); 713 if (!file->private_data) 714 return -ENOMEM; 715 return 0; 716 } 717 718 static int ext2_dir_release(struct inode *inode, struct file *file) 719 { 720 kfree(file->private_data); 721 return 0; 722 } 723 724 static loff_t ext2_dir_llseek(struct file *file, loff_t offset, int whence) 725 { 726 return generic_llseek_cookie(file, offset, whence, 727 (u64 *)file->private_data); 728 } 729 730 const struct file_operations ext2_dir_operations = { 731 .open = ext2_dir_open, 732 .release = ext2_dir_release, 733 .llseek = ext2_dir_llseek, 734 .read = generic_read_dir, 735 .iterate_shared = ext2_readdir, 736 .unlocked_ioctl = ext2_ioctl, 737 #ifdef CONFIG_COMPAT 738 .compat_ioctl = ext2_compat_ioctl, 739 #endif 740 .fsync = simple_fsync, 741 .setlease = generic_setlease, 742 }; 743