fs/ufs: restore s_lock mutex_init()
[linux/fpc-iii.git] / fs / ufs / dir.c
blob862d284d438e8ece7bd6026aa908ea5a8adfb4de
1 /*
2 * linux/fs/ufs/ufs_dir.c
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
9 * swab support by Francois-Rene Rideau <fare@tunes.org> 19970406
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>.
15 * Migration to usage of "page cache" on May 2006 by
16 * Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base.
19 #include <linux/time.h>
20 #include <linux/fs.h>
21 #include <linux/swap.h>
23 #include "ufs_fs.h"
24 #include "ufs.h"
25 #include "swab.h"
26 #include "util.h"
29 * NOTE! unlike strncmp, ufs_match returns 1 for success, 0 for failure.
31 * len <= UFS_MAXNAMLEN and de != NULL are guaranteed by caller.
33 static inline int ufs_match(struct super_block *sb, int len,
34 const unsigned char *name, struct ufs_dir_entry *de)
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);
43 static int ufs_commit_chunk(struct page *page, loff_t pos, unsigned len)
45 struct address_space *mapping = page->mapping;
46 struct inode *dir = mapping->host;
47 int err = 0;
49 dir->i_version++;
50 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);
55 if (IS_DIRSYNC(dir))
56 err = write_one_page(page, 1);
57 else
58 unlock_page(page);
59 return err;
62 static inline void ufs_put_page(struct page *page)
64 kunmap(page);
65 page_cache_release(page);
68 static inline unsigned long ufs_dir_pages(struct inode *inode)
70 return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
73 ino_t ufs_inode_by_name(struct inode *dir, const struct qstr *qstr)
75 ino_t res = 0;
76 struct ufs_dir_entry *de;
77 struct page *page;
79 de = ufs_find_entry(dir, qstr, &page);
80 if (de) {
81 res = fs32_to_cpu(dir->i_sb, de->d_ino);
82 ufs_put_page(page);
84 return res;
88 /* Releases the page */
89 void ufs_set_link(struct inode *dir, struct ufs_dir_entry *de,
90 struct page *page, struct inode *inode,
91 bool update_times)
93 loff_t pos = page_offset(page) +
94 (char *) de - (char *) page_address(page);
95 unsigned len = fs16_to_cpu(dir->i_sb, de->d_reclen);
96 int err;
98 lock_page(page);
99 err = ufs_prepare_chunk(page, pos, len);
100 BUG_ON(err);
102 de->d_ino = cpu_to_fs32(dir->i_sb, inode->i_ino);
103 ufs_set_de_type(dir->i_sb, de, inode->i_mode);
105 err = ufs_commit_chunk(page, pos, len);
106 ufs_put_page(page);
107 if (update_times)
108 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
109 mark_inode_dirty(dir);
113 static void ufs_check_page(struct page *page)
115 struct inode *dir = page->mapping->host;
116 struct super_block *sb = dir->i_sb;
117 char *kaddr = page_address(page);
118 unsigned offs, rec_len;
119 unsigned limit = PAGE_CACHE_SIZE;
120 const unsigned chunk_mask = UFS_SB(sb)->s_uspi->s_dirblksize - 1;
121 struct ufs_dir_entry *p;
122 char *error;
124 if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
125 limit = dir->i_size & ~PAGE_CACHE_MASK;
126 if (limit & chunk_mask)
127 goto Ebadsize;
128 if (!limit)
129 goto out;
131 for (offs = 0; offs <= limit - UFS_DIR_REC_LEN(1); offs += rec_len) {
132 p = (struct ufs_dir_entry *)(kaddr + offs);
133 rec_len = fs16_to_cpu(sb, p->d_reclen);
135 if (rec_len < UFS_DIR_REC_LEN(1))
136 goto Eshort;
137 if (rec_len & 3)
138 goto Ealign;
139 if (rec_len < UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, p)))
140 goto Enamelen;
141 if (((offs + rec_len - 1) ^ offs) & ~chunk_mask)
142 goto Espan;
143 if (fs32_to_cpu(sb, p->d_ino) > (UFS_SB(sb)->s_uspi->s_ipg *
144 UFS_SB(sb)->s_uspi->s_ncg))
145 goto Einumber;
147 if (offs != limit)
148 goto Eend;
149 out:
150 SetPageChecked(page);
151 return;
153 /* Too bad, we had an error */
155 Ebadsize:
156 ufs_error(sb, "ufs_check_page",
157 "size of directory #%lu is not a multiple of chunk size",
158 dir->i_ino
160 goto fail;
161 Eshort:
162 error = "rec_len is smaller than minimal";
163 goto bad_entry;
164 Ealign:
165 error = "unaligned directory entry";
166 goto bad_entry;
167 Enamelen:
168 error = "rec_len is too small for name_len";
169 goto bad_entry;
170 Espan:
171 error = "directory entry across blocks";
172 goto bad_entry;
173 Einumber:
174 error = "inode out of bounds";
175 bad_entry:
176 ufs_error (sb, "ufs_check_page", "bad entry in directory #%lu: %s - "
177 "offset=%lu, rec_len=%d, name_len=%d",
178 dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT)+offs,
179 rec_len, ufs_get_de_namlen(sb, p));
180 goto fail;
181 Eend:
182 p = (struct ufs_dir_entry *)(kaddr + offs);
183 ufs_error(sb, __func__,
184 "entry in directory #%lu spans the page boundary"
185 "offset=%lu",
186 dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs);
187 fail:
188 SetPageChecked(page);
189 SetPageError(page);
192 static struct page *ufs_get_page(struct inode *dir, unsigned long n)
194 struct address_space *mapping = dir->i_mapping;
195 struct page *page = read_mapping_page(mapping, n, NULL);
196 if (!IS_ERR(page)) {
197 kmap(page);
198 if (!PageChecked(page))
199 ufs_check_page(page);
200 if (PageError(page))
201 goto fail;
203 return page;
205 fail:
206 ufs_put_page(page);
207 return ERR_PTR(-EIO);
211 * Return the offset into page `page_nr' of the last valid
212 * byte in that page, plus one.
214 static unsigned
215 ufs_last_byte(struct inode *inode, unsigned long page_nr)
217 unsigned last_byte = inode->i_size;
219 last_byte -= page_nr << PAGE_CACHE_SHIFT;
220 if (last_byte > PAGE_CACHE_SIZE)
221 last_byte = PAGE_CACHE_SIZE;
222 return last_byte;
225 static inline struct ufs_dir_entry *
226 ufs_next_entry(struct super_block *sb, struct ufs_dir_entry *p)
228 return (struct ufs_dir_entry *)((char *)p +
229 fs16_to_cpu(sb, p->d_reclen));
232 struct ufs_dir_entry *ufs_dotdot(struct inode *dir, struct page **p)
234 struct page *page = ufs_get_page(dir, 0);
235 struct ufs_dir_entry *de = NULL;
237 if (!IS_ERR(page)) {
238 de = ufs_next_entry(dir->i_sb,
239 (struct ufs_dir_entry *)page_address(page));
240 *p = page;
242 return de;
246 * ufs_find_entry()
248 * finds an entry in the specified directory with the wanted name. It
249 * returns the page in which the entry was found, and the entry itself
250 * (as a parameter - res_dir). Page is returned mapped and unlocked.
251 * Entry is guaranteed to be valid.
253 struct ufs_dir_entry *ufs_find_entry(struct inode *dir, const struct qstr *qstr,
254 struct page **res_page)
256 struct super_block *sb = dir->i_sb;
257 const unsigned char *name = qstr->name;
258 int namelen = qstr->len;
259 unsigned reclen = UFS_DIR_REC_LEN(namelen);
260 unsigned long start, n;
261 unsigned long npages = ufs_dir_pages(dir);
262 struct page *page = NULL;
263 struct ufs_inode_info *ui = UFS_I(dir);
264 struct ufs_dir_entry *de;
266 UFSD("ENTER, dir_ino %lu, name %s, namlen %u\n", dir->i_ino, name, namelen);
268 if (npages == 0 || namelen > UFS_MAXNAMLEN)
269 goto out;
271 /* OFFSET_CACHE */
272 *res_page = NULL;
274 start = ui->i_dir_start_lookup;
276 if (start >= npages)
277 start = 0;
278 n = start;
279 do {
280 char *kaddr;
281 page = ufs_get_page(dir, n);
282 if (!IS_ERR(page)) {
283 kaddr = page_address(page);
284 de = (struct ufs_dir_entry *) kaddr;
285 kaddr += ufs_last_byte(dir, n) - reclen;
286 while ((char *) de <= kaddr) {
287 if (de->d_reclen == 0) {
288 ufs_error(dir->i_sb, __func__,
289 "zero-length directory entry");
290 ufs_put_page(page);
291 goto out;
293 if (ufs_match(sb, namelen, name, de))
294 goto found;
295 de = ufs_next_entry(sb, de);
297 ufs_put_page(page);
299 if (++n >= npages)
300 n = 0;
301 } while (n != start);
302 out:
303 return NULL;
305 found:
306 *res_page = page;
307 ui->i_dir_start_lookup = n;
308 return de;
312 * Parent is locked.
314 int ufs_add_link(struct dentry *dentry, struct inode *inode)
316 struct inode *dir = d_inode(dentry->d_parent);
317 const unsigned char *name = dentry->d_name.name;
318 int namelen = dentry->d_name.len;
319 struct super_block *sb = dir->i_sb;
320 unsigned reclen = UFS_DIR_REC_LEN(namelen);
321 const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize;
322 unsigned short rec_len, name_len;
323 struct page *page = NULL;
324 struct ufs_dir_entry *de;
325 unsigned long npages = ufs_dir_pages(dir);
326 unsigned long n;
327 char *kaddr;
328 loff_t pos;
329 int err;
331 UFSD("ENTER, name %s, namelen %u\n", name, namelen);
334 * We take care of directory expansion in the same loop.
335 * This code plays outside i_size, so it locks the page
336 * to protect that region.
338 for (n = 0; n <= npages; n++) {
339 char *dir_end;
341 page = ufs_get_page(dir, n);
342 err = PTR_ERR(page);
343 if (IS_ERR(page))
344 goto out;
345 lock_page(page);
346 kaddr = page_address(page);
347 dir_end = kaddr + ufs_last_byte(dir, n);
348 de = (struct ufs_dir_entry *)kaddr;
349 kaddr += PAGE_CACHE_SIZE - reclen;
350 while ((char *)de <= kaddr) {
351 if ((char *)de == dir_end) {
352 /* We hit i_size */
353 name_len = 0;
354 rec_len = chunk_size;
355 de->d_reclen = cpu_to_fs16(sb, chunk_size);
356 de->d_ino = 0;
357 goto got_it;
359 if (de->d_reclen == 0) {
360 ufs_error(dir->i_sb, __func__,
361 "zero-length directory entry");
362 err = -EIO;
363 goto out_unlock;
365 err = -EEXIST;
366 if (ufs_match(sb, namelen, name, de))
367 goto out_unlock;
368 name_len = UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, de));
369 rec_len = fs16_to_cpu(sb, de->d_reclen);
370 if (!de->d_ino && rec_len >= reclen)
371 goto got_it;
372 if (rec_len >= name_len + reclen)
373 goto got_it;
374 de = (struct ufs_dir_entry *) ((char *) de + rec_len);
376 unlock_page(page);
377 ufs_put_page(page);
379 BUG();
380 return -EINVAL;
382 got_it:
383 pos = page_offset(page) +
384 (char*)de - (char*)page_address(page);
385 err = ufs_prepare_chunk(page, pos, rec_len);
386 if (err)
387 goto out_unlock;
388 if (de->d_ino) {
389 struct ufs_dir_entry *de1 =
390 (struct ufs_dir_entry *) ((char *) de + name_len);
391 de1->d_reclen = cpu_to_fs16(sb, rec_len - name_len);
392 de->d_reclen = cpu_to_fs16(sb, name_len);
394 de = de1;
397 ufs_set_de_namlen(sb, de, namelen);
398 memcpy(de->d_name, name, namelen + 1);
399 de->d_ino = cpu_to_fs32(sb, inode->i_ino);
400 ufs_set_de_type(sb, de, inode->i_mode);
402 err = ufs_commit_chunk(page, pos, rec_len);
403 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
405 mark_inode_dirty(dir);
406 /* OFFSET_CACHE */
407 out_put:
408 ufs_put_page(page);
409 out:
410 return err;
411 out_unlock:
412 unlock_page(page);
413 goto out_put;
416 static inline unsigned
417 ufs_validate_entry(struct super_block *sb, char *base,
418 unsigned offset, unsigned mask)
420 struct ufs_dir_entry *de = (struct ufs_dir_entry*)(base + offset);
421 struct ufs_dir_entry *p = (struct ufs_dir_entry*)(base + (offset&mask));
422 while ((char*)p < (char*)de) {
423 if (p->d_reclen == 0)
424 break;
425 p = ufs_next_entry(sb, p);
427 return (char *)p - base;
432 * This is blatantly stolen from ext2fs
434 static int
435 ufs_readdir(struct file *file, struct dir_context *ctx)
437 loff_t pos = ctx->pos;
438 struct inode *inode = file_inode(file);
439 struct super_block *sb = inode->i_sb;
440 unsigned int offset = pos & ~PAGE_CACHE_MASK;
441 unsigned long n = pos >> PAGE_CACHE_SHIFT;
442 unsigned long npages = ufs_dir_pages(inode);
443 unsigned chunk_mask = ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1);
444 int need_revalidate = file->f_version != inode->i_version;
445 unsigned flags = UFS_SB(sb)->s_flags;
447 UFSD("BEGIN\n");
449 if (pos > inode->i_size - UFS_DIR_REC_LEN(1))
450 return 0;
452 for ( ; n < npages; n++, offset = 0) {
453 char *kaddr, *limit;
454 struct ufs_dir_entry *de;
456 struct page *page = ufs_get_page(inode, n);
458 if (IS_ERR(page)) {
459 ufs_error(sb, __func__,
460 "bad page in #%lu",
461 inode->i_ino);
462 ctx->pos += PAGE_CACHE_SIZE - offset;
463 return -EIO;
465 kaddr = page_address(page);
466 if (unlikely(need_revalidate)) {
467 if (offset) {
468 offset = ufs_validate_entry(sb, kaddr, offset, chunk_mask);
469 ctx->pos = (n<<PAGE_CACHE_SHIFT) + offset;
471 file->f_version = inode->i_version;
472 need_revalidate = 0;
474 de = (struct ufs_dir_entry *)(kaddr+offset);
475 limit = kaddr + ufs_last_byte(inode, n) - UFS_DIR_REC_LEN(1);
476 for ( ;(char*)de <= limit; de = ufs_next_entry(sb, de)) {
477 if (de->d_reclen == 0) {
478 ufs_error(sb, __func__,
479 "zero-length directory entry");
480 ufs_put_page(page);
481 return -EIO;
483 if (de->d_ino) {
484 unsigned char d_type = DT_UNKNOWN;
486 UFSD("filldir(%s,%u)\n", de->d_name,
487 fs32_to_cpu(sb, de->d_ino));
488 UFSD("namlen %u\n", ufs_get_de_namlen(sb, de));
490 if ((flags & UFS_DE_MASK) == UFS_DE_44BSD)
491 d_type = de->d_u.d_44.d_type;
493 if (!dir_emit(ctx, de->d_name,
494 ufs_get_de_namlen(sb, de),
495 fs32_to_cpu(sb, de->d_ino),
496 d_type)) {
497 ufs_put_page(page);
498 return 0;
501 ctx->pos += fs16_to_cpu(sb, de->d_reclen);
503 ufs_put_page(page);
505 return 0;
510 * ufs_delete_entry deletes a directory entry by merging it with the
511 * previous entry.
513 int ufs_delete_entry(struct inode *inode, struct ufs_dir_entry *dir,
514 struct page * page)
516 struct super_block *sb = inode->i_sb;
517 char *kaddr = page_address(page);
518 unsigned from = ((char*)dir - kaddr) & ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1);
519 unsigned to = ((char*)dir - kaddr) + fs16_to_cpu(sb, dir->d_reclen);
520 loff_t pos;
521 struct ufs_dir_entry *pde = NULL;
522 struct ufs_dir_entry *de = (struct ufs_dir_entry *) (kaddr + from);
523 int err;
525 UFSD("ENTER\n");
527 UFSD("ino %u, reclen %u, namlen %u, name %s\n",
528 fs32_to_cpu(sb, de->d_ino),
529 fs16_to_cpu(sb, de->d_reclen),
530 ufs_get_de_namlen(sb, de), de->d_name);
532 while ((char*)de < (char*)dir) {
533 if (de->d_reclen == 0) {
534 ufs_error(inode->i_sb, __func__,
535 "zero-length directory entry");
536 err = -EIO;
537 goto out;
539 pde = de;
540 de = ufs_next_entry(sb, de);
542 if (pde)
543 from = (char*)pde - (char*)page_address(page);
545 pos = page_offset(page) + from;
546 lock_page(page);
547 err = ufs_prepare_chunk(page, pos, to - from);
548 BUG_ON(err);
549 if (pde)
550 pde->d_reclen = cpu_to_fs16(sb, to - from);
551 dir->d_ino = 0;
552 err = ufs_commit_chunk(page, pos, to - from);
553 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
554 mark_inode_dirty(inode);
555 out:
556 ufs_put_page(page);
557 UFSD("EXIT\n");
558 return err;
561 int ufs_make_empty(struct inode * inode, struct inode *dir)
563 struct super_block * sb = dir->i_sb;
564 struct address_space *mapping = inode->i_mapping;
565 struct page *page = grab_cache_page(mapping, 0);
566 const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize;
567 struct ufs_dir_entry * de;
568 char *base;
569 int err;
571 if (!page)
572 return -ENOMEM;
574 err = ufs_prepare_chunk(page, 0, chunk_size);
575 if (err) {
576 unlock_page(page);
577 goto fail;
580 kmap(page);
581 base = (char*)page_address(page);
582 memset(base, 0, PAGE_CACHE_SIZE);
584 de = (struct ufs_dir_entry *) base;
586 de->d_ino = cpu_to_fs32(sb, inode->i_ino);
587 ufs_set_de_type(sb, de, inode->i_mode);
588 ufs_set_de_namlen(sb, de, 1);
589 de->d_reclen = cpu_to_fs16(sb, UFS_DIR_REC_LEN(1));
590 strcpy (de->d_name, ".");
591 de = (struct ufs_dir_entry *)
592 ((char *)de + fs16_to_cpu(sb, de->d_reclen));
593 de->d_ino = cpu_to_fs32(sb, dir->i_ino);
594 ufs_set_de_type(sb, de, dir->i_mode);
595 de->d_reclen = cpu_to_fs16(sb, chunk_size - UFS_DIR_REC_LEN(1));
596 ufs_set_de_namlen(sb, de, 2);
597 strcpy (de->d_name, "..");
598 kunmap(page);
600 err = ufs_commit_chunk(page, 0, chunk_size);
601 fail:
602 page_cache_release(page);
603 return err;
607 * routine to check that the specified directory is empty (for rmdir)
609 int ufs_empty_dir(struct inode * inode)
611 struct super_block *sb = inode->i_sb;
612 struct page *page = NULL;
613 unsigned long i, npages = ufs_dir_pages(inode);
615 for (i = 0; i < npages; i++) {
616 char *kaddr;
617 struct ufs_dir_entry *de;
618 page = ufs_get_page(inode, i);
620 if (IS_ERR(page))
621 continue;
623 kaddr = page_address(page);
624 de = (struct ufs_dir_entry *)kaddr;
625 kaddr += ufs_last_byte(inode, i) - UFS_DIR_REC_LEN(1);
627 while ((char *)de <= kaddr) {
628 if (de->d_reclen == 0) {
629 ufs_error(inode->i_sb, __func__,
630 "zero-length directory entry: "
631 "kaddr=%p, de=%p\n", kaddr, de);
632 goto not_empty;
634 if (de->d_ino) {
635 u16 namelen=ufs_get_de_namlen(sb, de);
636 /* check for . and .. */
637 if (de->d_name[0] != '.')
638 goto not_empty;
639 if (namelen > 2)
640 goto not_empty;
641 if (namelen < 2) {
642 if (inode->i_ino !=
643 fs32_to_cpu(sb, de->d_ino))
644 goto not_empty;
645 } else if (de->d_name[1] != '.')
646 goto not_empty;
648 de = ufs_next_entry(sb, de);
650 ufs_put_page(page);
652 return 1;
654 not_empty:
655 ufs_put_page(page);
656 return 0;
659 const struct file_operations ufs_dir_operations = {
660 .read = generic_read_dir,
661 .iterate = ufs_readdir,
662 .fsync = generic_file_fsync,
663 .llseek = generic_file_llseek,