blob: 2c2f179b69779b7b5887b9c12f8ff7b2c73c7b60 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 Pigginf34fb6e2007-10-16 01:25:04 -070026#include <linux/buffer_head.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/pagemap.h>
Nick Pigginf34fb6e2007-10-16 01:25:04 -070028#include <linux/swap.h>
Jeff Laytone1d747d2017-12-11 06:35:14 -050029#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31typedef struct ext2_dir_entry_2 ext2_dirent;
32
Eric Sandeen40a063f2010-12-07 11:51:05 -060033/*
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 Kara89910cc2007-10-21 16:41:40 -070038static inline unsigned ext2_rec_len_from_disk(__le16 dlen)
39{
40 unsigned len = le16_to_cpu(dlen);
41
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +030042#if (PAGE_SIZE >= 65536)
Jan Kara89910cc2007-10-21 16:41:40 -070043 if (len == EXT2_MAX_REC_LEN)
44 return 1 << 16;
Eric Sandeen40a063f2010-12-07 11:51:05 -060045#endif
Jan Kara89910cc2007-10-21 16:41:40 -070046 return len;
47}
48
49static inline __le16 ext2_rec_len_to_disk(unsigned len)
50{
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +030051#if (PAGE_SIZE >= 65536)
Jan Kara89910cc2007-10-21 16:41:40 -070052 if (len == (1 << 16))
53 return cpu_to_le16(EXT2_MAX_REC_LEN);
Julia Lawall2c116192008-04-28 02:16:02 -070054 else
55 BUG_ON(len > (1 << 16));
Eric Sandeen40a063f2010-12-07 11:51:05 -060056#endif
Jan Kara89910cc2007-10-21 16:41:40 -070057 return cpu_to_le16(len);
58}
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/*
61 * ext2 uses block-sized chunks. Arguably, sector-sized ones would be
62 * more robust, but we have what we have
63 */
64static inline unsigned ext2_chunk_size(struct inode *inode)
65{
66 return inode->i_sb->s_blocksize;
67}
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
70 * Return the offset into page `page_nr' of the last valid
71 * byte in that page, plus one.
72 */
73static unsigned
74ext2_last_byte(struct inode *inode, unsigned long page_nr)
75{
76 unsigned last_byte = inode->i_size;
77
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030078 last_byte -= page_nr << PAGE_SHIFT;
79 if (last_byte > PAGE_SIZE)
80 last_byte = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return last_byte;
82}
83
Nick Pigginf34fb6e2007-10-16 01:25:04 -070084static int ext2_commit_chunk(struct page *page, loff_t pos, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Nick Pigginf34fb6e2007-10-16 01:25:04 -070086 struct address_space *mapping = page->mapping;
87 struct inode *dir = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 int err = 0;
Nick Pigginf34fb6e2007-10-16 01:25:04 -070089
Jeff Laytone1d747d2017-12-11 06:35:14 -050090 inode_inc_iversion(dir);
Nick Pigginf34fb6e2007-10-16 01:25:04 -070091 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 Kara6b7021e2009-01-15 13:51:29 -080098 if (IS_DIRSYNC(dir)) {
Jeff Layton2b69c822017-07-05 15:26:48 -040099 err = write_one_page(page);
Jan Kara6b7021e2009-01-15 13:51:29 -0800100 if (!err)
Christoph Hellwigc37650162010-10-06 10:48:20 +0200101 err = sync_inode_metadata(dir, 1);
Jan Kara6b7021e2009-01-15 13:51:29 -0800102 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 unlock_page(page);
Jan Kara6b7021e2009-01-15 13:51:29 -0800104 }
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return err;
107}
108
Javier Pello728d3922021-07-14 18:54:48 +0200109static bool ext2_check_page(struct page *page, int quiet, char *kaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 struct inode *dir = page->mapping->host;
112 struct super_block *sb = dir->i_sb;
113 unsigned chunk_size = ext2_chunk_size(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 u32 max_inumber = le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count);
115 unsigned offs, rec_len;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300116 unsigned limit = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 ext2_dirent *p;
118 char *error;
119
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300120 if ((dir->i_size >> PAGE_SHIFT) == page->index) {
121 limit = dir->i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 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 Kara89910cc2007-10-21 16:41:40 -0700129 rec_len = ext2_rec_len_from_disk(p->rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Eric Sandeen40a063f2010-12-07 11:51:05 -0600131 if (unlikely(rec_len < EXT2_DIR_REC_LEN(1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 goto Eshort;
Eric Sandeen40a063f2010-12-07 11:51:05 -0600133 if (unlikely(rec_len & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 goto Ealign;
Eric Sandeen40a063f2010-12-07 11:51:05 -0600135 if (unlikely(rec_len < EXT2_DIR_REC_LEN(p->name_len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 goto Enamelen;
Eric Sandeen40a063f2010-12-07 11:51:05 -0600137 if (unlikely(((offs + rec_len - 1) ^ offs) & ~(chunk_size-1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 goto Espan;
Eric Sandeen40a063f2010-12-07 11:51:05 -0600139 if (unlikely(le32_to_cpu(p->inode) > max_inumber))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 goto Einumber;
141 }
142 if (offs != limit)
143 goto Eend;
144out:
145 SetPageChecked(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400146 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 /* Too bad, we had an error */
149
150Ebadsize:
Eric Sandeenbd395972008-10-15 22:04:02 -0700151 if (!quiet)
152 ext2_error(sb, __func__,
153 "size of directory #%lu is not a multiple "
154 "of chunk size", dir->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 goto fail;
156Eshort:
157 error = "rec_len is smaller than minimal";
158 goto bad_entry;
159Ealign:
160 error = "unaligned directory entry";
161 goto bad_entry;
162Enamelen:
163 error = "rec_len is too small for name_len";
164 goto bad_entry;
165Espan:
166 error = "directory entry across blocks";
167 goto bad_entry;
168Einumber:
169 error = "inode out of bounds";
170bad_entry:
Eric Sandeenbd395972008-10-15 22:04:02 -0700171 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. Shutemov09cbfea2016-04-01 15:29:47 +0300174 dir->i_ino, error, (page->index<<PAGE_SHIFT)+offs,
Eric Sandeenbd395972008-10-15 22:04:02 -0700175 (unsigned long) le32_to_cpu(p->inode),
176 rec_len, p->name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 goto fail;
178Eend:
Eric Sandeenbd395972008-10-15 22:04:02 -0700179 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. Shutemov09cbfea2016-04-01 15:29:47 +0300184 dir->i_ino, (page->index<<PAGE_SHIFT)+offs,
Eric Sandeenbd395972008-10-15 22:04:02 -0700185 (unsigned long) le32_to_cpu(p->inode));
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 SetPageError(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400189 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Ira Weiny782b76d2021-03-28 23:54:02 -0700192/*
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 Sandeenbd395972008-10-15 22:04:02 -0700199static struct page * ext2_get_page(struct inode *dir, unsigned long n,
Ira Weiny782b76d2021-03-28 23:54:02 -0700200 int quiet, void **page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 struct address_space *mapping = dir->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -0700203 struct page *page = read_mapping_page(mapping, n, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (!IS_ERR(page)) {
Ira Weiny782b76d2021-03-28 23:54:02 -0700205 *page_addr = kmap_local_page(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400206 if (unlikely(!PageChecked(page))) {
Javier Pello728d3922021-07-14 18:54:48 +0200207 if (PageError(page) || !ext2_check_page(page, quiet,
208 *page_addr))
Al Virobe5b82d2016-04-22 15:06:44 -0400209 goto fail;
210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212 return page;
213
214fail:
Ira Weiny782b76d2021-03-28 23:54:02 -0700215 ext2_put_page(page, *page_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 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 */
224static 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 */
237static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
238{
Jan Kara89910cc2007-10-21 16:41:40 -0700239 return (ext2_dirent *)((char *)p +
240 ext2_rec_len_from_disk(p->rec_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243static inline unsigned
244ext2_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 Torvalds1da177e2005-04-16 15:20:36 -0700256static inline void ext2_set_de_type(ext2_dirent *de, struct inode *inode)
257{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (EXT2_HAS_INCOMPAT_FEATURE(inode->i_sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
Phillip Pottere1089212019-01-21 00:54:31 +0000259 de->file_type = fs_umode_to_ftype(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 else
261 de->file_type = 0;
262}
263
264static int
Al Viro80886292013-05-15 18:51:49 -0400265ext2_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Al Viro80886292013-05-15 18:51:49 -0400267 loff_t pos = ctx->pos;
268 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 struct super_block *sb = inode->i_sb;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300270 unsigned int offset = pos & ~PAGE_MASK;
271 unsigned long n = pos >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 unsigned long npages = dir_pages(inode);
273 unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
Goffredo Baroncellic472c072018-02-01 08:15:25 -0500274 bool need_revalidate = !inode_eq_iversion(inode, file->f_version);
Phillip Pottere1089212019-01-21 00:54:31 +0000275 bool has_filetype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
Al Viro2d7f2ea2006-03-15 21:41:59 +0000278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Phillip Pottere1089212019-01-21 00:54:31 +0000280 has_filetype =
281 EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 for ( ; n < npages; n++, offset = 0) {
284 char *kaddr, *limit;
285 ext2_dirent *de;
Ira Weiny782b76d2021-03-28 23:54:02 -0700286 struct page *page = ext2_get_page(inode, n, 0, (void **)&kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (IS_ERR(page)) {
Harvey Harrison605afd62008-04-28 02:16:03 -0700289 ext2_error(sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 "bad page in #%lu",
291 inode->i_ino);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300292 ctx->pos += PAGE_SIZE - offset;
Akinobu Mitabbff2862008-04-28 02:16:00 -0700293 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
Al Viro2d7f2ea2006-03-15 21:41:59 +0000295 if (unlikely(need_revalidate)) {
296 if (offset) {
297 offset = ext2_validate_entry(kaddr, offset, chunk_mask);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300298 ctx->pos = (n<<PAGE_SHIFT) + offset;
Al Viro2d7f2ea2006-03-15 21:41:59 +0000299 }
Jeff Laytone1d747d2017-12-11 06:35:14 -0500300 file->f_version = inode_query_iversion(inode);
301 need_revalidate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
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 Harrison605afd62008-04-28 02:16:03 -0700307 ext2_error(sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 "zero-length directory entry");
Ira Weiny782b76d2021-03-28 23:54:02 -0700309 ext2_put_page(page, kaddr);
Al Viro2d7f2ea2006-03-15 21:41:59 +0000310 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312 if (de->inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 unsigned char d_type = DT_UNKNOWN;
314
Phillip Pottere1089212019-01-21 00:54:31 +0000315 if (has_filetype)
316 d_type = fs_ftype_to_dtype(de->file_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Al Viro80886292013-05-15 18:51:49 -0400318 if (!dir_emit(ctx, de->name, de->name_len,
319 le32_to_cpu(de->inode),
320 d_type)) {
Ira Weiny782b76d2021-03-28 23:54:02 -0700321 ext2_put_page(page, kaddr);
Al Viro2d7f2ea2006-03-15 21:41:59 +0000322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324 }
Al Viro80886292013-05-15 18:51:49 -0400325 ctx->pos += ext2_rec_len_from_disk(de->rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
Ira Weiny782b76d2021-03-28 23:54:02 -0700327 ext2_put_page(page, kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
Al Viro2d7f2ea2006-03-15 21:41:59 +0000329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332/*
333 * ext2_find_entry()
334 *
335 * finds an entry in the specified directory with the wanted name. It
Jérémy Cochoy92e12882009-11-30 17:58:43 +0100336 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700338 * Entry is guaranteed to be valid.
Ira Weinya6fbd0a2020-11-12 09:42:44 -0800339 *
340 * On Success ext2_put_page() should be called on *res_page.
Ira Weiny782b76d2021-03-28 23:54:02 -0700341 *
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 Torvalds1da177e2005-04-16 15:20:36 -0700347 */
Al Viroac3ba642016-07-20 22:47:26 -0400348struct ext2_dir_entry_2 *ext2_find_entry (struct inode *dir,
Ira Weiny782b76d2021-03-28 23:54:02 -0700349 const struct qstr *child, struct page **res_page,
350 void **res_page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Al Viroa9885442008-08-24 07:28:39 -0400352 const char *name = child->name;
353 int namelen = child->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 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 Weiny782b76d2021-03-28 23:54:02 -0700360 void *page_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 if (npages == 0)
363 goto out;
364
365 /* OFFSET_CACHE */
366 *res_page = NULL;
Ira Weiny782b76d2021-03-28 23:54:02 -0700367 *res_page_addr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 start = ei->i_dir_start_lookup;
370 if (start >= npages)
371 start = 0;
372 n = start;
373 do {
374 char *kaddr;
Ira Weiny782b76d2021-03-28 23:54:02 -0700375 page = ext2_get_page(dir, n, 0, &page_addr);
zhangyi (F)b4962092020-06-08 11:40:42 +0800376 if (IS_ERR(page))
377 return ERR_CAST(page);
378
Ira Weiny782b76d2021-03-28 23:54:02 -0700379 kaddr = page_addr;
zhangyi (F)b4962092020-06-08 11:40:42 +0800380 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 Weiny782b76d2021-03-28 23:54:02 -0700386 ext2_put_page(page, page_addr);
zhangyi (F)b4962092020-06-08 11:40:42 +0800387 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
zhangyi (F)b4962092020-06-08 11:40:42 +0800389 if (ext2_match(namelen, name, de))
390 goto found;
391 de = ext2_next_entry(de);
392 }
Ira Weiny782b76d2021-03-28 23:54:02 -0700393 ext2_put_page(page, page_addr);
Eric Sandeenbd395972008-10-15 22:04:02 -0700394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (++n >= npages)
396 n = 0;
Eric Sandeend8adb9c2007-02-10 01:45:06 -0800397 /* next page is past the blocks we've got */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300398 if (unlikely(n > (dir->i_blocks >> (PAGE_SHIFT - 9)))) {
Harvey Harrison605afd62008-04-28 02:16:03 -0700399 ext2_error(dir->i_sb, __func__,
Eric Sandeend8adb9c2007-02-10 01:45:06 -0800400 "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 Torvalds1da177e2005-04-16 15:20:36 -0700405 } while (n != start);
406out:
zhangyi (F)a43850a2020-06-08 11:40:43 +0800407 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409found:
410 *res_page = page;
Ira Weiny782b76d2021-03-28 23:54:02 -0700411 *res_page_addr = page_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 ei->i_dir_start_lookup = n;
413 return de;
414}
415
Ira Weinya6fbd0a2020-11-12 09:42:44 -0800416/**
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 Weiny782b76d2021-03-28 23:54:02 -0700421 *
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 Weinya6fbd0a2020-11-12 09:42:44 -0800427 */
Ira Weiny782b76d2021-03-28 23:54:02 -0700428struct ext2_dir_entry_2 *ext2_dotdot(struct inode *dir, struct page **p,
429 void **pa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Ira Weiny782b76d2021-03-28 23:54:02 -0700431 void *page_addr;
432 struct page *page = ext2_get_page(dir, 0, 0, &page_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 ext2_dirent *de = NULL;
434
435 if (!IS_ERR(page)) {
Ira Weiny782b76d2021-03-28 23:54:02 -0700436 de = ext2_next_entry((ext2_dirent *) page_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 *p = page;
Ira Weiny782b76d2021-03-28 23:54:02 -0700438 *pa = page_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440 return de;
441}
442
zhangyi (F)b4962092020-06-08 11:40:42 +0800443int ext2_inode_by_name(struct inode *dir, const struct qstr *child, ino_t *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Al Viroa9885442008-08-24 07:28:39 -0400445 struct ext2_dir_entry_2 *de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct page *page;
Ira Weiny782b76d2021-03-28 23:54:02 -0700447 void *page_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Ira Weiny782b76d2021-03-28 23:54:02 -0700449 de = ext2_find_entry(dir, child, &page, &page_addr);
zhangyi (F)a43850a2020-06-08 11:40:43 +0800450 if (IS_ERR(de))
zhangyi (F)b4962092020-06-08 11:40:42 +0800451 return PTR_ERR(de);
452
453 *ino = le32_to_cpu(de->inode);
Ira Weiny782b76d2021-03-28 23:54:02 -0700454 ext2_put_page(page, page_addr);
zhangyi (F)b4962092020-06-08 11:40:42 +0800455 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200458static int ext2_prepare_chunk(struct page *page, loff_t pos, unsigned len)
459{
Christoph Hellwig6e1db882010-06-04 11:29:57 +0200460 return __block_write_begin(page, pos, len, ext2_get_block);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200461}
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
Ira Weiny782b76d2021-03-28 23:54:02 -0700464 struct page *page, void *page_addr, struct inode *inode,
465 int update_times)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700467 loff_t pos = page_offset(page) +
Ira Weiny782b76d2021-03-28 23:54:02 -0700468 (char *) de - (char *) page_addr;
Jan Kara89910cc2007-10-21 16:41:40 -0700469 unsigned len = ext2_rec_len_from_disk(de->rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 int err;
471
472 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200473 err = ext2_prepare_chunk(page, pos, len);
Eric Sesterhenn309be532006-03-26 18:27:41 +0200474 BUG_ON(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 de->inode = cpu_to_le32(inode->i_ino);
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700476 ext2_set_de_type(de, inode);
477 err = ext2_commit_chunk(page, pos, len);
Jan Kara39fe7552009-06-17 16:26:20 -0700478 if (update_times)
Deepa Dinamani02027d42016-09-14 07:48:05 -0700479 dir->i_mtime = dir->i_ctime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
481 mark_inode_dirty(dir);
482}
483
484/*
485 * Parent is locked.
486 */
487int ext2_add_link (struct dentry *dentry, struct inode *inode)
488{
David Howells2b0143b2015-03-17 22:25:59 +0000489 struct inode *dir = d_inode(dentry->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 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 Weiny782b76d2021-03-28 23:54:02 -0700496 void *page_addr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 ext2_dirent * de;
498 unsigned long npages = dir_pages(dir);
499 unsigned long n;
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700500 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 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 Weiny782b76d2021-03-28 23:54:02 -0700509 char *kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 char *dir_end;
511
Ira Weiny782b76d2021-03-28 23:54:02 -0700512 page = ext2_get_page(dir, n, 0, &page_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 err = PTR_ERR(page);
514 if (IS_ERR(page))
515 goto out;
516 lock_page(page);
Ira Weiny782b76d2021-03-28 23:54:02 -0700517 kaddr = page_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 dir_end = kaddr + ext2_last_byte(dir, n);
519 de = (ext2_dirent *)kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300520 kaddr += PAGE_SIZE - reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 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 Kara89910cc2007-10-21 16:41:40 -0700526 de->rec_len = ext2_rec_len_to_disk(chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 de->inode = 0;
528 goto got_it;
529 }
530 if (de->rec_len == 0) {
Harvey Harrison605afd62008-04-28 02:16:03 -0700531 ext2_error(dir->i_sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 "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 Kara89910cc2007-10-21 16:41:40 -0700540 rec_len = ext2_rec_len_from_disk(de->rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 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 Weiny782b76d2021-03-28 23:54:02 -0700548 ext2_put_page(page, page_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550 BUG();
551 return -EINVAL;
552
553got_it:
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700554 pos = page_offset(page) +
Ira Weiny782b76d2021-03-28 23:54:02 -0700555 (char *)de - (char *)page_addr;
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200556 err = ext2_prepare_chunk(page, pos, rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (err)
558 goto out_unlock;
559 if (de->inode) {
560 ext2_dirent *de1 = (ext2_dirent *) ((char *) de + name_len);
Jan Kara89910cc2007-10-21 16:41:40 -0700561 de1->rec_len = ext2_rec_len_to_disk(rec_len - name_len);
562 de->rec_len = ext2_rec_len_to_disk(name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 de = de1;
564 }
565 de->name_len = namelen;
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700566 memcpy(de->name, name, namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 de->inode = cpu_to_le32(inode->i_ino);
568 ext2_set_de_type (de, inode);
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700569 err = ext2_commit_chunk(page, pos, rec_len);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700570 dir->i_mtime = dir->i_ctime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
572 mark_inode_dirty(dir);
573 /* OFFSET_CACHE */
574out_put:
Ira Weiny782b76d2021-03-28 23:54:02 -0700575 ext2_put_page(page, page_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576out:
577 return err;
578out_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 Weinye2ebb122021-03-28 23:54:01 -0700585 * previous entry. Page is up-to-date.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 */
Javier Pello728d3922021-07-14 18:54:48 +0200587int ext2_delete_entry (struct ext2_dir_entry_2 *dir, struct page *page,
588 char *kaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200590 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 unsigned from = ((char*)dir - kaddr) & ~(ext2_chunk_size(inode)-1);
Jan Kara89910cc2007-10-21 16:41:40 -0700592 unsigned to = ((char *)dir - kaddr) +
593 ext2_rec_len_from_disk(dir->rec_len);
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700594 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 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 Harrison605afd62008-04-28 02:16:03 -0700601 ext2_error(inode->i_sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 "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 Pello728d3922021-07-14 18:54:48 +0200610 from = (char *)pde - kaddr;
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700611 pos = page_offset(page) + from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200613 err = ext2_prepare_chunk(page, pos, to - from);
Eric Sesterhenn309be532006-03-26 18:27:41 +0200614 BUG_ON(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (pde)
Jan Kara89910cc2007-10-21 16:41:40 -0700616 pde->rec_len = ext2_rec_len_to_disk(to - from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 dir->inode = 0;
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700618 err = ext2_commit_chunk(page, pos, to - from);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700619 inode->i_ctime = inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 EXT2_I(inode)->i_flags &= ~EXT2_BTREE_FL;
621 mark_inode_dirty(inode);
622out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return err;
624}
625
626/*
627 * Set the first fragment of directory.
628 */
629int ext2_make_empty(struct inode *inode, struct inode *parent)
630{
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200631 struct page *page = grab_cache_page(inode->i_mapping, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 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 Pigginf34fb6e2007-10-16 01:25:04 -0700639
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200640 err = ext2_prepare_chunk(page, 0, chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (err) {
642 unlock_page(page);
643 goto fail;
644 }
Cong Wangd4a23ae2011-11-25 23:14:29 +0800645 kaddr = kmap_atomic(page);
Luiz Fernando Capitulino7823c7c2006-01-11 01:38:27 +0100646 memset(kaddr, 0, chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 de = (struct ext2_dir_entry_2 *)kaddr;
648 de->name_len = 1;
Jan Kara89910cc2007-10-21 16:41:40 -0700649 de->rec_len = ext2_rec_len_to_disk(EXT2_DIR_REC_LEN(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 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 Kara89910cc2007-10-21 16:41:40 -0700656 de->rec_len = ext2_rec_len_to_disk(chunk_size - EXT2_DIR_REC_LEN(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 de->inode = cpu_to_le32(parent->i_ino);
658 memcpy (de->name, "..\0", 4);
659 ext2_set_de_type (de, inode);
Cong Wangd4a23ae2011-11-25 23:14:29 +0800660 kunmap_atomic(kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 err = ext2_commit_chunk(page, 0, chunk_size);
662fail:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300663 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return err;
665}
666
667/*
668 * routine to check that the specified directory is empty (for rmdir)
669 */
670int ext2_empty_dir (struct inode * inode)
671{
Ira Weiny782b76d2021-03-28 23:54:02 -0700672 void *page_addr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 struct page *page = NULL;
674 unsigned long i, npages = dir_pages(inode);
Eric Sandeenbd395972008-10-15 22:04:02 -0700675 int dir_has_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 for (i = 0; i < npages; i++) {
678 char *kaddr;
679 ext2_dirent * de;
Ira Weiny782b76d2021-03-28 23:54:02 -0700680 page = ext2_get_page(inode, i, dir_has_error, &page_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Eric Sandeenbd395972008-10-15 22:04:02 -0700682 if (IS_ERR(page)) {
683 dir_has_error = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 continue;
Eric Sandeenbd395972008-10-15 22:04:02 -0700685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Ira Weiny782b76d2021-03-28 23:54:02 -0700687 kaddr = page_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 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 Harrison605afd62008-04-28 02:16:03 -0700693 ext2_error(inode->i_sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 "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 Weiny782b76d2021-03-28 23:54:02 -0700713 ext2_put_page(page, page_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 return 1;
716
717not_empty:
Ira Weiny782b76d2021-03-28 23:54:02 -0700718 ext2_put_page(page, page_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return 0;
720}
721
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800722const struct file_operations ext2_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 .llseek = generic_file_llseek,
724 .read = generic_read_dir,
Al Viro3b0a3c12016-04-20 23:42:46 -0400725 .iterate_shared = ext2_readdir,
Andi Kleen14f9f7b2008-02-06 01:40:10 -0800726 .unlocked_ioctl = ext2_ioctl,
David Howellse322ff02006-08-29 19:06:20 +0100727#ifdef CONFIG_COMPAT
728 .compat_ioctl = ext2_compat_ioctl,
729#endif
Jan Kara48bde862009-12-15 16:46:49 -0800730 .fsync = ext2_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731};