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