Linux Audio

Check our new training course

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