Linux Audio

Check our new training course

Loading...
v6.2
  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
 
 
 
 
 
 
 69/*
 70 * Return the offset into page `page_nr' of the last valid
 71 * byte in that page, plus one.
 72 */
 73static unsigned
 74ext2_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
 84static 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
 99static 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;
134out:
135	SetPageChecked(page);
136	return true;
137
138	/* Too bad, we had an error */
139
140Ebadsize:
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;
146Eshort:
147	error = "rec_len is smaller than minimal";
148	goto bad_entry;
149Ealign:
150	error = "unaligned directory entry";
151	goto bad_entry;
152Enamelen:
153	error = "rec_len is too small for name_len";
154	goto bad_entry;
155Espan:
156	error = "directory entry across blocks";
157	goto bad_entry;
158Einumber:
159	error = "inode out of bounds";
160bad_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;
168Eend:
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	}
177fail:
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 */
189static 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
204fail:
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 */
214static 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 */
227static 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
233static inline unsigned 
234ext2_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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246static 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
254static int
255ext2_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 */
338struct 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);
396out:
397	return ERR_PTR(-ENOENT);
398
399found:
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 */
418struct 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
433int 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
448static 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
454static 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
464void 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 */
489int 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
555got_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 */
577out_put:
578	ext2_put_page(page, page_addr);
579out:
580	return err;
581out_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 */
590int 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);
626out:
 
