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