xref: /openbmc/linux/fs/nilfs2/dir.c (revision 2382eae6)
1ae98043fSRyusuke Konishi // SPDX-License-Identifier: GPL-2.0+
22ba466d7SYoshiji Amagai /*
394ee1d91SRyusuke Konishi  * NILFS directory entry operations
42ba466d7SYoshiji Amagai  *
52ba466d7SYoshiji Amagai  * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
62ba466d7SYoshiji Amagai  *
74b420ab4SRyusuke Konishi  * Modified for NILFS by Amagai Yoshiji.
82ba466d7SYoshiji Amagai  */
92ba466d7SYoshiji Amagai /*
102ba466d7SYoshiji Amagai  *  linux/fs/ext2/dir.c
112ba466d7SYoshiji Amagai  *
122ba466d7SYoshiji Amagai  * Copyright (C) 1992, 1993, 1994, 1995
132ba466d7SYoshiji Amagai  * Remy Card (card@masi.ibp.fr)
142ba466d7SYoshiji Amagai  * Laboratoire MASI - Institut Blaise Pascal
152ba466d7SYoshiji Amagai  * Universite Pierre et Marie Curie (Paris VI)
162ba466d7SYoshiji Amagai  *
172ba466d7SYoshiji Amagai  *  from
182ba466d7SYoshiji Amagai  *
192ba466d7SYoshiji Amagai  *  linux/fs/minix/dir.c
202ba466d7SYoshiji Amagai  *
212ba466d7SYoshiji Amagai  *  Copyright (C) 1991, 1992  Linus Torvalds
222ba466d7SYoshiji Amagai  *
232ba466d7SYoshiji Amagai  *  ext2 directory handling functions
242ba466d7SYoshiji Amagai  *
252ba466d7SYoshiji Amagai  *  Big-endian to little-endian byte-swapping/bitmaps by
262ba466d7SYoshiji Amagai  *        David S. Miller (davem@caip.rutgers.edu), 1995
272ba466d7SYoshiji Amagai  *
282ba466d7SYoshiji Amagai  * All code that works with directory layout had been switched to pagecache
292ba466d7SYoshiji Amagai  * and moved here. AV
302ba466d7SYoshiji Amagai  */
312ba466d7SYoshiji Amagai 
322ba466d7SYoshiji Amagai #include <linux/pagemap.h>
332ba466d7SYoshiji Amagai #include "nilfs.h"
342ba466d7SYoshiji Amagai #include "page.h"
352ba466d7SYoshiji Amagai 
nilfs_rec_len_from_disk(__le16 dlen)36e63e88bcSRyusuke Konishi static inline unsigned int nilfs_rec_len_from_disk(__le16 dlen)
37e63e88bcSRyusuke Konishi {
38e63e88bcSRyusuke Konishi 	unsigned int len = le16_to_cpu(dlen);
39e63e88bcSRyusuke Konishi 
40e63e88bcSRyusuke Konishi #if (PAGE_SIZE >= 65536)
41e63e88bcSRyusuke Konishi 	if (len == NILFS_MAX_REC_LEN)
42e63e88bcSRyusuke Konishi 		return 1 << 16;
43e63e88bcSRyusuke Konishi #endif
44e63e88bcSRyusuke Konishi 	return len;
45e63e88bcSRyusuke Konishi }
46e63e88bcSRyusuke Konishi 
nilfs_rec_len_to_disk(unsigned int len)47e63e88bcSRyusuke Konishi static inline __le16 nilfs_rec_len_to_disk(unsigned int len)
48e63e88bcSRyusuke Konishi {
49e63e88bcSRyusuke Konishi #if (PAGE_SIZE >= 65536)
50e63e88bcSRyusuke Konishi 	if (len == (1 << 16))
51e63e88bcSRyusuke Konishi 		return cpu_to_le16(NILFS_MAX_REC_LEN);
52e63e88bcSRyusuke Konishi 
53e63e88bcSRyusuke Konishi 	BUG_ON(len > (1 << 16));
54e63e88bcSRyusuke Konishi #endif
55e63e88bcSRyusuke Konishi 	return cpu_to_le16(len);
56e63e88bcSRyusuke Konishi }
57e63e88bcSRyusuke Konishi 
582ba466d7SYoshiji Amagai /*
592ba466d7SYoshiji Amagai  * nilfs uses block-sized chunks. Arguably, sector-sized ones would be
602ba466d7SYoshiji Amagai  * more robust, but we have what we have
612ba466d7SYoshiji Amagai  */
nilfs_chunk_size(struct inode * inode)620c6c44cbSRyusuke Konishi static inline unsigned int nilfs_chunk_size(struct inode *inode)
632ba466d7SYoshiji Amagai {
642ba466d7SYoshiji Amagai 	return inode->i_sb->s_blocksize;
652ba466d7SYoshiji Amagai }
662ba466d7SYoshiji Amagai 
nilfs_put_page(struct page * page)672ba466d7SYoshiji Amagai static inline void nilfs_put_page(struct page *page)
682ba466d7SYoshiji Amagai {
692ba466d7SYoshiji Amagai 	kunmap(page);
7009cbfeafSKirill A. Shutemov 	put_page(page);
712ba466d7SYoshiji Amagai }
722ba466d7SYoshiji Amagai 
732ba466d7SYoshiji Amagai /*
742ba466d7SYoshiji Amagai  * Return the offset into page `page_nr' of the last valid
752ba466d7SYoshiji Amagai  * byte in that page, plus one.
762ba466d7SYoshiji Amagai  */
nilfs_last_byte(struct inode * inode,unsigned long page_nr)770c6c44cbSRyusuke Konishi static unsigned int nilfs_last_byte(struct inode *inode, unsigned long page_nr)
782ba466d7SYoshiji Amagai {
790c6c44cbSRyusuke Konishi 	unsigned int last_byte = inode->i_size;
802ba466d7SYoshiji Amagai 
8109cbfeafSKirill A. Shutemov 	last_byte -= page_nr << PAGE_SHIFT;
8209cbfeafSKirill A. Shutemov 	if (last_byte > PAGE_SIZE)
8309cbfeafSKirill A. Shutemov 		last_byte = PAGE_SIZE;
842ba466d7SYoshiji Amagai 	return last_byte;
852ba466d7SYoshiji Amagai }
862ba466d7SYoshiji Amagai 
nilfs_prepare_chunk(struct page * page,unsigned int from,unsigned int to)870c6c44cbSRyusuke Konishi static int nilfs_prepare_chunk(struct page *page, unsigned int from,
880c6c44cbSRyusuke Konishi 			       unsigned int to)
892ba466d7SYoshiji Amagai {
902ba466d7SYoshiji Amagai 	loff_t pos = page_offset(page) + from;
914ad364caSRyusuke Konishi 
926e1db88dSChristoph Hellwig 	return __block_write_begin(page, pos, to - from, nilfs_get_block);
932ba466d7SYoshiji Amagai }
942ba466d7SYoshiji Amagai 
nilfs_commit_chunk(struct page * page,struct address_space * mapping,unsigned int from,unsigned int to)952093abf9SJiro SEKIBA static void nilfs_commit_chunk(struct page *page,
962ba466d7SYoshiji Amagai 			       struct address_space *mapping,
970c6c44cbSRyusuke Konishi 			       unsigned int from, unsigned int to)
982ba466d7SYoshiji Amagai {
992ba466d7SYoshiji Amagai 	struct inode *dir = mapping->host;
1002ba466d7SYoshiji Amagai 	loff_t pos = page_offset(page) + from;
1010c6c44cbSRyusuke Konishi 	unsigned int len = to - from;
1020c6c44cbSRyusuke Konishi 	unsigned int nr_dirty, copied;
1032ba466d7SYoshiji Amagai 	int err;
1042ba466d7SYoshiji Amagai 
1052ba466d7SYoshiji Amagai 	nr_dirty = nilfs_page_count_clean_buffers(page, from, to);
1062ba466d7SYoshiji Amagai 	copied = block_write_end(NULL, mapping, pos, len, len, page, NULL);
10758d55471SJiro SEKIBA 	if (pos + copied > dir->i_size)
1082ba466d7SYoshiji Amagai 		i_size_write(dir, pos + copied);
1092ba466d7SYoshiji Amagai 	if (IS_DIRSYNC(dir))
1102ba466d7SYoshiji Amagai 		nilfs_set_transaction_flag(NILFS_TI_SYNC);
111bcbc8c64SRyusuke Konishi 	err = nilfs_set_file_dirty(dir, nr_dirty);
1122093abf9SJiro SEKIBA 	WARN_ON(err); /* do not happen */
1132ba466d7SYoshiji Amagai 	unlock_page(page);
1142ba466d7SYoshiji Amagai }
1152ba466d7SYoshiji Amagai 
nilfs_check_page(struct page * page)116be5b82dbSAl Viro static bool nilfs_check_page(struct page *page)
1172ba466d7SYoshiji Amagai {
1182ba466d7SYoshiji Amagai 	struct inode *dir = page->mapping->host;
1192ba466d7SYoshiji Amagai 	struct super_block *sb = dir->i_sb;
1200c6c44cbSRyusuke Konishi 	unsigned int chunk_size = nilfs_chunk_size(dir);
1212ba466d7SYoshiji Amagai 	char *kaddr = page_address(page);
1220c6c44cbSRyusuke Konishi 	unsigned int offs, rec_len;
1230c6c44cbSRyusuke Konishi 	unsigned int limit = PAGE_SIZE;
1242ba466d7SYoshiji Amagai 	struct nilfs_dir_entry *p;
1252ba466d7SYoshiji Amagai 	char *error;
1262ba466d7SYoshiji Amagai 
12709cbfeafSKirill A. Shutemov 	if ((dir->i_size >> PAGE_SHIFT) == page->index) {
12809cbfeafSKirill A. Shutemov 		limit = dir->i_size & ~PAGE_MASK;
1292ba466d7SYoshiji Amagai 		if (limit & (chunk_size - 1))
1302ba466d7SYoshiji Amagai 			goto Ebadsize;
1312ba466d7SYoshiji Amagai 		if (!limit)
1322ba466d7SYoshiji Amagai 			goto out;
1332ba466d7SYoshiji Amagai 	}
1342ba466d7SYoshiji Amagai 	for (offs = 0; offs <= limit - NILFS_DIR_REC_LEN(1); offs += rec_len) {
1352ba466d7SYoshiji Amagai 		p = (struct nilfs_dir_entry *)(kaddr + offs);
1366cda9fa2SRyusuke Konishi 		rec_len = nilfs_rec_len_from_disk(p->rec_len);
1372ba466d7SYoshiji Amagai 
1382ba466d7SYoshiji Amagai 		if (rec_len < NILFS_DIR_REC_LEN(1))
1392ba466d7SYoshiji Amagai 			goto Eshort;
1402ba466d7SYoshiji Amagai 		if (rec_len & 3)
1412ba466d7SYoshiji Amagai 			goto Ealign;
1422ba466d7SYoshiji Amagai 		if (rec_len < NILFS_DIR_REC_LEN(p->name_len))
1432ba466d7SYoshiji Amagai 			goto Enamelen;
1442ba466d7SYoshiji Amagai 		if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
1452ba466d7SYoshiji Amagai 			goto Espan;
1462ba466d7SYoshiji Amagai 	}
1472ba466d7SYoshiji Amagai 	if (offs != limit)
1482ba466d7SYoshiji Amagai 		goto Eend;
1492ba466d7SYoshiji Amagai out:
1502ba466d7SYoshiji Amagai 	SetPageChecked(page);
151be5b82dbSAl Viro 	return true;
1522ba466d7SYoshiji Amagai 
1532ba466d7SYoshiji Amagai 	/* Too bad, we had an error */
1542ba466d7SYoshiji Amagai 
1552ba466d7SYoshiji Amagai Ebadsize:
156cae3d4caSRyusuke Konishi 	nilfs_error(sb,
1572ba466d7SYoshiji Amagai 		    "size of directory #%lu is not a multiple of chunk size",
158cae3d4caSRyusuke Konishi 		    dir->i_ino);
1592ba466d7SYoshiji Amagai 	goto fail;
1602ba466d7SYoshiji Amagai Eshort:
1612ba466d7SYoshiji Amagai 	error = "rec_len is smaller than minimal";
1622ba466d7SYoshiji Amagai 	goto bad_entry;
1632ba466d7SYoshiji Amagai Ealign:
1642ba466d7SYoshiji Amagai 	error = "unaligned directory entry";
1652ba466d7SYoshiji Amagai 	goto bad_entry;
1662ba466d7SYoshiji Amagai Enamelen:
1672ba466d7SYoshiji Amagai 	error = "rec_len is too small for name_len";
1682ba466d7SYoshiji Amagai 	goto bad_entry;
1692ba466d7SYoshiji Amagai Espan:
1702ba466d7SYoshiji Amagai 	error = "directory entry across blocks";
1712ba466d7SYoshiji Amagai bad_entry:
172cae3d4caSRyusuke Konishi 	nilfs_error(sb,
173cae3d4caSRyusuke Konishi 		    "bad entry in directory #%lu: %s - offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
17409cbfeafSKirill A. Shutemov 		    dir->i_ino, error, (page->index << PAGE_SHIFT) + offs,
1752ba466d7SYoshiji Amagai 		    (unsigned long)le64_to_cpu(p->inode),
1762ba466d7SYoshiji Amagai 		    rec_len, p->name_len);
1772ba466d7SYoshiji Amagai 	goto fail;
1782ba466d7SYoshiji Amagai Eend:
1792ba466d7SYoshiji Amagai 	p = (struct nilfs_dir_entry *)(kaddr + offs);
180cae3d4caSRyusuke Konishi 	nilfs_error(sb,
181cae3d4caSRyusuke Konishi 		    "entry in directory #%lu spans the page boundary offset=%lu, inode=%lu",
18209cbfeafSKirill A. Shutemov 		    dir->i_ino, (page->index << PAGE_SHIFT) + offs,
1832ba466d7SYoshiji Amagai 		    (unsigned long)le64_to_cpu(p->inode));
1842ba466d7SYoshiji Amagai fail:
1852ba466d7SYoshiji Amagai 	SetPageError(page);
186be5b82dbSAl Viro 	return false;
1872ba466d7SYoshiji Amagai }
1882ba466d7SYoshiji Amagai 
nilfs_get_page(struct inode * dir,unsigned long n)1892ba466d7SYoshiji Amagai static struct page *nilfs_get_page(struct inode *dir, unsigned long n)
1902ba466d7SYoshiji Amagai {
1912ba466d7SYoshiji Amagai 	struct address_space *mapping = dir->i_mapping;
192c28e69d9SRyusuke Konishi 	struct page *page = read_mapping_page(mapping, n, NULL);
193c28e69d9SRyusuke Konishi 
1942ba466d7SYoshiji Amagai 	if (!IS_ERR(page)) {
1952ba466d7SYoshiji Amagai 		kmap(page);
196be5b82dbSAl Viro 		if (unlikely(!PageChecked(page))) {
19779ea6556SMatthew Wilcox (Oracle) 			if (!nilfs_check_page(page))
1982ba466d7SYoshiji Amagai 				goto fail;
1992ba466d7SYoshiji Amagai 		}
200be5b82dbSAl Viro 	}
2012ba466d7SYoshiji Amagai 	return page;
2022ba466d7SYoshiji Amagai 
2032ba466d7SYoshiji Amagai fail:
2042ba466d7SYoshiji Amagai 	nilfs_put_page(page);
2052ba466d7SYoshiji Amagai 	return ERR_PTR(-EIO);
2062ba466d7SYoshiji Amagai }
2072ba466d7SYoshiji Amagai 
2082ba466d7SYoshiji Amagai /*
2092ba466d7SYoshiji Amagai  * NOTE! unlike strncmp, nilfs_match returns 1 for success, 0 for failure.
2102ba466d7SYoshiji Amagai  *
2112ba466d7SYoshiji Amagai  * len <= NILFS_NAME_LEN and de != NULL are guaranteed by caller.
2122ba466d7SYoshiji Amagai  */
2132ba466d7SYoshiji Amagai static int
nilfs_match(int len,const unsigned char * name,struct nilfs_dir_entry * de)214072f98b4SAl Viro nilfs_match(int len, const unsigned char *name, struct nilfs_dir_entry *de)
2152ba466d7SYoshiji Amagai {
2162ba466d7SYoshiji Amagai 	if (len != de->name_len)
2172ba466d7SYoshiji Amagai 		return 0;
2182ba466d7SYoshiji Amagai 	if (!de->inode)
2192ba466d7SYoshiji Amagai 		return 0;
2202ba466d7SYoshiji Amagai 	return !memcmp(name, de->name, len);
2212ba466d7SYoshiji Amagai }
2222ba466d7SYoshiji Amagai 
2232ba466d7SYoshiji Amagai /*
2242ba466d7SYoshiji Amagai  * p is at least 6 bytes before the end of page
2252ba466d7SYoshiji Amagai  */
nilfs_next_entry(struct nilfs_dir_entry * p)2262ba466d7SYoshiji Amagai static struct nilfs_dir_entry *nilfs_next_entry(struct nilfs_dir_entry *p)
2272ba466d7SYoshiji Amagai {
2286cda9fa2SRyusuke Konishi 	return (struct nilfs_dir_entry *)((char *)p +
2296cda9fa2SRyusuke Konishi 					  nilfs_rec_len_from_disk(p->rec_len));
2302ba466d7SYoshiji Amagai }
2312ba466d7SYoshiji Amagai 
2322ba466d7SYoshiji Amagai static unsigned char
2332ba466d7SYoshiji Amagai nilfs_filetype_table[NILFS_FT_MAX] = {
2342ba466d7SYoshiji Amagai 	[NILFS_FT_UNKNOWN]	= DT_UNKNOWN,
2352ba466d7SYoshiji Amagai 	[NILFS_FT_REG_FILE]	= DT_REG,
2362ba466d7SYoshiji Amagai 	[NILFS_FT_DIR]		= DT_DIR,
2372ba466d7SYoshiji Amagai 	[NILFS_FT_CHRDEV]	= DT_CHR,
2382ba466d7SYoshiji Amagai 	[NILFS_FT_BLKDEV]	= DT_BLK,
2392ba466d7SYoshiji Amagai 	[NILFS_FT_FIFO]		= DT_FIFO,
2402ba466d7SYoshiji Amagai 	[NILFS_FT_SOCK]		= DT_SOCK,
2412ba466d7SYoshiji Amagai 	[NILFS_FT_SYMLINK]	= DT_LNK,
2422ba466d7SYoshiji Amagai };
2432ba466d7SYoshiji Amagai 
2442ba466d7SYoshiji Amagai #define S_SHIFT 12
2452ba466d7SYoshiji Amagai static unsigned char
2462382eae6SJeongjun Park nilfs_type_by_mode[(S_IFMT >> S_SHIFT) + 1] = {
2472ba466d7SYoshiji Amagai 	[S_IFREG >> S_SHIFT]	= NILFS_FT_REG_FILE,
2482ba466d7SYoshiji Amagai 	[S_IFDIR >> S_SHIFT]	= NILFS_FT_DIR,
2492ba466d7SYoshiji Amagai 	[S_IFCHR >> S_SHIFT]	= NILFS_FT_CHRDEV,
2502ba466d7SYoshiji Amagai 	[S_IFBLK >> S_SHIFT]	= NILFS_FT_BLKDEV,
2512ba466d7SYoshiji Amagai 	[S_IFIFO >> S_SHIFT]	= NILFS_FT_FIFO,
2522ba466d7SYoshiji Amagai 	[S_IFSOCK >> S_SHIFT]	= NILFS_FT_SOCK,
2532ba466d7SYoshiji Amagai 	[S_IFLNK >> S_SHIFT]	= NILFS_FT_SYMLINK,
2542ba466d7SYoshiji Amagai };
2552ba466d7SYoshiji Amagai 
nilfs_set_de_type(struct nilfs_dir_entry * de,struct inode * inode)2562ba466d7SYoshiji Amagai static void nilfs_set_de_type(struct nilfs_dir_entry *de, struct inode *inode)
2572ba466d7SYoshiji Amagai {
258c6e49e3fSAl Viro 	umode_t mode = inode->i_mode;
2592ba466d7SYoshiji Amagai 
2602ba466d7SYoshiji Amagai 	de->file_type = nilfs_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
2612ba466d7SYoshiji Amagai }
2622ba466d7SYoshiji Amagai 
nilfs_readdir(struct file * file,struct dir_context * ctx)2631616abe8SAl Viro static int nilfs_readdir(struct file *file, struct dir_context *ctx)
2642ba466d7SYoshiji Amagai {
2651616abe8SAl Viro 	loff_t pos = ctx->pos;
2661616abe8SAl Viro 	struct inode *inode = file_inode(file);
2672ba466d7SYoshiji Amagai 	struct super_block *sb = inode->i_sb;
26809cbfeafSKirill A. Shutemov 	unsigned int offset = pos & ~PAGE_MASK;
26909cbfeafSKirill A. Shutemov 	unsigned long n = pos >> PAGE_SHIFT;
2702ba466d7SYoshiji Amagai 	unsigned long npages = dir_pages(inode);
2712ba466d7SYoshiji Amagai 
2722ba466d7SYoshiji Amagai 	if (pos > inode->i_size - NILFS_DIR_REC_LEN(1))
2731616abe8SAl Viro 		return 0;
2742ba466d7SYoshiji Amagai 
2752ba466d7SYoshiji Amagai 	for ( ; n < npages; n++, offset = 0) {
2762ba466d7SYoshiji Amagai 		char *kaddr, *limit;
2772ba466d7SYoshiji Amagai 		struct nilfs_dir_entry *de;
2782ba466d7SYoshiji Amagai 		struct page *page = nilfs_get_page(inode, n);
2792ba466d7SYoshiji Amagai 
2802ba466d7SYoshiji Amagai 		if (IS_ERR(page)) {
281cae3d4caSRyusuke Konishi 			nilfs_error(sb, "bad page in #%lu", inode->i_ino);
28209cbfeafSKirill A. Shutemov 			ctx->pos += PAGE_SIZE - offset;
2831616abe8SAl Viro 			return -EIO;
2842ba466d7SYoshiji Amagai 		}
2852ba466d7SYoshiji Amagai 		kaddr = page_address(page);
2862ba466d7SYoshiji Amagai 		de = (struct nilfs_dir_entry *)(kaddr + offset);
2872ba466d7SYoshiji Amagai 		limit = kaddr + nilfs_last_byte(inode, n) -
2882ba466d7SYoshiji Amagai 			NILFS_DIR_REC_LEN(1);
2892ba466d7SYoshiji Amagai 		for ( ; (char *)de <= limit; de = nilfs_next_entry(de)) {
2902ba466d7SYoshiji Amagai 			if (de->rec_len == 0) {
291cae3d4caSRyusuke Konishi 				nilfs_error(sb, "zero-length directory entry");
2922ba466d7SYoshiji Amagai 				nilfs_put_page(page);
2931616abe8SAl Viro 				return -EIO;
2942ba466d7SYoshiji Amagai 			}
2952ba466d7SYoshiji Amagai 			if (de->inode) {
2961616abe8SAl Viro 				unsigned char t;
2972ba466d7SYoshiji Amagai 
2981616abe8SAl Viro 				if (de->file_type < NILFS_FT_MAX)
2991616abe8SAl Viro 					t = nilfs_filetype_table[de->file_type];
3001616abe8SAl Viro 				else
3011616abe8SAl Viro 					t = DT_UNKNOWN;
3022ba466d7SYoshiji Amagai 
3031616abe8SAl Viro 				if (!dir_emit(ctx, de->name, de->name_len,
3041616abe8SAl Viro 						le64_to_cpu(de->inode), t)) {
3052ba466d7SYoshiji Amagai 					nilfs_put_page(page);
3061616abe8SAl Viro 					return 0;
3072ba466d7SYoshiji Amagai 				}
3082ba466d7SYoshiji Amagai 			}
3091616abe8SAl Viro 			ctx->pos += nilfs_rec_len_from_disk(de->rec_len);
3102ba466d7SYoshiji Amagai 		}
3112ba466d7SYoshiji Amagai 		nilfs_put_page(page);
3122ba466d7SYoshiji Amagai 	}
3131616abe8SAl Viro 	return 0;
3142ba466d7SYoshiji Amagai }
3152ba466d7SYoshiji Amagai 
3162ba466d7SYoshiji Amagai /*
3172ba466d7SYoshiji Amagai  *	nilfs_find_entry()
3182ba466d7SYoshiji Amagai  *
3192ba466d7SYoshiji Amagai  * finds an entry in the specified directory with the wanted name. It
3202ba466d7SYoshiji Amagai  * returns the page in which the entry was found, and the entry itself
3212ba466d7SYoshiji Amagai  * (as a parameter - res_dir). Page is returned mapped and unlocked.
3222ba466d7SYoshiji Amagai  * Entry is guaranteed to be valid.
3232ba466d7SYoshiji Amagai  */
3242ba466d7SYoshiji Amagai struct nilfs_dir_entry *
nilfs_find_entry(struct inode * dir,const struct qstr * qstr,struct page ** res_page)3250319003dSAl Viro nilfs_find_entry(struct inode *dir, const struct qstr *qstr,
3262ba466d7SYoshiji Amagai 		 struct page **res_page)
3272ba466d7SYoshiji Amagai {
3280319003dSAl Viro 	const unsigned char *name = qstr->name;
3290319003dSAl Viro 	int namelen = qstr->len;
3300c6c44cbSRyusuke Konishi 	unsigned int reclen = NILFS_DIR_REC_LEN(namelen);
3312ba466d7SYoshiji Amagai 	unsigned long start, n;
3322ba466d7SYoshiji Amagai 	unsigned long npages = dir_pages(dir);
3332ba466d7SYoshiji Amagai 	struct page *page = NULL;
3342ba466d7SYoshiji Amagai 	struct nilfs_inode_info *ei = NILFS_I(dir);
3352ba466d7SYoshiji Amagai 	struct nilfs_dir_entry *de;
3362ba466d7SYoshiji Amagai 
3372ba466d7SYoshiji Amagai 	if (npages == 0)
3382ba466d7SYoshiji Amagai 		goto out;
3392ba466d7SYoshiji Amagai 
3402ba466d7SYoshiji Amagai 	/* OFFSET_CACHE */
3412ba466d7SYoshiji Amagai 	*res_page = NULL;
3422ba466d7SYoshiji Amagai 
3432ba466d7SYoshiji Amagai 	start = ei->i_dir_start_lookup;
3442ba466d7SYoshiji Amagai 	if (start >= npages)
3452ba466d7SYoshiji Amagai 		start = 0;
3462ba466d7SYoshiji Amagai 	n = start;
3472ba466d7SYoshiji Amagai 	do {
3482ba466d7SYoshiji Amagai 		char *kaddr;
3494ad364caSRyusuke Konishi 
3502ba466d7SYoshiji Amagai 		page = nilfs_get_page(dir, n);
3512ba466d7SYoshiji Amagai 		if (!IS_ERR(page)) {
3522ba466d7SYoshiji Amagai 			kaddr = page_address(page);
3532ba466d7SYoshiji Amagai 			de = (struct nilfs_dir_entry *)kaddr;
3542ba466d7SYoshiji Amagai 			kaddr += nilfs_last_byte(dir, n) - reclen;
3552ba466d7SYoshiji Amagai 			while ((char *) de <= kaddr) {
3562ba466d7SYoshiji Amagai 				if (de->rec_len == 0) {
357cae3d4caSRyusuke Konishi 					nilfs_error(dir->i_sb,
3582ba466d7SYoshiji Amagai 						"zero-length directory entry");
3592ba466d7SYoshiji Amagai 					nilfs_put_page(page);
3602ba466d7SYoshiji Amagai 					goto out;
3612ba466d7SYoshiji Amagai 				}
3622ba466d7SYoshiji Amagai 				if (nilfs_match(namelen, name, de))
3632ba466d7SYoshiji Amagai 					goto found;
3642ba466d7SYoshiji Amagai 				de = nilfs_next_entry(de);
3652ba466d7SYoshiji Amagai 			}
3662ba466d7SYoshiji Amagai 			nilfs_put_page(page);
3672ba466d7SYoshiji Amagai 		}
3682ba466d7SYoshiji Amagai 		if (++n >= npages)
3692ba466d7SYoshiji Amagai 			n = 0;
3702ba466d7SYoshiji Amagai 		/* next page is past the blocks we've got */
37109cbfeafSKirill A. Shutemov 		if (unlikely(n > (dir->i_blocks >> (PAGE_SHIFT - 9)))) {
372cae3d4caSRyusuke Konishi 			nilfs_error(dir->i_sb,
3731621562bSRyusuke Konishi 			       "dir %lu size %lld exceeds block count %llu",
3742ba466d7SYoshiji Amagai 			       dir->i_ino, dir->i_size,
3752ba466d7SYoshiji Amagai 			       (unsigned long long)dir->i_blocks);
3762ba466d7SYoshiji Amagai 			goto out;
3772ba466d7SYoshiji Amagai 		}
3782ba466d7SYoshiji Amagai 	} while (n != start);
3792ba466d7SYoshiji Amagai out:
3802ba466d7SYoshiji Amagai 	return NULL;
3812ba466d7SYoshiji Amagai 
3822ba466d7SYoshiji Amagai found:
3832ba466d7SYoshiji Amagai 	*res_page = page;
3842ba466d7SYoshiji Amagai 	ei->i_dir_start_lookup = n;
3852ba466d7SYoshiji Amagai 	return de;
3862ba466d7SYoshiji Amagai }
3872ba466d7SYoshiji Amagai 
nilfs_dotdot(struct inode * dir,struct page ** p)3882ba466d7SYoshiji Amagai struct nilfs_dir_entry *nilfs_dotdot(struct inode *dir, struct page **p)
3892ba466d7SYoshiji Amagai {
3902ba466d7SYoshiji Amagai 	struct page *page = nilfs_get_page(dir, 0);
3912ba466d7SYoshiji Amagai 	struct nilfs_dir_entry *de = NULL;
3922ba466d7SYoshiji Amagai 
3932ba466d7SYoshiji Amagai 	if (!IS_ERR(page)) {
3942ba466d7SYoshiji Amagai 		de = nilfs_next_entry(
3952ba466d7SYoshiji Amagai 			(struct nilfs_dir_entry *)page_address(page));
3962ba466d7SYoshiji Amagai 		*p = page;
3972ba466d7SYoshiji Amagai 	}
3982ba466d7SYoshiji Amagai 	return de;
3992ba466d7SYoshiji Amagai }
4002ba466d7SYoshiji Amagai 
nilfs_inode_by_name(struct inode * dir,const struct qstr * qstr)4010319003dSAl Viro ino_t nilfs_inode_by_name(struct inode *dir, const struct qstr *qstr)
4022ba466d7SYoshiji Amagai {
4032ba466d7SYoshiji Amagai 	ino_t res = 0;
4042ba466d7SYoshiji Amagai 	struct nilfs_dir_entry *de;
4052ba466d7SYoshiji Amagai 	struct page *page;
4062ba466d7SYoshiji Amagai 
4070319003dSAl Viro 	de = nilfs_find_entry(dir, qstr, &page);
4082ba466d7SYoshiji Amagai 	if (de) {
4092ba466d7SYoshiji Amagai 		res = le64_to_cpu(de->inode);
4102ba466d7SYoshiji Amagai 		kunmap(page);
41109cbfeafSKirill A. Shutemov 		put_page(page);
4122ba466d7SYoshiji Amagai 	}
4132ba466d7SYoshiji Amagai 	return res;
4142ba466d7SYoshiji Amagai }
4152ba466d7SYoshiji Amagai 
4162ba466d7SYoshiji Amagai /* Releases the page */
nilfs_set_link(struct inode * dir,struct nilfs_dir_entry * de,struct page * page,struct inode * inode)4172ba466d7SYoshiji Amagai void nilfs_set_link(struct inode *dir, struct nilfs_dir_entry *de,
4182ba466d7SYoshiji Amagai 		    struct page *page, struct inode *inode)
4192ba466d7SYoshiji Amagai {
4200c6c44cbSRyusuke Konishi 	unsigned int from = (char *)de - (char *)page_address(page);
4210c6c44cbSRyusuke Konishi 	unsigned int to = from + nilfs_rec_len_from_disk(de->rec_len);
4222ba466d7SYoshiji Amagai 	struct address_space *mapping = page->mapping;
4232ba466d7SYoshiji Amagai 	int err;
4242ba466d7SYoshiji Amagai 
4252ba466d7SYoshiji Amagai 	lock_page(page);
426f4e420dcSChristoph Hellwig 	err = nilfs_prepare_chunk(page, from, to);
4272ba466d7SYoshiji Amagai 	BUG_ON(err);
4282ba466d7SYoshiji Amagai 	de->inode = cpu_to_le64(inode->i_ino);
4292ba466d7SYoshiji Amagai 	nilfs_set_de_type(de, inode);
4302093abf9SJiro SEKIBA 	nilfs_commit_chunk(page, mapping, from, to);
4312ba466d7SYoshiji Amagai 	nilfs_put_page(page);
432e21d4f41SJeff Layton 	dir->i_mtime = inode_set_ctime_current(dir);
4332ba466d7SYoshiji Amagai }
4342ba466d7SYoshiji Amagai 
4352ba466d7SYoshiji Amagai /*
4362ba466d7SYoshiji Amagai  *	Parent is locked.
4372ba466d7SYoshiji Amagai  */
nilfs_add_link(struct dentry * dentry,struct inode * inode)4382ba466d7SYoshiji Amagai int nilfs_add_link(struct dentry *dentry, struct inode *inode)
4392ba466d7SYoshiji Amagai {
4402b0143b5SDavid Howells 	struct inode *dir = d_inode(dentry->d_parent);
441072f98b4SAl Viro 	const unsigned char *name = dentry->d_name.name;
4422ba466d7SYoshiji Amagai 	int namelen = dentry->d_name.len;
4430c6c44cbSRyusuke Konishi 	unsigned int chunk_size = nilfs_chunk_size(dir);
4440c6c44cbSRyusuke Konishi 	unsigned int reclen = NILFS_DIR_REC_LEN(namelen);
4452ba466d7SYoshiji Amagai 	unsigned short rec_len, name_len;
4462ba466d7SYoshiji Amagai 	struct page *page = NULL;
4472ba466d7SYoshiji Amagai 	struct nilfs_dir_entry *de;
4482ba466d7SYoshiji Amagai 	unsigned long npages = dir_pages(dir);
4492ba466d7SYoshiji Amagai 	unsigned long n;
4502ba466d7SYoshiji Amagai 	char *kaddr;
4510c6c44cbSRyusuke Konishi 	unsigned int from, to;
4522ba466d7SYoshiji Amagai 	int err;
4532ba466d7SYoshiji Amagai 
4542ba466d7SYoshiji Amagai 	/*
4552ba466d7SYoshiji Amagai 	 * We take care of directory expansion in the same loop.
4562ba466d7SYoshiji Amagai 	 * This code plays outside i_size, so it locks the page
4572ba466d7SYoshiji Amagai 	 * to protect that region.
4582ba466d7SYoshiji Amagai 	 */
4592ba466d7SYoshiji Amagai 	for (n = 0; n <= npages; n++) {
4602ba466d7SYoshiji Amagai 		char *dir_end;
4612ba466d7SYoshiji Amagai 
4622ba466d7SYoshiji Amagai 		page = nilfs_get_page(dir, n);
4632ba466d7SYoshiji Amagai 		err = PTR_ERR(page);
4642ba466d7SYoshiji Amagai 		if (IS_ERR(page))
4652ba466d7SYoshiji Amagai 			goto out;
4662ba466d7SYoshiji Amagai 		lock_page(page);
4672ba466d7SYoshiji Amagai 		kaddr = page_address(page);
4682ba466d7SYoshiji Amagai 		dir_end = kaddr + nilfs_last_byte(dir, n);
4692ba466d7SYoshiji Amagai 		de = (struct nilfs_dir_entry *)kaddr;
47009cbfeafSKirill A. Shutemov 		kaddr += PAGE_SIZE - reclen;
4712ba466d7SYoshiji Amagai 		while ((char *)de <= kaddr) {
4722ba466d7SYoshiji Amagai 			if ((char *)de == dir_end) {
4732ba466d7SYoshiji Amagai 				/* We hit i_size */
4742ba466d7SYoshiji Amagai 				name_len = 0;
4752ba466d7SYoshiji Amagai 				rec_len = chunk_size;
4766cda9fa2SRyusuke Konishi 				de->rec_len = nilfs_rec_len_to_disk(chunk_size);
4772ba466d7SYoshiji Amagai 				de->inode = 0;
4782ba466d7SYoshiji Amagai 				goto got_it;
4792ba466d7SYoshiji Amagai 			}
4802ba466d7SYoshiji Amagai 			if (de->rec_len == 0) {
481cae3d4caSRyusuke Konishi 				nilfs_error(dir->i_sb,
4822ba466d7SYoshiji Amagai 					    "zero-length directory entry");
4832ba466d7SYoshiji Amagai 				err = -EIO;
4842ba466d7SYoshiji Amagai 				goto out_unlock;
4852ba466d7SYoshiji Amagai 			}
4862ba466d7SYoshiji Amagai 			err = -EEXIST;
4872ba466d7SYoshiji Amagai 			if (nilfs_match(namelen, name, de))
4882ba466d7SYoshiji Amagai 				goto out_unlock;
4892ba466d7SYoshiji Amagai 			name_len = NILFS_DIR_REC_LEN(de->name_len);
4906cda9fa2SRyusuke Konishi 			rec_len = nilfs_rec_len_from_disk(de->rec_len);
4912ba466d7SYoshiji Amagai 			if (!de->inode && rec_len >= reclen)
4922ba466d7SYoshiji Amagai 				goto got_it;
4932ba466d7SYoshiji Amagai 			if (rec_len >= name_len + reclen)
4942ba466d7SYoshiji Amagai 				goto got_it;
4952ba466d7SYoshiji Amagai 			de = (struct nilfs_dir_entry *)((char *)de + rec_len);
4962ba466d7SYoshiji Amagai 		}
4972ba466d7SYoshiji Amagai 		unlock_page(page);
4982ba466d7SYoshiji Amagai 		nilfs_put_page(page);
4992ba466d7SYoshiji Amagai 	}
5002ba466d7SYoshiji Amagai 	BUG();
5012ba466d7SYoshiji Amagai 	return -EINVAL;
5022ba466d7SYoshiji Amagai 
5032ba466d7SYoshiji Amagai got_it:
5042ba466d7SYoshiji Amagai 	from = (char *)de - (char *)page_address(page);
5052ba466d7SYoshiji Amagai 	to = from + rec_len;
506f4e420dcSChristoph Hellwig 	err = nilfs_prepare_chunk(page, from, to);
5072ba466d7SYoshiji Amagai 	if (err)
5082ba466d7SYoshiji Amagai 		goto out_unlock;
5092ba466d7SYoshiji Amagai 	if (de->inode) {
5102ba466d7SYoshiji Amagai 		struct nilfs_dir_entry *de1;
5112ba466d7SYoshiji Amagai 
5122ba466d7SYoshiji Amagai 		de1 = (struct nilfs_dir_entry *)((char *)de + name_len);
5136cda9fa2SRyusuke Konishi 		de1->rec_len = nilfs_rec_len_to_disk(rec_len - name_len);
5146cda9fa2SRyusuke Konishi 		de->rec_len = nilfs_rec_len_to_disk(name_len);
5152ba466d7SYoshiji Amagai 		de = de1;
5162ba466d7SYoshiji Amagai 	}
5172ba466d7SYoshiji Amagai 	de->name_len = namelen;
5182ba466d7SYoshiji Amagai 	memcpy(de->name, name, namelen);
5192ba466d7SYoshiji Amagai 	de->inode = cpu_to_le64(inode->i_ino);
5202ba466d7SYoshiji Amagai 	nilfs_set_de_type(de, inode);
5212093abf9SJiro SEKIBA 	nilfs_commit_chunk(page, page->mapping, from, to);
522e21d4f41SJeff Layton 	dir->i_mtime = inode_set_ctime_current(dir);
523abdb318bSJiro SEKIBA 	nilfs_mark_inode_dirty(dir);
5242ba466d7SYoshiji Amagai 	/* OFFSET_CACHE */
5252ba466d7SYoshiji Amagai out_put:
5262ba466d7SYoshiji Amagai 	nilfs_put_page(page);
5272ba466d7SYoshiji Amagai out:
5282ba466d7SYoshiji Amagai 	return err;
5292ba466d7SYoshiji Amagai out_unlock:
5302ba466d7SYoshiji Amagai 	unlock_page(page);
5312ba466d7SYoshiji Amagai 	goto out_put;
5322ba466d7SYoshiji Amagai }
5332ba466d7SYoshiji Amagai 
5342ba466d7SYoshiji Amagai /*
5352ba466d7SYoshiji Amagai  * nilfs_delete_entry deletes a directory entry by merging it with the
5362ba466d7SYoshiji Amagai  * previous entry. Page is up-to-date. Releases the page.
5372ba466d7SYoshiji Amagai  */
nilfs_delete_entry(struct nilfs_dir_entry * dir,struct page * page)5382ba466d7SYoshiji Amagai int nilfs_delete_entry(struct nilfs_dir_entry *dir, struct page *page)
5392ba466d7SYoshiji Amagai {
5402ba466d7SYoshiji Amagai 	struct address_space *mapping = page->mapping;
5412ba466d7SYoshiji Amagai 	struct inode *inode = mapping->host;
5422ba466d7SYoshiji Amagai 	char *kaddr = page_address(page);
5430c6c44cbSRyusuke Konishi 	unsigned int from, to;
5440c6c44cbSRyusuke Konishi 	struct nilfs_dir_entry *de, *pde = NULL;
5452ba466d7SYoshiji Amagai 	int err;
5462ba466d7SYoshiji Amagai 
5470c6c44cbSRyusuke Konishi 	from = ((char *)dir - kaddr) & ~(nilfs_chunk_size(inode) - 1);
5480c6c44cbSRyusuke Konishi 	to = ((char *)dir - kaddr) + nilfs_rec_len_from_disk(dir->rec_len);
5490c6c44cbSRyusuke Konishi 	de = (struct nilfs_dir_entry *)(kaddr + from);
5500c6c44cbSRyusuke Konishi 
5512ba466d7SYoshiji Amagai 	while ((char *)de < (char *)dir) {
5522ba466d7SYoshiji Amagai 		if (de->rec_len == 0) {
553cae3d4caSRyusuke Konishi 			nilfs_error(inode->i_sb,
5542ba466d7SYoshiji Amagai 				    "zero-length directory entry");
5552ba466d7SYoshiji Amagai 			err = -EIO;
5562ba466d7SYoshiji Amagai 			goto out;
5572ba466d7SYoshiji Amagai 		}
5582ba466d7SYoshiji Amagai 		pde = de;
5592ba466d7SYoshiji Amagai 		de = nilfs_next_entry(de);
5602ba466d7SYoshiji Amagai 	}
5612ba466d7SYoshiji Amagai 	if (pde)
5622ba466d7SYoshiji Amagai 		from = (char *)pde - (char *)page_address(page);
5632ba466d7SYoshiji Amagai 	lock_page(page);
564f4e420dcSChristoph Hellwig 	err = nilfs_prepare_chunk(page, from, to);
5652ba466d7SYoshiji Amagai 	BUG_ON(err);
5662ba466d7SYoshiji Amagai 	if (pde)
5676cda9fa2SRyusuke Konishi 		pde->rec_len = nilfs_rec_len_to_disk(to - from);
5682ba466d7SYoshiji Amagai 	dir->inode = 0;
5692093abf9SJiro SEKIBA 	nilfs_commit_chunk(page, mapping, from, to);
570e21d4f41SJeff Layton 	inode->i_mtime = inode_set_ctime_current(inode);
5712ba466d7SYoshiji Amagai out:
5722ba466d7SYoshiji Amagai 	nilfs_put_page(page);
5732ba466d7SYoshiji Amagai 	return err;
5742ba466d7SYoshiji Amagai }
5752ba466d7SYoshiji Amagai 
5762ba466d7SYoshiji Amagai /*
5772ba466d7SYoshiji Amagai  * Set the first fragment of directory.
5782ba466d7SYoshiji Amagai  */
nilfs_make_empty(struct inode * inode,struct inode * parent)5792ba466d7SYoshiji Amagai int nilfs_make_empty(struct inode *inode, struct inode *parent)
5802ba466d7SYoshiji Amagai {
5812ba466d7SYoshiji Amagai 	struct address_space *mapping = inode->i_mapping;
5822ba466d7SYoshiji Amagai 	struct page *page = grab_cache_page(mapping, 0);
5830c6c44cbSRyusuke Konishi 	unsigned int chunk_size = nilfs_chunk_size(inode);
5842ba466d7SYoshiji Amagai 	struct nilfs_dir_entry *de;
5852ba466d7SYoshiji Amagai 	int err;
5862ba466d7SYoshiji Amagai 	void *kaddr;
5872ba466d7SYoshiji Amagai 
5882ba466d7SYoshiji Amagai 	if (!page)
5892ba466d7SYoshiji Amagai 		return -ENOMEM;
5902ba466d7SYoshiji Amagai 
591f4e420dcSChristoph Hellwig 	err = nilfs_prepare_chunk(page, 0, chunk_size);
5922ba466d7SYoshiji Amagai 	if (unlikely(err)) {
5932ba466d7SYoshiji Amagai 		unlock_page(page);
5942ba466d7SYoshiji Amagai 		goto fail;
5952ba466d7SYoshiji Amagai 	}
5967b9c0976SCong Wang 	kaddr = kmap_atomic(page);
5972ba466d7SYoshiji Amagai 	memset(kaddr, 0, chunk_size);
5982ba466d7SYoshiji Amagai 	de = (struct nilfs_dir_entry *)kaddr;
5992ba466d7SYoshiji Amagai 	de->name_len = 1;
6006cda9fa2SRyusuke Konishi 	de->rec_len = nilfs_rec_len_to_disk(NILFS_DIR_REC_LEN(1));
6012ba466d7SYoshiji Amagai 	memcpy(de->name, ".\0\0", 4);
6022ba466d7SYoshiji Amagai 	de->inode = cpu_to_le64(inode->i_ino);
6032ba466d7SYoshiji Amagai 	nilfs_set_de_type(de, inode);
6042ba466d7SYoshiji Amagai 
6052ba466d7SYoshiji Amagai 	de = (struct nilfs_dir_entry *)(kaddr + NILFS_DIR_REC_LEN(1));
6062ba466d7SYoshiji Amagai 	de->name_len = 2;
6076cda9fa2SRyusuke Konishi 	de->rec_len = nilfs_rec_len_to_disk(chunk_size - NILFS_DIR_REC_LEN(1));
6082ba466d7SYoshiji Amagai 	de->inode = cpu_to_le64(parent->i_ino);
6092ba466d7SYoshiji Amagai 	memcpy(de->name, "..\0", 4);
6102ba466d7SYoshiji Amagai 	nilfs_set_de_type(de, inode);
6117b9c0976SCong Wang 	kunmap_atomic(kaddr);
6122093abf9SJiro SEKIBA 	nilfs_commit_chunk(page, mapping, 0, chunk_size);
6132ba466d7SYoshiji Amagai fail:
61409cbfeafSKirill A. Shutemov 	put_page(page);
6152ba466d7SYoshiji Amagai 	return err;
6162ba466d7SYoshiji Amagai }
6172ba466d7SYoshiji Amagai 
6182ba466d7SYoshiji Amagai /*
6192ba466d7SYoshiji Amagai  * routine to check that the specified directory is empty (for rmdir)
6202ba466d7SYoshiji Amagai  */
nilfs_empty_dir(struct inode * inode)6212ba466d7SYoshiji Amagai int nilfs_empty_dir(struct inode *inode)
6222ba466d7SYoshiji Amagai {
6232ba466d7SYoshiji Amagai 	struct page *page = NULL;
6242ba466d7SYoshiji Amagai 	unsigned long i, npages = dir_pages(inode);
6252ba466d7SYoshiji Amagai 
6262ba466d7SYoshiji Amagai 	for (i = 0; i < npages; i++) {
6272ba466d7SYoshiji Amagai 		char *kaddr;
6282ba466d7SYoshiji Amagai 		struct nilfs_dir_entry *de;
6292ba466d7SYoshiji Amagai 
6302ba466d7SYoshiji Amagai 		page = nilfs_get_page(inode, i);
6312ba466d7SYoshiji Amagai 		if (IS_ERR(page))
6322ba466d7SYoshiji Amagai 			continue;
6332ba466d7SYoshiji Amagai 
6342ba466d7SYoshiji Amagai 		kaddr = page_address(page);
6352ba466d7SYoshiji Amagai 		de = (struct nilfs_dir_entry *)kaddr;
6362ba466d7SYoshiji Amagai 		kaddr += nilfs_last_byte(inode, i) - NILFS_DIR_REC_LEN(1);
6372ba466d7SYoshiji Amagai 
6382ba466d7SYoshiji Amagai 		while ((char *)de <= kaddr) {
6392ba466d7SYoshiji Amagai 			if (de->rec_len == 0) {
640cae3d4caSRyusuke Konishi 				nilfs_error(inode->i_sb,
64106f4abf6SRyusuke Konishi 					    "zero-length directory entry (kaddr=%p, de=%p)",
64206f4abf6SRyusuke Konishi 					    kaddr, de);
6432ba466d7SYoshiji Amagai 				goto not_empty;
6442ba466d7SYoshiji Amagai 			}
6452ba466d7SYoshiji Amagai 			if (de->inode != 0) {
6462ba466d7SYoshiji Amagai 				/* check for . and .. */
6472ba466d7SYoshiji Amagai 				if (de->name[0] != '.')
6482ba466d7SYoshiji Amagai 					goto not_empty;
6492ba466d7SYoshiji Amagai 				if (de->name_len > 2)
6502ba466d7SYoshiji Amagai 					goto not_empty;
6512ba466d7SYoshiji Amagai 				if (de->name_len < 2) {
6522ba466d7SYoshiji Amagai 					if (de->inode !=
6532ba466d7SYoshiji Amagai 					    cpu_to_le64(inode->i_ino))
6542ba466d7SYoshiji Amagai 						goto not_empty;
6552ba466d7SYoshiji Amagai 				} else if (de->name[1] != '.')
6562ba466d7SYoshiji Amagai 					goto not_empty;
6572ba466d7SYoshiji Amagai 			}
6582ba466d7SYoshiji Amagai 			de = nilfs_next_entry(de);
6592ba466d7SYoshiji Amagai 		}
6602ba466d7SYoshiji Amagai 		nilfs_put_page(page);
6612ba466d7SYoshiji Amagai 	}
6622ba466d7SYoshiji Amagai 	return 1;
6632ba466d7SYoshiji Amagai 
6642ba466d7SYoshiji Amagai not_empty:
6652ba466d7SYoshiji Amagai 	nilfs_put_page(page);
6662ba466d7SYoshiji Amagai 	return 0;
6672ba466d7SYoshiji Amagai }
6682ba466d7SYoshiji Amagai 
669828c0950SAlexey Dobriyan const struct file_operations nilfs_dir_operations = {
6702ba466d7SYoshiji Amagai 	.llseek		= generic_file_llseek,
6712ba466d7SYoshiji Amagai 	.read		= generic_read_dir,
672c51da20cSAl Viro 	.iterate_shared	= nilfs_readdir,
6737a946193SRyusuke Konishi 	.unlocked_ioctl	= nilfs_ioctl,
6742ba466d7SYoshiji Amagai #ifdef CONFIG_COMPAT
675828b1c50SRyusuke Konishi 	.compat_ioctl	= nilfs_compat_ioctl,
6762ba466d7SYoshiji Amagai #endif	/* CONFIG_COMPAT */
6772ba466d7SYoshiji Amagai 	.fsync		= nilfs_sync_file,
6782ba466d7SYoshiji Amagai 
6792ba466d7SYoshiji Amagai };
680