627	return err;
628}
629
630/*
631 * Set the first fragment of directory.
632 */
633int 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);
667fail:
668	put_page(page);
669	return err;
670}
671
672/*
673 * routine to check that the specified directory is empty (for rmdir)
674 */
675int 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
719not_empty:
720	ext2_put_page(page, page_addr);
721	return 0;
722}
723
724const 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};
v4.17
  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 unsigned char ext2_filetype_table[EXT2_FT_MAX] = {
256	[EXT2_FT_UNKNOWN]	= DT_UNKNOWN,
257	[EXT2_FT_REG_FILE]	= DT_REG,
258	[EXT2_FT_DIR]		= DT_DIR,
259	[EXT2_FT_CHRDEV]	= DT_CHR,
260	[EXT2_FT_BLKDEV]	= DT_BLK,
261	[EXT2_FT_FIFO]		= DT_FIFO,
262	[EXT2_FT_SOCK]		= DT_SOCK,
263	[EXT2_FT_SYMLINK]	= DT_LNK,
264};
265
266#define S_SHIFT 12
267static unsigned char ext2_type_by_mode[S_IFMT >> S_SHIFT] = {
268	[S_IFREG >> S_SHIFT]	= EXT2_FT_REG_FILE,
269	[S_IFDIR >> S_SHIFT]	= EXT2_FT_DIR,
270	[S_IFCHR >> S_SHIFT]	= EXT2_FT_CHRDEV,
271	[S_IFBLK >> S_SHIFT]	= EXT2_FT_BLKDEV,
272	[S_IFIFO >> S_SHIFT]	= EXT2_FT_FIFO,
273	[S_IFSOCK >> S_SHIFT]	= EXT2_FT_SOCK,
274	[S_IFLNK >> S_SHIFT]	= EXT2_FT_SYMLINK,
275};
276
277static inline void ext2_set_de_type(ext2_dirent *de, struct inode *inode)
278{
279	umode_t mode = inode->i_mode;
280	if (EXT2_HAS_INCOMPAT_FEATURE(inode->i_sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
281		de->file_type = ext2_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
282	else
283		de->file_type = 0;
284}
285
286static int
287ext2_readdir(struct file *file, struct dir_context *ctx)
288{
289	loff_t pos = ctx->pos;
290	struct inode *inode = file_inode(file);
291	struct super_block *sb = inode->i_sb;
292	unsigned int offset = pos & ~PAGE_MASK;
293	unsigned long n = pos >> PAGE_SHIFT;
294	unsigned long npages = dir_pages(inode);
295	unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
296	unsigned char *types = NULL;
297	bool need_revalidate = !inode_eq_iversion(inode, file->f_version);
 
298
299	if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
300		return 0;
301
302	if (EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
303		types = ext2_filetype_table;
304
305	for ( ; n < npages; n++, offset = 0) {
306		char *kaddr, *limit;
307		ext2_dirent *de;
308		struct page *page = ext2_get_page(inode, n, 0);
309
310		if (IS_ERR(page)) {
311			ext2_error(sb, __func__,
312				   "bad page in #%lu",
313				   inode->i_ino);
314			ctx->pos += PAGE_SIZE - offset;
315			return PTR_ERR(page);
316		}
317		kaddr = page_address(page);
318		if (unlikely(need_revalidate)) {
319			if (offset) {
320				offset = ext2_validate_entry(kaddr, offset, chunk_mask);
321				ctx->pos = (n<<PAGE_SHIFT) + offset;
322			}
323			file->f_version = inode_query_iversion(inode);
324			need_revalidate = false;
325		}
326		de = (ext2_dirent *)(kaddr+offset);
327		limit = kaddr + ext2_last_byte(inode, n) - EXT2_DIR_REC_LEN(1);
328		for ( ;(char*)de <= limit; de = ext2_next_entry(de)) {
329			if (de->rec_len == 0) {
330				ext2_error(sb, __func__,
331					"zero-length directory entry");
332				ext2_put_page(page);
333				return -EIO;
334			}
335			if (de->inode) {
336				unsigned char d_type = DT_UNKNOWN;
337
338				if (types && de->file_type < EXT2_FT_MAX)
339					d_type = types[de->file_type];
340
341				if (!dir_emit(ctx, de->name, de->name_len,
342						le32_to_cpu(de->inode),
343						d_type)) {
344					ext2_put_page(page);
345					return 0;
346				}
347			}
348			ctx->pos += ext2_rec_len_from_disk(de->rec_len);
349		}
350		ext2_put_page(page);
351	}
352	return 0;
353}
354
355/*
356 *	ext2_find_entry()
357 *
358 * finds an entry in the specified directory with the wanted name. It
359 * returns the page in which the entry was found (as a parameter - res_page),
360 * and the entry itself. Page is returned mapped and unlocked.
361 * Entry is guaranteed to be valid.
 
 
 
 
 
 
 
 
362 */
363struct ext2_dir_entry_2 *ext2_find_entry (struct inode *dir,
364			const struct qstr *child, struct page **res_page)
 
365{
366	const char *name = child->name;
367	int namelen = child->len;
368	unsigned reclen = EXT2_DIR_REC_LEN(namelen);
369	unsigned long start, n;
370	unsigned long npages = dir_pages(dir);
371	struct page *page = NULL;
372	struct ext2_inode_info *ei = EXT2_I(dir);
373	ext2_dirent * de;
374	int dir_has_error = 0;
375
376	if (npages == 0)
377		goto out;
378
379	/* OFFSET_CACHE */
380	*res_page = NULL;
 
381
382	start = ei->i_dir_start_lookup;
383	if (start >= npages)
384		start = 0;
385	n = start;
386	do {
387		char *kaddr;
388		page = ext2_get_page(dir, n, dir_has_error);
389		if (!IS_ERR(page)) {
390			kaddr = page_address(page);
391			de = (ext2_dirent *) kaddr;
392			kaddr += ext2_last_byte(dir, n) - reclen;
393			while ((char *) de <= kaddr) {
394				if (de->rec_len == 0) {
395					ext2_error(dir->i_sb, __func__,
396						"zero-length directory entry");
397					ext2_put_page(page);
398					goto out;
399				}
400				if (ext2_match (namelen, name, de))
401					goto found;
402				de = ext2_next_entry(de);
403			}
404			ext2_put_page(page);
405		} else
406			dir_has_error = 1;
 
 
407
408		if (++n >= npages)
409			n = 0;
410		/* next page is past the blocks we've got */
411		if (unlikely(n > (dir->i_blocks >> (PAGE_SHIFT - 9)))) {
412			ext2_error(dir->i_sb, __func__,
413				"dir %lu size %lld exceeds block count %llu",
414				dir->i_ino, dir->i_size,
415				(unsigned long long)dir->i_blocks);
416			goto out;
417		}
418	} while (n != start);
419out:
420	return NULL;
421
422found:
423	*res_page = page;
 
424	ei->i_dir_start_lookup = n;
425	return de;
426}
427
428struct ext2_dir_entry_2 * ext2_dotdot (struct inode *dir, struct page **p)
 
 
 
 
 
 
 
 
 
 
 
 
 
429{
430	struct page *page = ext2_get_page(dir, 0, 0);
 
431	ext2_dirent *de = NULL;
432
433	if (!IS_ERR(page)) {
434		de = ext2_next_entry((ext2_dirent *) page_address(page));
435		*p = page;
 
436	}
437	return de;
438}
439
440ino_t ext2_inode_by_name(struct inode *dir, const struct qstr *child)
441{
442	ino_t res = 0;
443	struct ext2_dir_entry_2 *de;
444	struct page *page;
 
445	
446	de = ext2_find_entry (dir, child, &page);
447	if (de) {
448		res = le32_to_cpu(de->inode);
449		ext2_put_page(page);
450	}
451	return res;
 
452}
453
454static int ext2_prepare_chunk(struct page *page, loff_t pos, unsigned len)
455{
456	return __block_write_begin(page, pos, len, ext2_get_block);
457}
458
459/* Releases the page */
 
 
 
 
 
 
 
 
 
 
460void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
461		   struct page *page, struct inode *inode, int update_times)
 
462{
463	loff_t pos = page_offset(page) +
464			(char *) de - (char *) page_address(page);
465	unsigned len = ext2_rec_len_from_disk(de->rec_len);
466	int err;
467
468	lock_page(page);
469	err = ext2_prepare_chunk(page, pos, len);
470	BUG_ON(err);
471	de->inode = cpu_to_le32(inode->i_ino);
472	ext2_set_de_type(de, inode);
473	err = ext2_commit_chunk(page, pos, len);
474	ext2_put_page(page);
475	if (update_times)
476		dir->i_mtime = dir->i_ctime = current_time(dir);
477	EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
478	mark_inode_dirty(dir);
 
479}
480
481/*
482 *	Parent is locked.
483 */
484int ext2_add_link (struct dentry *dentry, struct inode *inode)
485{
486	struct inode *dir = d_inode(dentry->d_parent);
487	const char *name = dentry->d_name.name;
488	int namelen = dentry->d_name.len;
489	unsigned chunk_size = ext2_chunk_size(dir);
490	unsigned reclen = EXT2_DIR_REC_LEN(namelen);
491	unsigned short rec_len, name_len;
492	struct page *page = NULL;
 
493	ext2_dirent * de;
494	unsigned long npages = dir_pages(dir);
495	unsigned long n;
496	char *kaddr;
497	loff_t pos;
498	int err;
499
500	/*
501	 * We take care of directory expansion in the same loop.
502	 * This code plays outside i_size, so it locks the page
503	 * to protect that region.
504	 */
505	for (n = 0; n <= npages; n++) {
 
506		char *dir_end;
507
508		page = ext2_get_page(dir, n, 0);
509		err = PTR_ERR(page);
510		if (IS_ERR(page))
511			goto out;
512		lock_page(page);
513		kaddr = page_address(page);
514		dir_end = kaddr + ext2_last_byte(dir, n);
515		de = (ext2_dirent *)kaddr;
516		kaddr += PAGE_SIZE - reclen;
517		while ((char *)de <= kaddr) {
518			if ((char *)de == dir_end) {
519				/* We hit i_size */
520				name_len = 0;
521				rec_len = chunk_size;
522				de->rec_len = ext2_rec_len_to_disk(chunk_size);
523				de->inode = 0;
524				goto got_it;
525			}
526			if (de->rec_len == 0) {
527				ext2_error(dir->i_sb, __func__,
528					"zero-length directory entry");
529				err = -EIO;
530				goto out_unlock;
531			}
532			err = -EEXIST;
533			if (ext2_match (namelen, name, de))
534				goto out_unlock;
535			name_len = EXT2_DIR_REC_LEN(de->name_len);
536			rec_len = ext2_rec_len_from_disk(de->rec_len);
537			if (!de->inode && rec_len >= reclen)
538				goto got_it;
539			if (rec_len >= name_len + reclen)
540				goto got_it;
541			de = (ext2_dirent *) ((char *) de + rec_len);
542		}
543		unlock_page(page);
544		ext2_put_page(page);
545	}
546	BUG();
547	return -EINVAL;
548
549got_it:
550	pos = page_offset(page) +
551		(char*)de - (char*)page_address(page);
552	err = ext2_prepare_chunk(page, pos, rec_len);
553	if (err)
554		goto out_unlock;
555	if (de->inode) {
556		ext2_dirent *de1 = (ext2_dirent *) ((char *) de + name_len);
557		de1->rec_len = ext2_rec_len_to_disk(rec_len - name_len);
558		de->rec_len = ext2_rec_len_to_disk(name_len);
559		de = de1;
560	}
561	de->name_len = namelen;
562	memcpy(de->name, name, namelen);
563	de->inode = cpu_to_le32(inode->i_ino);
564	ext2_set_de_type (de, inode);
565	err = ext2_commit_chunk(page, pos, rec_len);
566	dir->i_mtime = dir->i_ctime = current_time(dir);
567	EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
568	mark_inode_dirty(dir);
 
569	/* OFFSET_CACHE */
570out_put:
571	ext2_put_page(page);
572out:
573	return err;
574out_unlock:
575	unlock_page(page);
576	goto out_put;
577}
578
579/*
580 * ext2_delete_entry deletes a directory entry by merging it with the
581 * previous entry. Page is up-to-date. Releases the page.
582 */
583int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page )
 
584{
585	struct inode *inode = page->mapping->host;
586	char *kaddr = page_address(page);
587	unsigned from = ((char*)dir - kaddr) & ~(ext2_chunk_size(inode)-1);
588	unsigned to = ((char *)dir - kaddr) +
589				ext2_rec_len_from_disk(dir->rec_len);
590	loff_t pos;
591	ext2_dirent * pde = NULL;
592	ext2_dirent * de = (ext2_dirent *) (kaddr + from);
593	int err;
594
595	while ((char*)de < (char*)dir) {
596		if (de->rec_len == 0) {
597			ext2_error(inode->i_sb, __func__,
598				"zero-length directory entry");
599			err = -EIO;
600			goto out;
601		}
602		pde = de;
603		de = ext2_next_entry(de);
604	}
605	if (pde)
606		from = (char*)pde - (char*)page_address(page);
607	pos = page_offset(page) + from;
608	lock_page(page);
609	err = ext2_prepare_chunk(page, pos, to - from);
610	BUG_ON(err);
611	if (pde)
612		pde->rec_len = ext2_rec_len_to_disk(to - from);
613	dir->inode = 0;
614	err = ext2_commit_chunk(page, pos, to - from);
615	inode->i_ctime = inode->i_mtime = current_time(inode);
616	EXT2_I(inode)->i_flags &= ~EXT2_BTREE_FL;
617	mark_inode_dirty(inode);
 
618out:
619	ext2_put_page(page);
620	return err;
621}
622
623/*
624 * Set the first fragment of directory.
625 */
626int ext2_make_empty(struct inode *inode, struct inode *parent)
627{
628	struct page *page = grab_cache_page(inode->i_mapping, 0);
629	unsigned chunk_size = ext2_chunk_size(inode);
630	struct ext2_dir_entry_2 * de;
631	int err;
632	void *kaddr;
633
634	if (!page)
635		return -ENOMEM;
636
637	err = ext2_prepare_chunk(page, 0, chunk_size);
638	if (err) {
639		unlock_page(page);
640		goto fail;
641	}
642	kaddr = kmap_atomic(page);
643	memset(kaddr, 0, chunk_size);
644	de = (struct ext2_dir_entry_2 *)kaddr;
645	de->name_len = 1;
646	de->rec_len = ext2_rec_len_to_disk(EXT2_DIR_REC_LEN(1));
647	memcpy (de->name, ".\0\0", 4);
648	de->inode = cpu_to_le32(inode->i_ino);
649	ext2_set_de_type (de, inode);
650
651	de = (struct ext2_dir_entry_2 *)(kaddr + EXT2_DIR_REC_LEN(1));
652	de->name_len = 2;
653	de->rec_len = ext2_rec_len_to_disk(chunk_size - EXT2_DIR_REC_LEN(1));
654	de->inode = cpu_to_le32(parent->i_ino);
655	memcpy (de->name, "..\0", 4);
656	ext2_set_de_type (de, inode);
657	kunmap_atomic(kaddr);
658	err = ext2_commit_chunk(page, 0, chunk_size);
 
659fail:
660	put_page(page);
661	return err;
662}
663
664/*
665 * routine to check that the specified directory is empty (for rmdir)
666 */
667int ext2_empty_dir (struct inode * inode)
668{
 
669	struct page *page = NULL;
670	unsigned long i, npages = dir_pages(inode);
671	int dir_has_error = 0;
672
673	for (i = 0; i < npages; i++) {
674		char *kaddr;
675		ext2_dirent * de;
676		page = ext2_get_page(inode, i, dir_has_error);
677
678		if (IS_ERR(page)) {
679			dir_has_error = 1;
680			continue;
681		}
682
683		kaddr = page_address(page);
684		de = (ext2_dirent *)kaddr;
685		kaddr += ext2_last_byte(inode, i) - EXT2_DIR_REC_LEN(1);
686
687		while ((char *)de <= kaddr) {
688			if (de->rec_len == 0) {
689				ext2_error(inode->i_sb, __func__,
690					"zero-length directory entry");
691				printk("kaddr=%p, de=%p\n", kaddr, de);
692				goto not_empty;
693			}
694			if (de->inode != 0) {
695				/* check for . and .. */
696				if (de->name[0] != '.')
697					goto not_empty;
698				if (de->name_len > 2)
699					goto not_empty;
700				if (de->name_len < 2) {
701					if (de->inode !=
702					    cpu_to_le32(inode->i_ino))
703						goto not_empty;
704				} else if (de->name[1] != '.')
705					goto not_empty;
706			}
707			de = ext2_next_entry(de);
708		}
709		ext2_put_page(page);
710	}
711	return 1;
712
713not_empty:
714	ext2_put_page(page);
715	return 0;
716}
717
718const struct file_operations ext2_dir_operations = {
719	.llseek		= generic_file_llseek,
720	.read		= generic_read_dir,
721	.iterate_shared	= ext2_readdir,
722	.unlocked_ioctl = ext2_ioctl,
723#ifdef CONFIG_COMPAT
724	.compat_ioctl	= ext2_compat_ioctl,
725#endif
726	.fsync		= ext2_fsync,
727};