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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | /* |
| 70 | * Return the offset into page `page_nr' of the last valid |
| 71 | * byte in that page, plus one. |
| 72 | */ |
| 73 | static unsigned |
| 74 | ext2_last_byte(struct inode *inode, unsigned long page_nr) |
| 75 | { |
| 76 | unsigned last_byte = inode->i_size; |
| 77 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 78 | last_byte -= page_nr << PAGE_SHIFT; |
| 79 | if (last_byte > PAGE_SIZE) |
| 80 | last_byte = PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | return last_byte; |
| 82 | } |
| 83 | |
Nick Piggin | f34fb6e | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 84 | static int ext2_commit_chunk(struct page *page, loff_t pos, unsigned len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
Nick Piggin | f34fb6e | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 86 | struct address_space *mapping = page->mapping; |
| 87 | struct inode *dir = mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | int err = 0; |
Nick Piggin | f34fb6e | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 89 | |
Jeff Layton | e1d747d | 2017-12-11 06:35:14 -0500 | [diff] [blame] | 90 | inode_inc_iversion(dir); |
Nick Piggin | f34fb6e | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 91 | block_write_end(NULL, mapping, pos, len, len, page, NULL); |
| 92 | |
| 93 | if (pos+len > dir->i_size) { |
| 94 | i_size_write(dir, pos+len); |
| 95 | mark_inode_dirty(dir); |
| 96 | } |
| 97 | |
Jan Kara | 6b7021e | 2009-01-15 13:51:29 -0800 | [diff] [blame] | 98 | if (IS_DIRSYNC(dir)) { |
Jeff Layton | 2b69c82 | 2017-07-05 15:26:48 -0400 | [diff] [blame] | 99 | err = write_one_page(page); |
Jan Kara | 6b7021e | 2009-01-15 13:51:29 -0800 | [diff] [blame] | 100 | if (!err) |
Christoph Hellwig | c3765016 | 2010-10-06 10:48:20 +0200 | [diff] [blame] | 101 | err = sync_inode_metadata(dir, 1); |
Jan Kara | 6b7021e | 2009-01-15 13:51:29 -0800 | [diff] [blame] | 102 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | unlock_page(page); |
Jan Kara | 6b7021e | 2009-01-15 13:51:29 -0800 | [diff] [blame] | 104 | } |
Nick Piggin | f34fb6e | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | return err; |
| 107 | } |
| 108 | |
Javier Pello | 728d392 | 2021-07-14 18:54:48 +0200 | [diff] [blame] | 109 | static bool ext2_check_page(struct page *page, int quiet, char *kaddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
| 111 | struct inode *dir = page->mapping->host; |
| 112 | struct super_block *sb = dir->i_sb; |
| 113 | unsigned chunk_size = ext2_chunk_size(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | u32 max_inumber = le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count); |
| 115 | unsigned offs, rec_len; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 116 | unsigned limit = PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | ext2_dirent *p; |
| 118 | char *error; |
| 119 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 120 | if ((dir->i_size >> PAGE_SHIFT) == page->index) { |
| 121 | limit = dir->i_size & ~PAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | if (limit & (chunk_size - 1)) |
| 123 | goto Ebadsize; |
| 124 | if (!limit) |
| 125 | goto out; |
| 126 | } |
| 127 | for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) { |
| 128 | p = (ext2_dirent *)(kaddr + offs); |
Jan Kara | 89910cc | 2007-10-21 16:41:40 -0700 | [diff] [blame] | 129 | rec_len = ext2_rec_len_from_disk(p->rec_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Eric Sandeen | 40a063f | 2010-12-07 11:51:05 -0600 | [diff] [blame] | 131 | if (unlikely(rec_len < EXT2_DIR_REC_LEN(1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | goto Eshort; |
Eric Sandeen | 40a063f | 2010-12-07 11:51:05 -0600 | [diff] [blame] | 133 | if (unlikely(rec_len & 3)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | goto Ealign; |
Eric Sandeen | 40a063f | 2010-12-07 11:51:05 -0600 | [diff] [blame] | 135 | if (unlikely(rec_len < EXT2_DIR_REC_LEN(p->name_len))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | goto Enamelen; |
Eric Sandeen | 40a063f | 2010-12-07 11:51:05 -0600 | [diff] [blame] | 137 | if (unlikely(((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | goto Espan; |
Eric Sandeen | 40a063f | 2010-12-07 11:51:05 -0600 | [diff] [blame] | 139 | if (unlikely(le32_to_cpu(p->inode) > max_inumber)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | goto Einumber; |
| 141 | } |
| 142 | if (offs != limit) |
| 143 | goto Eend; |
| 144 | out: |
| 145 | SetPageChecked(page); |
Al Viro | be5b82d | 2016-04-22 15:06:44 -0400 | [diff] [blame] | 146 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
| 148 | /* Too bad, we had an error */ |
| 149 | |
| 150 | Ebadsize: |
Eric Sandeen | bd39597 | 2008-10-15 22:04:02 -0700 | [diff] [blame] | 151 | if (!quiet) |
| 152 | ext2_error(sb, __func__, |
| 153 | "size of directory #%lu is not a multiple " |
| 154 | "of chunk size", dir->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | goto fail; |
| 156 | Eshort: |
| 157 | error = "rec_len is smaller than minimal"; |
| 158 | goto bad_entry; |
| 159 | Ealign: |
| 160 | error = "unaligned directory entry"; |
| 161 | goto bad_entry; |
| 162 | Enamelen: |
| 163 | error = "rec_len is too small for name_len"; |
| 164 | goto bad_entry; |
| 165 | Espan: |
| 166 | error = "directory entry across blocks"; |
| 167 | goto bad_entry; |
| 168 | Einumber: |
| 169 | error = "inode out of bounds"; |
| 170 | bad_entry: |
Eric Sandeen | bd39597 | 2008-10-15 22:04:02 -0700 | [diff] [blame] | 171 | if (!quiet) |
| 172 | ext2_error(sb, __func__, "bad entry in directory #%lu: : %s - " |
| 173 | "offset=%lu, inode=%lu, rec_len=%d, name_len=%d", |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 174 | dir->i_ino, error, (page->index<<PAGE_SHIFT)+offs, |
Eric Sandeen | bd39597 | 2008-10-15 22:04:02 -0700 | [diff] [blame] | 175 | (unsigned long) le32_to_cpu(p->inode), |
| 176 | rec_len, p->name_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | goto fail; |
| 178 | Eend: |
Eric Sandeen | bd39597 | 2008-10-15 22:04:02 -0700 | [diff] [blame] | 179 | if (!quiet) { |
| 180 | p = (ext2_dirent *)(kaddr + offs); |
| 181 | ext2_error(sb, "ext2_check_page", |
| 182 | "entry in directory #%lu spans the page boundary" |
| 183 | "offset=%lu, inode=%lu", |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 184 | dir->i_ino, (page->index<<PAGE_SHIFT)+offs, |
Eric Sandeen | bd39597 | 2008-10-15 22:04:02 -0700 | [diff] [blame] | 185 | (unsigned long) le32_to_cpu(p->inode)); |
| 186 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | fail: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | SetPageError(page); |
Al Viro | be5b82d | 2016-04-22 15:06:44 -0400 | [diff] [blame] | 189 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 192 | /* |
| 193 | * Calls to ext2_get_page()/ext2_put_page() must be nested according to the |
| 194 | * rules documented in kmap_local_page()/kunmap_local(). |
| 195 | * |
| 196 | * NOTE: ext2_find_entry() and ext2_dotdot() act as a call to ext2_get_page() |
| 197 | * and should be treated as a call to ext2_get_page() for nesting purposes. |
| 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, |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 200 | int quiet, void **page_addr) |
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)) { |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 205 | *page_addr = kmap_local_page(page); |
Al Viro | be5b82d | 2016-04-22 15:06:44 -0400 | [diff] [blame] | 206 | if (unlikely(!PageChecked(page))) { |
Javier Pello | 728d392 | 2021-07-14 18:54:48 +0200 | [diff] [blame] | 207 | if (PageError(page) || !ext2_check_page(page, quiet, |
| 208 | *page_addr)) |
Al Viro | be5b82d | 2016-04-22 15:06:44 -0400 | [diff] [blame] | 209 | goto fail; |
| 210 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } |
| 212 | return page; |
| 213 | |
| 214 | fail: |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 215 | ext2_put_page(page, *page_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | return ERR_PTR(-EIO); |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure. |
| 221 | * |
| 222 | * len <= EXT2_NAME_LEN and de != NULL are guaranteed by caller. |
| 223 | */ |
| 224 | static inline int ext2_match (int len, const char * const name, |
| 225 | struct ext2_dir_entry_2 * de) |
| 226 | { |
| 227 | if (len != de->name_len) |
| 228 | return 0; |
| 229 | if (!de->inode) |
| 230 | return 0; |
| 231 | return !memcmp(name, de->name, len); |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * p is at least 6 bytes before the end of page |
| 236 | */ |
| 237 | static inline ext2_dirent *ext2_next_entry(ext2_dirent *p) |
| 238 | { |
Jan Kara | 89910cc | 2007-10-21 16:41:40 -0700 | [diff] [blame] | 239 | return (ext2_dirent *)((char *)p + |
| 240 | ext2_rec_len_from_disk(p->rec_len)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static inline unsigned |
| 244 | ext2_validate_entry(char *base, unsigned offset, unsigned mask) |
| 245 | { |
| 246 | ext2_dirent *de = (ext2_dirent*)(base + offset); |
| 247 | ext2_dirent *p = (ext2_dirent*)(base + (offset&mask)); |
| 248 | while ((char*)p < (char*)de) { |
| 249 | if (p->rec_len == 0) |
| 250 | break; |
| 251 | p = ext2_next_entry(p); |
| 252 | } |
| 253 | return (char *)p - base; |
| 254 | } |
| 255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | static inline void ext2_set_de_type(ext2_dirent *de, struct inode *inode) |
| 257 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | if (EXT2_HAS_INCOMPAT_FEATURE(inode->i_sb, EXT2_FEATURE_INCOMPAT_FILETYPE)) |
Phillip Potter | e108921 | 2019-01-21 00:54:31 +0000 | [diff] [blame] | 259 | de->file_type = fs_umode_to_ftype(inode->i_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | else |
| 261 | de->file_type = 0; |
| 262 | } |
| 263 | |
| 264 | static int |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 265 | ext2_readdir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 267 | loff_t pos = ctx->pos; |
| 268 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | struct super_block *sb = inode->i_sb; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 270 | unsigned int offset = pos & ~PAGE_MASK; |
| 271 | unsigned long n = pos >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | unsigned long npages = dir_pages(inode); |
| 273 | unsigned chunk_mask = ~(ext2_chunk_size(inode)-1); |
Goffredo Baroncelli | c472c07 | 2018-02-01 08:15:25 -0500 | [diff] [blame] | 274 | bool need_revalidate = !inode_eq_iversion(inode, file->f_version); |
Phillip Potter | e108921 | 2019-01-21 00:54:31 +0000 | [diff] [blame] | 275 | bool has_filetype; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
| 277 | if (pos > inode->i_size - EXT2_DIR_REC_LEN(1)) |
Al Viro | 2d7f2ea | 2006-03-15 21:41:59 +0000 | [diff] [blame] | 278 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
Phillip Potter | e108921 | 2019-01-21 00:54:31 +0000 | [diff] [blame] | 280 | has_filetype = |
| 281 | EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
| 283 | for ( ; n < npages; n++, offset = 0) { |
| 284 | char *kaddr, *limit; |
| 285 | ext2_dirent *de; |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 286 | struct page *page = ext2_get_page(inode, n, 0, (void **)&kaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | |
| 288 | if (IS_ERR(page)) { |
Harvey Harrison | 605afd6 | 2008-04-28 02:16:03 -0700 | [diff] [blame] | 289 | ext2_error(sb, __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | "bad page in #%lu", |
| 291 | inode->i_ino); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 292 | ctx->pos += PAGE_SIZE - offset; |
Akinobu Mita | bbff286 | 2008-04-28 02:16:00 -0700 | [diff] [blame] | 293 | return PTR_ERR(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
Al Viro | 2d7f2ea | 2006-03-15 21:41:59 +0000 | [diff] [blame] | 295 | if (unlikely(need_revalidate)) { |
| 296 | if (offset) { |
| 297 | offset = ext2_validate_entry(kaddr, offset, chunk_mask); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 298 | ctx->pos = (n<<PAGE_SHIFT) + offset; |
Al Viro | 2d7f2ea | 2006-03-15 21:41:59 +0000 | [diff] [blame] | 299 | } |
Jeff Layton | e1d747d | 2017-12-11 06:35:14 -0500 | [diff] [blame] | 300 | file->f_version = inode_query_iversion(inode); |
| 301 | need_revalidate = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | } |
| 303 | de = (ext2_dirent *)(kaddr+offset); |
| 304 | limit = kaddr + ext2_last_byte(inode, n) - EXT2_DIR_REC_LEN(1); |
| 305 | for ( ;(char*)de <= limit; de = ext2_next_entry(de)) { |
| 306 | if (de->rec_len == 0) { |
Harvey Harrison | 605afd6 | 2008-04-28 02:16:03 -0700 | [diff] [blame] | 307 | ext2_error(sb, __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | "zero-length directory entry"); |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 309 | ext2_put_page(page, kaddr); |
Al Viro | 2d7f2ea | 2006-03-15 21:41:59 +0000 | [diff] [blame] | 310 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } |
| 312 | if (de->inode) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | unsigned char d_type = DT_UNKNOWN; |
| 314 | |
Phillip Potter | e108921 | 2019-01-21 00:54:31 +0000 | [diff] [blame] | 315 | if (has_filetype) |
| 316 | d_type = fs_ftype_to_dtype(de->file_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 318 | if (!dir_emit(ctx, de->name, de->name_len, |
| 319 | le32_to_cpu(de->inode), |
| 320 | d_type)) { |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 321 | ext2_put_page(page, kaddr); |
Al Viro | 2d7f2ea | 2006-03-15 21:41:59 +0000 | [diff] [blame] | 322 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } |
| 324 | } |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 325 | ctx->pos += ext2_rec_len_from_disk(de->rec_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | } |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 327 | ext2_put_page(page, kaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
Al Viro | 2d7f2ea | 2006-03-15 21:41:59 +0000 | [diff] [blame] | 329 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /* |
| 333 | * ext2_find_entry() |
| 334 | * |
| 335 | * 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] | 336 | * returns the page in which the entry was found (as a parameter - res_page), |
| 337 | * and the entry itself. Page is returned mapped and unlocked. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | * Entry is guaranteed to be valid. |
Ira Weiny | a6fbd0a | 2020-11-12 09:42:44 -0800 | [diff] [blame] | 339 | * |
| 340 | * On Success ext2_put_page() should be called on *res_page. |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 341 | * |
| 342 | * NOTE: Calls to ext2_get_page()/ext2_put_page() must be nested according to |
| 343 | * the rules documented in kmap_local_page()/kunmap_local(). |
| 344 | * |
| 345 | * ext2_find_entry() and ext2_dotdot() act as a call to ext2_get_page() and |
| 346 | * should be treated as a call to ext2_get_page() for nesting purposes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | */ |
Al Viro | ac3ba64 | 2016-07-20 22:47:26 -0400 | [diff] [blame] | 348 | struct ext2_dir_entry_2 *ext2_find_entry (struct inode *dir, |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 349 | const struct qstr *child, struct page **res_page, |
| 350 | void **res_page_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | { |
Al Viro | a988544 | 2008-08-24 07:28:39 -0400 | [diff] [blame] | 352 | const char *name = child->name; |
| 353 | int namelen = child->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | unsigned reclen = EXT2_DIR_REC_LEN(namelen); |
| 355 | unsigned long start, n; |
| 356 | unsigned long npages = dir_pages(dir); |
| 357 | struct page *page = NULL; |
| 358 | struct ext2_inode_info *ei = EXT2_I(dir); |
| 359 | ext2_dirent * de; |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 360 | void *page_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | |
| 362 | if (npages == 0) |
| 363 | goto out; |
| 364 | |
| 365 | /* OFFSET_CACHE */ |
| 366 | *res_page = NULL; |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 367 | *res_page_addr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
| 369 | start = ei->i_dir_start_lookup; |
| 370 | if (start >= npages) |
| 371 | start = 0; |
| 372 | n = start; |
| 373 | do { |
| 374 | char *kaddr; |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 375 | page = ext2_get_page(dir, n, 0, &page_addr); |
zhangyi (F) | b496209 | 2020-06-08 11:40:42 +0800 | [diff] [blame] | 376 | if (IS_ERR(page)) |
| 377 | return ERR_CAST(page); |
| 378 | |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 379 | kaddr = page_addr; |
zhangyi (F) | b496209 | 2020-06-08 11:40:42 +0800 | [diff] [blame] | 380 | de = (ext2_dirent *) kaddr; |
| 381 | kaddr += ext2_last_byte(dir, n) - reclen; |
| 382 | while ((char *) de <= kaddr) { |
| 383 | if (de->rec_len == 0) { |
| 384 | ext2_error(dir->i_sb, __func__, |
| 385 | "zero-length directory entry"); |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 386 | ext2_put_page(page, page_addr); |
zhangyi (F) | b496209 | 2020-06-08 11:40:42 +0800 | [diff] [blame] | 387 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } |
zhangyi (F) | b496209 | 2020-06-08 11:40:42 +0800 | [diff] [blame] | 389 | if (ext2_match(namelen, name, de)) |
| 390 | goto found; |
| 391 | de = ext2_next_entry(de); |
| 392 | } |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 393 | ext2_put_page(page, page_addr); |
Eric Sandeen | bd39597 | 2008-10-15 22:04:02 -0700 | [diff] [blame] | 394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | if (++n >= npages) |
| 396 | n = 0; |
Eric Sandeen | d8adb9c | 2007-02-10 01:45:06 -0800 | [diff] [blame] | 397 | /* next page is past the blocks we've got */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 398 | if (unlikely(n > (dir->i_blocks >> (PAGE_SHIFT - 9)))) { |
Harvey Harrison | 605afd6 | 2008-04-28 02:16:03 -0700 | [diff] [blame] | 399 | ext2_error(dir->i_sb, __func__, |
Eric Sandeen | d8adb9c | 2007-02-10 01:45:06 -0800 | [diff] [blame] | 400 | "dir %lu size %lld exceeds block count %llu", |
| 401 | dir->i_ino, dir->i_size, |
| 402 | (unsigned long long)dir->i_blocks); |
| 403 | goto out; |
| 404 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | } while (n != start); |
| 406 | out: |
zhangyi (F) | a43850a | 2020-06-08 11:40:43 +0800 | [diff] [blame] | 407 | return ERR_PTR(-ENOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
| 409 | found: |
| 410 | *res_page = page; |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 411 | *res_page_addr = page_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | ei->i_dir_start_lookup = n; |
| 413 | return de; |
| 414 | } |
| 415 | |
Ira Weiny | a6fbd0a | 2020-11-12 09:42:44 -0800 | [diff] [blame] | 416 | /** |
| 417 | * Return the '..' directory entry and the page in which the entry was found |
| 418 | * (as a parameter - p). |
| 419 | * |
| 420 | * On Success ext2_put_page() should be called on *p. |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 421 | * |
| 422 | * NOTE: Calls to ext2_get_page()/ext2_put_page() must be nested according to |
| 423 | * the rules documented in kmap_local_page()/kunmap_local(). |
| 424 | * |
| 425 | * ext2_find_entry() and ext2_dotdot() act as a call to ext2_get_page() and |
| 426 | * should be treated as a call to ext2_get_page() for nesting purposes. |
Ira Weiny | a6fbd0a | 2020-11-12 09:42:44 -0800 | [diff] [blame] | 427 | */ |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 428 | struct ext2_dir_entry_2 *ext2_dotdot(struct inode *dir, struct page **p, |
| 429 | void **pa) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | { |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 431 | void *page_addr; |
| 432 | struct page *page = ext2_get_page(dir, 0, 0, &page_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | ext2_dirent *de = NULL; |
| 434 | |
| 435 | if (!IS_ERR(page)) { |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 436 | de = ext2_next_entry((ext2_dirent *) page_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | *p = page; |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 438 | *pa = page_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | } |
| 440 | return de; |
| 441 | } |
| 442 | |
zhangyi (F) | b496209 | 2020-06-08 11:40:42 +0800 | [diff] [blame] | 443 | int ext2_inode_by_name(struct inode *dir, const struct qstr *child, ino_t *ino) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | { |
Al Viro | a988544 | 2008-08-24 07:28:39 -0400 | [diff] [blame] | 445 | struct ext2_dir_entry_2 *de; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | struct page *page; |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 447 | void *page_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 449 | de = ext2_find_entry(dir, child, &page, &page_addr); |
zhangyi (F) | a43850a | 2020-06-08 11:40:43 +0800 | [diff] [blame] | 450 | if (IS_ERR(de)) |
zhangyi (F) | b496209 | 2020-06-08 11:40:42 +0800 | [diff] [blame] | 451 | return PTR_ERR(de); |
| 452 | |
| 453 | *ino = le32_to_cpu(de->inode); |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 454 | ext2_put_page(page, page_addr); |
zhangyi (F) | b496209 | 2020-06-08 11:40:42 +0800 | [diff] [blame] | 455 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 458 | static int ext2_prepare_chunk(struct page *page, loff_t pos, unsigned len) |
| 459 | { |
Christoph Hellwig | 6e1db88 | 2010-06-04 11:29:57 +0200 | [diff] [blame] | 460 | return __block_write_begin(page, pos, len, ext2_get_block); |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 461 | } |
| 462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de, |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 464 | struct page *page, void *page_addr, struct inode *inode, |
| 465 | int update_times) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | { |
Nick Piggin | f34fb6e | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 467 | loff_t pos = page_offset(page) + |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 468 | (char *) de - (char *) page_addr; |
Jan Kara | 89910cc | 2007-10-21 16:41:40 -0700 | [diff] [blame] | 469 | unsigned len = ext2_rec_len_from_disk(de->rec_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | int err; |
| 471 | |
| 472 | lock_page(page); |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 473 | err = ext2_prepare_chunk(page, pos, len); |
Eric Sesterhenn | 309be53 | 2006-03-26 18:27:41 +0200 | [diff] [blame] | 474 | BUG_ON(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | de->inode = cpu_to_le32(inode->i_ino); |
Nick Piggin | f34fb6e | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 476 | ext2_set_de_type(de, inode); |
| 477 | err = ext2_commit_chunk(page, pos, len); |
Jan Kara | 39fe755 | 2009-06-17 16:26:20 -0700 | [diff] [blame] | 478 | if (update_times) |
Deepa Dinamani | 02027d4 | 2016-09-14 07:48:05 -0700 | [diff] [blame] | 479 | dir->i_mtime = dir->i_ctime = current_time(dir); |
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; |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 496 | void *page_addr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | ext2_dirent * de; |
| 498 | unsigned long npages = dir_pages(dir); |
| 499 | unsigned long n; |
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++) { |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 509 | char *kaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | char *dir_end; |
| 511 | |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 512 | page = ext2_get_page(dir, n, 0, &page_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | err = PTR_ERR(page); |
| 514 | if (IS_ERR(page)) |
| 515 | goto out; |
| 516 | lock_page(page); |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 517 | kaddr = page_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | dir_end = kaddr + ext2_last_byte(dir, n); |
| 519 | de = (ext2_dirent *)kaddr; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 520 | kaddr += PAGE_SIZE - reclen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | while ((char *)de <= kaddr) { |
| 522 | if ((char *)de == dir_end) { |
| 523 | /* We hit i_size */ |
| 524 | name_len = 0; |
| 525 | rec_len = chunk_size; |
Jan Kara | 89910cc | 2007-10-21 16:41:40 -0700 | [diff] [blame] | 526 | de->rec_len = ext2_rec_len_to_disk(chunk_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | de->inode = 0; |
| 528 | goto got_it; |
| 529 | } |
| 530 | if (de->rec_len == 0) { |
Harvey Harrison | 605afd6 | 2008-04-28 02:16:03 -0700 | [diff] [blame] | 531 | ext2_error(dir->i_sb, __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | "zero-length directory entry"); |
| 533 | err = -EIO; |
| 534 | goto out_unlock; |
| 535 | } |
| 536 | err = -EEXIST; |
| 537 | if (ext2_match (namelen, name, de)) |
| 538 | goto out_unlock; |
| 539 | name_len = EXT2_DIR_REC_LEN(de->name_len); |
Jan Kara | 89910cc | 2007-10-21 16:41:40 -0700 | [diff] [blame] | 540 | rec_len = ext2_rec_len_from_disk(de->rec_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | if (!de->inode && rec_len >= reclen) |
| 542 | goto got_it; |
| 543 | if (rec_len >= name_len + reclen) |
| 544 | goto got_it; |
| 545 | de = (ext2_dirent *) ((char *) de + rec_len); |
| 546 | } |
| 547 | unlock_page(page); |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 548 | ext2_put_page(page, page_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | } |
| 550 | BUG(); |
| 551 | return -EINVAL; |
| 552 | |
| 553 | got_it: |
Nick Piggin | f34fb6e | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 554 | pos = page_offset(page) + |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 555 | (char *)de - (char *)page_addr; |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 556 | err = ext2_prepare_chunk(page, pos, rec_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | if (err) |
| 558 | goto out_unlock; |
| 559 | if (de->inode) { |
| 560 | ext2_dirent *de1 = (ext2_dirent *) ((char *) de + name_len); |
Jan Kara | 89910cc | 2007-10-21 16:41:40 -0700 | [diff] [blame] | 561 | de1->rec_len = ext2_rec_len_to_disk(rec_len - name_len); |
| 562 | de->rec_len = ext2_rec_len_to_disk(name_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | de = de1; |
| 564 | } |
| 565 | de->name_len = namelen; |
Nick Piggin | f34fb6e | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 566 | memcpy(de->name, name, namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | de->inode = cpu_to_le32(inode->i_ino); |
| 568 | ext2_set_de_type (de, inode); |
Nick Piggin | f34fb6e | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 569 | err = ext2_commit_chunk(page, pos, rec_len); |
Deepa Dinamani | 02027d4 | 2016-09-14 07:48:05 -0700 | [diff] [blame] | 570 | dir->i_mtime = dir->i_ctime = current_time(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL; |
| 572 | mark_inode_dirty(dir); |
| 573 | /* OFFSET_CACHE */ |
| 574 | out_put: |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 575 | ext2_put_page(page, page_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | out: |
| 577 | return err; |
| 578 | out_unlock: |
| 579 | unlock_page(page); |
| 580 | goto out_put; |
| 581 | } |
| 582 | |
| 583 | /* |
| 584 | * ext2_delete_entry deletes a directory entry by merging it with the |
Ira Weiny | e2ebb12 | 2021-03-28 23:54:01 -0700 | [diff] [blame] | 585 | * previous entry. Page is up-to-date. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | */ |
Javier Pello | 728d392 | 2021-07-14 18:54:48 +0200 | [diff] [blame] | 587 | int ext2_delete_entry (struct ext2_dir_entry_2 *dir, struct page *page, |
| 588 | char *kaddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | { |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 590 | struct inode *inode = page->mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | unsigned from = ((char*)dir - kaddr) & ~(ext2_chunk_size(inode)-1); |
Jan Kara | 89910cc | 2007-10-21 16:41:40 -0700 | [diff] [blame] | 592 | unsigned to = ((char *)dir - kaddr) + |
| 593 | ext2_rec_len_from_disk(dir->rec_len); |
Nick Piggin | f34fb6e | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 594 | loff_t pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | ext2_dirent * pde = NULL; |
| 596 | ext2_dirent * de = (ext2_dirent *) (kaddr + from); |
| 597 | int err; |
| 598 | |
| 599 | while ((char*)de < (char*)dir) { |
| 600 | if (de->rec_len == 0) { |
Harvey Harrison | 605afd6 | 2008-04-28 02:16:03 -0700 | [diff] [blame] | 601 | ext2_error(inode->i_sb, __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | "zero-length directory entry"); |
| 603 | err = -EIO; |
| 604 | goto out; |
| 605 | } |
| 606 | pde = de; |
| 607 | de = ext2_next_entry(de); |
| 608 | } |
| 609 | if (pde) |
Javier Pello | 728d392 | 2021-07-14 18:54:48 +0200 | [diff] [blame] | 610 | from = (char *)pde - kaddr; |
Nick Piggin | f34fb6e | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 611 | pos = page_offset(page) + from; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | lock_page(page); |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 613 | err = ext2_prepare_chunk(page, pos, to - from); |
Eric Sesterhenn | 309be53 | 2006-03-26 18:27:41 +0200 | [diff] [blame] | 614 | BUG_ON(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | if (pde) |
Jan Kara | 89910cc | 2007-10-21 16:41:40 -0700 | [diff] [blame] | 616 | pde->rec_len = ext2_rec_len_to_disk(to - from); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | dir->inode = 0; |
Nick Piggin | f34fb6e | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 618 | err = ext2_commit_chunk(page, pos, to - from); |
Deepa Dinamani | 02027d4 | 2016-09-14 07:48:05 -0700 | [diff] [blame] | 619 | inode->i_ctime = inode->i_mtime = current_time(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | EXT2_I(inode)->i_flags &= ~EXT2_BTREE_FL; |
| 621 | mark_inode_dirty(inode); |
| 622 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 663 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 672 | void *page_addr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | struct page *page = NULL; |
| 674 | unsigned long i, npages = dir_pages(inode); |
Eric Sandeen | bd39597 | 2008-10-15 22:04:02 -0700 | [diff] [blame] | 675 | int dir_has_error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | |
| 677 | for (i = 0; i < npages; i++) { |
| 678 | char *kaddr; |
| 679 | ext2_dirent * de; |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 680 | page = ext2_get_page(inode, i, dir_has_error, &page_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | |
Eric Sandeen | bd39597 | 2008-10-15 22:04:02 -0700 | [diff] [blame] | 682 | if (IS_ERR(page)) { |
| 683 | dir_has_error = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | continue; |
Eric Sandeen | bd39597 | 2008-10-15 22:04:02 -0700 | [diff] [blame] | 685 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 687 | kaddr = page_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | de = (ext2_dirent *)kaddr; |
| 689 | kaddr += ext2_last_byte(inode, i) - EXT2_DIR_REC_LEN(1); |
| 690 | |
| 691 | while ((char *)de <= kaddr) { |
| 692 | if (de->rec_len == 0) { |
Harvey Harrison | 605afd6 | 2008-04-28 02:16:03 -0700 | [diff] [blame] | 693 | ext2_error(inode->i_sb, __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | "zero-length directory entry"); |
| 695 | printk("kaddr=%p, de=%p\n", kaddr, de); |
| 696 | goto not_empty; |
| 697 | } |
| 698 | if (de->inode != 0) { |
| 699 | /* check for . and .. */ |
| 700 | if (de->name[0] != '.') |
| 701 | goto not_empty; |
| 702 | if (de->name_len > 2) |
| 703 | goto not_empty; |
| 704 | if (de->name_len < 2) { |
| 705 | if (de->inode != |
| 706 | cpu_to_le32(inode->i_ino)) |
| 707 | goto not_empty; |
| 708 | } else if (de->name[1] != '.') |
| 709 | goto not_empty; |
| 710 | } |
| 711 | de = ext2_next_entry(de); |
| 712 | } |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 713 | ext2_put_page(page, page_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | } |
| 715 | return 1; |
| 716 | |
| 717 | not_empty: |
Ira Weiny | 782b76d | 2021-03-28 23:54:02 -0700 | [diff] [blame] | 718 | ext2_put_page(page, page_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | return 0; |
| 720 | } |
| 721 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 722 | const struct file_operations ext2_dir_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | .llseek = generic_file_llseek, |
| 724 | .read = generic_read_dir, |
Al Viro | 3b0a3c1 | 2016-04-20 23:42:46 -0400 | [diff] [blame] | 725 | .iterate_shared = ext2_readdir, |
Andi Kleen | 14f9f7b | 2008-02-06 01:40:10 -0800 | [diff] [blame] | 726 | .unlocked_ioctl = ext2_ioctl, |
David Howells | e322ff0 | 2006-08-29 19:06:20 +0100 | [diff] [blame] | 727 | #ifdef CONFIG_COMPAT |
| 728 | .compat_ioctl = ext2_compat_ioctl, |
| 729 | #endif |
Jan Kara | 48bde86 | 2009-12-15 16:46:49 -0800 | [diff] [blame] | 730 | .fsync = ext2_fsync, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | }; |