xref: /openbmc/linux/fs/nilfs2/dir.c (revision 2f4e3926)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * NILFS directory entry operations
4  *
5  * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6  *
7  * Modified for NILFS by Amagai Yoshiji.
8  */
9 /*
10  *  linux/fs/ext2/dir.c
11  *
12  * Copyright (C) 1992, 1993, 1994, 1995
13  * Remy Card (card@masi.ibp.fr)
14  * Laboratoire MASI - Institut Blaise Pascal
15  * Universite Pierre et Marie Curie (Paris VI)
16  *
17  *  from
18  *
19  *  linux/fs/minix/dir.c
20  *
21  *  Copyright (C) 1991, 1992  Linus Torvalds
22  *
23  *  ext2 directory handling functions
24  *
25  *  Big-endian to little-endian byte-swapping/bitmaps by
26  *        David S. Miller (davem@caip.rutgers.edu), 1995
27  *
28  * All code that works with directory layout had been switched to pagecache
29  * and moved here. AV
30  */
31 
32 #include <linux/pagemap.h>
33 #include "nilfs.h"
34 #include "page.h"
35 
36 static inline unsigned int nilfs_rec_len_from_disk(__le16 dlen)
37 {
38 	unsigned int len = le16_to_cpu(dlen);
39 
40 #if (PAGE_SIZE >= 65536)
41 	if (len == NILFS_MAX_REC_LEN)
42 		return 1 << 16;
43 #endif
44 	return len;
45 }
46 
47 static inline __le16 nilfs_rec_len_to_disk(unsigned int len)
48 {
49 #if (PAGE_SIZE >= 65536)
50 	if (len == (1 << 16))
51 		return cpu_to_le16(NILFS_MAX_REC_LEN);
52 
53 	BUG_ON(len > (1 << 16));
54 #endif
55 	return cpu_to_le16(len);
56 }
57 
58 /*
59  * nilfs uses block-sized chunks. Arguably, sector-sized ones would be
60  * more robust, but we have what we have
61  */
62 static inline unsigned int nilfs_chunk_size(struct inode *inode)
63 {
64 	return inode->i_sb->s_blocksize;
65 }
66 
67 static inline void nilfs_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  */
77 static unsigned int nilfs_last_byte(struct inode *inode, unsigned long page_nr)
78 {
79 	unsigned int last_byte = inode->i_size;
80 
81 	last_byte -= page_nr << PAGE_SHIFT;
82 	if (last_byte > PAGE_SIZE)
83 		last_byte = PAGE_SIZE;
84 	return last_byte;
85 }
86 
87 static int nilfs_prepare_chunk(struct page *page, unsigned int from,
88 			       unsigned int to)
89 {
90 	loff_t pos = page_offset(page) + from;
91 
92 	return __block_write_begin(page, pos, to - from, nilfs_get_block);
93 }
94 
95 static void nilfs_commit_chunk(struct page *page,
96 			       struct address_space *mapping,
97 			       unsigned int from, unsigned int to)
98 {
99 	struct inode *dir = mapping->host;
100 	loff_t pos = page_offset(page) + from;
101 	unsigned int len = to - from;
102 	unsigned int nr_dirty, copied;
103 	int err;
104 
105 	nr_dirty = nilfs_page_count_clean_buffers(page, from, to);
106 	copied = block_write_end(NULL, mapping, pos, len, len, page, NULL);
107 	if (pos + copied > dir->i_size)
108 		i_size_write(dir, pos + copied);
109 	if (IS_DIRSYNC(dir))
110 		nilfs_set_transaction_flag(NILFS_TI_SYNC);
111 	err = nilfs_set_file_dirty(dir, nr_dirty);
112 	WARN_ON(err); /* do not happen */
113 	unlock_page(page);
114 }
115 
116 static bool nilfs_check_page(struct page *page)
117 {
118 	struct inode *dir = page->mapping->host;
119 	struct super_block *sb = dir->i_sb;
120 	unsigned int chunk_size = nilfs_chunk_size(dir);
121 	char *kaddr = page_address(page);
122 	unsigned int offs, rec_len;
123 	unsigned int limit = PAGE_SIZE;
124 	struct nilfs_dir_entry *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 - NILFS_DIR_REC_LEN(1); offs += rec_len) {
135 		p = (struct nilfs_dir_entry *)(kaddr + offs);
136 		rec_len = nilfs_rec_len_from_disk(p->rec_len);
137 
138 		if (rec_len < NILFS_DIR_REC_LEN(1))
139 			goto Eshort;
140 		if (rec_len & 3)
141 			goto Ealign;
142 		if (rec_len < NILFS_DIR_REC_LEN(p->name_len))
143 			goto Enamelen;
144 		if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
145 			goto Espan;
146 		if (unlikely(p->inode &&
147 			     NILFS_PRIVATE_INODE(le64_to_cpu(p->inode))))
148 			goto Einumber;
149 	}
150 	if (offs != limit)
151 		goto Eend;
152 out:
153 	SetPageChecked(page);
154 	return true;
155 
156 	/* Too bad, we had an error */
157 
158 Ebadsize:
159 	nilfs_error(sb,
160 		    "size of directory #%lu is not a multiple of chunk size",
161 		    dir->i_ino);
162 	goto fail;
163 Eshort:
164 	error = "rec_len is smaller than minimal";
165 	goto bad_entry;
166 Ealign:
167 	error = "unaligned directory entry";
168 	goto bad_entry;
169 Enamelen:
170 	error = "rec_len is too small for name_len";
171 	goto bad_entry;
172 Espan:
173 	error = "directory entry across blocks";
174 	goto bad_entry;
175 Einumber:
176 	error = "disallowed inode number";
177 bad_entry:
178 	nilfs_error(sb,
179 		    "bad entry in directory #%lu: %s - offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
180 		    dir->i_ino, error, (page->index << PAGE_SHIFT) + offs,
181 		    (unsigned long)le64_to_cpu(p->inode),
182 		    rec_len, p->name_len);
183 	goto fail;
184 Eend:
185 	p = (struct nilfs_dir_entry *)(kaddr + offs);
186 	nilfs_error(sb,
187 		    "entry in directory #%lu spans the page boundary offset=%lu, inode=%lu",
188 		    dir->i_ino, (page->index << PAGE_SHIFT) + offs,
189 		    (unsigned long)le64_to_cpu(p->inode));
190 fail:
191 	SetPageError(page);
192 	return false;
193 }
194 
195 static void *nilfs_get_page(struct inode *dir, unsigned long n,
196 		struct page **pagep)
197 {
198 	struct address_space *mapping = dir->i_mapping;
199 	struct page *page = read_mapping_page(mapping, n, NULL);
200 	void *kaddr;
201 
202 	if (IS_ERR(page))
203 		return page;
204 
205 	kaddr = kmap(page);
206 	if (unlikely(!PageChecked(page))) {
207 		if (!nilfs_check_page(page))
208 			goto fail;
209 	}
210 
211 	*pagep = page;
212 	return kaddr;
213 
214 fail:
215 	nilfs_put_page(page);
216 	return ERR_PTR(-EIO);
217 }
218 
219 /*
220  * NOTE! unlike strncmp, nilfs_match returns 1 for success, 0 for failure.
221  *
222  * len <= NILFS_NAME_LEN and de != NULL are guaranteed by caller.
223  */
224 static int
225 nilfs_match(int len, const unsigned char *name, struct nilfs_dir_entry *de)
226 {
227 	if (len != de->name_len)
228 		return 0;
229 	if (!de->inode)
230 		return 0;
231 	return !memcmp(name, de->name, len);
232 }
233 
234 /*
235  * p is at least 6 bytes before the end of page
236  */
237 static struct nilfs_dir_entry *nilfs_next_entry(struct nilfs_dir_entry *p)
238 {
239 	return (struct nilfs_dir_entry *)((char *)p +
240 					  nilfs_rec_len_from_disk(p->rec_len));
241 }
242 
243 static unsigned char
244 nilfs_filetype_table[NILFS_FT_MAX] = {
245 	[NILFS_FT_UNKNOWN]	= DT_UNKNOWN,
246 	[NILFS_FT_REG_FILE]	= DT_REG,
247 	[NILFS_FT_DIR]		= DT_DIR,
248 	[NILFS_FT_CHRDEV]	= DT_CHR,
249 	[NILFS_FT_BLKDEV]	= DT_BLK,
250 	[NILFS_FT_FIFO]		= DT_FIFO,
251 	[NILFS_FT_SOCK]		= DT_SOCK,
252 	[NILFS_FT_SYMLINK]	= DT_LNK,
253 };
254 
255 #define S_SHIFT 12
256 static unsigned char
257 nilfs_type_by_mode[(S_IFMT >> S_SHIFT) + 1] = {
258 	[S_IFREG >> S_SHIFT]	= NILFS_FT_REG_FILE,
259 	[S_IFDIR >> S_SHIFT]	= NILFS_FT_DIR,
260 	[S_IFCHR >> S_SHIFT]	= NILFS_FT_CHRDEV,
261 	[S_IFBLK >> S_SHIFT]	= NILFS_FT_BLKDEV,
262 	[S_IFIFO >> S_SHIFT]	= NILFS_FT_FIFO,
263 	[S_IFSOCK >> S_SHIFT]	= NILFS_FT_SOCK,
264 	[S_IFLNK >> S_SHIFT]	= NILFS_FT_SYMLINK,
265 };
266 
267 static void nilfs_set_de_type(struct nilfs_dir_entry *de, struct inode *inode)
268 {
269 	umode_t mode = inode->i_mode;
270 
271 	de->file_type = nilfs_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
272 }
273 
274 static int nilfs_readdir(struct file *file, struct dir_context *ctx)
275 {
276 	loff_t pos = ctx->pos;
277 	struct inode *inode = file_inode(file);
278 	struct super_block *sb = inode->i_sb;
279 	unsigned int offset = pos & ~PAGE_MASK;
280 	unsigned long n = pos >> PAGE_SHIFT;
281 	unsigned long npages = dir_pages(inode);
282 
283 	if (pos > inode->i_size - NILFS_DIR_REC_LEN(1))
284 		return 0;
285 
286 	for ( ; n < npages; n++, offset = 0) {
287 		char *kaddr, *limit;
288 		struct nilfs_dir_entry *de;
289 		struct page *page;
290 
291 		kaddr = nilfs_get_page(inode, n, &page);
292 		if (IS_ERR(kaddr)) {
293 			nilfs_error(sb, "bad page in #%lu", inode->i_ino);
294 			ctx->pos += PAGE_SIZE - offset;
295 			return -EIO;
296 		}
297 		de = (struct nilfs_dir_entry *)(kaddr + offset);
298 		limit = kaddr + nilfs_last_byte(inode, n) -
299 			NILFS_DIR_REC_LEN(1);
300 		for ( ; (char *)de <= limit; de = nilfs_next_entry(de)) {
301 			if (de->rec_len == 0) {
302 				nilfs_error(sb, "zero-length directory entry");
303 				nilfs_put_page(page);
304 				return -EIO;
305 			}
306 			if (de->inode) {
307 				unsigned char t;
308 
309 				if (de->file_type < NILFS_FT_MAX)
310 					t = nilfs_filetype_table[de->file_type];
311 				else
312 					t = DT_UNKNOWN;
313 
314 				if (!dir_emit(ctx, de->name, de->name_len,
315 						le64_to_cpu(de->inode), t)) {
316 					nilfs_put_page(page);
317 					return 0;
318 				}
319 			}
320 			ctx->pos += nilfs_rec_len_from_disk(de->rec_len);
321 		}
322 		nilfs_put_page(page);
323 	}
324 	return 0;
325 }
326 
327 /*
328  *	nilfs_find_entry()
329  *
330  * finds an entry in the specified directory with the wanted name. It
331  * returns the page in which the entry was found, and the entry itself
332  * (as a parameter - res_dir). Page is returned mapped and unlocked.
333  * Entry is guaranteed to be valid.
334  */
335 struct nilfs_dir_entry *
336 nilfs_find_entry(struct inode *dir, const struct qstr *qstr,
337 		 struct page **res_page)
338 {
339 	const unsigned char *name = qstr->name;
340 	int namelen = qstr->len;
341 	unsigned int reclen = NILFS_DIR_REC_LEN(namelen);
342 	unsigned long start, n;
343 	unsigned long npages = dir_pages(dir);
344 	struct page *page = NULL;
345 	struct nilfs_inode_info *ei = NILFS_I(dir);
346 	struct nilfs_dir_entry *de;
347 
348 	if (npages == 0)
349 		goto out;
350 
351 	/* OFFSET_CACHE */
352 	*res_page = NULL;
353 
354 	start = ei->i_dir_start_lookup;
355 	if (start >= npages)
356 		start = 0;
357 	n = start;
358 	do {
359 		char *kaddr = nilfs_get_page(dir, n, &page);
360 
361 		if (!IS_ERR(kaddr)) {
362 			de = (struct nilfs_dir_entry *)kaddr;
363 			kaddr += nilfs_last_byte(dir, n) - reclen;
364 			while ((char *) de <= kaddr) {
365 				if (de->rec_len == 0) {
366 					nilfs_error(dir->i_sb,
367 						"zero-length directory entry");
368 					nilfs_put_page(page);
369 					goto out;
370 				}
371 				if (nilfs_match(namelen, name, de))
372 					goto found;
373 				de = nilfs_next_entry(de);
374 			}
375 			nilfs_put_page(page);
376 		}
377 		if (++n >= npages)
378 			n = 0;
379 		/* next page is past the blocks we've got */
380 		if (unlikely(n > (dir->i_blocks >> (PAGE_SHIFT - 9)))) {
381 			nilfs_error(dir->i_sb,
382 			       "dir %lu size %lld exceeds block count %llu",
383 			       dir->i_ino, dir->i_size,
384 			       (unsigned long long)dir->i_blocks);
385 			goto out;
386 		}
387 	} while (n != start);
388 out:
389 	return NULL;
390 
391 found:
392 	*res_page = page;
393 	ei->i_dir_start_lookup = n;
394 	return de;
395 }
396 
397 struct nilfs_dir_entry *nilfs_dotdot(struct inode *dir, struct page **p)
398 {
399 	struct page *page;
400 	struct nilfs_dir_entry *de, *next_de;
401 	size_t limit;
402 	char *msg;
403 
404 	de = nilfs_get_page(dir, 0, &page);
405 	if (IS_ERR(de))
406 		return NULL;
407 
408 	limit = nilfs_last_byte(dir, 0);  /* is a multiple of chunk size */
409 	if (unlikely(!limit || le64_to_cpu(de->inode) != dir->i_ino ||
410 		     !nilfs_match(1, ".", de))) {
411 		msg = "missing '.'";
412 		goto fail;
413 	}
414 
415 	next_de = nilfs_next_entry(de);
416 	/*
417 	 * If "next_de" has not reached the end of the chunk, there is
418 	 * at least one more record.  Check whether it matches "..".
419 	 */
420 	if (unlikely((char *)next_de == (char *)de + nilfs_chunk_size(dir) ||
421 		     !nilfs_match(2, "..", next_de))) {
422 		msg = "missing '..'";
423 		goto fail;
424 	}
425 	*p = page;
426 	return next_de;
427 
428 fail:
429 	nilfs_error(dir->i_sb, "directory #%lu %s", dir->i_ino, msg);
430 	nilfs_put_page(page);
431 	return NULL;
432 }
433 
434 ino_t nilfs_inode_by_name(struct inode *dir, const struct qstr *qstr)
435 {
436 	ino_t res = 0;
437 	struct nilfs_dir_entry *de;
438 	struct page *page;
439 
440 	de = nilfs_find_entry(dir, qstr, &page);
441 	if (de) {
442 		res = le64_to_cpu(de->inode);
443 		kunmap(page);
444 		put_page(page);
445 	}
446 	return res;
447 }
448 
449 /* Releases the page */
450 void nilfs_set_link(struct inode *dir, struct nilfs_dir_entry *de,
451 		    struct page *page, struct inode *inode)
452 {
453 	unsigned int from = (char *)de - (char *)page_address(page);
454 	unsigned int to = from + nilfs_rec_len_from_disk(de->rec_len);
455 	struct address_space *mapping = page->mapping;
456 	int err;
457 
458 	lock_page(page);
459 	err = nilfs_prepare_chunk(page, from, to);
460 	BUG_ON(err);
461 	de->inode = cpu_to_le64(inode->i_ino);
462 	nilfs_set_de_type(de, inode);
463 	nilfs_commit_chunk(page, mapping, from, to);
464 	nilfs_put_page(page);
465 	dir->i_mtime = inode_set_ctime_current(dir);
466 }
467 
468 /*
469  *	Parent is locked.
470  */
471 int nilfs_add_link(struct dentry *dentry, struct inode *inode)
472 {
473 	struct inode *dir = d_inode(dentry->d_parent);
474 	const unsigned char *name = dentry->d_name.name;
475 	int namelen = dentry->d_name.len;
476 	unsigned int chunk_size = nilfs_chunk_size(dir);
477 	unsigned int reclen = NILFS_DIR_REC_LEN(namelen);
478 	unsigned short rec_len, name_len;
479 	struct page *page = NULL;
480 	struct nilfs_dir_entry *de;
481 	unsigned long npages = dir_pages(dir);
482 	unsigned long n;
483 	char *kaddr;
484 	unsigned int from, to;
485 	int err;
486 
487 	/*
488 	 * We take care of directory expansion in the same loop.
489 	 * This code plays outside i_size, so it locks the page
490 	 * to protect that region.
491 	 */
492 	for (n = 0; n <= npages; n++) {
493 		char *dir_end;
494 
495 		kaddr = nilfs_get_page(dir, n, &page);
496 		err = PTR_ERR(kaddr);
497 		if (IS_ERR(kaddr))
498 			goto out;
499 		lock_page(page);
500 		dir_end = kaddr + nilfs_last_byte(dir, n);
501 		de = (struct nilfs_dir_entry *)kaddr;
502 		kaddr += PAGE_SIZE - reclen;
503 		while ((char *)de <= kaddr) {
504 			if ((char *)de == dir_end) {
505 				/* We hit i_size */
506 				name_len = 0;
507 				rec_len = chunk_size;
508 				de->rec_len = nilfs_rec_len_to_disk(chunk_size);
509 				de->inode = 0;
510 				goto got_it;
511 			}
512 			if (de->rec_len == 0) {
513 				nilfs_error(dir->i_sb,
514 					    "zero-length directory entry");
515 				err = -EIO;
516 				goto out_unlock;
517 			}
518 			err = -EEXIST;
519 			if (nilfs_match(namelen, name, de))
520 				goto out_unlock;
521 			name_len = NILFS_DIR_REC_LEN(de->name_len);
522 			rec_len = nilfs_rec_len_from_disk(de->rec_len);
523 			if (!de->inode && rec_len >= reclen)
524 				goto got_it;
525 			if (rec_len >= name_len + reclen)
526 				goto got_it;
527 			de = (struct nilfs_dir_entry *)((char *)de + rec_len);
528 		}
529 		unlock_page(page);
530 		nilfs_put_page(page);
531 	}
532 	BUG();
533 	return -EINVAL;
534 
535 got_it:
536 	from = (char *)de - (char *)page_address(page);
537 	to = from + rec_len;
538 	err = nilfs_prepare_chunk(page, from, to);
539 	if (err)
540 		goto out_unlock;
541 	if (de->inode) {
542 		struct nilfs_dir_entry *de1;
543 
544 		de1 = (struct nilfs_dir_entry *)((char *)de + name_len);
545 		de1->rec_len = nilfs_rec_len_to_disk(rec_len - name_len);
546 		de->rec_len = nilfs_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_le64(inode->i_ino);
552 	nilfs_set_de_type(de, inode);
553 	nilfs_commit_chunk(page, page->mapping, from, to);
554 	dir->i_mtime = inode_set_ctime_current(dir);
555 	nilfs_mark_inode_dirty(dir);
556 	/* OFFSET_CACHE */
557 out_put:
558 	nilfs_put_page(page);
559 out:
560 	return err;
561 out_unlock:
562 	unlock_page(page);
563 	goto out_put;
564 }
565 
566 /*
567  * nilfs_delete_entry deletes a directory entry by merging it with the
568  * previous entry. Page is up-to-date. Releases the page.
569  */
570 int nilfs_delete_entry(struct nilfs_dir_entry *dir, struct page *page)
571 {
572 	struct address_space *mapping = page->mapping;
573 	struct inode *inode = mapping->host;
574 	char *kaddr = page_address(page);
575 	unsigned int from, to;
576 	struct nilfs_dir_entry *de, *pde = NULL;
577 	int err;
578 
579 	from = ((char *)dir - kaddr) & ~(nilfs_chunk_size(inode) - 1);
580 	to = ((char *)dir - kaddr) + nilfs_rec_len_from_disk(dir->rec_len);
581 	de = (struct nilfs_dir_entry *)(kaddr + from);
582 
583 	while ((char *)de < (char *)dir) {
584 		if (de->rec_len == 0) {
585 			nilfs_error(inode->i_sb,
586 				    "zero-length directory entry");
587 			err = -EIO;
588 			goto out;
589 		}
590 		pde = de;
591 		de = nilfs_next_entry(de);
592 	}
593 	if (pde)
594 		from = (char *)pde - (char *)page_address(page);
595 	lock_page(page);
596 	err = nilfs_prepare_chunk(page, from, to);
597 	BUG_ON(err);
598 	if (pde)
599 		pde->rec_len = nilfs_rec_len_to_disk(to - from);
600 	dir->inode = 0;
601 	nilfs_commit_chunk(page, mapping, from, to);
602 	inode->i_mtime = inode_set_ctime_current(inode);
603 out:
604 	nilfs_put_page(page);
605 	return err;
606 }
607 
608 /*
609  * Set the first fragment of directory.
610  */
611 int nilfs_make_empty(struct inode *inode, struct inode *parent)
612 {
613 	struct address_space *mapping = inode->i_mapping;
614 	struct page *page = grab_cache_page(mapping, 0);
615 	unsigned int chunk_size = nilfs_chunk_size(inode);
616 	struct nilfs_dir_entry *de;
617 	int err;
618 	void *kaddr;
619 
620 	if (!page)
621 		return -ENOMEM;
622 
623 	err = nilfs_prepare_chunk(page, 0, chunk_size);
624 	if (unlikely(err)) {
625 		unlock_page(page);
626 		goto fail;
627 	}
628 	kaddr = kmap_atomic(page);
629 	memset(kaddr, 0, chunk_size);
630 	de = (struct nilfs_dir_entry *)kaddr;
631 	de->name_len = 1;
632 	de->rec_len = nilfs_rec_len_to_disk(NILFS_DIR_REC_LEN(1));
633 	memcpy(de->name, ".\0\0", 4);
634 	de->inode = cpu_to_le64(inode->i_ino);
635 	nilfs_set_de_type(de, inode);
636 
637 	de = (struct nilfs_dir_entry *)(kaddr + NILFS_DIR_REC_LEN(1));
638 	de->name_len = 2;
639 	de->rec_len = nilfs_rec_len_to_disk(chunk_size - NILFS_DIR_REC_LEN(1));
640 	de->inode = cpu_to_le64(parent->i_ino);
641 	memcpy(de->name, "..\0", 4);
642 	nilfs_set_de_type(de, inode);
643 	kunmap_atomic(kaddr);
644 	nilfs_commit_chunk(page, mapping, 0, chunk_size);
645 fail:
646 	put_page(page);
647 	return err;
648 }
649 
650 /*
651  * routine to check that the specified directory is empty (for rmdir)
652  */
653 int nilfs_empty_dir(struct inode *inode)
654 {
655 	struct page *page = NULL;
656 	unsigned long i, npages = dir_pages(inode);
657 
658 	for (i = 0; i < npages; i++) {
659 		char *kaddr;
660 		struct nilfs_dir_entry *de;
661 
662 		kaddr = nilfs_get_page(inode, i, &page);
663 		if (IS_ERR(kaddr))
664 			return 0;
665 
666 		de = (struct nilfs_dir_entry *)kaddr;
667 		kaddr += nilfs_last_byte(inode, i) - NILFS_DIR_REC_LEN(1);
668 
669 		while ((char *)de <= kaddr) {
670 			if (de->rec_len == 0) {
671 				nilfs_error(inode->i_sb,
672 					    "zero-length directory entry (kaddr=%p, de=%p)",
673 					    kaddr, de);
674 				goto not_empty;
675 			}
676 			if (de->inode != 0) {
677 				/* check for . and .. */
678 				if (de->name[0] != '.')
679 					goto not_empty;
680 				if (de->name_len > 2)
681 					goto not_empty;
682 				if (de->name_len < 2) {
683 					if (de->inode !=
684 					    cpu_to_le64(inode->i_ino))
685 						goto not_empty;
686 				} else if (de->name[1] != '.')
687 					goto not_empty;
688 			}
689 			de = nilfs_next_entry(de);
690 		}
691 		nilfs_put_page(page);
692 	}
693 	return 1;
694 
695 not_empty:
696 	nilfs_put_page(page);
697 	return 0;
698 }
699 
700 const struct file_operations nilfs_dir_operations = {
701 	.llseek		= generic_file_llseek,
702 	.read		= generic_read_dir,
703 	.iterate_shared	= nilfs_readdir,
704 	.unlocked_ioctl	= nilfs_ioctl,
705 #ifdef CONFIG_COMPAT
706 	.compat_ioctl	= nilfs_compat_ioctl,
707 #endif	/* CONFIG_COMPAT */
708 	.fsync		= nilfs_sync_file,
709 
710 };
711