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