Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/ext2/dir.c |
| 3 | * |
| 4 | * Copyright (C) 1992, 1993, 1994, 1995 |
| 5 | * Remy Card (card@masi.ibp.fr) |
| 6 | * Laboratoire MASI - Institut Blaise Pascal |
| 7 | * Universite Pierre et Marie Curie (Paris VI) |
| 8 | * |
| 9 | * from |
| 10 | * |
| 11 | * linux/fs/minix/dir.c |
| 12 | * |
| 13 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 14 | * |
| 15 | * ext2 directory handling functions |
| 16 | * |
| 17 | * Big-endian to little-endian byte-swapping/bitmaps by |
| 18 | * David S. Miller (davem@caip.rutgers.edu), 1995 |
| 19 | * |
| 20 | * All code that works with directory layout had been switched to pagecache |
| 21 | * and moved here. AV |
| 22 | */ |
| 23 | |
| 24 | #include "ext2.h" |
| 25 | #include <linux/pagemap.h> |
| 26 | #include <linux/smp_lock.h> |
| 27 | |
| 28 | typedef struct ext2_dir_entry_2 ext2_dirent; |
| 29 | |
| 30 | /* |
| 31 | * ext2 uses block-sized chunks. Arguably, sector-sized ones would be |
| 32 | * more robust, but we have what we have |
| 33 | */ |
| 34 | static inline unsigned ext2_chunk_size(struct inode *inode) |
| 35 | { |
| 36 | return inode->i_sb->s_blocksize; |
| 37 | } |
| 38 | |
| 39 | static inline void ext2_put_page(struct page *page) |
| 40 | { |
| 41 | kunmap(page); |
| 42 | page_cache_release(page); |
| 43 | } |
| 44 | |
| 45 | static inline unsigned long dir_pages(struct inode *inode) |
| 46 | { |
| 47 | return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT; |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * Return the offset into page `page_nr' of the last valid |
| 52 | * byte in that page, plus one. |
| 53 | */ |
| 54 | static unsigned |
| 55 | ext2_last_byte(struct inode *inode, unsigned long page_nr) |
| 56 | { |
| 57 | unsigned last_byte = inode->i_size; |
| 58 | |
| 59 | last_byte -= page_nr << PAGE_CACHE_SHIFT; |
| 60 | if (last_byte > PAGE_CACHE_SIZE) |
| 61 | last_byte = PAGE_CACHE_SIZE; |
| 62 | return last_byte; |
| 63 | } |
| 64 | |
| 65 | static int ext2_commit_chunk(struct page *page, unsigned from, unsigned to) |
| 66 | { |
| 67 | struct inode *dir = page->mapping->host; |
| 68 | int err = 0; |
| 69 | dir->i_version++; |
| 70 | page->mapping->a_ops->commit_write(NULL, page, from, to); |
| 71 | if (IS_DIRSYNC(dir)) |
| 72 | err = write_one_page(page, 1); |
| 73 | else |
| 74 | unlock_page(page); |
| 75 | return err; |
| 76 | } |
| 77 | |
| 78 | static void ext2_check_page(struct page *page) |
| 79 | { |
| 80 | struct inode *dir = page->mapping->host; |
| 81 | struct super_block *sb = dir->i_sb; |
| 82 | unsigned chunk_size = ext2_chunk_size(dir); |
| 83 | char *kaddr = page_address(page); |
| 84 | u32 max_inumber = le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count); |
| 85 | unsigned offs, rec_len; |
| 86 | unsigned limit = PAGE_CACHE_SIZE; |
| 87 | ext2_dirent *p; |
| 88 | char *error; |
| 89 | |
| 90 | if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) { |
| 91 | limit = dir->i_size & ~PAGE_CACHE_MASK; |
| 92 | if (limit & (chunk_size - 1)) |
| 93 | goto Ebadsize; |
| 94 | if (!limit) |
| 95 | goto out; |
| 96 | } |
| 97 | for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) { |
| 98 | p = (ext2_dirent *)(kaddr + offs); |
| 99 | rec_len = le16_to_cpu(p->rec_len); |
| 100 | |
| 101 | if (rec_len < EXT2_DIR_REC_LEN(1)) |
| 102 | goto Eshort; |
| 103 | if (rec_len & 3) |
| 104 | goto Ealign; |
| 105 | if (rec_len < EXT2_DIR_REC_LEN(p->name_len)) |
| 106 | goto Enamelen; |
| 107 | if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1)) |
| 108 | goto Espan; |
| 109 | if (le32_to_cpu(p->inode) > max_inumber) |
| 110 | goto Einumber; |
| 111 | } |
| 112 | if (offs != limit) |
| 113 | goto Eend; |
| 114 | out: |
| 115 | SetPageChecked(page); |
| 116 | return; |
| 117 | |
| 118 | /* Too bad, we had an error */ |
| 119 | |
| 120 | Ebadsize: |
| 121 | ext2_error(sb, "ext2_check_page", |
| 122 | "size of directory #%lu is not a multiple of chunk size", |
| 123 | dir->i_ino |
| 124 | ); |
| 125 | goto fail; |
| 126 | Eshort: |
| 127 | error = "rec_len is smaller than minimal"; |
| 128 | goto bad_entry; |
| 129 | Ealign: |
| 130 | error = "unaligned directory entry"; |
| 131 | goto bad_entry; |
| 132 | Enamelen: |
| 133 | error = "rec_len is too small for name_len"; |
| 134 | goto bad_entry; |
| 135 | Espan: |
| 136 | error = "directory entry across blocks"; |
| 137 | goto bad_entry; |
| 138 | Einumber: |
| 139 | error = "inode out of bounds"; |
| 140 | bad_entry: |
| 141 | ext2_error (sb, "ext2_check_page", "bad entry in directory #%lu: %s - " |
| 142 | "offset=%lu, inode=%lu, rec_len=%d, name_len=%d", |
| 143 | dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT)+offs, |
| 144 | (unsigned long) le32_to_cpu(p->inode), |
| 145 | rec_len, p->name_len); |
| 146 | goto fail; |
| 147 | Eend: |
| 148 | p = (ext2_dirent *)(kaddr + offs); |
| 149 | ext2_error (sb, "ext2_check_page", |
| 150 | "entry in directory #%lu spans the page boundary" |
| 151 | "offset=%lu, inode=%lu", |
| 152 | dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs, |
| 153 | (unsigned long) le32_to_cpu(p->inode)); |
| 154 | fail: |
| 155 | SetPageChecked(page); |
| 156 | SetPageError(page); |
| 157 | } |
| 158 | |
| 159 | static struct page * ext2_get_page(struct inode *dir, unsigned long n) |
| 160 | { |
| 161 | struct address_space *mapping = dir->i_mapping; |
Pekka Enberg | 090d2b1 | 2006-06-23 02:05:08 -0700 | [diff] [blame^] | 162 | struct page *page = read_mapping_page(mapping, n, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | if (!IS_ERR(page)) { |
| 164 | wait_on_page_locked(page); |
| 165 | kmap(page); |
| 166 | if (!PageUptodate(page)) |
| 167 | goto fail; |
| 168 | if (!PageChecked(page)) |
| 169 | ext2_check_page(page); |
| 170 | if (PageError(page)) |
| 171 | goto fail; |
| 172 | } |
| 173 | return page; |
| 174 | |
| 175 | fail: |
| 176 | ext2_put_page(page); |
| 177 | return ERR_PTR(-EIO); |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure. |
| 182 | * |
| 183 | * len <= EXT2_NAME_LEN and de != NULL are guaranteed by caller. |
| 184 | */ |
| 185 | static inline int ext2_match (int len, const char * const name, |
| 186 | struct ext2_dir_entry_2 * de) |
| 187 | { |
| 188 | if (len != de->name_len) |
| 189 | return 0; |
| 190 | if (!de->inode) |
| 191 | return 0; |
| 192 | return !memcmp(name, de->name, len); |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * p is at least 6 bytes before the end of page |
| 197 | */ |
| 198 | static inline ext2_dirent *ext2_next_entry(ext2_dirent *p) |
| 199 | { |
| 200 | return (ext2_dirent *)((char*)p + le16_to_cpu(p->rec_len)); |
| 201 | } |
| 202 | |
| 203 | static inline unsigned |
| 204 | ext2_validate_entry(char *base, unsigned offset, unsigned mask) |
| 205 | { |
| 206 | ext2_dirent *de = (ext2_dirent*)(base + offset); |
| 207 | ext2_dirent *p = (ext2_dirent*)(base + (offset&mask)); |
| 208 | while ((char*)p < (char*)de) { |
| 209 | if (p->rec_len == 0) |
| 210 | break; |
| 211 | p = ext2_next_entry(p); |
| 212 | } |
| 213 | return (char *)p - base; |
| 214 | } |
| 215 | |
| 216 | static unsigned char ext2_filetype_table[EXT2_FT_MAX] = { |
| 217 | [EXT2_FT_UNKNOWN] = DT_UNKNOWN, |
| 218 | [EXT2_FT_REG_FILE] = DT_REG, |
| 219 | [EXT2_FT_DIR] = DT_DIR, |
| 220 | [EXT2_FT_CHRDEV] = DT_CHR, |
| 221 | [EXT2_FT_BLKDEV] = DT_BLK, |
| 222 | [EXT2_FT_FIFO] = DT_FIFO, |
| 223 | [EXT2_FT_SOCK] = DT_SOCK, |
| 224 | [EXT2_FT_SYMLINK] = DT_LNK, |
| 225 | }; |
| 226 | |
| 227 | #define S_SHIFT 12 |
| 228 | static unsigned char ext2_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 229 | [S_IFREG >> S_SHIFT] = EXT2_FT_REG_FILE, |
| 230 | [S_IFDIR >> S_SHIFT] = EXT2_FT_DIR, |
| 231 | [S_IFCHR >> S_SHIFT] = EXT2_FT_CHRDEV, |
| 232 | [S_IFBLK >> S_SHIFT] = EXT2_FT_BLKDEV, |
| 233 | [S_IFIFO >> S_SHIFT] = EXT2_FT_FIFO, |
| 234 | [S_IFSOCK >> S_SHIFT] = EXT2_FT_SOCK, |
| 235 | [S_IFLNK >> S_SHIFT] = EXT2_FT_SYMLINK, |
| 236 | }; |
| 237 | |
| 238 | static inline void ext2_set_de_type(ext2_dirent *de, struct inode *inode) |
| 239 | { |
| 240 | mode_t mode = inode->i_mode; |
| 241 | if (EXT2_HAS_INCOMPAT_FEATURE(inode->i_sb, EXT2_FEATURE_INCOMPAT_FILETYPE)) |
| 242 | de->file_type = ext2_type_by_mode[(mode & S_IFMT)>>S_SHIFT]; |
| 243 | else |
| 244 | de->file_type = 0; |
| 245 | } |
| 246 | |
| 247 | static int |
| 248 | ext2_readdir (struct file * filp, void * dirent, filldir_t filldir) |
| 249 | { |
| 250 | loff_t pos = filp->f_pos; |
| 251 | struct inode *inode = filp->f_dentry->d_inode; |
| 252 | struct super_block *sb = inode->i_sb; |
| 253 | unsigned int offset = pos & ~PAGE_CACHE_MASK; |
| 254 | unsigned long n = pos >> PAGE_CACHE_SHIFT; |
| 255 | unsigned long npages = dir_pages(inode); |
| 256 | unsigned chunk_mask = ~(ext2_chunk_size(inode)-1); |
| 257 | unsigned char *types = NULL; |
Al Viro | 2d7f2ea | 2006-03-15 21:41:59 +0000 | [diff] [blame] | 258 | int need_revalidate = filp->f_version != inode->i_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
| 260 | if (pos > inode->i_size - EXT2_DIR_REC_LEN(1)) |
Al Viro | 2d7f2ea | 2006-03-15 21:41:59 +0000 | [diff] [blame] | 261 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
| 263 | if (EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE)) |
| 264 | types = ext2_filetype_table; |
| 265 | |
| 266 | for ( ; n < npages; n++, offset = 0) { |
| 267 | char *kaddr, *limit; |
| 268 | ext2_dirent *de; |
| 269 | struct page *page = ext2_get_page(inode, n); |
| 270 | |
| 271 | if (IS_ERR(page)) { |
| 272 | ext2_error(sb, __FUNCTION__, |
| 273 | "bad page in #%lu", |
| 274 | inode->i_ino); |
| 275 | filp->f_pos += PAGE_CACHE_SIZE - offset; |
Al Viro | 2d7f2ea | 2006-03-15 21:41:59 +0000 | [diff] [blame] | 276 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | } |
| 278 | kaddr = page_address(page); |
Al Viro | 2d7f2ea | 2006-03-15 21:41:59 +0000 | [diff] [blame] | 279 | if (unlikely(need_revalidate)) { |
| 280 | if (offset) { |
| 281 | offset = ext2_validate_entry(kaddr, offset, chunk_mask); |
| 282 | filp->f_pos = (n<<PAGE_CACHE_SHIFT) + offset; |
| 283 | } |
| 284 | filp->f_version = inode->i_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | need_revalidate = 0; |
| 286 | } |
| 287 | de = (ext2_dirent *)(kaddr+offset); |
| 288 | limit = kaddr + ext2_last_byte(inode, n) - EXT2_DIR_REC_LEN(1); |
| 289 | for ( ;(char*)de <= limit; de = ext2_next_entry(de)) { |
| 290 | if (de->rec_len == 0) { |
| 291 | ext2_error(sb, __FUNCTION__, |
| 292 | "zero-length directory entry"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | ext2_put_page(page); |
Al Viro | 2d7f2ea | 2006-03-15 21:41:59 +0000 | [diff] [blame] | 294 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | if (de->inode) { |
| 297 | int over; |
| 298 | unsigned char d_type = DT_UNKNOWN; |
| 299 | |
| 300 | if (types && de->file_type < EXT2_FT_MAX) |
| 301 | d_type = types[de->file_type]; |
| 302 | |
| 303 | offset = (char *)de - kaddr; |
| 304 | over = filldir(dirent, de->name, de->name_len, |
| 305 | (n<<PAGE_CACHE_SHIFT) | offset, |
| 306 | le32_to_cpu(de->inode), d_type); |
| 307 | if (over) { |
| 308 | ext2_put_page(page); |
Al Viro | 2d7f2ea | 2006-03-15 21:41:59 +0000 | [diff] [blame] | 309 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | filp->f_pos += le16_to_cpu(de->rec_len); |
| 313 | } |
| 314 | ext2_put_page(page); |
| 315 | } |
Al Viro | 2d7f2ea | 2006-03-15 21:41:59 +0000 | [diff] [blame] | 316 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | /* |
| 320 | * ext2_find_entry() |
| 321 | * |
| 322 | * finds an entry in the specified directory with the wanted name. It |
| 323 | * returns the page in which the entry was found, and the entry itself |
| 324 | * (as a parameter - res_dir). Page is returned mapped and unlocked. |
| 325 | * Entry is guaranteed to be valid. |
| 326 | */ |
| 327 | struct ext2_dir_entry_2 * ext2_find_entry (struct inode * dir, |
| 328 | struct dentry *dentry, struct page ** res_page) |
| 329 | { |
| 330 | const char *name = dentry->d_name.name; |
| 331 | int namelen = dentry->d_name.len; |
| 332 | unsigned reclen = EXT2_DIR_REC_LEN(namelen); |
| 333 | unsigned long start, n; |
| 334 | unsigned long npages = dir_pages(dir); |
| 335 | struct page *page = NULL; |
| 336 | struct ext2_inode_info *ei = EXT2_I(dir); |
| 337 | ext2_dirent * de; |
| 338 | |
| 339 | if (npages == 0) |
| 340 | goto out; |
| 341 | |
| 342 | /* OFFSET_CACHE */ |
| 343 | *res_page = NULL; |
| 344 | |
| 345 | start = ei->i_dir_start_lookup; |
| 346 | if (start >= npages) |
| 347 | start = 0; |
| 348 | n = start; |
| 349 | do { |
| 350 | char *kaddr; |
| 351 | page = ext2_get_page(dir, n); |
| 352 | if (!IS_ERR(page)) { |
| 353 | kaddr = page_address(page); |
| 354 | de = (ext2_dirent *) kaddr; |
| 355 | kaddr += ext2_last_byte(dir, n) - reclen; |
| 356 | while ((char *) de <= kaddr) { |
| 357 | if (de->rec_len == 0) { |
| 358 | ext2_error(dir->i_sb, __FUNCTION__, |
| 359 | "zero-length directory entry"); |
| 360 | ext2_put_page(page); |
| 361 | goto out; |
| 362 | } |
| 363 | if (ext2_match (namelen, name, de)) |
| 364 | goto found; |
| 365 | de = ext2_next_entry(de); |
| 366 | } |
| 367 | ext2_put_page(page); |
| 368 | } |
| 369 | if (++n >= npages) |
| 370 | n = 0; |
| 371 | } while (n != start); |
| 372 | out: |
| 373 | return NULL; |
| 374 | |
| 375 | found: |
| 376 | *res_page = page; |
| 377 | ei->i_dir_start_lookup = n; |
| 378 | return de; |
| 379 | } |
| 380 | |
| 381 | struct ext2_dir_entry_2 * ext2_dotdot (struct inode *dir, struct page **p) |
| 382 | { |
| 383 | struct page *page = ext2_get_page(dir, 0); |
| 384 | ext2_dirent *de = NULL; |
| 385 | |
| 386 | if (!IS_ERR(page)) { |
| 387 | de = ext2_next_entry((ext2_dirent *) page_address(page)); |
| 388 | *p = page; |
| 389 | } |
| 390 | return de; |
| 391 | } |
| 392 | |
| 393 | ino_t ext2_inode_by_name(struct inode * dir, struct dentry *dentry) |
| 394 | { |
| 395 | ino_t res = 0; |
| 396 | struct ext2_dir_entry_2 * de; |
| 397 | struct page *page; |
| 398 | |
| 399 | de = ext2_find_entry (dir, dentry, &page); |
| 400 | if (de) { |
| 401 | res = le32_to_cpu(de->inode); |
| 402 | kunmap(page); |
| 403 | page_cache_release(page); |
| 404 | } |
| 405 | return res; |
| 406 | } |
| 407 | |
| 408 | /* Releases the page */ |
| 409 | void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de, |
| 410 | struct page *page, struct inode *inode) |
| 411 | { |
| 412 | unsigned from = (char *) de - (char *) page_address(page); |
| 413 | unsigned to = from + le16_to_cpu(de->rec_len); |
| 414 | int err; |
| 415 | |
| 416 | lock_page(page); |
| 417 | err = page->mapping->a_ops->prepare_write(NULL, page, from, to); |
Eric Sesterhenn | 309be53 | 2006-03-26 18:27:41 +0200 | [diff] [blame] | 418 | BUG_ON(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | de->inode = cpu_to_le32(inode->i_ino); |
| 420 | ext2_set_de_type (de, inode); |
| 421 | err = ext2_commit_chunk(page, from, to); |
| 422 | ext2_put_page(page); |
| 423 | dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC; |
| 424 | EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL; |
| 425 | mark_inode_dirty(dir); |
| 426 | } |
| 427 | |
| 428 | /* |
| 429 | * Parent is locked. |
| 430 | */ |
| 431 | int ext2_add_link (struct dentry *dentry, struct inode *inode) |
| 432 | { |
| 433 | struct inode *dir = dentry->d_parent->d_inode; |
| 434 | const char *name = dentry->d_name.name; |
| 435 | int namelen = dentry->d_name.len; |
| 436 | unsigned chunk_size = ext2_chunk_size(dir); |
| 437 | unsigned reclen = EXT2_DIR_REC_LEN(namelen); |
| 438 | unsigned short rec_len, name_len; |
| 439 | struct page *page = NULL; |
| 440 | ext2_dirent * de; |
| 441 | unsigned long npages = dir_pages(dir); |
| 442 | unsigned long n; |
| 443 | char *kaddr; |
| 444 | unsigned from, to; |
| 445 | int err; |
| 446 | |
| 447 | /* |
| 448 | * We take care of directory expansion in the same loop. |
| 449 | * This code plays outside i_size, so it locks the page |
| 450 | * to protect that region. |
| 451 | */ |
| 452 | for (n = 0; n <= npages; n++) { |
| 453 | char *dir_end; |
| 454 | |
| 455 | page = ext2_get_page(dir, n); |
| 456 | err = PTR_ERR(page); |
| 457 | if (IS_ERR(page)) |
| 458 | goto out; |
| 459 | lock_page(page); |
| 460 | kaddr = page_address(page); |
| 461 | dir_end = kaddr + ext2_last_byte(dir, n); |
| 462 | de = (ext2_dirent *)kaddr; |
| 463 | kaddr += PAGE_CACHE_SIZE - reclen; |
| 464 | while ((char *)de <= kaddr) { |
| 465 | if ((char *)de == dir_end) { |
| 466 | /* We hit i_size */ |
| 467 | name_len = 0; |
| 468 | rec_len = chunk_size; |
| 469 | de->rec_len = cpu_to_le16(chunk_size); |
| 470 | de->inode = 0; |
| 471 | goto got_it; |
| 472 | } |
| 473 | if (de->rec_len == 0) { |
| 474 | ext2_error(dir->i_sb, __FUNCTION__, |
| 475 | "zero-length directory entry"); |
| 476 | err = -EIO; |
| 477 | goto out_unlock; |
| 478 | } |
| 479 | err = -EEXIST; |
| 480 | if (ext2_match (namelen, name, de)) |
| 481 | goto out_unlock; |
| 482 | name_len = EXT2_DIR_REC_LEN(de->name_len); |
| 483 | rec_len = le16_to_cpu(de->rec_len); |
| 484 | if (!de->inode && rec_len >= reclen) |
| 485 | goto got_it; |
| 486 | if (rec_len >= name_len + reclen) |
| 487 | goto got_it; |
| 488 | de = (ext2_dirent *) ((char *) de + rec_len); |
| 489 | } |
| 490 | unlock_page(page); |
| 491 | ext2_put_page(page); |
| 492 | } |
| 493 | BUG(); |
| 494 | return -EINVAL; |
| 495 | |
| 496 | got_it: |
| 497 | from = (char*)de - (char*)page_address(page); |
| 498 | to = from + rec_len; |
| 499 | err = page->mapping->a_ops->prepare_write(NULL, page, from, to); |
| 500 | if (err) |
| 501 | goto out_unlock; |
| 502 | if (de->inode) { |
| 503 | ext2_dirent *de1 = (ext2_dirent *) ((char *) de + name_len); |
| 504 | de1->rec_len = cpu_to_le16(rec_len - name_len); |
| 505 | de->rec_len = cpu_to_le16(name_len); |
| 506 | de = de1; |
| 507 | } |
| 508 | de->name_len = namelen; |
| 509 | memcpy (de->name, name, namelen); |
| 510 | de->inode = cpu_to_le32(inode->i_ino); |
| 511 | ext2_set_de_type (de, inode); |
| 512 | err = ext2_commit_chunk(page, from, to); |
| 513 | dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC; |
| 514 | EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL; |
| 515 | mark_inode_dirty(dir); |
| 516 | /* OFFSET_CACHE */ |
| 517 | out_put: |
| 518 | ext2_put_page(page); |
| 519 | out: |
| 520 | return err; |
| 521 | out_unlock: |
| 522 | unlock_page(page); |
| 523 | goto out_put; |
| 524 | } |
| 525 | |
| 526 | /* |
| 527 | * ext2_delete_entry deletes a directory entry by merging it with the |
| 528 | * previous entry. Page is up-to-date. Releases the page. |
| 529 | */ |
| 530 | int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page ) |
| 531 | { |
| 532 | struct address_space *mapping = page->mapping; |
| 533 | struct inode *inode = mapping->host; |
| 534 | char *kaddr = page_address(page); |
| 535 | unsigned from = ((char*)dir - kaddr) & ~(ext2_chunk_size(inode)-1); |
| 536 | unsigned to = ((char*)dir - kaddr) + le16_to_cpu(dir->rec_len); |
| 537 | ext2_dirent * pde = NULL; |
| 538 | ext2_dirent * de = (ext2_dirent *) (kaddr + from); |
| 539 | int err; |
| 540 | |
| 541 | while ((char*)de < (char*)dir) { |
| 542 | if (de->rec_len == 0) { |
| 543 | ext2_error(inode->i_sb, __FUNCTION__, |
| 544 | "zero-length directory entry"); |
| 545 | err = -EIO; |
| 546 | goto out; |
| 547 | } |
| 548 | pde = de; |
| 549 | de = ext2_next_entry(de); |
| 550 | } |
| 551 | if (pde) |
| 552 | from = (char*)pde - (char*)page_address(page); |
| 553 | lock_page(page); |
| 554 | err = mapping->a_ops->prepare_write(NULL, page, from, to); |
Eric Sesterhenn | 309be53 | 2006-03-26 18:27:41 +0200 | [diff] [blame] | 555 | BUG_ON(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | if (pde) |
| 557 | pde->rec_len = cpu_to_le16(to-from); |
| 558 | dir->inode = 0; |
| 559 | err = ext2_commit_chunk(page, from, to); |
| 560 | inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC; |
| 561 | EXT2_I(inode)->i_flags &= ~EXT2_BTREE_FL; |
| 562 | mark_inode_dirty(inode); |
| 563 | out: |
| 564 | ext2_put_page(page); |
| 565 | return err; |
| 566 | } |
| 567 | |
| 568 | /* |
| 569 | * Set the first fragment of directory. |
| 570 | */ |
| 571 | int ext2_make_empty(struct inode *inode, struct inode *parent) |
| 572 | { |
| 573 | struct address_space *mapping = inode->i_mapping; |
| 574 | struct page *page = grab_cache_page(mapping, 0); |
| 575 | unsigned chunk_size = ext2_chunk_size(inode); |
| 576 | struct ext2_dir_entry_2 * de; |
| 577 | int err; |
| 578 | void *kaddr; |
| 579 | |
| 580 | if (!page) |
| 581 | return -ENOMEM; |
| 582 | err = mapping->a_ops->prepare_write(NULL, page, 0, chunk_size); |
| 583 | if (err) { |
| 584 | unlock_page(page); |
| 585 | goto fail; |
| 586 | } |
| 587 | kaddr = kmap_atomic(page, KM_USER0); |
Luiz Fernando Capitulino | 7823c7c | 2006-01-11 01:38:27 +0100 | [diff] [blame] | 588 | memset(kaddr, 0, chunk_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | de = (struct ext2_dir_entry_2 *)kaddr; |
| 590 | de->name_len = 1; |
| 591 | de->rec_len = cpu_to_le16(EXT2_DIR_REC_LEN(1)); |
| 592 | memcpy (de->name, ".\0\0", 4); |
| 593 | de->inode = cpu_to_le32(inode->i_ino); |
| 594 | ext2_set_de_type (de, inode); |
| 595 | |
| 596 | de = (struct ext2_dir_entry_2 *)(kaddr + EXT2_DIR_REC_LEN(1)); |
| 597 | de->name_len = 2; |
| 598 | de->rec_len = cpu_to_le16(chunk_size - EXT2_DIR_REC_LEN(1)); |
| 599 | de->inode = cpu_to_le32(parent->i_ino); |
| 600 | memcpy (de->name, "..\0", 4); |
| 601 | ext2_set_de_type (de, inode); |
| 602 | kunmap_atomic(kaddr, KM_USER0); |
| 603 | err = ext2_commit_chunk(page, 0, chunk_size); |
| 604 | fail: |
| 605 | page_cache_release(page); |
| 606 | return err; |
| 607 | } |
| 608 | |
| 609 | /* |
| 610 | * routine to check that the specified directory is empty (for rmdir) |
| 611 | */ |
| 612 | int ext2_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 | ext2_dirent * de; |
| 620 | page = ext2_get_page(inode, i); |
| 621 | |
| 622 | if (IS_ERR(page)) |
| 623 | continue; |
| 624 | |
| 625 | kaddr = page_address(page); |
| 626 | de = (ext2_dirent *)kaddr; |
| 627 | kaddr += ext2_last_byte(inode, i) - EXT2_DIR_REC_LEN(1); |
| 628 | |
| 629 | while ((char *)de <= kaddr) { |
| 630 | if (de->rec_len == 0) { |
| 631 | ext2_error(inode->i_sb, __FUNCTION__, |
| 632 | "zero-length directory entry"); |
| 633 | printk("kaddr=%p, de=%p\n", 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_le32(inode->i_ino)) |
| 645 | goto not_empty; |
| 646 | } else if (de->name[1] != '.') |
| 647 | goto not_empty; |
| 648 | } |
| 649 | de = ext2_next_entry(de); |
| 650 | } |
| 651 | ext2_put_page(page); |
| 652 | } |
| 653 | return 1; |
| 654 | |
| 655 | not_empty: |
| 656 | ext2_put_page(page); |
| 657 | return 0; |
| 658 | } |
| 659 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 660 | const struct file_operations ext2_dir_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | .llseek = generic_file_llseek, |
| 662 | .read = generic_read_dir, |
| 663 | .readdir = ext2_readdir, |
| 664 | .ioctl = ext2_ioctl, |
| 665 | .fsync = ext2_sync_file, |
| 666 | }; |