blob: 7f0a0aa63584e729d53d01177ed74a0372ea830a [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>
21#include <linux/ufs_fs.h>
22#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/sched.h>
24
25#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,
34 const char * const name, struct ufs_dir_entry * de)
35{
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
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070043static int ufs_commit_chunk(struct page *page, unsigned from, unsigned to)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070045 struct inode *dir = page->mapping->host;
46 int err = 0;
47 dir->i_version++;
48 page->mapping->a_ops->commit_write(NULL, page, from, to);
49 if (IS_DIRSYNC(dir))
50 err = write_one_page(page, 1);
51 else
52 unlock_page(page);
53 return err;
54}
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070056static inline void ufs_put_page(struct page *page)
57{
58 kunmap(page);
59 page_cache_release(page);
60}
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070062static inline unsigned long ufs_dir_pages(struct inode *inode)
63{
64 return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
65}
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070067ino_t ufs_inode_by_name(struct inode *dir, struct dentry *dentry)
68{
69 ino_t res = 0;
70 struct ufs_dir_entry *de;
71 struct page *page;
72
73 de = ufs_find_entry(dir, dentry, &page);
74 if (de) {
75 res = fs32_to_cpu(dir->i_sb, de->d_ino);
76 ufs_put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070078 return res;
79}
80
81
82/* Releases the page */
83void ufs_set_link(struct inode *dir, struct ufs_dir_entry *de,
84 struct page *page, struct inode *inode)
85{
86 unsigned from = (char *) de - (char *) page_address(page);
87 unsigned to = from + fs16_to_cpu(dir->i_sb, de->d_reclen);
88 int err;
89
90 lock_page(page);
91 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
92 BUG_ON(err);
93 de->d_ino = cpu_to_fs32(dir->i_sb, inode->i_ino);
94 ufs_set_de_type(dir->i_sb, de, inode->i_mode);
95 err = ufs_commit_chunk(page, from, to);
96 ufs_put_page(page);
97 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
98 mark_inode_dirty(dir);
99}
100
101
102static void ufs_check_page(struct page *page)
103{
104 struct inode *dir = page->mapping->host;
105 struct super_block *sb = dir->i_sb;
106 char *kaddr = page_address(page);
107 unsigned offs, rec_len;
108 unsigned limit = PAGE_CACHE_SIZE;
109 struct ufs_dir_entry *p;
110 char *error;
111
112 if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
113 limit = dir->i_size & ~PAGE_CACHE_MASK;
114 if (limit & (UFS_SECTOR_SIZE - 1))
115 goto Ebadsize;
116 if (!limit)
117 goto out;
118 }
119 for (offs = 0; offs <= limit - UFS_DIR_REC_LEN(1); offs += rec_len) {
120 p = (struct ufs_dir_entry *)(kaddr + offs);
121 rec_len = fs16_to_cpu(sb, p->d_reclen);
122
123 if (rec_len < UFS_DIR_REC_LEN(1))
124 goto Eshort;
125 if (rec_len & 3)
126 goto Ealign;
127 if (rec_len < UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, p)))
128 goto Enamelen;
129 if (((offs + rec_len - 1) ^ offs) & ~(UFS_SECTOR_SIZE-1))
130 goto Espan;
131 if (fs32_to_cpu(sb, p->d_ino) > (UFS_SB(sb)->s_uspi->s_ipg *
132 UFS_SB(sb)->s_uspi->s_ncg))
133 goto Einumber;
134 }
135 if (offs != limit)
136 goto Eend;
137out:
138 SetPageChecked(page);
139 return;
140
141 /* Too bad, we had an error */
142
143Ebadsize:
144 ufs_error(sb, "ufs_check_page",
145 "size of directory #%lu is not a multiple of chunk size",
146 dir->i_ino
147 );
148 goto fail;
149Eshort:
150 error = "rec_len is smaller than minimal";
151 goto bad_entry;
152Ealign:
153 error = "unaligned directory entry";
154 goto bad_entry;
155Enamelen:
156 error = "rec_len is too small for name_len";
157 goto bad_entry;
158Espan:
159 error = "directory entry across blocks";
160 goto bad_entry;
161Einumber:
162 error = "inode out of bounds";
163bad_entry:
164 ufs_error (sb, "ufs_check_page", "bad entry in directory #%lu: %s - "
165 "offset=%lu, rec_len=%d, name_len=%d",
166 dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT)+offs,
167 rec_len, ufs_get_de_namlen(sb, p));
168 goto fail;
169Eend:
170 p = (struct ufs_dir_entry *)(kaddr + offs);
171 ufs_error (sb, "ext2_check_page",
172 "entry in directory #%lu spans the page boundary"
173 "offset=%lu",
174 dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs);
175fail:
176 SetPageChecked(page);
177 SetPageError(page);
178}
179
180static struct page *ufs_get_page(struct inode *dir, unsigned long n)
181{
182 struct address_space *mapping = dir->i_mapping;
183 struct page *page = read_cache_page(mapping, n,
184 (filler_t*)mapping->a_ops->readpage, NULL);
185 if (!IS_ERR(page)) {
186 wait_on_page_locked(page);
187 kmap(page);
188 if (!PageUptodate(page))
189 goto fail;
190 if (!PageChecked(page))
191 ufs_check_page(page);
192 if (PageError(page))
193 goto fail;
194 }
195 return page;
196
197fail:
198 ufs_put_page(page);
199 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202/*
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700203 * Return the offset into page `page_nr' of the last valid
204 * byte in that page, plus one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 */
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700206static unsigned
207ufs_last_byte(struct inode *inode, unsigned long page_nr)
208{
209 unsigned last_byte = inode->i_size;
210
211 last_byte -= page_nr << PAGE_CACHE_SHIFT;
212 if (last_byte > PAGE_CACHE_SIZE)
213 last_byte = PAGE_CACHE_SIZE;
214 return last_byte;
215}
216
217static inline struct ufs_dir_entry *
218ufs_next_entry(struct super_block *sb, struct ufs_dir_entry *p)
219{
220 return (struct ufs_dir_entry *)((char *)p +
221 fs16_to_cpu(sb, p->d_reclen));
222}
223
224struct ufs_dir_entry *ufs_dotdot(struct inode *dir, struct page **p)
225{
226 struct page *page = ufs_get_page(dir, 0);
227 struct ufs_dir_entry *de = NULL;
228
229 if (!IS_ERR(page)) {
230 de = ufs_next_entry(dir->i_sb,
231 (struct ufs_dir_entry *)page_address(page));
232 *p = page;
233 }
234 return de;
235}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237/*
238 * ufs_find_entry()
239 *
240 * finds an entry in the specified directory with the wanted name. It
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700241 * returns the page in which the entry was found, and the entry itself
242 * (as a parameter - res_dir). Page is returned mapped and unlocked.
243 * Entry is guaranteed to be valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 */
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700245struct ufs_dir_entry *ufs_find_entry(struct inode *dir, struct dentry *dentry,
246 struct page **res_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 struct super_block *sb = dir->i_sb;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700249 const char *name = dentry->d_name.name;
250 int namelen = dentry->d_name.len;
251 unsigned reclen = UFS_DIR_REC_LEN(namelen);
252 unsigned long start, n;
253 unsigned long npages = ufs_dir_pages(dir);
254 struct page *page = NULL;
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700255 struct ufs_inode_info *ui = UFS_I(dir);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700256 struct ufs_dir_entry *de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700258 UFSD("ENTER, dir_ino %lu, name %s, namlen %u\n", dir->i_ino, name, namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700260 if (npages == 0 || namelen > UFS_MAXNAMLEN)
261 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700263 /* OFFSET_CACHE */
264 *res_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700266 start = ui->i_dir_start_lookup;
267
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700268 if (start >= npages)
269 start = 0;
270 n = start;
271 do {
272 char *kaddr;
273 page = ufs_get_page(dir, n);
274 if (!IS_ERR(page)) {
275 kaddr = page_address(page);
276 de = (struct ufs_dir_entry *) kaddr;
277 kaddr += ufs_last_byte(dir, n) - reclen;
278 while ((char *) de <= kaddr) {
279 if (de->d_reclen == 0) {
280 ufs_error(dir->i_sb, __FUNCTION__,
281 "zero-length directory entry");
282 ufs_put_page(page);
283 goto out;
284 }
285 if (ufs_match(sb, namelen, name, de))
286 goto found;
287 de = ufs_next_entry(sb, de);
288 }
289 ufs_put_page(page);
290 }
291 if (++n >= npages)
292 n = 0;
293 } while (n != start);
294out:
295 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700297found:
298 *res_page = page;
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700299 ui->i_dir_start_lookup = n;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700300 return de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
303/*
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700304 * Parent is locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 */
306int ufs_add_link(struct dentry *dentry, struct inode *inode)
307{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct inode *dir = dentry->d_parent->d_inode;
309 const char *name = dentry->d_name.name;
310 int namelen = dentry->d_name.len;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700311 struct super_block *sb = dir->i_sb;
312 unsigned reclen = UFS_DIR_REC_LEN(namelen);
313 unsigned short rec_len, name_len;
314 struct page *page = NULL;
315 struct ufs_dir_entry *de;
316 unsigned long npages = ufs_dir_pages(dir);
317 unsigned long n;
318 char *kaddr;
319 unsigned from, to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int err;
321
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700322 UFSD("ENTER, name %s, namelen %u\n", name, namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700324 /*
325 * We take care of directory expansion in the same loop.
326 * This code plays outside i_size, so it locks the page
327 * to protect that region.
328 */
329 for (n = 0; n <= npages; n++) {
330 char *dir_end;
331
332 page = ufs_get_page(dir, n);
333 err = PTR_ERR(page);
334 if (IS_ERR(page))
335 goto out;
336 lock_page(page);
337 kaddr = page_address(page);
338 dir_end = kaddr + ufs_last_byte(dir, n);
339 de = (struct ufs_dir_entry *)kaddr;
340 kaddr += PAGE_CACHE_SIZE - reclen;
341 while ((char *)de <= kaddr) {
342 if ((char *)de == dir_end) {
343 /* We hit i_size */
344 name_len = 0;
345 rec_len = UFS_SECTOR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 de->d_reclen = cpu_to_fs16(sb, UFS_SECTOR_SIZE);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700347 de->d_ino = 0;
348 goto got_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700350 if (de->d_reclen == 0) {
351 ufs_error(dir->i_sb, __FUNCTION__,
352 "zero-length directory entry");
353 err = -EIO;
354 goto out_unlock;
355 }
356 err = -EEXIST;
357 if (ufs_match(sb, namelen, name, de))
358 goto out_unlock;
359 name_len = UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, de));
360 rec_len = fs16_to_cpu(sb, de->d_reclen);
361 if (!de->d_ino && rec_len >= reclen)
362 goto got_it;
363 if (rec_len >= name_len + reclen)
364 goto got_it;
365 de = (struct ufs_dir_entry *) ((char *) de + rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700367 unlock_page(page);
368 ufs_put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700370 BUG();
371 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700373got_it:
374 from = (char*)de - (char*)page_address(page);
375 to = from + rec_len;
376 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
377 if (err)
378 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (de->d_ino) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700380 struct ufs_dir_entry *de1 =
381 (struct ufs_dir_entry *) ((char *) de + name_len);
382 de1->d_reclen = cpu_to_fs16(sb, rec_len - name_len);
383 de->d_reclen = cpu_to_fs16(sb, name_len);
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 de = de1;
386 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 ufs_set_de_namlen(sb, de, namelen);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700389 memcpy(de->d_name, name, namelen + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 de->d_ino = cpu_to_fs32(sb, inode->i_ino);
391 ufs_set_de_type(sb, de, inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700393 err = ufs_commit_chunk(page, from, to);
394 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
395
396 mark_inode_dirty(dir);
397 /* OFFSET_CACHE */
398out_put:
399 ufs_put_page(page);
400out:
401 return err;
402out_unlock:
403 unlock_page(page);
404 goto out_put;
405}
406
407static inline unsigned
408ufs_validate_entry(struct super_block *sb, char *base,
409 unsigned offset, unsigned mask)
410{
411 struct ufs_dir_entry *de = (struct ufs_dir_entry*)(base + offset);
412 struct ufs_dir_entry *p = (struct ufs_dir_entry*)(base + (offset&mask));
413 while ((char*)p < (char*)de) {
414 if (p->d_reclen == 0)
415 break;
416 p = ufs_next_entry(sb, p);
417 }
418 return (char *)p - base;
419}
420
421
422/*
423 * This is blatantly stolen from ext2fs
424 */
425static int
426ufs_readdir(struct file *filp, void *dirent, filldir_t filldir)
427{
428 loff_t pos = filp->f_pos;
429 struct inode *inode = filp->f_dentry->d_inode;
430 struct super_block *sb = inode->i_sb;
431 unsigned int offset = pos & ~PAGE_CACHE_MASK;
432 unsigned long n = pos >> PAGE_CACHE_SHIFT;
433 unsigned long npages = ufs_dir_pages(inode);
434 unsigned chunk_mask = ~(UFS_SECTOR_SIZE - 1);
435 int need_revalidate = filp->f_version != inode->i_version;
436 unsigned flags = UFS_SB(sb)->s_flags;
437
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700438 UFSD("BEGIN\n");
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700439
440 if (pos > inode->i_size - UFS_DIR_REC_LEN(1))
441 return 0;
442
443 for ( ; n < npages; n++, offset = 0) {
444 char *kaddr, *limit;
445 struct ufs_dir_entry *de;
446
447 struct page *page = ufs_get_page(inode, n);
448
449 if (IS_ERR(page)) {
450 ufs_error(sb, __FUNCTION__,
451 "bad page in #%lu",
452 inode->i_ino);
453 filp->f_pos += PAGE_CACHE_SIZE - offset;
454 return -EIO;
455 }
456 kaddr = page_address(page);
457 if (unlikely(need_revalidate)) {
458 if (offset) {
459 offset = ufs_validate_entry(sb, kaddr, offset, chunk_mask);
460 filp->f_pos = (n<<PAGE_CACHE_SHIFT) + offset;
461 }
462 filp->f_version = inode->i_version;
463 need_revalidate = 0;
464 }
465 de = (struct ufs_dir_entry *)(kaddr+offset);
466 limit = kaddr + ufs_last_byte(inode, n) - UFS_DIR_REC_LEN(1);
467 for ( ;(char*)de <= limit; de = ufs_next_entry(sb, de)) {
468 if (de->d_reclen == 0) {
469 ufs_error(sb, __FUNCTION__,
470 "zero-length directory entry");
471 ufs_put_page(page);
472 return -EIO;
473 }
474 if (de->d_ino) {
475 int over;
476 unsigned char d_type = DT_UNKNOWN;
477
478 offset = (char *)de - kaddr;
479
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700480 UFSD("filldir(%s,%u)\n", de->d_name,
481 fs32_to_cpu(sb, de->d_ino));
482 UFSD("namlen %u\n", ufs_get_de_namlen(sb, de));
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700483
484 if ((flags & UFS_DE_MASK) == UFS_DE_44BSD)
485 d_type = de->d_u.d_44.d_type;
486
487 over = filldir(dirent, de->d_name,
488 ufs_get_de_namlen(sb, de),
489 (n<<PAGE_CACHE_SHIFT) | offset,
490 fs32_to_cpu(sb, de->d_ino), d_type);
491 if (over) {
492 ufs_put_page(page);
493 return 0;
494 }
495 }
496 filp->f_pos += fs16_to_cpu(sb, de->d_reclen);
497 }
498 ufs_put_page(page);
499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return 0;
501}
502
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504/*
505 * ufs_delete_entry deletes a directory entry by merging it with the
506 * previous entry.
507 */
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700508int ufs_delete_entry(struct inode *inode, struct ufs_dir_entry *dir,
509 struct page * page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700511 struct super_block *sb = inode->i_sb;
512 struct address_space *mapping = page->mapping;
513 char *kaddr = page_address(page);
514 unsigned from = ((char*)dir - kaddr) & ~(UFS_SECTOR_SIZE - 1);
515 unsigned to = ((char*)dir - kaddr) + fs16_to_cpu(sb, dir->d_reclen);
516 struct ufs_dir_entry *pde = NULL;
517 struct ufs_dir_entry *de = (struct ufs_dir_entry *) (kaddr + from);
518 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700520 UFSD("ENTER\n");
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700521
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700522 UFSD("ino %u, reclen %u, namlen %u, name %s\n",
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700523 fs32_to_cpu(sb, de->d_ino),
524 fs16_to_cpu(sb, de->d_reclen),
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700525 ufs_get_de_namlen(sb, de), de->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700527 while ((char*)de < (char*)dir) {
528 if (de->d_reclen == 0) {
529 ufs_error(inode->i_sb, __FUNCTION__,
530 "zero-length directory entry");
531 err = -EIO;
532 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700534 pde = de;
535 de = ufs_next_entry(sb, de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700537 if (pde)
538 from = (char*)pde - (char*)page_address(page);
539 lock_page(page);
540 err = mapping->a_ops->prepare_write(NULL, page, from, to);
541 BUG_ON(err);
542 if (pde)
543 pde->d_reclen = cpu_to_fs16(sb, to-from);
544 dir->d_ino = 0;
545 err = ufs_commit_chunk(page, from, to);
546 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
547 mark_inode_dirty(inode);
548out:
549 ufs_put_page(page);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700550 UFSD("EXIT\n");
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700551 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
554int ufs_make_empty(struct inode * inode, struct inode *dir)
555{
556 struct super_block * sb = dir->i_sb;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700557 struct address_space *mapping = inode->i_mapping;
558 struct page *page = grab_cache_page(mapping, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 struct ufs_dir_entry * de;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700560 char *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 int err;
562
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700563 if (!page)
564 return -ENOMEM;
565 kmap(page);
566 err = mapping->a_ops->prepare_write(NULL, page, 0, UFS_SECTOR_SIZE);
567 if (err) {
568 unlock_page(page);
569 goto fail;
570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700572
573 base = (char*)page_address(page);
574 memset(base, 0, PAGE_CACHE_SIZE);
575
576 de = (struct ufs_dir_entry *) base;
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 de->d_ino = cpu_to_fs32(sb, inode->i_ino);
579 ufs_set_de_type(sb, de, inode->i_mode);
580 ufs_set_de_namlen(sb, de, 1);
581 de->d_reclen = cpu_to_fs16(sb, UFS_DIR_REC_LEN(1));
582 strcpy (de->d_name, ".");
583 de = (struct ufs_dir_entry *)
584 ((char *)de + fs16_to_cpu(sb, de->d_reclen));
585 de->d_ino = cpu_to_fs32(sb, dir->i_ino);
586 ufs_set_de_type(sb, de, dir->i_mode);
587 de->d_reclen = cpu_to_fs16(sb, UFS_SECTOR_SIZE - UFS_DIR_REC_LEN(1));
588 ufs_set_de_namlen(sb, de, 2);
589 strcpy (de->d_name, "..");
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700590
591 err = ufs_commit_chunk(page, 0, UFS_SECTOR_SIZE);
592fail:
593 kunmap(page);
594 page_cache_release(page);
595 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
598/*
599 * routine to check that the specified directory is empty (for rmdir)
600 */
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700601int ufs_empty_dir(struct inode * inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700603 struct super_block *sb = inode->i_sb;
604 struct page *page = NULL;
605 unsigned long i, npages = ufs_dir_pages(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700607 for (i = 0; i < npages; i++) {
608 char *kaddr;
609 struct ufs_dir_entry *de;
610 page = ufs_get_page(inode, i);
611
612 if (IS_ERR(page))
613 continue;
614
615 kaddr = page_address(page);
616 de = (struct ufs_dir_entry *)kaddr;
617 kaddr += ufs_last_byte(inode, i) - UFS_DIR_REC_LEN(1);
618
619 while ((char *)de <= kaddr) {
620 if (de->d_reclen == 0) {
621 ufs_error(inode->i_sb, __FUNCTION__,
622 "zero-length directory entry: "
623 "kaddr=%p, de=%p\n", kaddr, de);
624 goto not_empty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700626 if (de->d_ino) {
627 u16 namelen=ufs_get_de_namlen(sb, de);
628 /* check for . and .. */
629 if (de->d_name[0] != '.')
630 goto not_empty;
631 if (namelen > 2)
632 goto not_empty;
633 if (namelen < 2) {
634 if (inode->i_ino !=
635 fs32_to_cpu(sb, de->d_ino))
636 goto not_empty;
637 } else if (de->d_name[1] != '.')
638 goto not_empty;
639 }
640 de = ufs_next_entry(sb, de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700642 ufs_put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return 1;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700645
646not_empty:
647 ufs_put_page(page);
648 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800651const struct file_operations ufs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 .read = generic_read_dir,
653 .readdir = ufs_readdir,
654 .fsync = file_fsync,
655};