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