blob: 3a75ca09c506591910e45a5bad970e80382b970c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/ufs/ufs_dir.c
3 *
4 * Copyright (C) 1996
5 * Adrian Rodriguez (adrian@franklins-tower.rutgers.edu)
6 * Laboratory for Computer Science Research Computing Facility
7 * Rutgers, The State University of New Jersey
8 *
9 * swab support by Francois-Rene Rideau <fare@tunes.org> 19970406
10 *
11 * 4.4BSD (FreeBSD) support added on February 1st 1998 by
12 * Niels Kristian Bech Jensen <nkbj@image.dk> partially based
13 * on code by Martin von Loewis <martin@mira.isdn.cs.tu-berlin.de>.
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070014 *
15 * Migration to usage of "page cache" on May 2006 by
16 * Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
19#include <linux/time.h>
20#include <linux/fs.h>
Nick Piggin82b9d1d2007-10-16 01:25:19 -070021#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Mike Frysingere5420592008-02-08 04:21:31 -080023#include "ufs_fs.h"
Christoph Hellwigbcd6d4e2007-10-16 23:26:51 -070024#include "ufs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "swab.h"
26#include "util.h"
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/*
29 * NOTE! unlike strncmp, ufs_match returns 1 for success, 0 for failure.
30 *
31 * len <= UFS_MAXNAMLEN and de != NULL are guaranteed by caller.
32 */
33static inline int ufs_match(struct super_block *sb, int len,
Al Viro89031bc2010-01-31 20:49:54 -050034 const unsigned char *name, struct ufs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
36 if (len != ufs_get_de_namlen(sb, de))
37 return 0;
38 if (!de->d_ino)
39 return 0;
40 return !memcmp(name, de->d_name, len);
41}
42
Nick Piggin82b9d1d2007-10-16 01:25:19 -070043static int ufs_commit_chunk(struct page *page, loff_t pos, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Nick Piggin82b9d1d2007-10-16 01:25:19 -070045 struct address_space *mapping = page->mapping;
46 struct inode *dir = mapping->host;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070047 int err = 0;
Nick Piggin82b9d1d2007-10-16 01:25:19 -070048
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070049 dir->i_version++;
Nick Piggin82b9d1d2007-10-16 01:25:19 -070050 block_write_end(NULL, mapping, pos, len, len, page, NULL);
51 if (pos+len > dir->i_size) {
52 i_size_write(dir, pos+len);
53 mark_inode_dirty(dir);
54 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070055 if (IS_DIRSYNC(dir))
56 err = write_one_page(page, 1);
57 else
58 unlock_page(page);
59 return err;
60}
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070062static inline void ufs_put_page(struct page *page)
63{
64 kunmap(page);
65 page_cache_release(page);
66}
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070068static inline unsigned long ufs_dir_pages(struct inode *inode)
69{
70 return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
71}
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Al Viro89031bc2010-01-31 20:49:54 -050073ino_t ufs_inode_by_name(struct inode *dir, const struct qstr *qstr)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070074{
75 ino_t res = 0;
76 struct ufs_dir_entry *de;
77 struct page *page;
78
Alexey Dobriyan08049702009-12-15 16:46:50 -080079 de = ufs_find_entry(dir, qstr, &page);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070080 if (de) {
81 res = fs32_to_cpu(dir->i_sb, de->d_ino);
82 ufs_put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070084 return res;
85}
86
87
88/* Releases the page */
89void ufs_set_link(struct inode *dir, struct ufs_dir_entry *de,
90 struct page *page, struct inode *inode)
91{
Nick Piggin82b9d1d2007-10-16 01:25:19 -070092 loff_t pos = page_offset(page) +
93 (char *) de - (char *) page_address(page);
94 unsigned len = fs16_to_cpu(dir->i_sb, de->d_reclen);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070095 int err;
96
97 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +020098 err = ufs_prepare_chunk(page, pos, len);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070099 BUG_ON(err);
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700100
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700101 de->d_ino = cpu_to_fs32(dir->i_sb, inode->i_ino);
102 ufs_set_de_type(dir->i_sb, de, inode->i_mode);
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700103
104 err = ufs_commit_chunk(page, pos, len);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700105 ufs_put_page(page);
106 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
107 mark_inode_dirty(dir);
108}
109
110
111static void ufs_check_page(struct page *page)
112{
113 struct inode *dir = page->mapping->host;
114 struct super_block *sb = dir->i_sb;
115 char *kaddr = page_address(page);
116 unsigned offs, rec_len;
117 unsigned limit = PAGE_CACHE_SIZE;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800118 const unsigned chunk_mask = UFS_SB(sb)->s_uspi->s_dirblksize - 1;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700119 struct ufs_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;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800124 if (limit & chunk_mask)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700125 goto Ebadsize;
126 if (!limit)
127 goto out;
128 }
129 for (offs = 0; offs <= limit - UFS_DIR_REC_LEN(1); offs += rec_len) {
130 p = (struct ufs_dir_entry *)(kaddr + offs);
131 rec_len = fs16_to_cpu(sb, p->d_reclen);
132
133 if (rec_len < UFS_DIR_REC_LEN(1))
134 goto Eshort;
135 if (rec_len & 3)
136 goto Ealign;
137 if (rec_len < UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, p)))
138 goto Enamelen;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800139 if (((offs + rec_len - 1) ^ offs) & ~chunk_mask)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700140 goto Espan;
141 if (fs32_to_cpu(sb, p->d_ino) > (UFS_SB(sb)->s_uspi->s_ipg *
142 UFS_SB(sb)->s_uspi->s_ncg))
143 goto Einumber;
144 }
145 if (offs != limit)
146 goto Eend;
147out:
148 SetPageChecked(page);
149 return;
150
151 /* Too bad, we had an error */
152
153Ebadsize:
154 ufs_error(sb, "ufs_check_page",
155 "size of directory #%lu is not a multiple of chunk size",
156 dir->i_ino
157 );
158 goto fail;
159Eshort:
160 error = "rec_len is smaller than minimal";
161 goto bad_entry;
162Ealign:
163 error = "unaligned directory entry";
164 goto bad_entry;
165Enamelen:
166 error = "rec_len is too small for name_len";
167 goto bad_entry;
168Espan:
169 error = "directory entry across blocks";
170 goto bad_entry;
171Einumber:
172 error = "inode out of bounds";
173bad_entry:
174 ufs_error (sb, "ufs_check_page", "bad entry in directory #%lu: %s - "
175 "offset=%lu, rec_len=%d, name_len=%d",
176 dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT)+offs,
177 rec_len, ufs_get_de_namlen(sb, p));
178 goto fail;
179Eend:
180 p = (struct ufs_dir_entry *)(kaddr + offs);
Harvey Harrison9746077a72008-04-28 02:16:17 -0700181 ufs_error(sb, __func__,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700182 "entry in directory #%lu spans the page boundary"
183 "offset=%lu",
184 dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs);
185fail:
186 SetPageChecked(page);
187 SetPageError(page);
188}
189
190static struct page *ufs_get_page(struct inode *dir, unsigned long n)
191{
192 struct address_space *mapping = dir->i_mapping;
Nick Piggin6fe69002007-05-06 14:49:04 -0700193 struct page *page = read_mapping_page(mapping, n, NULL);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700194 if (!IS_ERR(page)) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700195 kmap(page);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700196 if (!PageChecked(page))
197 ufs_check_page(page);
198 if (PageError(page))
199 goto fail;
200 }
201 return page;
202
203fail:
204 ufs_put_page(page);
205 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208/*
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700209 * Return the offset into page `page_nr' of the last valid
210 * byte in that page, plus one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 */
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700212static unsigned
213ufs_last_byte(struct inode *inode, unsigned long page_nr)
214{
215 unsigned last_byte = inode->i_size;
216
217 last_byte -= page_nr << PAGE_CACHE_SHIFT;
218 if (last_byte > PAGE_CACHE_SIZE)
219 last_byte = PAGE_CACHE_SIZE;
220 return last_byte;
221}
222
223static inline struct ufs_dir_entry *
224ufs_next_entry(struct super_block *sb, struct ufs_dir_entry *p)
225{
226 return (struct ufs_dir_entry *)((char *)p +
227 fs16_to_cpu(sb, p->d_reclen));
228}
229
230struct ufs_dir_entry *ufs_dotdot(struct inode *dir, struct page **p)
231{
232 struct page *page = ufs_get_page(dir, 0);
233 struct ufs_dir_entry *de = NULL;
234
235 if (!IS_ERR(page)) {
236 de = ufs_next_entry(dir->i_sb,
237 (struct ufs_dir_entry *)page_address(page));
238 *p = page;
239 }
240 return de;
241}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243/*
244 * ufs_find_entry()
245 *
246 * finds an entry in the specified directory with the wanted name. It
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700247 * returns the page in which the entry was found, and the entry itself
248 * (as a parameter - res_dir). Page is returned mapped and unlocked.
249 * Entry is guaranteed to be valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 */
Al Viro89031bc2010-01-31 20:49:54 -0500251struct ufs_dir_entry *ufs_find_entry(struct inode *dir, const struct qstr *qstr,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700252 struct page **res_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 struct super_block *sb = dir->i_sb;
Al Viro89031bc2010-01-31 20:49:54 -0500255 const unsigned char *name = qstr->name;
Alexey Dobriyan08049702009-12-15 16:46:50 -0800256 int namelen = qstr->len;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700257 unsigned reclen = UFS_DIR_REC_LEN(namelen);
258 unsigned long start, n;
259 unsigned long npages = ufs_dir_pages(dir);
260 struct page *page = NULL;
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700261 struct ufs_inode_info *ui = UFS_I(dir);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700262 struct ufs_dir_entry *de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700264 UFSD("ENTER, dir_ino %lu, name %s, namlen %u\n", dir->i_ino, name, namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700266 if (npages == 0 || namelen > UFS_MAXNAMLEN)
267 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700269 /* OFFSET_CACHE */
270 *res_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700272 start = ui->i_dir_start_lookup;
273
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700274 if (start >= npages)
275 start = 0;
276 n = start;
277 do {
278 char *kaddr;
279 page = ufs_get_page(dir, n);
280 if (!IS_ERR(page)) {
281 kaddr = page_address(page);
282 de = (struct ufs_dir_entry *) kaddr;
283 kaddr += ufs_last_byte(dir, n) - reclen;
284 while ((char *) de <= kaddr) {
285 if (de->d_reclen == 0) {
Harvey Harrison9746077a72008-04-28 02:16:17 -0700286 ufs_error(dir->i_sb, __func__,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700287 "zero-length directory entry");
288 ufs_put_page(page);
289 goto out;
290 }
291 if (ufs_match(sb, namelen, name, de))
292 goto found;
293 de = ufs_next_entry(sb, de);
294 }
295 ufs_put_page(page);
296 }
297 if (++n >= npages)
298 n = 0;
299 } while (n != start);
300out:
301 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700303found:
304 *res_page = page;
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700305 ui->i_dir_start_lookup = n;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700306 return de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309/*
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700310 * Parent is locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 */
312int ufs_add_link(struct dentry *dentry, struct inode *inode)
313{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 struct inode *dir = dentry->d_parent->d_inode;
Al Viro89031bc2010-01-31 20:49:54 -0500315 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 int namelen = dentry->d_name.len;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700317 struct super_block *sb = dir->i_sb;
318 unsigned reclen = UFS_DIR_REC_LEN(namelen);
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800319 const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700320 unsigned short rec_len, name_len;
321 struct page *page = NULL;
322 struct ufs_dir_entry *de;
323 unsigned long npages = ufs_dir_pages(dir);
324 unsigned long n;
325 char *kaddr;
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700326 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 int err;
328
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700329 UFSD("ENTER, name %s, namelen %u\n", name, namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700331 /*
332 * We take care of directory expansion in the same loop.
333 * This code plays outside i_size, so it locks the page
334 * to protect that region.
335 */
336 for (n = 0; n <= npages; n++) {
337 char *dir_end;
338
339 page = ufs_get_page(dir, n);
340 err = PTR_ERR(page);
341 if (IS_ERR(page))
342 goto out;
343 lock_page(page);
344 kaddr = page_address(page);
345 dir_end = kaddr + ufs_last_byte(dir, n);
346 de = (struct ufs_dir_entry *)kaddr;
347 kaddr += PAGE_CACHE_SIZE - reclen;
348 while ((char *)de <= kaddr) {
349 if ((char *)de == dir_end) {
350 /* We hit i_size */
351 name_len = 0;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800352 rec_len = chunk_size;
353 de->d_reclen = cpu_to_fs16(sb, chunk_size);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700354 de->d_ino = 0;
355 goto got_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700357 if (de->d_reclen == 0) {
Harvey Harrison9746077a72008-04-28 02:16:17 -0700358 ufs_error(dir->i_sb, __func__,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700359 "zero-length directory entry");
360 err = -EIO;
361 goto out_unlock;
362 }
363 err = -EEXIST;
364 if (ufs_match(sb, namelen, name, de))
365 goto out_unlock;
366 name_len = UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, de));
367 rec_len = fs16_to_cpu(sb, de->d_reclen);
368 if (!de->d_ino && rec_len >= reclen)
369 goto got_it;
370 if (rec_len >= name_len + reclen)
371 goto got_it;
372 de = (struct ufs_dir_entry *) ((char *) de + rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700374 unlock_page(page);
375 ufs_put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700377 BUG();
378 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700380got_it:
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700381 pos = page_offset(page) +
382 (char*)de - (char*)page_address(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200383 err = ufs_prepare_chunk(page, pos, rec_len);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700384 if (err)
385 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (de->d_ino) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700387 struct ufs_dir_entry *de1 =
388 (struct ufs_dir_entry *) ((char *) de + name_len);
389 de1->d_reclen = cpu_to_fs16(sb, rec_len - name_len);
390 de->d_reclen = cpu_to_fs16(sb, name_len);
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 de = de1;
393 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 ufs_set_de_namlen(sb, de, namelen);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700396 memcpy(de->d_name, name, namelen + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 de->d_ino = cpu_to_fs32(sb, inode->i_ino);
398 ufs_set_de_type(sb, de, inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700400 err = ufs_commit_chunk(page, pos, rec_len);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700401 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
402
403 mark_inode_dirty(dir);
404 /* OFFSET_CACHE */
405out_put:
406 ufs_put_page(page);
407out:
408 return err;
409out_unlock:
410 unlock_page(page);
411 goto out_put;
412}
413
414static inline unsigned
415ufs_validate_entry(struct super_block *sb, char *base,
416 unsigned offset, unsigned mask)
417{
418 struct ufs_dir_entry *de = (struct ufs_dir_entry*)(base + offset);
419 struct ufs_dir_entry *p = (struct ufs_dir_entry*)(base + (offset&mask));
420 while ((char*)p < (char*)de) {
421 if (p->d_reclen == 0)
422 break;
423 p = ufs_next_entry(sb, p);
424 }
425 return (char *)p - base;
426}
427
428
429/*
430 * This is blatantly stolen from ext2fs
431 */
432static int
433ufs_readdir(struct file *filp, void *dirent, filldir_t filldir)
434{
435 loff_t pos = filp->f_pos;
Al Viro496ad9a2013-01-23 17:07:38 -0500436 struct inode *inode = file_inode(filp);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700437 struct super_block *sb = inode->i_sb;
438 unsigned int offset = pos & ~PAGE_CACHE_MASK;
439 unsigned long n = pos >> PAGE_CACHE_SHIFT;
440 unsigned long npages = ufs_dir_pages(inode);
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800441 unsigned chunk_mask = ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700442 int need_revalidate = filp->f_version != inode->i_version;
443 unsigned flags = UFS_SB(sb)->s_flags;
444
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700445 UFSD("BEGIN\n");
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700446
447 if (pos > inode->i_size - UFS_DIR_REC_LEN(1))
448 return 0;
449
450 for ( ; n < npages; n++, offset = 0) {
451 char *kaddr, *limit;
452 struct ufs_dir_entry *de;
453
454 struct page *page = ufs_get_page(inode, n);
455
456 if (IS_ERR(page)) {
Harvey Harrison9746077a72008-04-28 02:16:17 -0700457 ufs_error(sb, __func__,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700458 "bad page in #%lu",
459 inode->i_ino);
460 filp->f_pos += PAGE_CACHE_SIZE - offset;
461 return -EIO;
462 }
463 kaddr = page_address(page);
464 if (unlikely(need_revalidate)) {
465 if (offset) {
466 offset = ufs_validate_entry(sb, kaddr, offset, chunk_mask);
467 filp->f_pos = (n<<PAGE_CACHE_SHIFT) + offset;
468 }
469 filp->f_version = inode->i_version;
470 need_revalidate = 0;
471 }
472 de = (struct ufs_dir_entry *)(kaddr+offset);
473 limit = kaddr + ufs_last_byte(inode, n) - UFS_DIR_REC_LEN(1);
474 for ( ;(char*)de <= limit; de = ufs_next_entry(sb, de)) {
475 if (de->d_reclen == 0) {
Harvey Harrison9746077a72008-04-28 02:16:17 -0700476 ufs_error(sb, __func__,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700477 "zero-length directory entry");
478 ufs_put_page(page);
479 return -EIO;
480 }
481 if (de->d_ino) {
482 int over;
483 unsigned char d_type = DT_UNKNOWN;
484
485 offset = (char *)de - kaddr;
486
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700487 UFSD("filldir(%s,%u)\n", de->d_name,
488 fs32_to_cpu(sb, de->d_ino));
489 UFSD("namlen %u\n", ufs_get_de_namlen(sb, de));
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700490
491 if ((flags & UFS_DE_MASK) == UFS_DE_44BSD)
492 d_type = de->d_u.d_44.d_type;
493
494 over = filldir(dirent, de->d_name,
495 ufs_get_de_namlen(sb, de),
496 (n<<PAGE_CACHE_SHIFT) | offset,
497 fs32_to_cpu(sb, de->d_ino), d_type);
498 if (over) {
499 ufs_put_page(page);
500 return 0;
501 }
502 }
503 filp->f_pos += fs16_to_cpu(sb, de->d_reclen);
504 }
505 ufs_put_page(page);
506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return 0;
508}
509
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511/*
512 * ufs_delete_entry deletes a directory entry by merging it with the
513 * previous entry.
514 */
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700515int ufs_delete_entry(struct inode *inode, struct ufs_dir_entry *dir,
516 struct page * page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700518 struct super_block *sb = inode->i_sb;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700519 char *kaddr = page_address(page);
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800520 unsigned from = ((char*)dir - kaddr) & ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700521 unsigned to = ((char*)dir - kaddr) + fs16_to_cpu(sb, dir->d_reclen);
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700522 loff_t pos;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700523 struct ufs_dir_entry *pde = NULL;
524 struct ufs_dir_entry *de = (struct ufs_dir_entry *) (kaddr + from);
525 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700527 UFSD("ENTER\n");
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700528
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700529 UFSD("ino %u, reclen %u, namlen %u, name %s\n",
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700530 fs32_to_cpu(sb, de->d_ino),
531 fs16_to_cpu(sb, de->d_reclen),
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700532 ufs_get_de_namlen(sb, de), de->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700534 while ((char*)de < (char*)dir) {
535 if (de->d_reclen == 0) {
Harvey Harrison9746077a72008-04-28 02:16:17 -0700536 ufs_error(inode->i_sb, __func__,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700537 "zero-length directory entry");
538 err = -EIO;
539 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700541 pde = de;
542 de = ufs_next_entry(sb, de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700544 if (pde)
545 from = (char*)pde - (char*)page_address(page);
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700546
547 pos = page_offset(page) + from;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700548 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200549 err = ufs_prepare_chunk(page, pos, to - from);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700550 BUG_ON(err);
551 if (pde)
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700552 pde->d_reclen = cpu_to_fs16(sb, to - from);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700553 dir->d_ino = 0;
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700554 err = ufs_commit_chunk(page, pos, to - from);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700555 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
556 mark_inode_dirty(inode);
557out:
558 ufs_put_page(page);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700559 UFSD("EXIT\n");
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700560 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
563int ufs_make_empty(struct inode * inode, struct inode *dir)
564{
565 struct super_block * sb = dir->i_sb;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700566 struct address_space *mapping = inode->i_mapping;
567 struct page *page = grab_cache_page(mapping, 0);
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800568 const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 struct ufs_dir_entry * de;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700570 char *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 int err;
572
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700573 if (!page)
574 return -ENOMEM;
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700575
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200576 err = ufs_prepare_chunk(page, 0, chunk_size);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700577 if (err) {
578 unlock_page(page);
579 goto fail;
580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700582 kmap(page);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700583 base = (char*)page_address(page);
584 memset(base, 0, PAGE_CACHE_SIZE);
585
586 de = (struct ufs_dir_entry *) base;
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 de->d_ino = cpu_to_fs32(sb, inode->i_ino);
589 ufs_set_de_type(sb, de, inode->i_mode);
590 ufs_set_de_namlen(sb, de, 1);
591 de->d_reclen = cpu_to_fs16(sb, UFS_DIR_REC_LEN(1));
592 strcpy (de->d_name, ".");
593 de = (struct ufs_dir_entry *)
594 ((char *)de + fs16_to_cpu(sb, de->d_reclen));
595 de->d_ino = cpu_to_fs32(sb, dir->i_ino);
596 ufs_set_de_type(sb, de, dir->i_mode);
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800597 de->d_reclen = cpu_to_fs16(sb, chunk_size - UFS_DIR_REC_LEN(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 ufs_set_de_namlen(sb, de, 2);
599 strcpy (de->d_name, "..");
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700600 kunmap(page);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700601
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800602 err = ufs_commit_chunk(page, 0, chunk_size);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700603fail:
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700604 page_cache_release(page);
605 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
608/*
609 * routine to check that the specified directory is empty (for rmdir)
610 */
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700611int ufs_empty_dir(struct inode * inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700613 struct super_block *sb = inode->i_sb;
614 struct page *page = NULL;
615 unsigned long i, npages = ufs_dir_pages(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700617 for (i = 0; i < npages; i++) {
618 char *kaddr;
619 struct ufs_dir_entry *de;
620 page = ufs_get_page(inode, i);
621
622 if (IS_ERR(page))
623 continue;
624
625 kaddr = page_address(page);
626 de = (struct ufs_dir_entry *)kaddr;
627 kaddr += ufs_last_byte(inode, i) - UFS_DIR_REC_LEN(1);
628
629 while ((char *)de <= kaddr) {
630 if (de->d_reclen == 0) {
Harvey Harrison9746077a72008-04-28 02:16:17 -0700631 ufs_error(inode->i_sb, __func__,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700632 "zero-length directory entry: "
633 "kaddr=%p, de=%p\n", kaddr, de);
634 goto not_empty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700636 if (de->d_ino) {
637 u16 namelen=ufs_get_de_namlen(sb, de);
638 /* check for . and .. */
639 if (de->d_name[0] != '.')
640 goto not_empty;
641 if (namelen > 2)
642 goto not_empty;
643 if (namelen < 2) {
644 if (inode->i_ino !=
645 fs32_to_cpu(sb, de->d_ino))
646 goto not_empty;
647 } else if (de->d_name[1] != '.')
648 goto not_empty;
649 }
650 de = ufs_next_entry(sb, de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700652 ufs_put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return 1;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700655
656not_empty:
657 ufs_put_page(page);
658 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800661const struct file_operations ufs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 .read = generic_read_dir,
663 .readdir = ufs_readdir,
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200664 .fsync = generic_file_fsync,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +0200665 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666};