blob: 9d45773b79e63f9d7be433ca604caef7fc518cea [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;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -070094 loff_t pos = page_offset(page) + from;
95 unsigned len = to - from;
96 unsigned nr_dirty, copied;
97 int err;
98
99 nr_dirty = nilfs_page_count_clean_buffers(page, from, to);
100 copied = block_write_end(NULL, mapping, pos, len, len, page, NULL);
Jiro SEKIBA58d55472009-11-27 19:41:12 +0900101 if (pos + copied > dir->i_size)
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700102 i_size_write(dir, pos + copied);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700103 if (IS_DIRSYNC(dir))
104 nilfs_set_transaction_flag(NILFS_TI_SYNC);
Ryusuke Konishibcbc8c62010-12-27 00:05:49 +0900105 err = nilfs_set_file_dirty(dir, nr_dirty);
Jiro SEKIBA2093abf2009-11-27 19:41:11 +0900106 WARN_ON(err); /* do not happen */
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700107 unlock_page(page);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700108}
109
110static void nilfs_check_page(struct page *page)
111{
112 struct inode *dir = page->mapping->host;
113 struct super_block *sb = dir->i_sb;
114 unsigned chunk_size = nilfs_chunk_size(dir);
115 char *kaddr = page_address(page);
116 unsigned offs, rec_len;
117 unsigned limit = PAGE_CACHE_SIZE;
118 struct nilfs_dir_entry *p;
119 char *error;
120
121 if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
122 limit = dir->i_size & ~PAGE_CACHE_MASK;
123 if (limit & (chunk_size - 1))
124 goto Ebadsize;
125 if (!limit)
126 goto out;
127 }
128 for (offs = 0; offs <= limit - NILFS_DIR_REC_LEN(1); offs += rec_len) {
129 p = (struct nilfs_dir_entry *)(kaddr + offs);
Ryusuke Konishi6cda9fa2010-07-25 20:39:03 +0900130 rec_len = nilfs_rec_len_from_disk(p->rec_len);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700131
132 if (rec_len < NILFS_DIR_REC_LEN(1))
133 goto Eshort;
134 if (rec_len & 3)
135 goto Ealign;
136 if (rec_len < NILFS_DIR_REC_LEN(p->name_len))
137 goto Enamelen;
138 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
139 goto Espan;
140 }
141 if (offs != limit)
142 goto Eend;
143out:
144 SetPageChecked(page);
145 return;
146
147 /* Too bad, we had an error */
148
149Ebadsize:
150 nilfs_error(sb, "nilfs_check_page",
151 "size of directory #%lu is not a multiple of chunk size",
152 dir->i_ino
153 );
154 goto fail;
155Eshort:
156 error = "rec_len is smaller than minimal";
157 goto bad_entry;
158Ealign:
159 error = "unaligned directory entry";
160 goto bad_entry;
161Enamelen:
162 error = "rec_len is too small for name_len";
163 goto bad_entry;
164Espan:
165 error = "directory entry across blocks";
166bad_entry:
167 nilfs_error(sb, "nilfs_check_page", "bad entry in directory #%lu: %s - "
168 "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
169 dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT)+offs,
170 (unsigned long) le64_to_cpu(p->inode),
171 rec_len, p->name_len);
172 goto fail;
173Eend:
174 p = (struct nilfs_dir_entry *)(kaddr + offs);
175 nilfs_error(sb, "nilfs_check_page",
176 "entry in directory #%lu spans the page boundary"
177 "offset=%lu, inode=%lu",
178 dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs,
179 (unsigned long) le64_to_cpu(p->inode));
180fail:
181 SetPageChecked(page);
182 SetPageError(page);
183}
184
185static struct page *nilfs_get_page(struct inode *dir, unsigned long n)
186{
187 struct address_space *mapping = dir->i_mapping;
Ryusuke Konishic28e69d2010-07-24 17:09:10 +0900188 struct page *page = read_mapping_page(mapping, n, NULL);
189
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700190 if (!IS_ERR(page)) {
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700191 kmap(page);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700192 if (!PageChecked(page))
193 nilfs_check_page(page);
194 if (PageError(page))
195 goto fail;
196 }
197 return page;
198
199fail:
200 nilfs_put_page(page);
201 return ERR_PTR(-EIO);
202}
203
204/*
205 * NOTE! unlike strncmp, nilfs_match returns 1 for success, 0 for failure.
206 *
207 * len <= NILFS_NAME_LEN and de != NULL are guaranteed by caller.
208 */
209static int
Al Viro072f98b2010-01-31 21:03:58 -0500210nilfs_match(int len, const unsigned char *name, struct nilfs_dir_entry *de)
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700211{
212 if (len != de->name_len)
213 return 0;
214 if (!de->inode)
215 return 0;
216 return !memcmp(name, de->name, len);
217}
218
219/*
220 * p is at least 6 bytes before the end of page
221 */
222static struct nilfs_dir_entry *nilfs_next_entry(struct nilfs_dir_entry *p)
223{
Ryusuke Konishi6cda9fa2010-07-25 20:39:03 +0900224 return (struct nilfs_dir_entry *)((char *)p +
225 nilfs_rec_len_from_disk(p->rec_len));
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700226}
227
228static unsigned char
229nilfs_filetype_table[NILFS_FT_MAX] = {
230 [NILFS_FT_UNKNOWN] = DT_UNKNOWN,
231 [NILFS_FT_REG_FILE] = DT_REG,
232 [NILFS_FT_DIR] = DT_DIR,
233 [NILFS_FT_CHRDEV] = DT_CHR,
234 [NILFS_FT_BLKDEV] = DT_BLK,
235 [NILFS_FT_FIFO] = DT_FIFO,
236 [NILFS_FT_SOCK] = DT_SOCK,
237 [NILFS_FT_SYMLINK] = DT_LNK,
238};
239
240#define S_SHIFT 12
241static unsigned char
242nilfs_type_by_mode[S_IFMT >> S_SHIFT] = {
243 [S_IFREG >> S_SHIFT] = NILFS_FT_REG_FILE,
244 [S_IFDIR >> S_SHIFT] = NILFS_FT_DIR,
245 [S_IFCHR >> S_SHIFT] = NILFS_FT_CHRDEV,
246 [S_IFBLK >> S_SHIFT] = NILFS_FT_BLKDEV,
247 [S_IFIFO >> S_SHIFT] = NILFS_FT_FIFO,
248 [S_IFSOCK >> S_SHIFT] = NILFS_FT_SOCK,
249 [S_IFLNK >> S_SHIFT] = NILFS_FT_SYMLINK,
250};
251
252static void nilfs_set_de_type(struct nilfs_dir_entry *de, struct inode *inode)
253{
254 mode_t mode = inode->i_mode;
255
256 de->file_type = nilfs_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
257}
258
259static int nilfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
260{
261 loff_t pos = filp->f_pos;
262 struct inode *inode = filp->f_dentry->d_inode;
263 struct super_block *sb = inode->i_sb;
264 unsigned int offset = pos & ~PAGE_CACHE_MASK;
265 unsigned long n = pos >> PAGE_CACHE_SHIFT;
266 unsigned long npages = dir_pages(inode);
267/* unsigned chunk_mask = ~(nilfs_chunk_size(inode)-1); */
268 unsigned char *types = NULL;
269 int ret;
270
271 if (pos > inode->i_size - NILFS_DIR_REC_LEN(1))
272 goto success;
273
274 types = nilfs_filetype_table;
275
276 for ( ; n < npages; n++, offset = 0) {
277 char *kaddr, *limit;
278 struct nilfs_dir_entry *de;
279 struct page *page = nilfs_get_page(inode, n);
280
281 if (IS_ERR(page)) {
282 nilfs_error(sb, __func__, "bad page in #%lu",
283 inode->i_ino);
284 filp->f_pos += PAGE_CACHE_SIZE - offset;
285 ret = -EIO;
286 goto done;
287 }
288 kaddr = page_address(page);
289 de = (struct nilfs_dir_entry *)(kaddr + offset);
290 limit = kaddr + nilfs_last_byte(inode, n) -
291 NILFS_DIR_REC_LEN(1);
292 for ( ; (char *)de <= limit; de = nilfs_next_entry(de)) {
293 if (de->rec_len == 0) {
294 nilfs_error(sb, __func__,
295 "zero-length directory entry");
296 ret = -EIO;
297 nilfs_put_page(page);
298 goto done;
299 }
300 if (de->inode) {
301 int over;
302 unsigned char d_type = DT_UNKNOWN;
303
304 if (types && de->file_type < NILFS_FT_MAX)
305 d_type = types[de->file_type];
306
307 offset = (char *)de - kaddr;
308 over = filldir(dirent, de->name, de->name_len,
309 (n<<PAGE_CACHE_SHIFT) | offset,
310 le64_to_cpu(de->inode), d_type);
311 if (over) {
312 nilfs_put_page(page);
313 goto success;
314 }
315 }
Ryusuke Konishi6cda9fa2010-07-25 20:39:03 +0900316 filp->f_pos += nilfs_rec_len_from_disk(de->rec_len);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700317 }
318 nilfs_put_page(page);
319 }
320
321success:
322 ret = 0;
323done:
324 return ret;
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 */
335struct nilfs_dir_entry *
Al Viro03190032010-01-31 21:02:09 -0500336nilfs_find_entry(struct inode *dir, const struct qstr *qstr,
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700337 struct page **res_page)
338{
Al Viro03190032010-01-31 21:02:09 -0500339 const unsigned char *name = qstr->name;
340 int namelen = qstr->len;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700341 unsigned 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;
360 page = nilfs_get_page(dir, n);
361 if (!IS_ERR(page)) {
362 kaddr = page_address(page);
363 de = (struct nilfs_dir_entry *)kaddr;
364 kaddr += nilfs_last_byte(dir, n) - reclen;
365 while ((char *) de <= kaddr) {
366 if (de->rec_len == 0) {
367 nilfs_error(dir->i_sb, __func__,
368 "zero-length directory entry");
369 nilfs_put_page(page);
370 goto out;
371 }
372 if (nilfs_match(namelen, name, de))
373 goto found;
374 de = nilfs_next_entry(de);
375 }
376 nilfs_put_page(page);
377 }
378 if (++n >= npages)
379 n = 0;
380 /* next page is past the blocks we've got */
381 if (unlikely(n > (dir->i_blocks >> (PAGE_CACHE_SHIFT - 9)))) {
382 nilfs_error(dir->i_sb, __func__,
Ryusuke Konishi16215622010-03-14 03:17:45 +0900383 "dir %lu size %lld exceeds block count %llu",
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700384 dir->i_ino, dir->i_size,
385 (unsigned long long)dir->i_blocks);
386 goto out;
387 }
388 } while (n != start);
389out:
390 return NULL;
391
392found:
393 *res_page = page;
394 ei->i_dir_start_lookup = n;
395 return de;
396}
397
398struct nilfs_dir_entry *nilfs_dotdot(struct inode *dir, struct page **p)
399{
400 struct page *page = nilfs_get_page(dir, 0);
401 struct nilfs_dir_entry *de = NULL;
402
403 if (!IS_ERR(page)) {
404 de = nilfs_next_entry(
405 (struct nilfs_dir_entry *)page_address(page));
406 *p = page;
407 }
408 return de;
409}
410
Al Viro03190032010-01-31 21:02:09 -0500411ino_t nilfs_inode_by_name(struct inode *dir, const struct qstr *qstr)
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700412{
413 ino_t res = 0;
414 struct nilfs_dir_entry *de;
415 struct page *page;
416
Al Viro03190032010-01-31 21:02:09 -0500417 de = nilfs_find_entry(dir, qstr, &page);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700418 if (de) {
419 res = le64_to_cpu(de->inode);
420 kunmap(page);
421 page_cache_release(page);
422 }
423 return res;
424}
425
426/* Releases the page */
427void nilfs_set_link(struct inode *dir, struct nilfs_dir_entry *de,
428 struct page *page, struct inode *inode)
429{
430 unsigned from = (char *) de - (char *) page_address(page);
Ryusuke Konishi6cda9fa2010-07-25 20:39:03 +0900431 unsigned to = from + nilfs_rec_len_from_disk(de->rec_len);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700432 struct address_space *mapping = page->mapping;
433 int err;
434
435 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200436 err = nilfs_prepare_chunk(page, from, to);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700437 BUG_ON(err);
438 de->inode = cpu_to_le64(inode->i_ino);
439 nilfs_set_de_type(de, inode);
Jiro SEKIBA2093abf2009-11-27 19:41:11 +0900440 nilfs_commit_chunk(page, mapping, from, to);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700441 nilfs_put_page(page);
442 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
443/* NILFS_I(dir)->i_flags &= ~NILFS_BTREE_FL; */
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700444}
445
446/*
447 * Parent is locked.
448 */
449int nilfs_add_link(struct dentry *dentry, struct inode *inode)
450{
451 struct inode *dir = dentry->d_parent->d_inode;
Al Viro072f98b2010-01-31 21:03:58 -0500452 const unsigned char *name = dentry->d_name.name;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700453 int namelen = dentry->d_name.len;
454 unsigned chunk_size = nilfs_chunk_size(dir);
455 unsigned reclen = NILFS_DIR_REC_LEN(namelen);
456 unsigned short rec_len, name_len;
457 struct page *page = NULL;
458 struct nilfs_dir_entry *de;
459 unsigned long npages = dir_pages(dir);
460 unsigned long n;
461 char *kaddr;
462 unsigned from, to;
463 int err;
464
465 /*
466 * We take care of directory expansion in the same loop.
467 * This code plays outside i_size, so it locks the page
468 * to protect that region.
469 */
470 for (n = 0; n <= npages; n++) {
471 char *dir_end;
472
473 page = nilfs_get_page(dir, n);
474 err = PTR_ERR(page);
475 if (IS_ERR(page))
476 goto out;
477 lock_page(page);
478 kaddr = page_address(page);
479 dir_end = kaddr + nilfs_last_byte(dir, n);
480 de = (struct nilfs_dir_entry *)kaddr;
481 kaddr += PAGE_CACHE_SIZE - reclen;
482 while ((char *)de <= kaddr) {
483 if ((char *)de == dir_end) {
484 /* We hit i_size */
485 name_len = 0;
486 rec_len = chunk_size;
Ryusuke Konishi6cda9fa2010-07-25 20:39:03 +0900487 de->rec_len = nilfs_rec_len_to_disk(chunk_size);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700488 de->inode = 0;
489 goto got_it;
490 }
491 if (de->rec_len == 0) {
492 nilfs_error(dir->i_sb, __func__,
493 "zero-length directory entry");
494 err = -EIO;
495 goto out_unlock;
496 }
497 err = -EEXIST;
498 if (nilfs_match(namelen, name, de))
499 goto out_unlock;
500 name_len = NILFS_DIR_REC_LEN(de->name_len);
Ryusuke Konishi6cda9fa2010-07-25 20:39:03 +0900501 rec_len = nilfs_rec_len_from_disk(de->rec_len);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700502 if (!de->inode && rec_len >= reclen)
503 goto got_it;
504 if (rec_len >= name_len + reclen)
505 goto got_it;
506 de = (struct nilfs_dir_entry *)((char *)de + rec_len);
507 }
508 unlock_page(page);
509 nilfs_put_page(page);
510 }
511 BUG();
512 return -EINVAL;
513
514got_it:
515 from = (char *)de - (char *)page_address(page);
516 to = from + rec_len;
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200517 err = nilfs_prepare_chunk(page, from, to);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700518 if (err)
519 goto out_unlock;
520 if (de->inode) {
521 struct nilfs_dir_entry *de1;
522
523 de1 = (struct nilfs_dir_entry *)((char *)de + name_len);
Ryusuke Konishi6cda9fa2010-07-25 20:39:03 +0900524 de1->rec_len = nilfs_rec_len_to_disk(rec_len - name_len);
525 de->rec_len = nilfs_rec_len_to_disk(name_len);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700526 de = de1;
527 }
528 de->name_len = namelen;
529 memcpy(de->name, name, namelen);
530 de->inode = cpu_to_le64(inode->i_ino);
531 nilfs_set_de_type(de, inode);
Jiro SEKIBA2093abf2009-11-27 19:41:11 +0900532 nilfs_commit_chunk(page, page->mapping, from, to);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700533 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
534/* NILFS_I(dir)->i_flags &= ~NILFS_BTREE_FL; */
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900535 nilfs_mark_inode_dirty(dir);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700536 /* OFFSET_CACHE */
537out_put:
538 nilfs_put_page(page);
539out:
540 return err;
541out_unlock:
542 unlock_page(page);
543 goto out_put;
544}
545
546/*
547 * nilfs_delete_entry deletes a directory entry by merging it with the
548 * previous entry. Page is up-to-date. Releases the page.
549 */
550int nilfs_delete_entry(struct nilfs_dir_entry *dir, struct page *page)
551{
552 struct address_space *mapping = page->mapping;
553 struct inode *inode = mapping->host;
554 char *kaddr = page_address(page);
555 unsigned from = ((char *)dir - kaddr) & ~(nilfs_chunk_size(inode) - 1);
Ryusuke Konishi6cda9fa2010-07-25 20:39:03 +0900556 unsigned to = ((char *)dir - kaddr) +
557 nilfs_rec_len_from_disk(dir->rec_len);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700558 struct nilfs_dir_entry *pde = NULL;
559 struct nilfs_dir_entry *de = (struct nilfs_dir_entry *)(kaddr + from);
560 int err;
561
562 while ((char *)de < (char *)dir) {
563 if (de->rec_len == 0) {
564 nilfs_error(inode->i_sb, __func__,
565 "zero-length directory entry");
566 err = -EIO;
567 goto out;
568 }
569 pde = de;
570 de = nilfs_next_entry(de);
571 }
572 if (pde)
573 from = (char *)pde - (char *)page_address(page);
574 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200575 err = nilfs_prepare_chunk(page, from, to);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700576 BUG_ON(err);
577 if (pde)
Ryusuke Konishi6cda9fa2010-07-25 20:39:03 +0900578 pde->rec_len = nilfs_rec_len_to_disk(to - from);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700579 dir->inode = 0;
Jiro SEKIBA2093abf2009-11-27 19:41:11 +0900580 nilfs_commit_chunk(page, mapping, from, to);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700581 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
582/* NILFS_I(inode)->i_flags &= ~NILFS_BTREE_FL; */
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700583out:
584 nilfs_put_page(page);
585 return err;
586}
587
588/*
589 * Set the first fragment of directory.
590 */
591int nilfs_make_empty(struct inode *inode, struct inode *parent)
592{
593 struct address_space *mapping = inode->i_mapping;
594 struct page *page = grab_cache_page(mapping, 0);
595 unsigned chunk_size = nilfs_chunk_size(inode);
596 struct nilfs_dir_entry *de;
597 int err;
598 void *kaddr;
599
600 if (!page)
601 return -ENOMEM;
602
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200603 err = nilfs_prepare_chunk(page, 0, chunk_size);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700604 if (unlikely(err)) {
605 unlock_page(page);
606 goto fail;
607 }
608 kaddr = kmap_atomic(page, KM_USER0);
609 memset(kaddr, 0, chunk_size);
610 de = (struct nilfs_dir_entry *)kaddr;
611 de->name_len = 1;
Ryusuke Konishi6cda9fa2010-07-25 20:39:03 +0900612 de->rec_len = nilfs_rec_len_to_disk(NILFS_DIR_REC_LEN(1));
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700613 memcpy(de->name, ".\0\0", 4);
614 de->inode = cpu_to_le64(inode->i_ino);
615 nilfs_set_de_type(de, inode);
616
617 de = (struct nilfs_dir_entry *)(kaddr + NILFS_DIR_REC_LEN(1));
618 de->name_len = 2;
Ryusuke Konishi6cda9fa2010-07-25 20:39:03 +0900619 de->rec_len = nilfs_rec_len_to_disk(chunk_size - NILFS_DIR_REC_LEN(1));
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700620 de->inode = cpu_to_le64(parent->i_ino);
621 memcpy(de->name, "..\0", 4);
622 nilfs_set_de_type(de, inode);
623 kunmap_atomic(kaddr, KM_USER0);
Jiro SEKIBA2093abf2009-11-27 19:41:11 +0900624 nilfs_commit_chunk(page, mapping, 0, chunk_size);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700625fail:
626 page_cache_release(page);
627 return err;
628}
629
630/*
631 * routine to check that the specified directory is empty (for rmdir)
632 */
633int nilfs_empty_dir(struct inode *inode)
634{
635 struct page *page = NULL;
636 unsigned long i, npages = dir_pages(inode);
637
638 for (i = 0; i < npages; i++) {
639 char *kaddr;
640 struct nilfs_dir_entry *de;
641
642 page = nilfs_get_page(inode, i);
643 if (IS_ERR(page))
644 continue;
645
646 kaddr = page_address(page);
647 de = (struct nilfs_dir_entry *)kaddr;
648 kaddr += nilfs_last_byte(inode, i) - NILFS_DIR_REC_LEN(1);
649
650 while ((char *)de <= kaddr) {
651 if (de->rec_len == 0) {
652 nilfs_error(inode->i_sb, __func__,
653 "zero-length directory entry "
654 "(kaddr=%p, de=%p)\n", kaddr, de);
655 goto not_empty;
656 }
657 if (de->inode != 0) {
658 /* check for . and .. */
659 if (de->name[0] != '.')
660 goto not_empty;
661 if (de->name_len > 2)
662 goto not_empty;
663 if (de->name_len < 2) {
664 if (de->inode !=
665 cpu_to_le64(inode->i_ino))
666 goto not_empty;
667 } else if (de->name[1] != '.')
668 goto not_empty;
669 }
670 de = nilfs_next_entry(de);
671 }
672 nilfs_put_page(page);
673 }
674 return 1;
675
676not_empty:
677 nilfs_put_page(page);
678 return 0;
679}
680
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700681const struct file_operations nilfs_dir_operations = {
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700682 .llseek = generic_file_llseek,
683 .read = generic_read_dir,
684 .readdir = nilfs_readdir,
Ryusuke Konishi7a946192009-04-06 19:01:53 -0700685 .unlocked_ioctl = nilfs_ioctl,
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700686#ifdef CONFIG_COMPAT
Ryusuke Konishi8082d362009-04-06 19:01:53 -0700687 .compat_ioctl = nilfs_ioctl,
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700688#endif /* CONFIG_COMPAT */
689 .fsync = nilfs_sync_file,
690
691};