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