blob: 95e4f0bd55a3ea93d5a198b8463e27b2726a95bb [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
69static inline void ext2_put_page(struct page *page)
70{
71 kunmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030072 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/*
76 * Return the offset into page `page_nr' of the last valid
77 * byte in that page, plus one.
78 */
79static unsigned
80ext2_last_byte(struct inode *inode, unsigned long page_nr)
81{
82 unsigned last_byte = inode->i_size;
83
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030084 last_byte -= page_nr << PAGE_SHIFT;
85 if (last_byte > PAGE_SIZE)
86 last_byte = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return last_byte;
88}
89
Nick Pigginf34fb6e2007-10-16 01:25:04 -070090static int ext2_commit_chunk(struct page *page, loff_t pos, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Nick Pigginf34fb6e2007-10-16 01:25:04 -070092 struct address_space *mapping = page->mapping;
93 struct inode *dir = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 int err = 0;
Nick Pigginf34fb6e2007-10-16 01:25:04 -070095
Jeff Laytone1d747d2017-12-11 06:35:14 -050096 inode_inc_iversion(dir);
Nick Pigginf34fb6e2007-10-16 01:25:04 -070097 block_write_end(NULL, mapping, pos, len, len, page, NULL);
98
99 if (pos+len > dir->i_size) {
100 i_size_write(dir, pos+len);
101 mark_inode_dirty(dir);
102 }
103
Jan Kara6b7021e2009-01-15 13:51:29 -0800104 if (IS_DIRSYNC(dir)) {
Jeff Layton2b69c822017-07-05 15:26:48 -0400105 err = write_one_page(page);
Jan Kara6b7021e2009-01-15 13:51:29 -0800106 if (!err)
Christoph Hellwigc37650162010-10-06 10:48:20 +0200107 err = sync_inode_metadata(dir, 1);
Jan Kara6b7021e2009-01-15 13:51:29 -0800108 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 unlock_page(page);
Jan Kara6b7021e2009-01-15 13:51:29 -0800110 }
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return err;
113}
114
Al Virobe5b82d2016-04-22 15:06:44 -0400115static bool ext2_check_page(struct page *page, int quiet)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 struct inode *dir = page->mapping->host;
118 struct super_block *sb = dir->i_sb;
119 unsigned chunk_size = ext2_chunk_size(dir);
120 char *kaddr = page_address(page);
121 u32 max_inumber = le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count);
122 unsigned offs, rec_len;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300123 unsigned limit = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 ext2_dirent *p;
125 char *error;
126
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300127 if ((dir->i_size >> PAGE_SHIFT) == page->index) {
128 limit = dir->i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (limit & (chunk_size - 1))
130 goto Ebadsize;
131 if (!limit)
132 goto out;
133 }
134 for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
135 p = (ext2_dirent *)(kaddr + offs);
Jan Kara89910cc2007-10-21 16:41:40 -0700136 rec_len = ext2_rec_len_from_disk(p->rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Eric Sandeen40a063f2010-12-07 11:51:05 -0600138 if (unlikely(rec_len < EXT2_DIR_REC_LEN(1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 goto Eshort;
Eric Sandeen40a063f2010-12-07 11:51:05 -0600140 if (unlikely(rec_len & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 goto Ealign;
Eric Sandeen40a063f2010-12-07 11:51:05 -0600142 if (unlikely(rec_len < EXT2_DIR_REC_LEN(p->name_len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 goto Enamelen;
Eric Sandeen40a063f2010-12-07 11:51:05 -0600144 if (unlikely(((offs + rec_len - 1) ^ offs) & ~(chunk_size-1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 goto Espan;
Eric Sandeen40a063f2010-12-07 11:51:05 -0600146 if (unlikely(le32_to_cpu(p->inode) > max_inumber))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 goto Einumber;
148 }
149 if (offs != limit)
150 goto Eend;
151out:
152 SetPageChecked(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400153 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 /* Too bad, we had an error */
156
157Ebadsize:
Eric Sandeenbd395972008-10-15 22:04:02 -0700158 if (!quiet)
159 ext2_error(sb, __func__,
160 "size of directory #%lu is not a multiple "
161 "of chunk size", dir->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 goto fail;
163Eshort:
164 error = "rec_len is smaller than minimal";
165 goto bad_entry;
166Ealign:
167 error = "unaligned directory entry";
168 goto bad_entry;
169Enamelen:
170 error = "rec_len is too small for name_len";
171 goto bad_entry;
172Espan:
173 error = "directory entry across blocks";
174 goto bad_entry;
175Einumber:
176 error = "inode out of bounds";
177bad_entry:
Eric Sandeenbd395972008-10-15 22:04:02 -0700178 if (!quiet)
179 ext2_error(sb, __func__, "bad entry in directory #%lu: : %s - "
180 "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300181 dir->i_ino, error, (page->index<<PAGE_SHIFT)+offs,
Eric Sandeenbd395972008-10-15 22:04:02 -0700182 (unsigned long) le32_to_cpu(p->inode),
183 rec_len, p->name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 goto fail;
185Eend:
Eric Sandeenbd395972008-10-15 22:04:02 -0700186 if (!quiet) {
187 p = (ext2_dirent *)(kaddr + offs);
188 ext2_error(sb, "ext2_check_page",
189 "entry in directory #%lu spans the page boundary"
190 "offset=%lu, inode=%lu",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300191 dir->i_ino, (page->index<<PAGE_SHIFT)+offs,
Eric Sandeenbd395972008-10-15 22:04:02 -0700192 (unsigned long) le32_to_cpu(p->inode));
193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 SetPageError(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400196 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Eric Sandeenbd395972008-10-15 22:04:02 -0700199static struct page * ext2_get_page(struct inode *dir, unsigned long n,
200 int quiet)
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)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 kmap(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400206 if (unlikely(!PageChecked(page))) {
207 if (PageError(page) || !ext2_check_page(page, quiet))
208 goto fail;
209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211 return page;
212
213fail:
214 ext2_put_page(page);
215 return ERR_PTR(-EIO);
216}
217
218/*
219 * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure.
220 *
221 * len <= EXT2_NAME_LEN and de != NULL are guaranteed by caller.
222 */
223static inline int ext2_match (int len, const char * const name,
224 struct ext2_dir_entry_2 * de)
225{
226 if (len != de->name_len)
227 return 0;
228 if (!de->inode)
229 return 0;
230 return !memcmp(name, de->name, len);
231}
232
233/*
234 * p is at least 6 bytes before the end of page
235 */
236static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
237{
Jan Kara89910cc2007-10-21 16:41:40 -0700238 return (ext2_dirent *)((char *)p +
239 ext2_rec_len_from_disk(p->rec_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242static inline unsigned
243ext2_validate_entry(char *base, unsigned offset, unsigned mask)
244{
245 ext2_dirent *de = (ext2_dirent*)(base + offset);
246 ext2_dirent *p = (ext2_dirent*)(base + (offset&mask));
247 while ((char*)p < (char*)de) {
248 if (p->rec_len == 0)
249 break;
250 p = ext2_next_entry(p);
251 }
252 return (char *)p - base;
253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255static inline void ext2_set_de_type(ext2_dirent *de, struct inode *inode)
256{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (EXT2_HAS_INCOMPAT_FEATURE(inode->i_sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
Phillip Pottere1089212019-01-21 00:54:31 +0000258 de->file_type = fs_umode_to_ftype(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 else
260 de->file_type = 0;
261}
262
263static int
Al Viro80886292013-05-15 18:51:49 -0400264ext2_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Al Viro80886292013-05-15 18:51:49 -0400266 loff_t pos = ctx->pos;
267 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct super_block *sb = inode->i_sb;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300269 unsigned int offset = pos & ~PAGE_MASK;
270 unsigned long n = pos >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 unsigned long npages = dir_pages(inode);
272 unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
Goffredo Baroncellic472c072018-02-01 08:15:25 -0500273 bool need_revalidate = !inode_eq_iversion(inode, file->f_version);
Phillip Pottere1089212019-01-21 00:54:31 +0000274 bool has_filetype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
Al Viro2d7f2ea2006-03-15 21:41:59 +0000277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Phillip Pottere1089212019-01-21 00:54:31 +0000279 has_filetype =
280 EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 for ( ; n < npages; n++, offset = 0) {
283 char *kaddr, *limit;
284 ext2_dirent *de;
Eric Sandeenbd395972008-10-15 22:04:02 -0700285 struct page *page = ext2_get_page(inode, n, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 if (IS_ERR(page)) {
Harvey Harrison605afd62008-04-28 02:16:03 -0700288 ext2_error(sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 "bad page in #%lu",
290 inode->i_ino);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300291 ctx->pos += PAGE_SIZE - offset;
Akinobu Mitabbff2862008-04-28 02:16:00 -0700292 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294 kaddr = page_address(page);
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");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 ext2_put_page(page);
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)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 ext2_put_page(page);
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 }
327 ext2_put_page(page);
328 }
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.
339 */
Al Viroac3ba642016-07-20 22:47:26 -0400340struct ext2_dir_entry_2 *ext2_find_entry (struct inode *dir,
341 const struct qstr *child, struct page **res_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Al Viroa9885442008-08-24 07:28:39 -0400343 const char *name = child->name;
344 int namelen = child->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 unsigned reclen = EXT2_DIR_REC_LEN(namelen);
346 unsigned long start, n;
347 unsigned long npages = dir_pages(dir);
348 struct page *page = NULL;
349 struct ext2_inode_info *ei = EXT2_I(dir);
350 ext2_dirent * de;
351
352 if (npages == 0)
353 goto out;
354
355 /* OFFSET_CACHE */
356 *res_page = NULL;
357
358 start = ei->i_dir_start_lookup;
359 if (start >= npages)
360 start = 0;
361 n = start;
362 do {
363 char *kaddr;
zhangyi (F)b4962092020-06-08 11:40:42 +0800364 page = ext2_get_page(dir, n, 0);
365 if (IS_ERR(page))
366 return ERR_CAST(page);
367
368 kaddr = page_address(page);
369 de = (ext2_dirent *) kaddr;
370 kaddr += ext2_last_byte(dir, n) - reclen;
371 while ((char *) de <= kaddr) {
372 if (de->rec_len == 0) {
373 ext2_error(dir->i_sb, __func__,
374 "zero-length directory entry");
375 ext2_put_page(page);
376 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
zhangyi (F)b4962092020-06-08 11:40:42 +0800378 if (ext2_match(namelen, name, de))
379 goto found;
380 de = ext2_next_entry(de);
381 }
382 ext2_put_page(page);
Eric Sandeenbd395972008-10-15 22:04:02 -0700383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (++n >= npages)
385 n = 0;
Eric Sandeend8adb9c2007-02-10 01:45:06 -0800386 /* next page is past the blocks we've got */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300387 if (unlikely(n > (dir->i_blocks >> (PAGE_SHIFT - 9)))) {
Harvey Harrison605afd62008-04-28 02:16:03 -0700388 ext2_error(dir->i_sb, __func__,
Eric Sandeend8adb9c2007-02-10 01:45:06 -0800389 "dir %lu size %lld exceeds block count %llu",
390 dir->i_ino, dir->i_size,
391 (unsigned long long)dir->i_blocks);
392 goto out;
393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 } while (n != start);
395out:
396 return NULL;
397
398found:
399 *res_page = page;
400 ei->i_dir_start_lookup = n;
401 return de;
402}
403
404struct ext2_dir_entry_2 * ext2_dotdot (struct inode *dir, struct page **p)
405{
Eric Sandeenbd395972008-10-15 22:04:02 -0700406 struct page *page = ext2_get_page(dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 ext2_dirent *de = NULL;
408
409 if (!IS_ERR(page)) {
410 de = ext2_next_entry((ext2_dirent *) page_address(page));
411 *p = page;
412 }
413 return de;
414}
415
zhangyi (F)b4962092020-06-08 11:40:42 +0800416int ext2_inode_by_name(struct inode *dir, const struct qstr *child, ino_t *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Al Viroa9885442008-08-24 07:28:39 -0400418 struct ext2_dir_entry_2 *de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 struct page *page;
420
zhangyi (F)b4962092020-06-08 11:40:42 +0800421 de = ext2_find_entry(dir, child, &page);
422 if (IS_ERR_OR_NULL(de))
423 return PTR_ERR(de);
424
425 *ino = le32_to_cpu(de->inode);
426 ext2_put_page(page);
427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200430static int ext2_prepare_chunk(struct page *page, loff_t pos, unsigned len)
431{
Christoph Hellwig6e1db882010-06-04 11:29:57 +0200432 return __block_write_begin(page, pos, len, ext2_get_block);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200433}
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/* Releases the page */
436void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
Jan Kara39fe7552009-06-17 16:26:20 -0700437 struct page *page, struct inode *inode, int update_times)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700439 loff_t pos = page_offset(page) +
440 (char *) de - (char *) page_address(page);
Jan Kara89910cc2007-10-21 16:41:40 -0700441 unsigned len = ext2_rec_len_from_disk(de->rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 int err;
443
444 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200445 err = ext2_prepare_chunk(page, pos, len);
Eric Sesterhenn309be532006-03-26 18:27:41 +0200446 BUG_ON(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 de->inode = cpu_to_le32(inode->i_ino);
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700448 ext2_set_de_type(de, inode);
449 err = ext2_commit_chunk(page, pos, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 ext2_put_page(page);
Jan Kara39fe7552009-06-17 16:26:20 -0700451 if (update_times)
Deepa Dinamani02027d42016-09-14 07:48:05 -0700452 dir->i_mtime = dir->i_ctime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
454 mark_inode_dirty(dir);
455}
456
457/*
458 * Parent is locked.
459 */
460int ext2_add_link (struct dentry *dentry, struct inode *inode)
461{
David Howells2b0143b2015-03-17 22:25:59 +0000462 struct inode *dir = d_inode(dentry->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 const char *name = dentry->d_name.name;
464 int namelen = dentry->d_name.len;
465 unsigned chunk_size = ext2_chunk_size(dir);
466 unsigned reclen = EXT2_DIR_REC_LEN(namelen);
467 unsigned short rec_len, name_len;
468 struct page *page = NULL;
469 ext2_dirent * de;
470 unsigned long npages = dir_pages(dir);
471 unsigned long n;
472 char *kaddr;
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700473 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 int err;
475
476 /*
477 * We take care of directory expansion in the same loop.
478 * This code plays outside i_size, so it locks the page
479 * to protect that region.
480 */
481 for (n = 0; n <= npages; n++) {
482 char *dir_end;
483
Eric Sandeenbd395972008-10-15 22:04:02 -0700484 page = ext2_get_page(dir, n, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 err = PTR_ERR(page);
486 if (IS_ERR(page))
487 goto out;
488 lock_page(page);
489 kaddr = page_address(page);
490 dir_end = kaddr + ext2_last_byte(dir, n);
491 de = (ext2_dirent *)kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300492 kaddr += PAGE_SIZE - reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 while ((char *)de <= kaddr) {
494 if ((char *)de == dir_end) {
495 /* We hit i_size */
496 name_len = 0;
497 rec_len = chunk_size;
Jan Kara89910cc2007-10-21 16:41:40 -0700498 de->rec_len = ext2_rec_len_to_disk(chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 de->inode = 0;
500 goto got_it;
501 }
502 if (de->rec_len == 0) {
Harvey Harrison605afd62008-04-28 02:16:03 -0700503 ext2_error(dir->i_sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 "zero-length directory entry");
505 err = -EIO;
506 goto out_unlock;
507 }
508 err = -EEXIST;
509 if (ext2_match (namelen, name, de))
510 goto out_unlock;
511 name_len = EXT2_DIR_REC_LEN(de->name_len);
Jan Kara89910cc2007-10-21 16:41:40 -0700512 rec_len = ext2_rec_len_from_disk(de->rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (!de->inode && rec_len >= reclen)
514 goto got_it;
515 if (rec_len >= name_len + reclen)
516 goto got_it;
517 de = (ext2_dirent *) ((char *) de + rec_len);
518 }
519 unlock_page(page);
520 ext2_put_page(page);
521 }
522 BUG();
523 return -EINVAL;
524
525got_it:
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700526 pos = page_offset(page) +
527 (char*)de - (char*)page_address(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200528 err = ext2_prepare_chunk(page, pos, rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (err)
530 goto out_unlock;
531 if (de->inode) {
532 ext2_dirent *de1 = (ext2_dirent *) ((char *) de + name_len);
Jan Kara89910cc2007-10-21 16:41:40 -0700533 de1->rec_len = ext2_rec_len_to_disk(rec_len - name_len);
534 de->rec_len = ext2_rec_len_to_disk(name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 de = de1;
536 }
537 de->name_len = namelen;
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700538 memcpy(de->name, name, namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 de->inode = cpu_to_le32(inode->i_ino);
540 ext2_set_de_type (de, inode);
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700541 err = ext2_commit_chunk(page, pos, rec_len);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700542 dir->i_mtime = dir->i_ctime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
544 mark_inode_dirty(dir);
545 /* OFFSET_CACHE */
546out_put:
547 ext2_put_page(page);
548out:
549 return err;
550out_unlock:
551 unlock_page(page);
552 goto out_put;
553}
554
555/*
556 * ext2_delete_entry deletes a directory entry by merging it with the
557 * previous entry. Page is up-to-date. Releases the page.
558 */
559int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page )
560{
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200561 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 char *kaddr = page_address(page);
563 unsigned from = ((char*)dir - kaddr) & ~(ext2_chunk_size(inode)-1);
Jan Kara89910cc2007-10-21 16:41:40 -0700564 unsigned to = ((char *)dir - kaddr) +
565 ext2_rec_len_from_disk(dir->rec_len);
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700566 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 ext2_dirent * pde = NULL;
568 ext2_dirent * de = (ext2_dirent *) (kaddr + from);
569 int err;
570
571 while ((char*)de < (char*)dir) {
572 if (de->rec_len == 0) {
Harvey Harrison605afd62008-04-28 02:16:03 -0700573 ext2_error(inode->i_sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 "zero-length directory entry");
575 err = -EIO;
576 goto out;
577 }
578 pde = de;
579 de = ext2_next_entry(de);
580 }
581 if (pde)
582 from = (char*)pde - (char*)page_address(page);
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700583 pos = page_offset(page) + from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200585 err = ext2_prepare_chunk(page, pos, to - from);
Eric Sesterhenn309be532006-03-26 18:27:41 +0200586 BUG_ON(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (pde)
Jan Kara89910cc2007-10-21 16:41:40 -0700588 pde->rec_len = ext2_rec_len_to_disk(to - from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 dir->inode = 0;
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700590 err = ext2_commit_chunk(page, pos, to - from);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700591 inode->i_ctime = inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 EXT2_I(inode)->i_flags &= ~EXT2_BTREE_FL;
593 mark_inode_dirty(inode);
594out:
595 ext2_put_page(page);
596 return err;
597}
598
599/*
600 * Set the first fragment of directory.
601 */
602int ext2_make_empty(struct inode *inode, struct inode *parent)
603{
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200604 struct page *page = grab_cache_page(inode->i_mapping, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 unsigned chunk_size = ext2_chunk_size(inode);
606 struct ext2_dir_entry_2 * de;
607 int err;
608 void *kaddr;
609
610 if (!page)
611 return -ENOMEM;
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700612
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200613 err = ext2_prepare_chunk(page, 0, chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (err) {
615 unlock_page(page);
616 goto fail;
617 }
Cong Wangd4a23ae2011-11-25 23:14:29 +0800618 kaddr = kmap_atomic(page);
Luiz Fernando Capitulino7823c7c2006-01-11 01:38:27 +0100619 memset(kaddr, 0, chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 de = (struct ext2_dir_entry_2 *)kaddr;
621 de->name_len = 1;
Jan Kara89910cc2007-10-21 16:41:40 -0700622 de->rec_len = ext2_rec_len_to_disk(EXT2_DIR_REC_LEN(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 memcpy (de->name, ".\0\0", 4);
624 de->inode = cpu_to_le32(inode->i_ino);
625 ext2_set_de_type (de, inode);
626
627 de = (struct ext2_dir_entry_2 *)(kaddr + EXT2_DIR_REC_LEN(1));
628 de->name_len = 2;
Jan Kara89910cc2007-10-21 16:41:40 -0700629 de->rec_len = ext2_rec_len_to_disk(chunk_size - EXT2_DIR_REC_LEN(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 de->inode = cpu_to_le32(parent->i_ino);
631 memcpy (de->name, "..\0", 4);
632 ext2_set_de_type (de, inode);
Cong Wangd4a23ae2011-11-25 23:14:29 +0800633 kunmap_atomic(kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 err = ext2_commit_chunk(page, 0, chunk_size);
635fail:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300636 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return err;
638}
639
640/*
641 * routine to check that the specified directory is empty (for rmdir)
642 */
643int ext2_empty_dir (struct inode * inode)
644{
645 struct page *page = NULL;
646 unsigned long i, npages = dir_pages(inode);
Eric Sandeenbd395972008-10-15 22:04:02 -0700647 int dir_has_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 for (i = 0; i < npages; i++) {
650 char *kaddr;
651 ext2_dirent * de;
Eric Sandeenbd395972008-10-15 22:04:02 -0700652 page = ext2_get_page(inode, i, dir_has_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Eric Sandeenbd395972008-10-15 22:04:02 -0700654 if (IS_ERR(page)) {
655 dir_has_error = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 continue;
Eric Sandeenbd395972008-10-15 22:04:02 -0700657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 kaddr = page_address(page);
660 de = (ext2_dirent *)kaddr;
661 kaddr += ext2_last_byte(inode, i) - EXT2_DIR_REC_LEN(1);
662
663 while ((char *)de <= kaddr) {
664 if (de->rec_len == 0) {
Harvey Harrison605afd62008-04-28 02:16:03 -0700665 ext2_error(inode->i_sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 "zero-length directory entry");
667 printk("kaddr=%p, de=%p\n", kaddr, de);
668 goto not_empty;
669 }
670 if (de->inode != 0) {
671 /* check for . and .. */
672 if (de->name[0] != '.')
673 goto not_empty;
674 if (de->name_len > 2)
675 goto not_empty;
676 if (de->name_len < 2) {
677 if (de->inode !=
678 cpu_to_le32(inode->i_ino))
679 goto not_empty;
680 } else if (de->name[1] != '.')
681 goto not_empty;
682 }
683 de = ext2_next_entry(de);
684 }
685 ext2_put_page(page);
686 }
687 return 1;
688
689not_empty:
690 ext2_put_page(page);
691 return 0;
692}
693
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800694const struct file_operations ext2_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 .llseek = generic_file_llseek,
696 .read = generic_read_dir,
Al Viro3b0a3c12016-04-20 23:42:46 -0400697 .iterate_shared = ext2_readdir,
Andi Kleen14f9f7b2008-02-06 01:40:10 -0800698 .unlocked_ioctl = ext2_ioctl,
David Howellse322ff02006-08-29 19:06:20 +0100699#ifdef CONFIG_COMPAT
700 .compat_ioctl = ext2_compat_ioctl,
701#endif
Jan Kara48bde862009-12-15 16:46:49 -0800702 .fsync = ext2_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703};