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