xref: /openbmc/linux/fs/ext2/dir.c (revision 9d5dbfe0)
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 struct page * ext2_get_page(struct inode *dir, unsigned long n,
190 				   int quiet, void **page_addr)
191 {
192 	struct address_space *mapping = dir->i_mapping;
193 	struct folio *folio = read_mapping_folio(mapping, n, NULL);
194 
195 	if (IS_ERR(folio))
196 		return &folio->page;
197 	*page_addr = kmap_local_folio(folio, n & (folio_nr_pages(folio) - 1));
198 	if (unlikely(!folio_test_checked(folio))) {
199 		if (!ext2_check_page(&folio->page, quiet, *page_addr))
200 			goto fail;
201 	}
202 	return &folio->page;
203 
204 fail:
205 	ext2_put_page(&folio->page, *page_addr);
206 	return ERR_PTR(-EIO);
207 }
208 
209 /*
210  * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure.
211  *
212  * len <= EXT2_NAME_LEN and de != NULL are guaranteed by caller.
213  */
214 static inline int ext2_match (int len, const char * const name,
215 					struct ext2_dir_entry_2 * de)
216 {
217 	if (len != de->name_len)
218 		return 0;
219 	if (!de->inode)
220 		return 0;
221 	return !memcmp(name, de->name, len);
222 }
223 
224 /*
225  * p is at least 6 bytes before the end of page
226  */
227 static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
228 {
229 	return (ext2_dirent *)((char *)p +
230 			ext2_rec_len_from_disk(p->rec_len));
231 }
232 
233 static inline unsigned
234 ext2_validate_entry(char *base, unsigned offset, unsigned mask)
235 {
236 	ext2_dirent *de = (ext2_dirent*)(base + offset);
237 	ext2_dirent *p = (ext2_dirent*)(base + (offset&mask));
238 	while ((char*)p < (char*)de) {
239 		if (p->rec_len == 0)
240 			break;
241 		p = ext2_next_entry(p);
242 	}
243 	return (char *)p - base;
244 }
245 
246 static inline void ext2_set_de_type(ext2_dirent *de, struct inode *inode)
247 {
248 	if (EXT2_HAS_INCOMPAT_FEATURE(inode->i_sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
249 		de->file_type = fs_umode_to_ftype(inode->i_mode);
250 	else
251 		de->file_type = 0;
252 }
253 
254 static int
255 ext2_readdir(struct file *file, struct dir_context *ctx)
256 {
257 	loff_t pos = ctx->pos;
258 	struct inode *inode = file_inode(file);
259 	struct super_block *sb = inode->i_sb;
260 	unsigned int offset = pos & ~PAGE_MASK;
261 	unsigned long n = pos >> PAGE_SHIFT;
262 	unsigned long npages = dir_pages(inode);
263 	unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
264 	bool need_revalidate = !inode_eq_iversion(inode, file->f_version);
265 	bool has_filetype;
266 
267 	if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
268 		return 0;
269 
270 	has_filetype =
271 		EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE);
272 
273 	for ( ; n < npages; n++, offset = 0) {
274 		char *kaddr, *limit;
275 		ext2_dirent *de;
276 		struct page *page = ext2_get_page(inode, n, 0, (void **)&kaddr);
277 
278 		if (IS_ERR(page)) {
279 			ext2_error(sb, __func__,
280 				   "bad page in #%lu",
281 				   inode->i_ino);
282 			ctx->pos += PAGE_SIZE - offset;
283 			return PTR_ERR(page);
284 		}
285 		if (unlikely(need_revalidate)) {
286 			if (offset) {
287 				offset = ext2_validate_entry(kaddr, offset, chunk_mask);
288 				ctx->pos = (n<<PAGE_SHIFT) + offset;
289 			}
290 			file->f_version = inode_query_iversion(inode);
291 			need_revalidate = false;
292 		}
293 		de = (ext2_dirent *)(kaddr+offset);
294 		limit = kaddr + ext2_last_byte(inode, n) - EXT2_DIR_REC_LEN(1);
295 		for ( ;(char*)de <= limit; de = ext2_next_entry(de)) {
296 			if (de->rec_len == 0) {
297 				ext2_error(sb, __func__,
298 					"zero-length directory entry");
299 				ext2_put_page(page, kaddr);
300 				return -EIO;
301 			}
302 			if (de->inode) {
303 				unsigned char d_type = DT_UNKNOWN;
304 
305 				if (has_filetype)
306 					d_type = fs_ftype_to_dtype(de->file_type);
307 
308 				if (!dir_emit(ctx, de->name, de->name_len,
309 						le32_to_cpu(de->inode),
310 						d_type)) {
311 					ext2_put_page(page, kaddr);
312 					return 0;
313 				}
314 			}
315 			ctx->pos += ext2_rec_len_from_disk(de->rec_len);
316 		}
317 		ext2_put_page(page, kaddr);
318 	}
319 	return 0;
320 }
321 
322 /*
323  *	ext2_find_entry()
324  *
325  * finds an entry in the specified directory with the wanted name. It
326  * returns the page in which the entry was found (as a parameter - res_page),
327  * and the entry itself. Page is returned mapped and unlocked.
328  * Entry is guaranteed to be valid.
329  *
330  * On Success ext2_put_page() should be called on *res_page.
331  *
332  * NOTE: Calls to ext2_get_page()/ext2_put_page() must be nested according to
333  * the rules documented in kmap_local_page()/kunmap_local().
334  *
335  * ext2_find_entry() and ext2_dotdot() act as a call to ext2_get_page() and
336  * should be treated as a call to ext2_get_page() for nesting purposes.
337  */
338 struct ext2_dir_entry_2 *ext2_find_entry (struct inode *dir,
339 			const struct qstr *child, struct page **res_page,
340 			void **res_page_addr)
341 {
342 	const char *name = child->name;
343 	int namelen = child->len;
344 	unsigned reclen = EXT2_DIR_REC_LEN(namelen);
345 	unsigned long start, n;
346 	unsigned long npages = dir_pages(dir);
347 	struct page *page = NULL;
348 	struct ext2_inode_info *ei = EXT2_I(dir);
349 	ext2_dirent * de;
350 	void *page_addr;
351 
352 	if (npages == 0)
353 		goto out;
354 
355 	/* OFFSET_CACHE */
356 	*res_page = NULL;
357 	*res_page_addr = NULL;
358 
359 	start = ei->i_dir_start_lookup;
360 	if (start >= npages)
361 		start = 0;
362 	n = start;
363 	do {
364 		char *kaddr;
365 		page = ext2_get_page(dir, n, 0, &page_addr);
366 		if (IS_ERR(page))
367 			return ERR_CAST(page);
368 
369 		kaddr = page_addr;
370 		de = (ext2_dirent *) kaddr;
371 		kaddr += ext2_last_byte(dir, n) - reclen;
372 		while ((char *) de <= kaddr) {
373 			if (de->rec_len == 0) {
374 				ext2_error(dir->i_sb, __func__,
375 					"zero-length directory entry");
376 				ext2_put_page(page, page_addr);
377 				goto out;
378 			}
379 			if (ext2_match(namelen, name, de))
380 				goto found;
381 			de = ext2_next_entry(de);
382 		}
383 		ext2_put_page(page, page_addr);
384 
385 		if (++n >= npages)
386 			n = 0;
387 		/* next page is past the blocks we've got */
388 		if (unlikely(n > (dir->i_blocks >> (PAGE_SHIFT - 9)))) {
389 			ext2_error(dir->i_sb, __func__,
390 				"dir %lu size %lld exceeds block count %llu",
391 				dir->i_ino, dir->i_size,
392 				(unsigned long long)dir->i_blocks);
393 			goto out;
394 		}
395 	} while (n != start);
396 out:
397 	return ERR_PTR(-ENOENT);
398 
399 found:
400 	*res_page = page;
401 	*res_page_addr = page_addr;
402 	ei->i_dir_start_lookup = n;
403 	return de;
404 }
405 
406 /*
407  * Return the '..' directory entry and the page in which the entry was found
408  * (as a parameter - p).
409  *
410  * On Success ext2_put_page() should be called on *p.
411  *
412  * NOTE: Calls to ext2_get_page()/ext2_put_page() must be nested according to
413  * the rules documented in kmap_local_page()/kunmap_local().
414  *
415  * ext2_find_entry() and ext2_dotdot() act as a call to ext2_get_page() and
416  * should be treated as a call to ext2_get_page() for nesting purposes.
417  */
418 struct ext2_dir_entry_2 *ext2_dotdot(struct inode *dir, struct page **p,
419 				     void **pa)
420 {
421 	void *page_addr;
422 	struct page *page = ext2_get_page(dir, 0, 0, &page_addr);
423 	ext2_dirent *de = NULL;
424 
425 	if (!IS_ERR(page)) {
426 		de = ext2_next_entry((ext2_dirent *) page_addr);
427 		*p = page;
428 		*pa = page_addr;
429 	}
430 	return de;
431 }
432 
433 int ext2_inode_by_name(struct inode *dir, const struct qstr *child, ino_t *ino)
434 {
435 	struct ext2_dir_entry_2 *de;
436 	struct page *page;
437 	void *page_addr;
438 
439 	de = ext2_find_entry(dir, child, &page, &page_addr);
440 	if (IS_ERR(de))
441 		return PTR_ERR(de);
442 
443 	*ino = le32_to_cpu(de->inode);
444 	ext2_put_page(page, page_addr);
445 	return 0;
446 }
447 
448 static int ext2_prepare_chunk(struct page *page, loff_t pos, unsigned len)
449 {
450 	return __block_write_begin(page, pos, len, ext2_get_block);
451 }
452 
453 
454 static int ext2_handle_dirsync(struct inode *dir)
455 {
456 	int err;
457 
458 	err = filemap_write_and_wait(dir->i_mapping);
459 	if (!err)
460 		err = sync_inode_metadata(dir, 1);
461 	return err;
462 }
463 
464 void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
465 		   struct page *page, void *page_addr, struct inode *inode,
466 		   int update_times)
467 {
468 	loff_t pos = page_offset(page) +
469 			(char *) de - (char *) page_addr;
470 	unsigned len = ext2_rec_len_from_disk(de->rec_len);
471 	int err;
472 
473 	lock_page(page);
474 	err = ext2_prepare_chunk(page, pos, len);
475 	BUG_ON(err);
476 	de->inode = cpu_to_le32(inode->i_ino);
477 	ext2_set_de_type(de, inode);
478 	ext2_commit_chunk(page, pos, len);
479 	if (update_times)
480 		dir->i_mtime = dir->i_ctime = current_time(dir);
481 	EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
482 	mark_inode_dirty(dir);
483 	ext2_handle_dirsync(dir);
484 }
485 
486 /*
487  *	Parent is locked.
488  */
489 int ext2_add_link (struct dentry *dentry, struct inode *inode)
490 {
491 	struct inode *dir = d_inode(dentry->d_parent);
492 	const char *name = dentry->d_name.name;
493 	int namelen = dentry->d_name.len;
494 	unsigned chunk_size = ext2_chunk_size(dir);
495 	unsigned reclen = EXT2_DIR_REC_LEN(namelen);
496 	unsigned short rec_len, name_len;
497 	struct page *page = NULL;
498 	void *page_addr = NULL;
499 	ext2_dirent * de;
500 	unsigned long npages = dir_pages(dir);
501 	unsigned long n;
502 	loff_t pos;
503 	int err;
504 
505 	/*
506 	 * We take care of directory expansion in the same loop.
507 	 * This code plays outside i_size, so it locks the page
508 	 * to protect that region.
509 	 */
510 	for (n = 0; n <= npages; n++) {
511 		char *kaddr;
512 		char *dir_end;
513 
514 		page = ext2_get_page(dir, n, 0, &page_addr);
515 		err = PTR_ERR(page);
516 		if (IS_ERR(page))
517 			goto out;
518 		lock_page(page);
519 		kaddr = page_addr;
520 		dir_end = kaddr + ext2_last_byte(dir, n);
521 		de = (ext2_dirent *)kaddr;
522 		kaddr += PAGE_SIZE - reclen;
523 		while ((char *)de <= kaddr) {
524 			if ((char *)de == dir_end) {
525 				/* We hit i_size */
526 				name_len = 0;
527 				rec_len = chunk_size;
528 				de->rec_len = ext2_rec_len_to_disk(chunk_size);
529 				de->inode = 0;
530 				goto got_it;
531 			}
532 			if (de->rec_len == 0) {
533 				ext2_error(dir->i_sb, __func__,
534 					"zero-length directory entry");
535 				err = -EIO;
536 				goto out_unlock;
537 			}
538 			err = -EEXIST;
539 			if (ext2_match (namelen, name, de))
540 				goto out_unlock;
541 			name_len = EXT2_DIR_REC_LEN(de->name_len);
542 			rec_len = ext2_rec_len_from_disk(de->rec_len);
543 			if (!de->inode && rec_len >= reclen)
544 				goto got_it;
545 			if (rec_len >= name_len + reclen)
546 				goto got_it;
547 			de = (ext2_dirent *) ((char *) de + rec_len);
548 		}
549 		unlock_page(page);
550 		ext2_put_page(page, page_addr);
551 	}
552 	BUG();
553 	return -EINVAL;
554 
555 got_it:
556 	pos = page_offset(page) +
557 		(char *)de - (char *)page_addr;
558 	err = ext2_prepare_chunk(page, pos, rec_len);
559 	if (err)
560 		goto out_unlock;
561 	if (de->inode) {
562 		ext2_dirent *de1 = (ext2_dirent *) ((char *) de + name_len);
563 		de1->rec_len = ext2_rec_len_to_disk(rec_len - name_len);
564 		de->rec_len = ext2_rec_len_to_disk(name_len);
565 		de = de1;
566 	}
567 	de->name_len = namelen;
568 	memcpy(de->name, name, namelen);
569 	de->inode = cpu_to_le32(inode->i_ino);
570 	ext2_set_de_type (de, inode);
571 	ext2_commit_chunk(page, pos, rec_len);
572 	dir->i_mtime = dir->i_ctime = current_time(dir);
573 	EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
574 	mark_inode_dirty(dir);
575 	err = ext2_handle_dirsync(dir);
576 	/* OFFSET_CACHE */
577 out_put:
578 	ext2_put_page(page, page_addr);
579 out:
580 	return err;
581 out_unlock:
582 	unlock_page(page);
583 	goto out_put;
584 }
585 
586 /*
587  * ext2_delete_entry deletes a directory entry by merging it with the
588  * previous entry. Page is up-to-date.
589  */
590 int ext2_delete_entry (struct ext2_dir_entry_2 *dir, struct page *page,
591 			char *kaddr)
592 {
593 	struct inode *inode = page->mapping->host;
594 	unsigned from = ((char*)dir - kaddr) & ~(ext2_chunk_size(inode)-1);
595 	unsigned to = ((char *)dir - kaddr) +
596 				ext2_rec_len_from_disk(dir->rec_len);
597 	loff_t pos;
598 	ext2_dirent * pde = NULL;
599 	ext2_dirent * de = (ext2_dirent *) (kaddr + from);
600 	int err;
601 
602 	while ((char*)de < (char*)dir) {
603 		if (de->rec_len == 0) {
604 			ext2_error(inode->i_sb, __func__,
605 				"zero-length directory entry");
606 			err = -EIO;
607 			goto out;
608 		}
609 		pde = de;
610 		de = ext2_next_entry(de);
611 	}
612 	if (pde)
613 		from = (char *)pde - kaddr;
614 	pos = page_offset(page) + from;
615 	lock_page(page);
616 	err = ext2_prepare_chunk(page, pos, to - from);
617 	BUG_ON(err);
618 	if (pde)
619 		pde->rec_len = ext2_rec_len_to_disk(to - from);
620 	dir->inode = 0;
621 	ext2_commit_chunk(page, pos, to - from);
622 	inode->i_ctime = inode->i_mtime = current_time(inode);
623 	EXT2_I(inode)->i_flags &= ~EXT2_BTREE_FL;
624 	mark_inode_dirty(inode);
625 	err = ext2_handle_dirsync(inode);
626 out:
627 	return err;
628 }
629 
630 /*
631  * Set the first fragment of directory.
632  */
633 int ext2_make_empty(struct inode *inode, struct inode *parent)
634 {
635 	struct page *page = grab_cache_page(inode->i_mapping, 0);
636 	unsigned chunk_size = ext2_chunk_size(inode);
637 	struct ext2_dir_entry_2 * de;
638 	int err;
639 	void *kaddr;
640 
641 	if (!page)
642 		return -ENOMEM;
643 
644 	err = ext2_prepare_chunk(page, 0, chunk_size);
645 	if (err) {
646 		unlock_page(page);
647 		goto fail;
648 	}
649 	kaddr = kmap_atomic(page);
650 	memset(kaddr, 0, chunk_size);
651 	de = (struct ext2_dir_entry_2 *)kaddr;
652 	de->name_len = 1;
653 	de->rec_len = ext2_rec_len_to_disk(EXT2_DIR_REC_LEN(1));
654 	memcpy (de->name, ".\0\0", 4);
655 	de->inode = cpu_to_le32(inode->i_ino);
656 	ext2_set_de_type (de, inode);
657 
658 	de = (struct ext2_dir_entry_2 *)(kaddr + EXT2_DIR_REC_LEN(1));
659 	de->name_len = 2;
660 	de->rec_len = ext2_rec_len_to_disk(chunk_size - EXT2_DIR_REC_LEN(1));
661 	de->inode = cpu_to_le32(parent->i_ino);
662 	memcpy (de->name, "..\0", 4);
663 	ext2_set_de_type (de, inode);
664 	kunmap_atomic(kaddr);
665 	ext2_commit_chunk(page, 0, chunk_size);
666 	err = ext2_handle_dirsync(inode);
667 fail:
668 	put_page(page);
669 	return err;
670 }
671 
672 /*
673  * routine to check that the specified directory is empty (for rmdir)
674  */
675 int ext2_empty_dir (struct inode * inode)
676 {
677 	void *page_addr = NULL;
678 	struct page *page = NULL;
679 	unsigned long i, npages = dir_pages(inode);
680 
681 	for (i = 0; i < npages; i++) {
682 		char *kaddr;
683 		ext2_dirent * de;
684 		page = ext2_get_page(inode, i, 0, &page_addr);
685 
686 		if (IS_ERR(page))
687 			return 0;
688 
689 		kaddr = page_addr;
690 		de = (ext2_dirent *)kaddr;
691 		kaddr += ext2_last_byte(inode, i) - EXT2_DIR_REC_LEN(1);
692 
693 		while ((char *)de <= kaddr) {
694 			if (de->rec_len == 0) {
695 				ext2_error(inode->i_sb, __func__,
696 					"zero-length directory entry");
697 				printk("kaddr=%p, de=%p\n", kaddr, de);
698 				goto not_empty;
699 			}
700 			if (de->inode != 0) {
701 				/* check for . and .. */
702 				if (de->name[0] != '.')
703 					goto not_empty;
704 				if (de->name_len > 2)
705 					goto not_empty;
706 				if (de->name_len < 2) {
707 					if (de->inode !=
708 					    cpu_to_le32(inode->i_ino))
709 						goto not_empty;
710 				} else if (de->name[1] != '.')
711 					goto not_empty;
712 			}
713 			de = ext2_next_entry(de);
714 		}
715 		ext2_put_page(page, page_addr);
716 	}
717 	return 1;
718 
719 not_empty:
720 	ext2_put_page(page, page_addr);
721 	return 0;
722 }
723 
724 const struct file_operations ext2_dir_operations = {
725 	.llseek		= generic_file_llseek,
726 	.read		= generic_read_dir,
727 	.iterate_shared	= ext2_readdir,
728 	.unlocked_ioctl = ext2_ioctl,
729 #ifdef CONFIG_COMPAT
730 	.compat_ioctl	= ext2_compat_ioctl,
731 #endif
732 	.fsync		= ext2_fsync,
733 };
734