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