1 /* 2 * dir.c - NILFS directory entry operations 3 * 4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation. 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * Modified for NILFS by Amagai Yoshiji. 17 */ 18 /* 19 * linux/fs/ext2/dir.c 20 * 21 * Copyright (C) 1992, 1993, 1994, 1995 22 * Remy Card (card@masi.ibp.fr) 23 * Laboratoire MASI - Institut Blaise Pascal 24 * Universite Pierre et Marie Curie (Paris VI) 25 * 26 * from 27 * 28 * linux/fs/minix/dir.c 29 * 30 * Copyright (C) 1991, 1992 Linus Torvalds 31 * 32 * ext2 directory handling functions 33 * 34 * Big-endian to little-endian byte-swapping/bitmaps by 35 * David S. Miller (davem@caip.rutgers.edu), 1995 36 * 37 * All code that works with directory layout had been switched to pagecache 38 * and moved here. AV 39 */ 40 41 #include <linux/pagemap.h> 42 #include "nilfs.h" 43 #include "page.h" 44 45 /* 46 * nilfs uses block-sized chunks. Arguably, sector-sized ones would be 47 * more robust, but we have what we have 48 */ 49 static inline unsigned int nilfs_chunk_size(struct inode *inode) 50 { 51 return inode->i_sb->s_blocksize; 52 } 53 54 static inline void nilfs_put_page(struct page *page) 55 { 56 kunmap(page); 57 put_page(page); 58 } 59 60 /* 61 * Return the offset into page `page_nr' of the last valid 62 * byte in that page, plus one. 63 */ 64 static unsigned int nilfs_last_byte(struct inode *inode, unsigned long page_nr) 65 { 66 unsigned int last_byte = inode->i_size; 67 68 last_byte -= page_nr << PAGE_SHIFT; 69 if (last_byte > PAGE_SIZE) 70 last_byte = PAGE_SIZE; 71 return last_byte; 72 } 73 74 static int nilfs_prepare_chunk(struct page *page, unsigned int from, 75 unsigned int to) 76 { 77 loff_t pos = page_offset(page) + from; 78 79 return __block_write_begin(page, pos, to - from, nilfs_get_block); 80 } 81 82 static void nilfs_commit_chunk(struct page *page, 83 struct address_space *mapping, 84 unsigned int from, unsigned int to) 85 { 86 struct inode *dir = mapping->host; 87 loff_t pos = page_offset(page) + from; 88 unsigned int len = to - from; 89 unsigned int nr_dirty, copied; 90 int err; 91 92 nr_dirty = nilfs_page_count_clean_buffers(page, from, to); 93 copied = block_write_end(NULL, mapping, pos, len, len, page, NULL); 94 if (pos + copied > dir->i_size) 95 i_size_write(dir, pos + copied); 96 if (IS_DIRSYNC(dir)) 97 nilfs_set_transaction_flag(NILFS_TI_SYNC); 98 err = nilfs_set_file_dirty(dir, nr_dirty); 99 WARN_ON(err); /* do not happen */ 100 unlock_page(page); 101 } 102 103 static bool nilfs_check_page(struct page *page) 104 { 105 struct inode *dir = page->mapping->host; 106 struct super_block *sb = dir->i_sb; 107 unsigned int chunk_size = nilfs_chunk_size(dir); 108 char *kaddr = page_address(page); 109 unsigned int offs, rec_len; 110 unsigned int limit = PAGE_SIZE; 111 struct nilfs_dir_entry *p; 112 char *error; 113 114 if ((dir->i_size >> PAGE_SHIFT) == page->index) { 115 limit = dir->i_size & ~PAGE_MASK; 116 if (limit & (chunk_size - 1)) 117 goto Ebadsize; 118 if (!limit) 119 goto out; 120 } 121 for (offs = 0; offs <= limit - NILFS_DIR_REC_LEN(1); offs += rec_len) { 122 p = (struct nilfs_dir_entry *)(kaddr + offs); 123 rec_len = nilfs_rec_len_from_disk(p->rec_len); 124 125 if (rec_len < NILFS_DIR_REC_LEN(1)) 126 goto Eshort; 127 if (rec_len & 3) 128 goto Ealign; 129 if (rec_len < NILFS_DIR_REC_LEN(p->name_len)) 130 goto Enamelen; 131 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1)) 132 goto Espan; 133 } 134 if (offs != limit) 135 goto Eend; 136 out: 137 SetPageChecked(page); 138 return true; 139 140 /* Too bad, we had an error */ 141 142 Ebadsize: 143 nilfs_error(sb, "nilfs_check_page", 144 "size of directory #%lu is not a multiple of chunk size", 145 dir->i_ino 146 ); 147 goto fail; 148 Eshort: 149 error = "rec_len is smaller than minimal"; 150 goto bad_entry; 151 Ealign: 152 error = "unaligned directory entry"; 153 goto bad_entry; 154 Enamelen: 155 error = "rec_len is too small for name_len"; 156 goto bad_entry; 157 Espan: 158 error = "directory entry across blocks"; 159 bad_entry: 160 nilfs_error(sb, "nilfs_check_page", "bad entry in directory #%lu: %s - " 161 "offset=%lu, inode=%lu, rec_len=%d, name_len=%d", 162 dir->i_ino, error, (page->index<<PAGE_SHIFT)+offs, 163 (unsigned long) le64_to_cpu(p->inode), 164 rec_len, p->name_len); 165 goto fail; 166 Eend: 167 p = (struct nilfs_dir_entry *)(kaddr + offs); 168 nilfs_error(sb, "nilfs_check_page", 169 "entry in directory #%lu spans the page boundary" 170 "offset=%lu, inode=%lu", 171 dir->i_ino, (page->index<<PAGE_SHIFT)+offs, 172 (unsigned long) le64_to_cpu(p->inode)); 173 fail: 174 SetPageError(page); 175 return false; 176 } 177 178 static struct page *nilfs_get_page(struct inode *dir, unsigned long n) 179 { 180 struct address_space *mapping = dir->i_mapping; 181 struct page *page = read_mapping_page(mapping, n, NULL); 182 183 if (!IS_ERR(page)) { 184 kmap(page); 185 if (unlikely(!PageChecked(page))) { 186 if (PageError(page) || !nilfs_check_page(page)) 187 goto fail; 188 } 189 } 190 return page; 191 192 fail: 193 nilfs_put_page(page); 194 return ERR_PTR(-EIO); 195 } 196 197 /* 198 * NOTE! unlike strncmp, nilfs_match returns 1 for success, 0 for failure. 199 * 200 * len <= NILFS_NAME_LEN and de != NULL are guaranteed by caller. 201 */ 202 static int 203 nilfs_match(int len, const unsigned char *name, struct nilfs_dir_entry *de) 204 { 205 if (len != de->name_len) 206 return 0; 207 if (!de->inode) 208 return 0; 209 return !memcmp(name, de->name, len); 210 } 211 212 /* 213 * p is at least 6 bytes before the end of page 214 */ 215 static struct nilfs_dir_entry *nilfs_next_entry(struct nilfs_dir_entry *p) 216 { 217 return (struct nilfs_dir_entry *)((char *)p + 218 nilfs_rec_len_from_disk(p->rec_len)); 219 } 220 221 static unsigned char 222 nilfs_filetype_table[NILFS_FT_MAX] = { 223 [NILFS_FT_UNKNOWN] = DT_UNKNOWN, 224 [NILFS_FT_REG_FILE] = DT_REG, 225 [NILFS_FT_DIR] = DT_DIR, 226 [NILFS_FT_CHRDEV] = DT_CHR, 227 [NILFS_FT_BLKDEV] = DT_BLK, 228 [NILFS_FT_FIFO] = DT_FIFO, 229 [NILFS_FT_SOCK] = DT_SOCK, 230 [NILFS_FT_SYMLINK] = DT_LNK, 231 }; 232 233 #define S_SHIFT 12 234 static unsigned char 235 nilfs_type_by_mode[S_IFMT >> S_SHIFT] = { 236 [S_IFREG >> S_SHIFT] = NILFS_FT_REG_FILE, 237 [S_IFDIR >> S_SHIFT] = NILFS_FT_DIR, 238 [S_IFCHR >> S_SHIFT] = NILFS_FT_CHRDEV, 239 [S_IFBLK >> S_SHIFT] = NILFS_FT_BLKDEV, 240 [S_IFIFO >> S_SHIFT] = NILFS_FT_FIFO, 241 [S_IFSOCK >> S_SHIFT] = NILFS_FT_SOCK, 242 [S_IFLNK >> S_SHIFT] = NILFS_FT_SYMLINK, 243 }; 244 245 static void nilfs_set_de_type(struct nilfs_dir_entry *de, struct inode *inode) 246 { 247 umode_t mode = inode->i_mode; 248 249 de->file_type = nilfs_type_by_mode[(mode & S_IFMT)>>S_SHIFT]; 250 } 251 252 static int nilfs_readdir(struct file *file, struct dir_context *ctx) 253 { 254 loff_t pos = ctx->pos; 255 struct inode *inode = file_inode(file); 256 struct super_block *sb = inode->i_sb; 257 unsigned int offset = pos & ~PAGE_MASK; 258 unsigned long n = pos >> PAGE_SHIFT; 259 unsigned long npages = dir_pages(inode); 260 261 if (pos > inode->i_size - NILFS_DIR_REC_LEN(1)) 262 return 0; 263 264 for ( ; n < npages; n++, offset = 0) { 265 char *kaddr, *limit; 266 struct nilfs_dir_entry *de; 267 struct page *page = nilfs_get_page(inode, n); 268 269 if (IS_ERR(page)) { 270 nilfs_error(sb, __func__, "bad page in #%lu", 271 inode->i_ino); 272 ctx->pos += PAGE_SIZE - offset; 273 return -EIO; 274 } 275 kaddr = page_address(page); 276 de = (struct nilfs_dir_entry *)(kaddr + offset); 277 limit = kaddr + nilfs_last_byte(inode, n) - 278 NILFS_DIR_REC_LEN(1); 279 for ( ; (char *)de <= limit; de = nilfs_next_entry(de)) { 280 if (de->rec_len == 0) { 281 nilfs_error(sb, __func__, 282 "zero-length directory entry"); 283 nilfs_put_page(page); 284 return -EIO; 285 } 286 if (de->inode) { 287 unsigned char t; 288 289 if (de->file_type < NILFS_FT_MAX) 290 t = nilfs_filetype_table[de->file_type]; 291 else 292 t = DT_UNKNOWN; 293 294 if (!dir_emit(ctx, de->name, de->name_len, 295 le64_to_cpu(de->inode), t)) { 296 nilfs_put_page(page); 297 return 0; 298 } 299 } 300 ctx->pos += nilfs_rec_len_from_disk(de->rec_len); 301 } 302 nilfs_put_page(page); 303 } 304 return 0; 305 } 306 307 /* 308 * nilfs_find_entry() 309 * 310 * finds an entry in the specified directory with the wanted name. It 311 * returns the page in which the entry was found, and the entry itself 312 * (as a parameter - res_dir). Page is returned mapped and unlocked. 313 * Entry is guaranteed to be valid. 314 */ 315 struct nilfs_dir_entry * 316 nilfs_find_entry(struct inode *dir, const struct qstr *qstr, 317 struct page **res_page) 318 { 319 const unsigned char *name = qstr->name; 320 int namelen = qstr->len; 321 unsigned int reclen = NILFS_DIR_REC_LEN(namelen); 322 unsigned long start, n; 323 unsigned long npages = dir_pages(dir); 324 struct page *page = NULL; 325 struct nilfs_inode_info *ei = NILFS_I(dir); 326 struct nilfs_dir_entry *de; 327 328 if (npages == 0) 329 goto out; 330 331 /* OFFSET_CACHE */ 332 *res_page = NULL; 333 334 start = ei->i_dir_start_lookup; 335 if (start >= npages) 336 start = 0; 337 n = start; 338 do { 339 char *kaddr; 340 341 page = nilfs_get_page(dir, n); 342 if (!IS_ERR(page)) { 343 kaddr = page_address(page); 344 de = (struct nilfs_dir_entry *)kaddr; 345 kaddr += nilfs_last_byte(dir, n) - reclen; 346 while ((char *) de <= kaddr) { 347 if (de->rec_len == 0) { 348 nilfs_error(dir->i_sb, __func__, 349 "zero-length directory entry"); 350 nilfs_put_page(page); 351 goto out; 352 } 353 if (nilfs_match(namelen, name, de)) 354 goto found; 355 de = nilfs_next_entry(de); 356 } 357 nilfs_put_page(page); 358 } 359 if (++n >= npages) 360 n = 0; 361 /* next page is past the blocks we've got */ 362 if (unlikely(n > (dir->i_blocks >> (PAGE_SHIFT - 9)))) { 363 nilfs_error(dir->i_sb, __func__, 364 "dir %lu size %lld exceeds block count %llu", 365 dir->i_ino, dir->i_size, 366 (unsigned long long)dir->i_blocks); 367 goto out; 368 } 369 } while (n != start); 370 out: 371 return NULL; 372 373 found: 374 *res_page = page; 375 ei->i_dir_start_lookup = n; 376 return de; 377 } 378 379 struct nilfs_dir_entry *nilfs_dotdot(struct inode *dir, struct page **p) 380 { 381 struct page *page = nilfs_get_page(dir, 0); 382 struct nilfs_dir_entry *de = NULL; 383 384 if (!IS_ERR(page)) { 385 de = nilfs_next_entry( 386 (struct nilfs_dir_entry *)page_address(page)); 387 *p = page; 388 } 389 return de; 390 } 391 392 ino_t nilfs_inode_by_name(struct inode *dir, const struct qstr *qstr) 393 { 394 ino_t res = 0; 395 struct nilfs_dir_entry *de; 396 struct page *page; 397 398 de = nilfs_find_entry(dir, qstr, &page); 399 if (de) { 400 res = le64_to_cpu(de->inode); 401 kunmap(page); 402 put_page(page); 403 } 404 return res; 405 } 406 407 /* Releases the page */ 408 void nilfs_set_link(struct inode *dir, struct nilfs_dir_entry *de, 409 struct page *page, struct inode *inode) 410 { 411 unsigned int from = (char *)de - (char *)page_address(page); 412 unsigned int to = from + nilfs_rec_len_from_disk(de->rec_len); 413 struct address_space *mapping = page->mapping; 414 int err; 415 416 lock_page(page); 417 err = nilfs_prepare_chunk(page, from, to); 418 BUG_ON(err); 419 de->inode = cpu_to_le64(inode->i_ino); 420 nilfs_set_de_type(de, inode); 421 nilfs_commit_chunk(page, mapping, from, to); 422 nilfs_put_page(page); 423 dir->i_mtime = dir->i_ctime = CURRENT_TIME; 424 } 425 426 /* 427 * Parent is locked. 428 */ 429 int nilfs_add_link(struct dentry *dentry, struct inode *inode) 430 { 431 struct inode *dir = d_inode(dentry->d_parent); 432 const unsigned char *name = dentry->d_name.name; 433 int namelen = dentry->d_name.len; 434 unsigned int chunk_size = nilfs_chunk_size(dir); 435 unsigned int reclen = NILFS_DIR_REC_LEN(namelen); 436 unsigned short rec_len, name_len; 437 struct page *page = NULL; 438 struct nilfs_dir_entry *de; 439 unsigned long npages = dir_pages(dir); 440 unsigned long n; 441 char *kaddr; 442 unsigned int from, to; 443 int err; 444 445 /* 446 * We take care of directory expansion in the same loop. 447 * This code plays outside i_size, so it locks the page 448 * to protect that region. 449 */ 450 for (n = 0; n <= npages; n++) { 451 char *dir_end; 452 453 page = nilfs_get_page(dir, n); 454 err = PTR_ERR(page); 455 if (IS_ERR(page)) 456 goto out; 457 lock_page(page); 458 kaddr = page_address(page); 459 dir_end = kaddr + nilfs_last_byte(dir, n); 460 de = (struct nilfs_dir_entry *)kaddr; 461 kaddr += PAGE_SIZE - reclen; 462 while ((char *)de <= kaddr) { 463 if ((char *)de == dir_end) { 464 /* We hit i_size */ 465 name_len = 0; 466 rec_len = chunk_size; 467 de->rec_len = nilfs_rec_len_to_disk(chunk_size); 468 de->inode = 0; 469 goto got_it; 470 } 471 if (de->rec_len == 0) { 472 nilfs_error(dir->i_sb, __func__, 473 "zero-length directory entry"); 474 err = -EIO; 475 goto out_unlock; 476 } 477 err = -EEXIST; 478 if (nilfs_match(namelen, name, de)) 479 goto out_unlock; 480 name_len = NILFS_DIR_REC_LEN(de->name_len); 481 rec_len = nilfs_rec_len_from_disk(de->rec_len); 482 if (!de->inode && rec_len >= reclen) 483 goto got_it; 484 if (rec_len >= name_len + reclen) 485 goto got_it; 486 de = (struct nilfs_dir_entry *)((char *)de + rec_len); 487 } 488 unlock_page(page); 489 nilfs_put_page(page); 490 } 491 BUG(); 492 return -EINVAL; 493 494 got_it: 495 from = (char *)de - (char *)page_address(page); 496 to = from + rec_len; 497 err = nilfs_prepare_chunk(page, from, to); 498 if (err) 499 goto out_unlock; 500 if (de->inode) { 501 struct nilfs_dir_entry *de1; 502 503 de1 = (struct nilfs_dir_entry *)((char *)de + name_len); 504 de1->rec_len = nilfs_rec_len_to_disk(rec_len - name_len); 505 de->rec_len = nilfs_rec_len_to_disk(name_len); 506 de = de1; 507 } 508 de->name_len = namelen; 509 memcpy(de->name, name, namelen); 510 de->inode = cpu_to_le64(inode->i_ino); 511 nilfs_set_de_type(de, inode); 512 nilfs_commit_chunk(page, page->mapping, from, to); 513 dir->i_mtime = dir->i_ctime = CURRENT_TIME; 514 nilfs_mark_inode_dirty(dir); 515 /* OFFSET_CACHE */ 516 out_put: 517 nilfs_put_page(page); 518 out: 519 return err; 520 out_unlock: 521 unlock_page(page); 522 goto out_put; 523 } 524 525 /* 526 * nilfs_delete_entry deletes a directory entry by merging it with the 527 * previous entry. Page is up-to-date. Releases the page. 528 */ 529 int nilfs_delete_entry(struct nilfs_dir_entry *dir, struct page *page) 530 { 531 struct address_space *mapping = page->mapping; 532 struct inode *inode = mapping->host; 533 char *kaddr = page_address(page); 534 unsigned int from, to; 535 struct nilfs_dir_entry *de, *pde = NULL; 536 int err; 537 538 from = ((char *)dir - kaddr) & ~(nilfs_chunk_size(inode) - 1); 539 to = ((char *)dir - kaddr) + nilfs_rec_len_from_disk(dir->rec_len); 540 de = (struct nilfs_dir_entry *)(kaddr + from); 541 542 while ((char *)de < (char *)dir) { 543 if (de->rec_len == 0) { 544 nilfs_error(inode->i_sb, __func__, 545 "zero-length directory entry"); 546 err = -EIO; 547 goto out; 548 } 549 pde = de; 550 de = nilfs_next_entry(de); 551 } 552 if (pde) 553 from = (char *)pde - (char *)page_address(page); 554 lock_page(page); 555 err = nilfs_prepare_chunk(page, from, to); 556 BUG_ON(err); 557 if (pde) 558 pde->rec_len = nilfs_rec_len_to_disk(to - from); 559 dir->inode = 0; 560 nilfs_commit_chunk(page, mapping, from, to); 561 inode->i_ctime = inode->i_mtime = CURRENT_TIME; 562 out: 563 nilfs_put_page(page); 564 return err; 565 } 566 567 /* 568 * Set the first fragment of directory. 569 */ 570 int nilfs_make_empty(struct inode *inode, struct inode *parent) 571 { 572 struct address_space *mapping = inode->i_mapping; 573 struct page *page = grab_cache_page(mapping, 0); 574 unsigned int chunk_size = nilfs_chunk_size(inode); 575 struct nilfs_dir_entry *de; 576 int err; 577 void *kaddr; 578 579 if (!page) 580 return -ENOMEM; 581 582 err = nilfs_prepare_chunk(page, 0, chunk_size); 583 if (unlikely(err)) { 584 unlock_page(page); 585 goto fail; 586 } 587 kaddr = kmap_atomic(page); 588 memset(kaddr, 0, chunk_size); 589 de = (struct nilfs_dir_entry *)kaddr; 590 de->name_len = 1; 591 de->rec_len = nilfs_rec_len_to_disk(NILFS_DIR_REC_LEN(1)); 592 memcpy(de->name, ".\0\0", 4); 593 de->inode = cpu_to_le64(inode->i_ino); 594 nilfs_set_de_type(de, inode); 595 596 de = (struct nilfs_dir_entry *)(kaddr + NILFS_DIR_REC_LEN(1)); 597 de->name_len = 2; 598 de->rec_len = nilfs_rec_len_to_disk(chunk_size - NILFS_DIR_REC_LEN(1)); 599 de->inode = cpu_to_le64(parent->i_ino); 600 memcpy(de->name, "..\0", 4); 601 nilfs_set_de_type(de, inode); 602 kunmap_atomic(kaddr); 603 nilfs_commit_chunk(page, mapping, 0, chunk_size); 604 fail: 605 put_page(page); 606 return err; 607 } 608 609 /* 610 * routine to check that the specified directory is empty (for rmdir) 611 */ 612 int nilfs_empty_dir(struct inode *inode) 613 { 614 struct page *page = NULL; 615 unsigned long i, npages = dir_pages(inode); 616 617 for (i = 0; i < npages; i++) { 618 char *kaddr; 619 struct nilfs_dir_entry *de; 620 621 page = nilfs_get_page(inode, i); 622 if (IS_ERR(page)) 623 continue; 624 625 kaddr = page_address(page); 626 de = (struct nilfs_dir_entry *)kaddr; 627 kaddr += nilfs_last_byte(inode, i) - NILFS_DIR_REC_LEN(1); 628 629 while ((char *)de <= kaddr) { 630 if (de->rec_len == 0) { 631 nilfs_error(inode->i_sb, __func__, 632 "zero-length directory entry (kaddr=%p, de=%p)", 633 kaddr, de); 634 goto not_empty; 635 } 636 if (de->inode != 0) { 637 /* check for . and .. */ 638 if (de->name[0] != '.') 639 goto not_empty; 640 if (de->name_len > 2) 641 goto not_empty; 642 if (de->name_len < 2) { 643 if (de->inode != 644 cpu_to_le64(inode->i_ino)) 645 goto not_empty; 646 } else if (de->name[1] != '.') 647 goto not_empty; 648 } 649 de = nilfs_next_entry(de); 650 } 651 nilfs_put_page(page); 652 } 653 return 1; 654 655 not_empty: 656 nilfs_put_page(page); 657 return 0; 658 } 659 660 const struct file_operations nilfs_dir_operations = { 661 .llseek = generic_file_llseek, 662 .read = generic_read_dir, 663 .iterate_shared = nilfs_readdir, 664 .unlocked_ioctl = nilfs_ioctl, 665 #ifdef CONFIG_COMPAT 666 .compat_ioctl = nilfs_compat_ioctl, 667 #endif /* CONFIG_COMPAT */ 668 .fsync = nilfs_sync_file, 669 670 }; 671