blob: 14aa45316ad214a05109d18a419189447e0741f8 [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
Al Virobe5b82d2016-04-22 15:06:44 -0400109static bool ext2_check_page(struct page *page, int quiet)
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);
114 char *kaddr = page_address(page);
115 u32 max_inumber = le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count);
116 unsigned offs, rec_len;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300117 unsigned limit = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 ext2_dirent *p;
119 char *error;
120
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300121 if ((dir->i_size >> PAGE_SHIFT) == page->index) {
122 limit = dir->i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (limit & (chunk_size - 1))
124 goto Ebadsize;
125 if (!limit)
126 goto out;
127 }
128 for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
129 p = (ext2_dirent *)(kaddr + offs);
Jan Kara89910cc2007-10-21 16:41:40 -0700130 rec_len = ext2_rec_len_from_disk(p->rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Eric Sandeen40a063f2010-12-07 11:51:05 -0600132 if (unlikely(rec_len < EXT2_DIR_REC_LEN(1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 goto Eshort;
Eric Sandeen40a063f2010-12-07 11:51:05 -0600134 if (unlikely(rec_len & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 goto Ealign;
Eric Sandeen40a063f2010-12-07 11:51:05 -0600136 if (unlikely(rec_len < EXT2_DIR_REC_LEN(p->name_len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 goto Enamelen;
Eric Sandeen40a063f2010-12-07 11:51:05 -0600138 if (unlikely(((offs + rec_len - 1) ^ offs) & ~(chunk_size-1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 goto Espan;
Eric Sandeen40a063f2010-12-07 11:51:05 -0600140 if (unlikely(le32_to_cpu(p->inode) > max_inumber))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 goto Einumber;
142 }
143 if (offs != limit)
144 goto Eend;
145out:
146 SetPageChecked(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400147 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 /* Too bad, we had an error */
150
151Ebadsize:
Eric Sandeenbd395972008-10-15 22:04:02 -0700152 if (!quiet)
153 ext2_error(sb, __func__,
154 "size of directory #%lu is not a multiple "
155 "of chunk size", dir->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 goto fail;
157Eshort:
158 error = "rec_len is smaller than minimal";
159 goto bad_entry;
160Ealign:
161 error = "unaligned directory entry";
162 goto bad_entry;
163Enamelen:
164 error = "rec_len is too small for name_len";
165 goto bad_entry;
166Espan:
167 error = "directory entry across blocks";
168 goto bad_entry;
169Einumber:
170 error = "inode out of bounds";
171bad_entry:
Eric Sandeenbd395972008-10-15 22:04:02 -0700172 if (!quiet)
173 ext2_error(sb, __func__, "bad entry in directory #%lu: : %s - "
174 "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300175 dir->i_ino, error, (page->index<<PAGE_SHIFT)+offs,
Eric Sandeenbd395972008-10-15 22:04:02 -0700176 (unsigned long) le32_to_cpu(p->inode),
177 rec_len, p->name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 goto fail;
179Eend:
Eric Sandeenbd395972008-10-15 22:04:02 -0700180 if (!quiet) {
181 p = (ext2_dirent *)(kaddr + offs);
182 ext2_error(sb, "ext2_check_page",
183 "entry in directory #%lu spans the page boundary"
184 "offset=%lu, inode=%lu",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300185 dir->i_ino, (page->index<<PAGE_SHIFT)+offs,
Eric Sandeenbd395972008-10-15 22:04:02 -0700186 (unsigned long) le32_to_cpu(p->inode));
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 SetPageError(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400190 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Eric Sandeenbd395972008-10-15 22:04:02 -0700193static struct page * ext2_get_page(struct inode *dir, unsigned long n,
194 int quiet)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 struct address_space *mapping = dir->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -0700197 struct page *page = read_mapping_page(mapping, n, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (!IS_ERR(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 kmap(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400200 if (unlikely(!PageChecked(page))) {
201 if (PageError(page) || !ext2_check_page(page, quiet))
202 goto fail;
203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205 return page;
206
207fail:
208 ext2_put_page(page);
209 return ERR_PTR(-EIO);
210}
211
212/*
213 * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure.
214 *
215 * len <= EXT2_NAME_LEN and de != NULL are guaranteed by caller.
216 */
217static inline int ext2_match (int len, const char * const name,
218 struct ext2_dir_entry_2 * de)
219{
220 if (len != de->name_len)
221 return 0;
222 if (!de->inode)
223 return 0;
224 return !memcmp(name, de->name, len);
225}
226
227/*
228 * p is at least 6 bytes before the end of page
229 */
230static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
231{
Jan Kara89910cc2007-10-21 16:41:40 -0700232 return (ext2_dirent *)((char *)p +
233 ext2_rec_len_from_disk(p->rec_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236static inline unsigned
237ext2_validate_entry(char *base, unsigned offset, unsigned mask)
238{
239 ext2_dirent *de = (ext2_dirent*)(base + offset);
240 ext2_dirent *p = (ext2_dirent*)(base + (offset&mask));
241 while ((char*)p < (char*)de) {
242 if (p->rec_len == 0)
243 break;
244 p = ext2_next_entry(p);
245 }
246 return (char *)p - base;
247}
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249static inline void ext2_set_de_type(ext2_dirent *de, struct inode *inode)
250{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (EXT2_HAS_INCOMPAT_FEATURE(inode->i_sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
Phillip Pottere1089212019-01-21 00:54:31 +0000252 de->file_type = fs_umode_to_ftype(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 else
254 de->file_type = 0;
255}
256
257static int
Al Viro80886292013-05-15 18:51:49 -0400258ext2_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Al Viro80886292013-05-15 18:51:49 -0400260 loff_t pos = ctx->pos;
261 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 struct super_block *sb = inode->i_sb;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300263 unsigned int offset = pos & ~PAGE_MASK;
264 unsigned long n = pos >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 unsigned long npages = dir_pages(inode);
266 unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
Goffredo Baroncellic472c072018-02-01 08:15:25 -0500267 bool need_revalidate = !inode_eq_iversion(inode, file->f_version);
Phillip Pottere1089212019-01-21 00:54:31 +0000268 bool has_filetype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
Al Viro2d7f2ea2006-03-15 21:41:59 +0000271 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Phillip Pottere1089212019-01-21 00:54:31 +0000273 has_filetype =
274 EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 for ( ; n < npages; n++, offset = 0) {
277 char *kaddr, *limit;
278 ext2_dirent *de;
Eric Sandeenbd395972008-10-15 22:04:02 -0700279 struct page *page = ext2_get_page(inode, n, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 if (IS_ERR(page)) {
Harvey Harrison605afd62008-04-28 02:16:03 -0700282 ext2_error(sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 "bad page in #%lu",
284 inode->i_ino);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300285 ctx->pos += PAGE_SIZE - offset;
Akinobu Mitabbff2862008-04-28 02:16:00 -0700286 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288 kaddr = page_address(page);
Al Viro2d7f2ea2006-03-15 21:41:59 +0000289 if (unlikely(need_revalidate)) {
290 if (offset) {
291 offset = ext2_validate_entry(kaddr, offset, chunk_mask);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300292 ctx->pos = (n<<PAGE_SHIFT) + offset;
Al Viro2d7f2ea2006-03-15 21:41:59 +0000293 }
Jeff Laytone1d747d2017-12-11 06:35:14 -0500294 file->f_version = inode_query_iversion(inode);
295 need_revalidate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297 de = (ext2_dirent *)(kaddr+offset);
298 limit = kaddr + ext2_last_byte(inode, n) - EXT2_DIR_REC_LEN(1);
299 for ( ;(char*)de <= limit; de = ext2_next_entry(de)) {
300 if (de->rec_len == 0) {
Harvey Harrison605afd62008-04-28 02:16:03 -0700301 ext2_error(sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 "zero-length directory entry");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 ext2_put_page(page);
Al Viro2d7f2ea2006-03-15 21:41:59 +0000304 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306 if (de->inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 unsigned char d_type = DT_UNKNOWN;
308
Phillip Pottere1089212019-01-21 00:54:31 +0000309 if (has_filetype)
310 d_type = fs_ftype_to_dtype(de->file_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Al Viro80886292013-05-15 18:51:49 -0400312 if (!dir_emit(ctx, de->name, de->name_len,
313 le32_to_cpu(de->inode),
314 d_type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 ext2_put_page(page);
Al Viro2d7f2ea2006-03-15 21:41:59 +0000316 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318 }
Al Viro80886292013-05-15 18:51:49 -0400319 ctx->pos += ext2_rec_len_from_disk(de->rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321 ext2_put_page(page);
322 }
Al Viro2d7f2ea2006-03-15 21:41:59 +0000323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
326/*
327 * ext2_find_entry()
328 *
329 * finds an entry in the specified directory with the wanted name. It
Jérémy Cochoy92e12882009-11-30 17:58:43 +0100330 * returns the page in which the entry was found (as a parameter - res_page),
331 * and the entry itself. Page is returned mapped and unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 * Entry is guaranteed to be valid.
Ira Weinya6fbd0a2020-11-12 09:42:44 -0800333 *
334 * On Success ext2_put_page() should be called on *res_page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 */
Al Viroac3ba642016-07-20 22:47:26 -0400336struct ext2_dir_entry_2 *ext2_find_entry (struct inode *dir,
337 const struct qstr *child, struct page **res_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Al Viroa9885442008-08-24 07:28:39 -0400339 const char *name = child->name;
340 int namelen = child->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 unsigned reclen = EXT2_DIR_REC_LEN(namelen);
342 unsigned long start, n;
343 unsigned long npages = dir_pages(dir);
344 struct page *page = NULL;
345 struct ext2_inode_info *ei = EXT2_I(dir);
346 ext2_dirent * de;
347
348 if (npages == 0)
349 goto out;
350
351 /* OFFSET_CACHE */
352 *res_page = NULL;
353
354 start = ei->i_dir_start_lookup;
355 if (start >= npages)
356 start = 0;
357 n = start;
358 do {
359 char *kaddr;
zhangyi (F)b4962092020-06-08 11:40:42 +0800360 page = ext2_get_page(dir, n, 0);
361 if (IS_ERR(page))
362 return ERR_CAST(page);
363
364 kaddr = page_address(page);
365 de = (ext2_dirent *) kaddr;
366 kaddr += ext2_last_byte(dir, n) - reclen;
367 while ((char *) de <= kaddr) {
368 if (de->rec_len == 0) {
369 ext2_error(dir->i_sb, __func__,
370 "zero-length directory entry");
371 ext2_put_page(page);
372 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
zhangyi (F)b4962092020-06-08 11:40:42 +0800374 if (ext2_match(namelen, name, de))
375 goto found;
376 de = ext2_next_entry(de);
377 }
378 ext2_put_page(page);
Eric Sandeenbd395972008-10-15 22:04:02 -0700379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (++n >= npages)
381 n = 0;
Eric Sandeend8adb9c2007-02-10 01:45:06 -0800382 /* next page is past the blocks we've got */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300383 if (unlikely(n > (dir->i_blocks >> (PAGE_SHIFT - 9)))) {
Harvey Harrison605afd62008-04-28 02:16:03 -0700384 ext2_error(dir->i_sb, __func__,
Eric Sandeend8adb9c2007-02-10 01:45:06 -0800385 "dir %lu size %lld exceeds block count %llu",
386 dir->i_ino, dir->i_size,
387 (unsigned long long)dir->i_blocks);
388 goto out;
389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 } while (n != start);
391out:
zhangyi (F)a43850a2020-06-08 11:40:43 +0800392 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394found:
395 *res_page = page;
396 ei->i_dir_start_lookup = n;
397 return de;
398}
399
Ira Weinya6fbd0a2020-11-12 09:42:44 -0800400/**
401 * Return the '..' directory entry and the page in which the entry was found
402 * (as a parameter - p).
403 *
404 * On Success ext2_put_page() should be called on *p.
405 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406struct ext2_dir_entry_2 * ext2_dotdot (struct inode *dir, struct page **p)
407{
Eric Sandeenbd395972008-10-15 22:04:02 -0700408 struct page *page = ext2_get_page(dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 ext2_dirent *de = NULL;
410
411 if (!IS_ERR(page)) {
412 de = ext2_next_entry((ext2_dirent *) page_address(page));
413 *p = page;
414 }
415 return de;
416}
417
zhangyi (F)b4962092020-06-08 11:40:42 +0800418int ext2_inode_by_name(struct inode *dir, const struct qstr *child, ino_t *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Al Viroa9885442008-08-24 07:28:39 -0400420 struct ext2_dir_entry_2 *de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 struct page *page;
422
zhangyi (F)b4962092020-06-08 11:40:42 +0800423 de = ext2_find_entry(dir, child, &page);
zhangyi (F)a43850a2020-06-08 11:40:43 +0800424 if (IS_ERR(de))
zhangyi (F)b4962092020-06-08 11:40:42 +0800425 return PTR_ERR(de);
426
427 *ino = le32_to_cpu(de->inode);
428 ext2_put_page(page);
429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200432static int ext2_prepare_chunk(struct page *page, loff_t pos, unsigned len)
433{
Christoph Hellwig6e1db882010-06-04 11:29:57 +0200434 return __block_write_begin(page, pos, len, ext2_get_block);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200435}
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437/* Releases the page */
438void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
Jan Kara39fe7552009-06-17 16:26:20 -0700439 struct page *page, struct inode *inode, int update_times)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700441 loff_t pos = page_offset(page) +
442 (char *) de - (char *) page_address(page);
Jan Kara89910cc2007-10-21 16:41:40 -0700443 unsigned len = ext2_rec_len_from_disk(de->rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 int err;
445
446 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200447 err = ext2_prepare_chunk(page, pos, len);
Eric Sesterhenn309be532006-03-26 18:27:41 +0200448 BUG_ON(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 de->inode = cpu_to_le32(inode->i_ino);
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700450 ext2_set_de_type(de, inode);
451 err = ext2_commit_chunk(page, pos, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 ext2_put_page(page);
Jan Kara39fe7552009-06-17 16:26:20 -0700453 if (update_times)
Deepa Dinamani02027d42016-09-14 07:48:05 -0700454 dir->i_mtime = dir->i_ctime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
456 mark_inode_dirty(dir);
457}
458
459/*
460 * Parent is locked.
461 */
462int ext2_add_link (struct dentry *dentry, struct inode *inode)
463{
David Howells2b0143b2015-03-17 22:25:59 +0000464 struct inode *dir = d_inode(dentry->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 const char *name = dentry->d_name.name;
466 int namelen = dentry->d_name.len;
467 unsigned chunk_size = ext2_chunk_size(dir);
468 unsigned reclen = EXT2_DIR_REC_LEN(namelen);
469 unsigned short rec_len, name_len;
470 struct page *page = NULL;
471 ext2_dirent * de;
472 unsigned long npages = dir_pages(dir);
473 unsigned long n;
474 char *kaddr;
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700475 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 int err;
477
478 /*
479 * We take care of directory expansion in the same loop.
480 * This code plays outside i_size, so it locks the page
481 * to protect that region.
482 */
483 for (n = 0; n <= npages; n++) {
484 char *dir_end;
485
Eric Sandeenbd395972008-10-15 22:04:02 -0700486 page = ext2_get_page(dir, n, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 err = PTR_ERR(page);
488 if (IS_ERR(page))
489 goto out;
490 lock_page(page);
491 kaddr = page_address(page);
492 dir_end = kaddr + ext2_last_byte(dir, n);
493 de = (ext2_dirent *)kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300494 kaddr += PAGE_SIZE - reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 while ((char *)de <= kaddr) {
496 if ((char *)de == dir_end) {
497 /* We hit i_size */
498 name_len = 0;
499 rec_len = chunk_size;
Jan Kara89910cc2007-10-21 16:41:40 -0700500 de->rec_len = ext2_rec_len_to_disk(chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 de->inode = 0;
502 goto got_it;
503 }
504 if (de->rec_len == 0) {
Harvey Harrison605afd62008-04-28 02:16:03 -0700505 ext2_error(dir->i_sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 "zero-length directory entry");
507 err = -EIO;
508 goto out_unlock;
509 }
510 err = -EEXIST;
511 if (ext2_match (namelen, name, de))
512 goto out_unlock;
513 name_len = EXT2_DIR_REC_LEN(de->name_len);
Jan Kara89910cc2007-10-21 16:41:40 -0700514 rec_len = ext2_rec_len_from_disk(de->rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (!de->inode && rec_len >= reclen)
516 goto got_it;
517 if (rec_len >= name_len + reclen)
518 goto got_it;
519 de = (ext2_dirent *) ((char *) de + rec_len);
520 }
521 unlock_page(page);
522 ext2_put_page(page);
523 }
524 BUG();
525 return -EINVAL;
526
527got_it:
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700528 pos = page_offset(page) +
529 (char*)de - (char*)page_address(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200530 err = ext2_prepare_chunk(page, pos, rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (err)
532 goto out_unlock;
533 if (de->inode) {
534 ext2_dirent *de1 = (ext2_dirent *) ((char *) de + name_len);
Jan Kara89910cc2007-10-21 16:41:40 -0700535 de1->rec_len = ext2_rec_len_to_disk(rec_len - name_len);
536 de->rec_len = ext2_rec_len_to_disk(name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 de = de1;
538 }
539 de->name_len = namelen;
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700540 memcpy(de->name, name, namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 de->inode = cpu_to_le32(inode->i_ino);
542 ext2_set_de_type (de, inode);
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700543 err = ext2_commit_chunk(page, pos, rec_len);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700544 dir->i_mtime = dir->i_ctime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
546 mark_inode_dirty(dir);
547 /* OFFSET_CACHE */
548out_put:
549 ext2_put_page(page);
550out:
551 return err;
552out_unlock:
553 unlock_page(page);
554 goto out_put;
555}
556
557/*
558 * ext2_delete_entry deletes a directory entry by merging it with the
559 * previous entry. Page is up-to-date. Releases the page.
560 */
561int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page )
562{
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200563 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 char *kaddr = page_address(page);
565 unsigned from = ((char*)dir - kaddr) & ~(ext2_chunk_size(inode)-1);
Jan Kara89910cc2007-10-21 16:41:40 -0700566 unsigned to = ((char *)dir - kaddr) +
567 ext2_rec_len_from_disk(dir->rec_len);
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700568 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 ext2_dirent * pde = NULL;
570 ext2_dirent * de = (ext2_dirent *) (kaddr + from);
571 int err;
572
573 while ((char*)de < (char*)dir) {
574 if (de->rec_len == 0) {
Harvey Harrison605afd62008-04-28 02:16:03 -0700575 ext2_error(inode->i_sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 "zero-length directory entry");
577 err = -EIO;
578 goto out;
579 }
580 pde = de;
581 de = ext2_next_entry(de);
582 }
583 if (pde)
584 from = (char*)pde - (char*)page_address(page);
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700585 pos = page_offset(page) + from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200587 err = ext2_prepare_chunk(page, pos, to - from);
Eric Sesterhenn309be532006-03-26 18:27:41 +0200588 BUG_ON(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (pde)
Jan Kara89910cc2007-10-21 16:41:40 -0700590 pde->rec_len = ext2_rec_len_to_disk(to - from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 dir->inode = 0;
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700592 err = ext2_commit_chunk(page, pos, to - from);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700593 inode->i_ctime = inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 EXT2_I(inode)->i_flags &= ~EXT2_BTREE_FL;
595 mark_inode_dirty(inode);
596out:
597 ext2_put_page(page);
598 return err;
599}
600
601/*
602 * Set the first fragment of directory.
603 */
604int ext2_make_empty(struct inode *inode, struct inode *parent)
605{
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200606 struct page *page = grab_cache_page(inode->i_mapping, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 unsigned chunk_size = ext2_chunk_size(inode);
608 struct ext2_dir_entry_2 * de;
609 int err;
610 void *kaddr;
611
612 if (!page)
613 return -ENOMEM;
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700614
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200615 err = ext2_prepare_chunk(page, 0, chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (err) {
617 unlock_page(page);
618 goto fail;
619 }
Cong Wangd4a23ae2011-11-25 23:14:29 +0800620 kaddr = kmap_atomic(page);
Luiz Fernando Capitulino7823c7c2006-01-11 01:38:27 +0100621 memset(kaddr, 0, chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 de = (struct ext2_dir_entry_2 *)kaddr;
623 de->name_len = 1;
Jan Kara89910cc2007-10-21 16:41:40 -0700624 de->rec_len = ext2_rec_len_to_disk(EXT2_DIR_REC_LEN(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 memcpy (de->name, ".\0\0", 4);
626 de->inode = cpu_to_le32(inode->i_ino);
627 ext2_set_de_type (de, inode);
628
629 de = (struct ext2_dir_entry_2 *)(kaddr + EXT2_DIR_REC_LEN(1));
630 de->name_len = 2;
Jan Kara89910cc2007-10-21 16:41:40 -0700631 de->rec_len = ext2_rec_len_to_disk(chunk_size - EXT2_DIR_REC_LEN(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 de->inode = cpu_to_le32(parent->i_ino);
633 memcpy (de->name, "..\0", 4);
634 ext2_set_de_type (de, inode);
Cong Wangd4a23ae2011-11-25 23:14:29 +0800635 kunmap_atomic(kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 err = ext2_commit_chunk(page, 0, chunk_size);
637fail:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300638 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return err;
640}
641
642/*
643 * routine to check that the specified directory is empty (for rmdir)
644 */
645int ext2_empty_dir (struct inode * inode)
646{
647 struct page *page = NULL;
648 unsigned long i, npages = dir_pages(inode);
Eric Sandeenbd395972008-10-15 22:04:02 -0700649 int dir_has_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 for (i = 0; i < npages; i++) {
652 char *kaddr;
653 ext2_dirent * de;
Eric Sandeenbd395972008-10-15 22:04:02 -0700654 page = ext2_get_page(inode, i, dir_has_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Eric Sandeenbd395972008-10-15 22:04:02 -0700656 if (IS_ERR(page)) {
657 dir_has_error = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 continue;
Eric Sandeenbd395972008-10-15 22:04:02 -0700659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 kaddr = page_address(page);
662 de = (ext2_dirent *)kaddr;
663 kaddr += ext2_last_byte(inode, i) - EXT2_DIR_REC_LEN(1);
664
665 while ((char *)de <= kaddr) {
666 if (de->rec_len == 0) {
Harvey Harrison605afd62008-04-28 02:16:03 -0700667 ext2_error(inode->i_sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 "zero-length directory entry");
669 printk("kaddr=%p, de=%p\n", kaddr, de);
670 goto not_empty;
671 }
672 if (de->inode != 0) {
673 /* check for . and .. */
674 if (de->name[0] != '.')
675 goto not_empty;
676 if (de->name_len > 2)
677 goto not_empty;
678 if (de->name_len < 2) {
679 if (de->inode !=
680 cpu_to_le32(inode->i_ino))
681 goto not_empty;
682 } else if (de->name[1] != '.')
683 goto not_empty;
684 }
685 de = ext2_next_entry(de);
686 }
687 ext2_put_page(page);
688 }
689 return 1;
690
691not_empty:
692 ext2_put_page(page);
693 return 0;
694}
695
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800696const struct file_operations ext2_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 .llseek = generic_file_llseek,
698 .read = generic_read_dir,
Al Viro3b0a3c12016-04-20 23:42:46 -0400699 .iterate_shared = ext2_readdir,
Andi Kleen14f9f7b2008-02-06 01:40:10 -0800700 .unlocked_ioctl = ext2_ioctl,
David Howellse322ff02006-08-29 19:06:20 +0100701#ifdef CONFIG_COMPAT
702 .compat_ioctl = ext2_compat_ioctl,
703#endif
Jan Kara48bde862009-12-15 16:46:49 -0800704 .fsync = ext2_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705};