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