blob: d14e3b94d81fb4f58376dfa0a08b09f769fa136d [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
Christoph Hellwigf4e420d2010-06-04 11:29:56 +020083static int nilfs_prepare_chunk(struct page *page, unsigned from, unsigned to)
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -070084{
85 loff_t pos = page_offset(page) + from;
Christoph Hellwig6e1db882010-06-04 11:29:57 +020086 return __block_write_begin(page, pos, to - from, nilfs_get_block);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -070087}
88
Jiro SEKIBA2093abf2009-11-27 19:41:11 +090089static void nilfs_commit_chunk(struct page *page,
90 struct address_space *mapping,
91 unsigned from, unsigned to)
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -070092{
93 struct inode *dir = mapping->host;
94 struct nilfs_sb_info *sbi = NILFS_SB(dir->i_sb);
95 loff_t pos = page_offset(page) + from;
96 unsigned len = to - from;
97 unsigned nr_dirty, copied;
98 int err;
99
100 nr_dirty = nilfs_page_count_clean_buffers(page, from, to);
101 copied = block_write_end(NULL, mapping, pos, len, len, page, NULL);
Jiro SEKIBA58d55472009-11-27 19:41:12 +0900102 if (pos + copied > dir->i_size)
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700103 i_size_write(dir, pos + copied);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700104 if (IS_DIRSYNC(dir))
105 nilfs_set_transaction_flag(NILFS_TI_SYNC);
106 err = nilfs_set_file_dirty(sbi, dir, nr_dirty);
Jiro SEKIBA2093abf2009-11-27 19:41:11 +0900107 WARN_ON(err); /* do not happen */
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700108 unlock_page(page);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700109}
110
111static void nilfs_check_page(struct page *page)
112{
113 struct inode *dir = page->mapping->host;
114 struct super_block *sb = dir->i_sb;
115 unsigned chunk_size = nilfs_chunk_size(dir);
116 char *kaddr = page_address(page);
117 unsigned offs, rec_len;
118 unsigned limit = PAGE_CACHE_SIZE;
119 struct nilfs_dir_entry *p;
120 char *error;
121
122 if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
123 limit = dir->i_size & ~PAGE_CACHE_MASK;
124 if (limit & (chunk_size - 1))
125 goto Ebadsize;
126 if (!limit)
127 goto out;
128 }
129 for (offs = 0; offs <= limit - NILFS_DIR_REC_LEN(1); offs += rec_len) {
130 p = (struct nilfs_dir_entry *)(kaddr + offs);
131 rec_len = le16_to_cpu(p->rec_len);
132
133 if (rec_len < NILFS_DIR_REC_LEN(1))
134 goto Eshort;
135 if (rec_len & 3)
136 goto Ealign;
137 if (rec_len < NILFS_DIR_REC_LEN(p->name_len))
138 goto Enamelen;
139 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
140 goto Espan;
141 }
142 if (offs != limit)
143 goto Eend;
144out:
145 SetPageChecked(page);
146 return;
147
148 /* Too bad, we had an error */
149
150Ebadsize:
151 nilfs_error(sb, "nilfs_check_page",
152 "size of directory #%lu is not a multiple of chunk size",
153 dir->i_ino
154 );
155 goto fail;
156Eshort:
157 error = "rec_len is smaller than minimal";
158 goto bad_entry;
159Ealign:
160 error = "unaligned directory entry";
161 goto bad_entry;
162Enamelen:
163 error = "rec_len is too small for name_len";
164 goto bad_entry;
165Espan:
166 error = "directory entry across blocks";
167bad_entry:
168 nilfs_error(sb, "nilfs_check_page", "bad entry in directory #%lu: %s - "
169 "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
170 dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT)+offs,
171 (unsigned long) le64_to_cpu(p->inode),
172 rec_len, p->name_len);
173 goto fail;
174Eend:
175 p = (struct nilfs_dir_entry *)(kaddr + offs);
176 nilfs_error(sb, "nilfs_check_page",
177 "entry in directory #%lu spans the page boundary"
178 "offset=%lu, inode=%lu",
179 dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs,
180 (unsigned long) le64_to_cpu(p->inode));
181fail:
182 SetPageChecked(page);
183 SetPageError(page);
184}
185
186static struct page *nilfs_get_page(struct inode *dir, unsigned long n)
187{
188 struct address_space *mapping = dir->i_mapping;
189 struct page *page = read_cache_page(mapping, n,
190 (filler_t *)mapping->a_ops->readpage, NULL);
191 if (!IS_ERR(page)) {
192 wait_on_page_locked(page);
193 kmap(page);
194 if (!PageUptodate(page))
195 goto fail;
196 if (!PageChecked(page))
197 nilfs_check_page(page);
198 if (PageError(page))
199 goto fail;
200 }
201 return page;
202
203fail:
204 nilfs_put_page(page);
205 return ERR_PTR(-EIO);
206}
207
208/*
209 * NOTE! unlike strncmp, nilfs_match returns 1 for success, 0 for failure.
210 *
211 * len <= NILFS_NAME_LEN and de != NULL are guaranteed by caller.
212 */
213static int
Al Viro072f98b2010-01-31 21:03:58 -0500214nilfs_match(int len, const unsigned char *name, struct nilfs_dir_entry *de)
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700215{
216 if (len != de->name_len)
217 return 0;
218 if (!de->inode)
219 return 0;
220 return !memcmp(name, de->name, len);
221}
222
223/*
224 * p is at least 6 bytes before the end of page
225 */
226static struct nilfs_dir_entry *nilfs_next_entry(struct nilfs_dir_entry *p)
227{
228 return (struct nilfs_dir_entry *)((char *)p + le16_to_cpu(p->rec_len));
229}
230
231static unsigned char
232nilfs_filetype_table[NILFS_FT_MAX] = {
233 [NILFS_FT_UNKNOWN] = DT_UNKNOWN,
234 [NILFS_FT_REG_FILE] = DT_REG,
235 [NILFS_FT_DIR] = DT_DIR,
236 [NILFS_FT_CHRDEV] = DT_CHR,
237 [NILFS_FT_BLKDEV] = DT_BLK,
238 [NILFS_FT_FIFO] = DT_FIFO,
239 [NILFS_FT_SOCK] = DT_SOCK,
240 [NILFS_FT_SYMLINK] = DT_LNK,
241};
242
243#define S_SHIFT 12
244static unsigned char
245nilfs_type_by_mode[S_IFMT >> S_SHIFT] = {
246 [S_IFREG >> S_SHIFT] = NILFS_FT_REG_FILE,
247 [S_IFDIR >> S_SHIFT] = NILFS_FT_DIR,
248 [S_IFCHR >> S_SHIFT] = NILFS_FT_CHRDEV,
249 [S_IFBLK >> S_SHIFT] = NILFS_FT_BLKDEV,
250 [S_IFIFO >> S_SHIFT] = NILFS_FT_FIFO,
251 [S_IFSOCK >> S_SHIFT] = NILFS_FT_SOCK,
252 [S_IFLNK >> S_SHIFT] = NILFS_FT_SYMLINK,
253};
254
255static void nilfs_set_de_type(struct nilfs_dir_entry *de, struct inode *inode)
256{
257 mode_t mode = inode->i_mode;
258
259 de->file_type = nilfs_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
260}
261
262static int nilfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
263{
264 loff_t pos = filp->f_pos;
265 struct inode *inode = filp->f_dentry->d_inode;
266 struct super_block *sb = inode->i_sb;
267 unsigned int offset = pos & ~PAGE_CACHE_MASK;
268 unsigned long n = pos >> PAGE_CACHE_SHIFT;
269 unsigned long npages = dir_pages(inode);
270/* unsigned chunk_mask = ~(nilfs_chunk_size(inode)-1); */
271 unsigned char *types = NULL;
272 int ret;
273
274 if (pos > inode->i_size - NILFS_DIR_REC_LEN(1))
275 goto success;
276
277 types = nilfs_filetype_table;
278
279 for ( ; n < npages; n++, offset = 0) {
280 char *kaddr, *limit;
281 struct nilfs_dir_entry *de;
282 struct page *page = nilfs_get_page(inode, n);
283
284 if (IS_ERR(page)) {
285 nilfs_error(sb, __func__, "bad page in #%lu",
286 inode->i_ino);
287 filp->f_pos += PAGE_CACHE_SIZE - offset;
288 ret = -EIO;
289 goto done;
290 }
291 kaddr = page_address(page);
292 de = (struct nilfs_dir_entry *)(kaddr + offset);
293 limit = kaddr + nilfs_last_byte(inode, n) -
294 NILFS_DIR_REC_LEN(1);
295 for ( ; (char *)de <= limit; de = nilfs_next_entry(de)) {
296 if (de->rec_len == 0) {
297 nilfs_error(sb, __func__,
298 "zero-length directory entry");
299 ret = -EIO;
300 nilfs_put_page(page);
301 goto done;
302 }
303 if (de->inode) {
304 int over;
305 unsigned char d_type = DT_UNKNOWN;
306
307 if (types && de->file_type < NILFS_FT_MAX)
308 d_type = types[de->file_type];
309
310 offset = (char *)de - kaddr;
311 over = filldir(dirent, de->name, de->name_len,
312 (n<<PAGE_CACHE_SHIFT) | offset,
313 le64_to_cpu(de->inode), d_type);
314 if (over) {
315 nilfs_put_page(page);
316 goto success;
317 }
318 }
319 filp->f_pos += le16_to_cpu(de->rec_len);
320 }
321 nilfs_put_page(page);
322 }
323
324success:
325 ret = 0;
326done:
327 return ret;
328}
329
330/*
331 * nilfs_find_entry()
332 *
333 * finds an entry in the specified directory with the wanted name. It
334 * returns the page in which the entry was found, and the entry itself
335 * (as a parameter - res_dir). Page is returned mapped and unlocked.
336 * Entry is guaranteed to be valid.
337 */
338struct nilfs_dir_entry *
Al Viro03190032010-01-31 21:02:09 -0500339nilfs_find_entry(struct inode *dir, const struct qstr *qstr,
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700340 struct page **res_page)
341{
Al Viro03190032010-01-31 21:02:09 -0500342 const unsigned char *name = qstr->name;
343 int namelen = qstr->len;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700344 unsigned reclen = NILFS_DIR_REC_LEN(namelen);
345 unsigned long start, n;
346 unsigned long npages = dir_pages(dir);
347 struct page *page = NULL;
348 struct nilfs_inode_info *ei = NILFS_I(dir);
349 struct nilfs_dir_entry *de;
350
351 if (npages == 0)
352 goto out;
353
354 /* OFFSET_CACHE */
355 *res_page = NULL;
356
357 start = ei->i_dir_start_lookup;
358 if (start >= npages)
359 start = 0;
360 n = start;
361 do {
362 char *kaddr;
363 page = nilfs_get_page(dir, n);
364 if (!IS_ERR(page)) {
365 kaddr = page_address(page);
366 de = (struct nilfs_dir_entry *)kaddr;
367 kaddr += nilfs_last_byte(dir, n) - reclen;
368 while ((char *) de <= kaddr) {
369 if (de->rec_len == 0) {
370 nilfs_error(dir->i_sb, __func__,
371 "zero-length directory entry");
372 nilfs_put_page(page);
373 goto out;
374 }
375 if (nilfs_match(namelen, name, de))
376 goto found;
377 de = nilfs_next_entry(de);
378 }
379 nilfs_put_page(page);
380 }
381 if (++n >= npages)
382 n = 0;
383 /* next page is past the blocks we've got */
384 if (unlikely(n > (dir->i_blocks >> (PAGE_CACHE_SHIFT - 9)))) {
385 nilfs_error(dir->i_sb, __func__,
Ryusuke Konishi16215622010-03-14 03:17:45 +0900386 "dir %lu size %lld exceeds block count %llu",
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700387 dir->i_ino, dir->i_size,
388 (unsigned long long)dir->i_blocks);
389 goto out;
390 }
391 } while (n != start);
392out:
393 return NULL;
394
395found:
396 *res_page = page;
397 ei->i_dir_start_lookup = n;
398 return de;
399}
400
401struct nilfs_dir_entry *nilfs_dotdot(struct inode *dir, struct page **p)
402{
403 struct page *page = nilfs_get_page(dir, 0);
404 struct nilfs_dir_entry *de = NULL;
405
406 if (!IS_ERR(page)) {
407 de = nilfs_next_entry(
408 (struct nilfs_dir_entry *)page_address(page));
409 *p = page;
410 }
411 return de;
412}
413
Al Viro03190032010-01-31 21:02:09 -0500414ino_t nilfs_inode_by_name(struct inode *dir, const struct qstr *qstr)
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700415{
416 ino_t res = 0;
417 struct nilfs_dir_entry *de;
418 struct page *page;
419
Al Viro03190032010-01-31 21:02:09 -0500420 de = nilfs_find_entry(dir, qstr, &page);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700421 if (de) {
422 res = le64_to_cpu(de->inode);
423 kunmap(page);
424 page_cache_release(page);
425 }
426 return res;
427}
428
429/* Releases the page */
430void nilfs_set_link(struct inode *dir, struct nilfs_dir_entry *de,
431 struct page *page, struct inode *inode)
432{
433 unsigned from = (char *) de - (char *) page_address(page);
434 unsigned to = from + le16_to_cpu(de->rec_len);
435 struct address_space *mapping = page->mapping;
436 int err;
437
438 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200439 err = nilfs_prepare_chunk(page, from, to);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700440 BUG_ON(err);
441 de->inode = cpu_to_le64(inode->i_ino);
442 nilfs_set_de_type(de, inode);
Jiro SEKIBA2093abf2009-11-27 19:41:11 +0900443 nilfs_commit_chunk(page, mapping, from, to);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700444 nilfs_put_page(page);
445 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
446/* NILFS_I(dir)->i_flags &= ~NILFS_BTREE_FL; */
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700447}
448
449/*
450 * Parent is locked.
451 */
452int nilfs_add_link(struct dentry *dentry, struct inode *inode)
453{
454 struct inode *dir = dentry->d_parent->d_inode;
Al Viro072f98b2010-01-31 21:03:58 -0500455 const unsigned char *name = dentry->d_name.name;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700456 int namelen = dentry->d_name.len;
457 unsigned chunk_size = nilfs_chunk_size(dir);
458 unsigned reclen = NILFS_DIR_REC_LEN(namelen);
459 unsigned short rec_len, name_len;
460 struct page *page = NULL;
461 struct nilfs_dir_entry *de;
462 unsigned long npages = dir_pages(dir);
463 unsigned long n;
464 char *kaddr;
465 unsigned from, to;
466 int err;
467
468 /*
469 * We take care of directory expansion in the same loop.
470 * This code plays outside i_size, so it locks the page
471 * to protect that region.
472 */
473 for (n = 0; n <= npages; n++) {
474 char *dir_end;
475
476 page = nilfs_get_page(dir, n);
477 err = PTR_ERR(page);
478 if (IS_ERR(page))
479 goto out;
480 lock_page(page);
481 kaddr = page_address(page);
482 dir_end = kaddr + nilfs_last_byte(dir, n);
483 de = (struct nilfs_dir_entry *)kaddr;
484 kaddr += PAGE_CACHE_SIZE - reclen;
485 while ((char *)de <= kaddr) {
486 if ((char *)de == dir_end) {
487 /* We hit i_size */
488 name_len = 0;
489 rec_len = chunk_size;
490 de->rec_len = cpu_to_le16(chunk_size);
491 de->inode = 0;
492 goto got_it;
493 }
494 if (de->rec_len == 0) {
495 nilfs_error(dir->i_sb, __func__,
496 "zero-length directory entry");
497 err = -EIO;
498 goto out_unlock;
499 }
500 err = -EEXIST;
501 if (nilfs_match(namelen, name, de))
502 goto out_unlock;
503 name_len = NILFS_DIR_REC_LEN(de->name_len);
504 rec_len = le16_to_cpu(de->rec_len);
505 if (!de->inode && rec_len >= reclen)
506 goto got_it;
507 if (rec_len >= name_len + reclen)
508 goto got_it;
509 de = (struct nilfs_dir_entry *)((char *)de + rec_len);
510 }
511 unlock_page(page);
512 nilfs_put_page(page);
513 }
514 BUG();
515 return -EINVAL;
516
517got_it:
518 from = (char *)de - (char *)page_address(page);
519 to = from + rec_len;
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200520 err = nilfs_prepare_chunk(page, from, to);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700521 if (err)
522 goto out_unlock;
523 if (de->inode) {
524 struct nilfs_dir_entry *de1;
525
526 de1 = (struct nilfs_dir_entry *)((char *)de + name_len);
527 de1->rec_len = cpu_to_le16(rec_len - name_len);
528 de->rec_len = cpu_to_le16(name_len);
529 de = de1;
530 }
531 de->name_len = namelen;
532 memcpy(de->name, name, namelen);
533 de->inode = cpu_to_le64(inode->i_ino);
534 nilfs_set_de_type(de, inode);
Jiro SEKIBA2093abf2009-11-27 19:41:11 +0900535 nilfs_commit_chunk(page, page->mapping, from, to);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700536 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
537/* NILFS_I(dir)->i_flags &= ~NILFS_BTREE_FL; */
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900538 nilfs_mark_inode_dirty(dir);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700539 /* OFFSET_CACHE */
540out_put:
541 nilfs_put_page(page);
542out:
543 return err;
544out_unlock:
545 unlock_page(page);
546 goto out_put;
547}
548
549/*
550 * nilfs_delete_entry deletes a directory entry by merging it with the
551 * previous entry. Page is up-to-date. Releases the page.
552 */
553int nilfs_delete_entry(struct nilfs_dir_entry *dir, struct page *page)
554{
555 struct address_space *mapping = page->mapping;
556 struct inode *inode = mapping->host;
557 char *kaddr = page_address(page);
558 unsigned from = ((char *)dir - kaddr) & ~(nilfs_chunk_size(inode) - 1);
559 unsigned to = ((char *)dir - kaddr) + le16_to_cpu(dir->rec_len);
560 struct nilfs_dir_entry *pde = NULL;
561 struct nilfs_dir_entry *de = (struct nilfs_dir_entry *)(kaddr + from);
562 int err;
563
564 while ((char *)de < (char *)dir) {
565 if (de->rec_len == 0) {
566 nilfs_error(inode->i_sb, __func__,
567 "zero-length directory entry");
568 err = -EIO;
569 goto out;
570 }
571 pde = de;
572 de = nilfs_next_entry(de);
573 }
574 if (pde)
575 from = (char *)pde - (char *)page_address(page);
576 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200577 err = nilfs_prepare_chunk(page, from, to);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700578 BUG_ON(err);
579 if (pde)
580 pde->rec_len = cpu_to_le16(to - from);
581 dir->inode = 0;
Jiro SEKIBA2093abf2009-11-27 19:41:11 +0900582 nilfs_commit_chunk(page, mapping, from, to);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700583 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
584/* NILFS_I(inode)->i_flags &= ~NILFS_BTREE_FL; */
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700585out:
586 nilfs_put_page(page);
587 return err;
588}
589
590/*
591 * Set the first fragment of directory.
592 */
593int nilfs_make_empty(struct inode *inode, struct inode *parent)
594{
595 struct address_space *mapping = inode->i_mapping;
596 struct page *page = grab_cache_page(mapping, 0);
597 unsigned chunk_size = nilfs_chunk_size(inode);
598 struct nilfs_dir_entry *de;
599 int err;
600 void *kaddr;
601
602 if (!page)
603 return -ENOMEM;
604
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200605 err = nilfs_prepare_chunk(page, 0, chunk_size);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700606 if (unlikely(err)) {
607 unlock_page(page);
608 goto fail;
609 }
610 kaddr = kmap_atomic(page, KM_USER0);
611 memset(kaddr, 0, chunk_size);
612 de = (struct nilfs_dir_entry *)kaddr;
613 de->name_len = 1;
614 de->rec_len = cpu_to_le16(NILFS_DIR_REC_LEN(1));
615 memcpy(de->name, ".\0\0", 4);
616 de->inode = cpu_to_le64(inode->i_ino);
617 nilfs_set_de_type(de, inode);
618
619 de = (struct nilfs_dir_entry *)(kaddr + NILFS_DIR_REC_LEN(1));
620 de->name_len = 2;
621 de->rec_len = cpu_to_le16(chunk_size - NILFS_DIR_REC_LEN(1));
622 de->inode = cpu_to_le64(parent->i_ino);
623 memcpy(de->name, "..\0", 4);
624 nilfs_set_de_type(de, inode);
625 kunmap_atomic(kaddr, KM_USER0);
Jiro SEKIBA2093abf2009-11-27 19:41:11 +0900626 nilfs_commit_chunk(page, mapping, 0, chunk_size);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700627fail:
628 page_cache_release(page);
629 return err;
630}
631
632/*
633 * routine to check that the specified directory is empty (for rmdir)
634 */
635int nilfs_empty_dir(struct inode *inode)
636{
637 struct page *page = NULL;
638 unsigned long i, npages = dir_pages(inode);
639
640 for (i = 0; i < npages; i++) {
641 char *kaddr;
642 struct nilfs_dir_entry *de;
643
644 page = nilfs_get_page(inode, i);
645 if (IS_ERR(page))
646 continue;
647
648 kaddr = page_address(page);
649 de = (struct nilfs_dir_entry *)kaddr;
650 kaddr += nilfs_last_byte(inode, i) - NILFS_DIR_REC_LEN(1);
651
652 while ((char *)de <= kaddr) {
653 if (de->rec_len == 0) {
654 nilfs_error(inode->i_sb, __func__,
655 "zero-length directory entry "
656 "(kaddr=%p, de=%p)\n", kaddr, de);
657 goto not_empty;
658 }
659 if (de->inode != 0) {
660 /* check for . and .. */
661 if (de->name[0] != '.')
662 goto not_empty;
663 if (de->name_len > 2)
664 goto not_empty;
665 if (de->name_len < 2) {
666 if (de->inode !=
667 cpu_to_le64(inode->i_ino))
668 goto not_empty;
669 } else if (de->name[1] != '.')
670 goto not_empty;
671 }
672 de = nilfs_next_entry(de);
673 }
674 nilfs_put_page(page);
675 }
676 return 1;
677
678not_empty:
679 nilfs_put_page(page);
680 return 0;
681}
682
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700683const struct file_operations nilfs_dir_operations = {
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700684 .llseek = generic_file_llseek,
685 .read = generic_read_dir,
686 .readdir = nilfs_readdir,
Ryusuke Konishi7a946192009-04-06 19:01:53 -0700687 .unlocked_ioctl = nilfs_ioctl,
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700688#ifdef CONFIG_COMPAT
Ryusuke Konishi8082d362009-04-06 19:01:53 -0700689 .compat_ioctl = nilfs_ioctl,
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700690#endif /* CONFIG_COMPAT */
691 .fsync = nilfs_sync_file,
692
693};