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