Linux Audio

Check our new training course

Real-Time Linux with PREEMPT_RT training

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