blob: 746956d2937a9e835b9527a9bc0c32cbdea72588 [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 *
Ryusuke Konishi4b420ab2016-05-23 16:23:09 -070016 * Modified for NILFS by Amagai Yoshiji.
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -070017 */
18/*
19 * linux/fs/ext2/dir.c
20 *
21 * Copyright (C) 1992, 1993, 1994, 1995
22 * Remy Card (card@masi.ibp.fr)
23 * Laboratoire MASI - Institut Blaise Pascal
24 * Universite Pierre et Marie Curie (Paris VI)
25 *
26 * from
27 *
28 * linux/fs/minix/dir.c
29 *
30 * Copyright (C) 1991, 1992 Linus Torvalds
31 *
32 * ext2 directory handling functions
33 *
34 * Big-endian to little-endian byte-swapping/bitmaps by
35 * David S. Miller (davem@caip.rutgers.edu), 1995
36 *
37 * All code that works with directory layout had been switched to pagecache
38 * and moved here. AV
39 */
40
41#include <linux/pagemap.h>
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -070042#include "nilfs.h"
43#include "page.h"
44
45/*
46 * nilfs uses block-sized chunks. Arguably, sector-sized ones would be
47 * more robust, but we have what we have
48 */
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -070049static inline unsigned int nilfs_chunk_size(struct inode *inode)
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -070050{
51 return inode->i_sb->s_blocksize;
52}
53
54static inline void nilfs_put_page(struct page *page)
55{
56 kunmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030057 put_page(page);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -070058}
59
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -070060/*
61 * Return the offset into page `page_nr' of the last valid
62 * byte in that page, plus one.
63 */
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -070064static unsigned int nilfs_last_byte(struct inode *inode, unsigned long page_nr)
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -070065{
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -070066 unsigned int last_byte = inode->i_size;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -070067
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030068 last_byte -= page_nr << PAGE_SHIFT;
69 if (last_byte > PAGE_SIZE)
70 last_byte = PAGE_SIZE;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -070071 return last_byte;
72}
73
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -070074static int nilfs_prepare_chunk(struct page *page, unsigned int from,
75 unsigned int to)
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -070076{
77 loff_t pos = page_offset(page) + from;
Ryusuke Konishi4ad364c2016-05-23 16:23:25 -070078
Christoph Hellwig6e1db882010-06-04 11:29:57 +020079 return __block_write_begin(page, pos, to - from, nilfs_get_block);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -070080}
81
Jiro SEKIBA2093abf2009-11-27 19:41:11 +090082static void nilfs_commit_chunk(struct page *page,
83 struct address_space *mapping,
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -070084 unsigned int from, unsigned int to)
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -070085{
86 struct inode *dir = mapping->host;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -070087 loff_t pos = page_offset(page) + from;
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -070088 unsigned int len = to - from;
89 unsigned int nr_dirty, copied;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -070090 int err;
91
92 nr_dirty = nilfs_page_count_clean_buffers(page, from, to);
93 copied = block_write_end(NULL, mapping, pos, len, len, page, NULL);
Jiro SEKIBA58d55472009-11-27 19:41:12 +090094 if (pos + copied > dir->i_size)
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -070095 i_size_write(dir, pos + copied);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -070096 if (IS_DIRSYNC(dir))
97 nilfs_set_transaction_flag(NILFS_TI_SYNC);
Ryusuke Konishibcbc8c62010-12-27 00:05:49 +090098 err = nilfs_set_file_dirty(dir, nr_dirty);
Jiro SEKIBA2093abf2009-11-27 19:41:11 +090099 WARN_ON(err); /* do not happen */
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700100 unlock_page(page);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700101}
102
Al Virobe5b82d2016-04-22 15:06:44 -0400103static bool nilfs_check_page(struct page *page)
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700104{
105 struct inode *dir = page->mapping->host;
106 struct super_block *sb = dir->i_sb;
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700107 unsigned int chunk_size = nilfs_chunk_size(dir);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700108 char *kaddr = page_address(page);
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700109 unsigned int offs, rec_len;
110 unsigned int limit = PAGE_SIZE;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700111 struct nilfs_dir_entry *p;
112 char *error;
113
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300114 if ((dir->i_size >> PAGE_SHIFT) == page->index) {
115 limit = dir->i_size & ~PAGE_MASK;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700116 if (limit & (chunk_size - 1))
117 goto Ebadsize;
118 if (!limit)
119 goto out;
120 }
121 for (offs = 0; offs <= limit - NILFS_DIR_REC_LEN(1); offs += rec_len) {
122 p = (struct nilfs_dir_entry *)(kaddr + offs);
Ryusuke Konishi6cda9fa2010-07-25 20:39:03 +0900123 rec_len = nilfs_rec_len_from_disk(p->rec_len);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700124
125 if (rec_len < NILFS_DIR_REC_LEN(1))
126 goto Eshort;
127 if (rec_len & 3)
128 goto Ealign;
129 if (rec_len < NILFS_DIR_REC_LEN(p->name_len))
130 goto Enamelen;
131 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
132 goto Espan;
133 }
134 if (offs != limit)
135 goto Eend;
136out:
137 SetPageChecked(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400138 return true;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700139
140 /* Too bad, we had an error */
141
142Ebadsize:
Ryusuke Konishicae3d4c2016-08-02 14:05:00 -0700143 nilfs_error(sb,
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700144 "size of directory #%lu is not a multiple of chunk size",
Ryusuke Konishicae3d4c2016-08-02 14:05:00 -0700145 dir->i_ino);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700146 goto fail;
147Eshort:
148 error = "rec_len is smaller than minimal";
149 goto bad_entry;
150Ealign:
151 error = "unaligned directory entry";
152 goto bad_entry;
153Enamelen:
154 error = "rec_len is too small for name_len";
155 goto bad_entry;
156Espan:
157 error = "directory entry across blocks";
158bad_entry:
Ryusuke Konishicae3d4c2016-08-02 14:05:00 -0700159 nilfs_error(sb,
160 "bad entry in directory #%lu: %s - offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
161 dir->i_ino, error, (page->index << PAGE_SHIFT) + offs,
162 (unsigned long)le64_to_cpu(p->inode),
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700163 rec_len, p->name_len);
164 goto fail;
165Eend:
166 p = (struct nilfs_dir_entry *)(kaddr + offs);
Ryusuke Konishicae3d4c2016-08-02 14:05:00 -0700167 nilfs_error(sb,
168 "entry in directory #%lu spans the page boundary offset=%lu, inode=%lu",
169 dir->i_ino, (page->index << PAGE_SHIFT) + offs,
170 (unsigned long)le64_to_cpu(p->inode));
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700171fail:
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700172 SetPageError(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400173 return false;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700174}
175
176static struct page *nilfs_get_page(struct inode *dir, unsigned long n)
177{
178 struct address_space *mapping = dir->i_mapping;
Ryusuke Konishic28e69d2010-07-24 17:09:10 +0900179 struct page *page = read_mapping_page(mapping, n, NULL);
180
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700181 if (!IS_ERR(page)) {
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700182 kmap(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400183 if (unlikely(!PageChecked(page))) {
184 if (PageError(page) || !nilfs_check_page(page))
185 goto fail;
186 }
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700187 }
188 return page;
189
190fail:
191 nilfs_put_page(page);
192 return ERR_PTR(-EIO);
193}
194
195/*
196 * NOTE! unlike strncmp, nilfs_match returns 1 for success, 0 for failure.
197 *
198 * len <= NILFS_NAME_LEN and de != NULL are guaranteed by caller.
199 */
200static int
Al Viro072f98b2010-01-31 21:03:58 -0500201nilfs_match(int len, const unsigned char *name, struct nilfs_dir_entry *de)
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700202{
203 if (len != de->name_len)
204 return 0;
205 if (!de->inode)
206 return 0;
207 return !memcmp(name, de->name, len);
208}
209
210/*
211 * p is at least 6 bytes before the end of page
212 */
213static struct nilfs_dir_entry *nilfs_next_entry(struct nilfs_dir_entry *p)
214{
Ryusuke Konishi6cda9fa2010-07-25 20:39:03 +0900215 return (struct nilfs_dir_entry *)((char *)p +
216 nilfs_rec_len_from_disk(p->rec_len));
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700217}
218
219static unsigned char
220nilfs_filetype_table[NILFS_FT_MAX] = {
221 [NILFS_FT_UNKNOWN] = DT_UNKNOWN,
222 [NILFS_FT_REG_FILE] = DT_REG,
223 [NILFS_FT_DIR] = DT_DIR,
224 [NILFS_FT_CHRDEV] = DT_CHR,
225 [NILFS_FT_BLKDEV] = DT_BLK,
226 [NILFS_FT_FIFO] = DT_FIFO,
227 [NILFS_FT_SOCK] = DT_SOCK,
228 [NILFS_FT_SYMLINK] = DT_LNK,
229};
230
231#define S_SHIFT 12
232static unsigned char
233nilfs_type_by_mode[S_IFMT >> S_SHIFT] = {
234 [S_IFREG >> S_SHIFT] = NILFS_FT_REG_FILE,
235 [S_IFDIR >> S_SHIFT] = NILFS_FT_DIR,
236 [S_IFCHR >> S_SHIFT] = NILFS_FT_CHRDEV,
237 [S_IFBLK >> S_SHIFT] = NILFS_FT_BLKDEV,
238 [S_IFIFO >> S_SHIFT] = NILFS_FT_FIFO,
239 [S_IFSOCK >> S_SHIFT] = NILFS_FT_SOCK,
240 [S_IFLNK >> S_SHIFT] = NILFS_FT_SYMLINK,
241};
242
243static void nilfs_set_de_type(struct nilfs_dir_entry *de, struct inode *inode)
244{
Al Viroc6e49e32011-07-26 03:07:14 -0400245 umode_t mode = inode->i_mode;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700246
247 de->file_type = nilfs_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
248}
249
Al Viro1616abe2013-05-16 14:36:14 -0400250static int nilfs_readdir(struct file *file, struct dir_context *ctx)
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700251{
Al Viro1616abe2013-05-16 14:36:14 -0400252 loff_t pos = ctx->pos;
253 struct inode *inode = file_inode(file);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700254 struct super_block *sb = inode->i_sb;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300255 unsigned int offset = pos & ~PAGE_MASK;
256 unsigned long n = pos >> PAGE_SHIFT;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700257 unsigned long npages = dir_pages(inode);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700258
259 if (pos > inode->i_size - NILFS_DIR_REC_LEN(1))
Al Viro1616abe2013-05-16 14:36:14 -0400260 return 0;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700261
262 for ( ; n < npages; n++, offset = 0) {
263 char *kaddr, *limit;
264 struct nilfs_dir_entry *de;
265 struct page *page = nilfs_get_page(inode, n);
266
267 if (IS_ERR(page)) {
Ryusuke Konishicae3d4c2016-08-02 14:05:00 -0700268 nilfs_error(sb, "bad page in #%lu", inode->i_ino);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300269 ctx->pos += PAGE_SIZE - offset;
Al Viro1616abe2013-05-16 14:36:14 -0400270 return -EIO;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700271 }
272 kaddr = page_address(page);
273 de = (struct nilfs_dir_entry *)(kaddr + offset);
274 limit = kaddr + nilfs_last_byte(inode, n) -
275 NILFS_DIR_REC_LEN(1);
276 for ( ; (char *)de <= limit; de = nilfs_next_entry(de)) {
277 if (de->rec_len == 0) {
Ryusuke Konishicae3d4c2016-08-02 14:05:00 -0700278 nilfs_error(sb, "zero-length directory entry");
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700279 nilfs_put_page(page);
Al Viro1616abe2013-05-16 14:36:14 -0400280 return -EIO;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700281 }
282 if (de->inode) {
Al Viro1616abe2013-05-16 14:36:14 -0400283 unsigned char t;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700284
Al Viro1616abe2013-05-16 14:36:14 -0400285 if (de->file_type < NILFS_FT_MAX)
286 t = nilfs_filetype_table[de->file_type];
287 else
288 t = DT_UNKNOWN;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700289
Al Viro1616abe2013-05-16 14:36:14 -0400290 if (!dir_emit(ctx, de->name, de->name_len,
291 le64_to_cpu(de->inode), t)) {
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700292 nilfs_put_page(page);
Al Viro1616abe2013-05-16 14:36:14 -0400293 return 0;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700294 }
295 }
Al Viro1616abe2013-05-16 14:36:14 -0400296 ctx->pos += nilfs_rec_len_from_disk(de->rec_len);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700297 }
298 nilfs_put_page(page);
299 }
Al Viro1616abe2013-05-16 14:36:14 -0400300 return 0;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700301}
302
303/*
304 * nilfs_find_entry()
305 *
306 * finds an entry in the specified directory with the wanted name. It
307 * returns the page in which the entry was found, and the entry itself
308 * (as a parameter - res_dir). Page is returned mapped and unlocked.
309 * Entry is guaranteed to be valid.
310 */
311struct nilfs_dir_entry *
Al Viro03190032010-01-31 21:02:09 -0500312nilfs_find_entry(struct inode *dir, const struct qstr *qstr,
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700313 struct page **res_page)
314{
Al Viro03190032010-01-31 21:02:09 -0500315 const unsigned char *name = qstr->name;
316 int namelen = qstr->len;
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700317 unsigned int reclen = NILFS_DIR_REC_LEN(namelen);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700318 unsigned long start, n;
319 unsigned long npages = dir_pages(dir);
320 struct page *page = NULL;
321 struct nilfs_inode_info *ei = NILFS_I(dir);
322 struct nilfs_dir_entry *de;
323
324 if (npages == 0)
325 goto out;
326
327 /* OFFSET_CACHE */
328 *res_page = NULL;
329
330 start = ei->i_dir_start_lookup;
331 if (start >= npages)
332 start = 0;
333 n = start;
334 do {
335 char *kaddr;
Ryusuke Konishi4ad364c2016-05-23 16:23:25 -0700336
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700337 page = nilfs_get_page(dir, n);
338 if (!IS_ERR(page)) {
339 kaddr = page_address(page);
340 de = (struct nilfs_dir_entry *)kaddr;
341 kaddr += nilfs_last_byte(dir, n) - reclen;
342 while ((char *) de <= kaddr) {
343 if (de->rec_len == 0) {
Ryusuke Konishicae3d4c2016-08-02 14:05:00 -0700344 nilfs_error(dir->i_sb,
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700345 "zero-length directory entry");
346 nilfs_put_page(page);
347 goto out;
348 }
349 if (nilfs_match(namelen, name, de))
350 goto found;
351 de = nilfs_next_entry(de);
352 }
353 nilfs_put_page(page);
354 }
355 if (++n >= npages)
356 n = 0;
357 /* next page is past the blocks we've got */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300358 if (unlikely(n > (dir->i_blocks >> (PAGE_SHIFT - 9)))) {
Ryusuke Konishicae3d4c2016-08-02 14:05:00 -0700359 nilfs_error(dir->i_sb,
Ryusuke Konishi16215622010-03-14 03:17:45 +0900360 "dir %lu size %lld exceeds block count %llu",
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700361 dir->i_ino, dir->i_size,
362 (unsigned long long)dir->i_blocks);
363 goto out;
364 }
365 } while (n != start);
366out:
367 return NULL;
368
369found:
370 *res_page = page;
371 ei->i_dir_start_lookup = n;
372 return de;
373}
374
375struct nilfs_dir_entry *nilfs_dotdot(struct inode *dir, struct page **p)
376{
377 struct page *page = nilfs_get_page(dir, 0);
378 struct nilfs_dir_entry *de = NULL;
379
380 if (!IS_ERR(page)) {
381 de = nilfs_next_entry(
382 (struct nilfs_dir_entry *)page_address(page));
383 *p = page;
384 }
385 return de;
386}
387
Al Viro03190032010-01-31 21:02:09 -0500388ino_t nilfs_inode_by_name(struct inode *dir, const struct qstr *qstr)
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700389{
390 ino_t res = 0;
391 struct nilfs_dir_entry *de;
392 struct page *page;
393
Al Viro03190032010-01-31 21:02:09 -0500394 de = nilfs_find_entry(dir, qstr, &page);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700395 if (de) {
396 res = le64_to_cpu(de->inode);
397 kunmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300398 put_page(page);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700399 }
400 return res;
401}
402
403/* Releases the page */
404void nilfs_set_link(struct inode *dir, struct nilfs_dir_entry *de,
405 struct page *page, struct inode *inode)
406{
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700407 unsigned int from = (char *)de - (char *)page_address(page);
408 unsigned int to = from + nilfs_rec_len_from_disk(de->rec_len);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700409 struct address_space *mapping = page->mapping;
410 int err;
411
412 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200413 err = nilfs_prepare_chunk(page, from, to);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700414 BUG_ON(err);
415 de->inode = cpu_to_le64(inode->i_ino);
416 nilfs_set_de_type(de, inode);
Jiro SEKIBA2093abf2009-11-27 19:41:11 +0900417 nilfs_commit_chunk(page, mapping, from, to);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700418 nilfs_put_page(page);
419 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700420}
421
422/*
423 * Parent is locked.
424 */
425int nilfs_add_link(struct dentry *dentry, struct inode *inode)
426{
David Howells2b0143b2015-03-17 22:25:59 +0000427 struct inode *dir = d_inode(dentry->d_parent);
Al Viro072f98b2010-01-31 21:03:58 -0500428 const unsigned char *name = dentry->d_name.name;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700429 int namelen = dentry->d_name.len;
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700430 unsigned int chunk_size = nilfs_chunk_size(dir);
431 unsigned int reclen = NILFS_DIR_REC_LEN(namelen);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700432 unsigned short rec_len, name_len;
433 struct page *page = NULL;
434 struct nilfs_dir_entry *de;
435 unsigned long npages = dir_pages(dir);
436 unsigned long n;
437 char *kaddr;
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700438 unsigned int from, to;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700439 int err;
440
441 /*
442 * We take care of directory expansion in the same loop.
443 * This code plays outside i_size, so it locks the page
444 * to protect that region.
445 */
446 for (n = 0; n <= npages; n++) {
447 char *dir_end;
448
449 page = nilfs_get_page(dir, n);
450 err = PTR_ERR(page);
451 if (IS_ERR(page))
452 goto out;
453 lock_page(page);
454 kaddr = page_address(page);
455 dir_end = kaddr + nilfs_last_byte(dir, n);
456 de = (struct nilfs_dir_entry *)kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300457 kaddr += PAGE_SIZE - reclen;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700458 while ((char *)de <= kaddr) {
459 if ((char *)de == dir_end) {
460 /* We hit i_size */
461 name_len = 0;
462 rec_len = chunk_size;
Ryusuke Konishi6cda9fa2010-07-25 20:39:03 +0900463 de->rec_len = nilfs_rec_len_to_disk(chunk_size);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700464 de->inode = 0;
465 goto got_it;
466 }
467 if (de->rec_len == 0) {
Ryusuke Konishicae3d4c2016-08-02 14:05:00 -0700468 nilfs_error(dir->i_sb,
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700469 "zero-length directory entry");
470 err = -EIO;
471 goto out_unlock;
472 }
473 err = -EEXIST;
474 if (nilfs_match(namelen, name, de))
475 goto out_unlock;
476 name_len = NILFS_DIR_REC_LEN(de->name_len);
Ryusuke Konishi6cda9fa2010-07-25 20:39:03 +0900477 rec_len = nilfs_rec_len_from_disk(de->rec_len);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700478 if (!de->inode && rec_len >= reclen)
479 goto got_it;
480 if (rec_len >= name_len + reclen)
481 goto got_it;
482 de = (struct nilfs_dir_entry *)((char *)de + rec_len);
483 }
484 unlock_page(page);
485 nilfs_put_page(page);
486 }
487 BUG();
488 return -EINVAL;
489
490got_it:
491 from = (char *)de - (char *)page_address(page);
492 to = from + rec_len;
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200493 err = nilfs_prepare_chunk(page, from, to);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700494 if (err)
495 goto out_unlock;
496 if (de->inode) {
497 struct nilfs_dir_entry *de1;
498
499 de1 = (struct nilfs_dir_entry *)((char *)de + name_len);
Ryusuke Konishi6cda9fa2010-07-25 20:39:03 +0900500 de1->rec_len = nilfs_rec_len_to_disk(rec_len - name_len);
501 de->rec_len = nilfs_rec_len_to_disk(name_len);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700502 de = de1;
503 }
504 de->name_len = namelen;
505 memcpy(de->name, name, namelen);
506 de->inode = cpu_to_le64(inode->i_ino);
507 nilfs_set_de_type(de, inode);
Jiro SEKIBA2093abf2009-11-27 19:41:11 +0900508 nilfs_commit_chunk(page, page->mapping, from, to);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700509 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900510 nilfs_mark_inode_dirty(dir);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700511 /* OFFSET_CACHE */
512out_put:
513 nilfs_put_page(page);
514out:
515 return err;
516out_unlock:
517 unlock_page(page);
518 goto out_put;
519}
520
521/*
522 * nilfs_delete_entry deletes a directory entry by merging it with the
523 * previous entry. Page is up-to-date. Releases the page.
524 */
525int nilfs_delete_entry(struct nilfs_dir_entry *dir, struct page *page)
526{
527 struct address_space *mapping = page->mapping;
528 struct inode *inode = mapping->host;
529 char *kaddr = page_address(page);
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700530 unsigned int from, to;
531 struct nilfs_dir_entry *de, *pde = NULL;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700532 int err;
533
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700534 from = ((char *)dir - kaddr) & ~(nilfs_chunk_size(inode) - 1);
535 to = ((char *)dir - kaddr) + nilfs_rec_len_from_disk(dir->rec_len);
536 de = (struct nilfs_dir_entry *)(kaddr + from);
537
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700538 while ((char *)de < (char *)dir) {
539 if (de->rec_len == 0) {
Ryusuke Konishicae3d4c2016-08-02 14:05:00 -0700540 nilfs_error(inode->i_sb,
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700541 "zero-length directory entry");
542 err = -EIO;
543 goto out;
544 }
545 pde = de;
546 de = nilfs_next_entry(de);
547 }
548 if (pde)
549 from = (char *)pde - (char *)page_address(page);
550 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200551 err = nilfs_prepare_chunk(page, from, to);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700552 BUG_ON(err);
553 if (pde)
Ryusuke Konishi6cda9fa2010-07-25 20:39:03 +0900554 pde->rec_len = nilfs_rec_len_to_disk(to - from);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700555 dir->inode = 0;
Jiro SEKIBA2093abf2009-11-27 19:41:11 +0900556 nilfs_commit_chunk(page, mapping, from, to);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700557 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700558out:
559 nilfs_put_page(page);
560 return err;
561}
562
563/*
564 * Set the first fragment of directory.
565 */
566int nilfs_make_empty(struct inode *inode, struct inode *parent)
567{
568 struct address_space *mapping = inode->i_mapping;
569 struct page *page = grab_cache_page(mapping, 0);
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700570 unsigned int chunk_size = nilfs_chunk_size(inode);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700571 struct nilfs_dir_entry *de;
572 int err;
573 void *kaddr;
574
575 if (!page)
576 return -ENOMEM;
577
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200578 err = nilfs_prepare_chunk(page, 0, chunk_size);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700579 if (unlikely(err)) {
580 unlock_page(page);
581 goto fail;
582 }
Cong Wang7b9c0972011-11-25 23:14:33 +0800583 kaddr = kmap_atomic(page);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700584 memset(kaddr, 0, chunk_size);
585 de = (struct nilfs_dir_entry *)kaddr;
586 de->name_len = 1;
Ryusuke Konishi6cda9fa2010-07-25 20:39:03 +0900587 de->rec_len = nilfs_rec_len_to_disk(NILFS_DIR_REC_LEN(1));
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700588 memcpy(de->name, ".\0\0", 4);
589 de->inode = cpu_to_le64(inode->i_ino);
590 nilfs_set_de_type(de, inode);
591
592 de = (struct nilfs_dir_entry *)(kaddr + NILFS_DIR_REC_LEN(1));
593 de->name_len = 2;
Ryusuke Konishi6cda9fa2010-07-25 20:39:03 +0900594 de->rec_len = nilfs_rec_len_to_disk(chunk_size - NILFS_DIR_REC_LEN(1));
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700595 de->inode = cpu_to_le64(parent->i_ino);
596 memcpy(de->name, "..\0", 4);
597 nilfs_set_de_type(de, inode);
Cong Wang7b9c0972011-11-25 23:14:33 +0800598 kunmap_atomic(kaddr);
Jiro SEKIBA2093abf2009-11-27 19:41:11 +0900599 nilfs_commit_chunk(page, mapping, 0, chunk_size);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700600fail:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300601 put_page(page);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700602 return err;
603}
604
605/*
606 * routine to check that the specified directory is empty (for rmdir)
607 */
608int nilfs_empty_dir(struct inode *inode)
609{
610 struct page *page = NULL;
611 unsigned long i, npages = dir_pages(inode);
612
613 for (i = 0; i < npages; i++) {
614 char *kaddr;
615 struct nilfs_dir_entry *de;
616
617 page = nilfs_get_page(inode, i);
618 if (IS_ERR(page))
619 continue;
620
621 kaddr = page_address(page);
622 de = (struct nilfs_dir_entry *)kaddr;
623 kaddr += nilfs_last_byte(inode, i) - NILFS_DIR_REC_LEN(1);
624
625 while ((char *)de <= kaddr) {
626 if (de->rec_len == 0) {
Ryusuke Konishicae3d4c2016-08-02 14:05:00 -0700627 nilfs_error(inode->i_sb,
Ryusuke Konishi06f4abf2016-05-23 16:23:31 -0700628 "zero-length directory entry (kaddr=%p, de=%p)",
629 kaddr, de);
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700630 goto not_empty;
631 }
632 if (de->inode != 0) {
633 /* check for . and .. */
634 if (de->name[0] != '.')
635 goto not_empty;
636 if (de->name_len > 2)
637 goto not_empty;
638 if (de->name_len < 2) {
639 if (de->inode !=
640 cpu_to_le64(inode->i_ino))
641 goto not_empty;
642 } else if (de->name[1] != '.')
643 goto not_empty;
644 }
645 de = nilfs_next_entry(de);
646 }
647 nilfs_put_page(page);
648 }
649 return 1;
650
651not_empty:
652 nilfs_put_page(page);
653 return 0;
654}
655
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700656const struct file_operations nilfs_dir_operations = {
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700657 .llseek = generic_file_llseek,
658 .read = generic_read_dir,
Al Viroc51da202016-04-30 22:37:34 -0400659 .iterate_shared = nilfs_readdir,
Ryusuke Konishi7a946192009-04-06 19:01:53 -0700660 .unlocked_ioctl = nilfs_ioctl,
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700661#ifdef CONFIG_COMPAT
Ryusuke Konishi828b1c52011-02-03 21:26:17 +0900662 .compat_ioctl = nilfs_compat_ioctl,
Yoshiji Amagai2ba466d2009-04-06 19:01:34 -0700663#endif /* CONFIG_COMPAT */
664 .fsync = nilfs_sync_file,
665
666};