Whamcloud - gitweb
031910b1031fdab406518f71a5190b1782bf87cc
[fs/lustre-release.git] / lustre / obdfs / namei.c
1 /*
2  *  linux/fs/obdfs/namei.c
3  *
4  * Copyright (C) 1992, 1993, 1994, 1995
5  * Remy Card (card@masi.ibp.fr)
6  * Laboratoire MASI - Institut Blaise Pascal
7  * Universite Pierre et Marie Curie (Paris VI)
8  *
9  *  from
10  *
11  *  linux/fs/ext2/namei.c
12  *
13  *  Copyright (C) 1991, 1992  Linus Torvalds
14  *
15  *  Big-endian to little-endian byte-swapping/bitmaps by
16  *        David S. Miller (davem@caip.rutgers.edu), 1995
17  *  Directory entry file type support and forward compatibility hooks
18  *      for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
19  * 
20  *  Changes for use in OBDFS
21  *  Copyright (c) 1999, Seagate Technology Inc.
22  * 
23  */
24
25 #include <asm/uaccess.h>
26
27 #include <linux/errno.h>
28 #include <linux/fs.h>
29 #include <linux/ext2_fs.h>
30 #include <linux/fcntl.h>
31 #include <linux/sched.h>
32 #include <linux/stat.h>
33 #include <linux/string.h>
34 #include <linux/locks.h>
35 #include <linux/quotaops.h>
36 #include <linux/iobuf.h>
37
38 #include <linux/obd_support.h>
39 #include <linux/obdfs.h>
40
41 /*
42  * define how far ahead to read directories while searching them.
43  */
44 #define NAMEI_RA_CHUNKS  2
45 #define NAMEI_RA_BLOCKS  4
46 #define NAMEI_RA_SIZE        (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
47 #define NAMEI_RA_INDEX(c,b)  (((c) * NAMEI_RA_BLOCKS) + (b))
48
49 /*
50  * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure.
51  *
52  * `len <= EXT2_NAME_LEN' is guaranteed by caller.
53  * `de != NULL' is guaranteed by caller.
54  */
55 static inline int ext2_match (int len, const char * const name,
56                        struct ext2_dir_entry_2 * de)
57 {
58         if (len != de->name_len)
59                 return 0;
60         if (!de->inode)
61                 return 0;
62         return !memcmp(name, de->name, len);
63 }
64
65 /*
66  *      ext2_find_entry()
67  *
68  * finds an entry in the specified directory with the wanted name. It
69  * returns the cache buffer in which the entry was found, and the entry
70  * itself (as a parameter - res_dir). It does NOT read the inode of the
71  * entry - you'll have to do that yourself if you want to.
72  */
73 static struct page * obdfs_find_entry (struct inode * dir,
74                                              const char * const name, int namelen,
75                                              struct ext2_dir_entry_2 ** res_dir, int lock)
76 {
77         struct super_block * sb;
78         unsigned long offset;
79         struct page * page;
80         ENTRY;
81         CDEBUG(D_INODE, "find entry for %*s\n", namelen, name);
82
83         *res_dir = NULL;
84         sb = dir->i_sb;
85
86         if (namelen > EXT2_NAME_LEN)
87                 return NULL;
88
89         CDEBUG(D_INODE, "dirsize is %Ld\n", dir->i_size);
90
91         page = 0;
92         offset = 0;
93         while ( offset < dir->i_size ) {
94                 struct ext2_dir_entry_2 * de;
95                 char * dlimit;
96
97                 page = obdfs_getpage(dir, offset, 0, lock);
98
99                 if ( !page ) {
100                         CDEBUG(D_INODE, "No page, offset %lx\n", offset);
101                         return NULL;
102                 }
103
104                 de = (struct ext2_dir_entry_2 *) page_address(page);
105                 dlimit = (char *)page_address(page) + PAGE_SIZE; 
106                 while ((char *) de < dlimit) {
107                         /* this code is executed quadratically often */
108                         /* do minimal checking `by hand' */
109                         int de_len;
110                         /* CDEBUG(D_INODE, "Entry %p len %d, page at %#lx - %#lx , offset %lx\n",
111                                de, le16_to_cpu(de->rec_len), page_address(page),
112                                page_address(page) + PAGE_SIZE, offset); */
113
114                         if ((char *) de + namelen <= dlimit &&
115                             ext2_match (namelen, name, de)) {
116                                 /* found a match -
117                                    just to be sure, do a full check */
118                                 if (!obdfs_check_dir_entry("ext2_find_entry",
119                                                           dir, de, page, offset))
120                                         goto failure;
121                                 *res_dir = de;
122                                 EXIT;
123                                 return page;
124                         }
125                         /* prevent looping on a bad block */
126                         de_len = le16_to_cpu(de->rec_len);
127                         if (de_len <= 0) {
128                                 printk("Bad entry at %p len %d\n", de, de_len);
129                                 goto failure;
130                         }
131                         offset += de_len;
132                         de = (struct ext2_dir_entry_2 *)
133                                 ((char *) de + de_len);
134                         /* CDEBUG(D_INODE, "Next while %lx\n", offset); */
135                 }
136                 if ( lock ) 
137                         UnlockPage(page);
138                 page_cache_release(page);
139                 page = NULL;
140                 CDEBUG(D_INODE, "Next for %lx\n", offset);
141         }
142
143 failure:
144         CDEBUG(D_INODE, "Negative case, page %p, offset %ld\n", page, offset);
145         if (page) {
146                 if (lock) 
147                         UnlockPage(page);
148                 page_cache_release(page);
149         }
150         EXIT;
151         return NULL;
152 }
153
154 struct dentry *obdfs_lookup(struct inode * dir, struct dentry *dentry)
155 {
156         struct inode * inode;
157         struct ext2_dir_entry_2 * de;
158         struct page * page;
159         ENTRY;
160
161         if (dentry->d_name.len > EXT2_NAME_LEN)
162                 return ERR_PTR(-ENAMETOOLONG);
163
164         page = obdfs_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, LOCKED);
165         inode = NULL;
166         if ( !page ) 
167                 CDEBUG(D_INODE, "No page - negative entry.\n");
168         if ( page && !de ) {
169                 CDEBUG(D_INODE, "Danger: PAGE but de.\n");
170                 return ERR_PTR(-ENOENT);
171         }
172         if (page) {
173                 unsigned long ino = le32_to_cpu(de->inode);
174                 UnlockPage(page);
175                 page_cache_release(page);
176                 inode = iget(dir->i_sb, ino);
177
178                 if (!inode) { 
179                         CDEBUG(D_INODE, "No inode.\n");
180                         EXIT;
181                         return ERR_PTR(-EACCES);
182                 }
183         }
184         d_add(dentry, inode);
185         EXIT;
186         return NULL;
187 }
188
189
190 /*
191  *      ext2_add_entry()
192  *
193  * adds a file entry to the specified directory, using the same
194  * semantics as ext2_find_entry(). It returns NULL if it failed.
195  *
196  * NOTE!! The inode part of 'de' is left at 0 - which means you
197  * may not sleep between calling this and putting something into
198  * the entry, as someone else might have used it while you slept.
199
200  * returns a locked and held page upon success 
201  */
202 static struct page *obdfs_add_entry (struct inode * dir,
203                                      const char * name, int namelen,
204                                      struct ext2_dir_entry_2 ** res_dir,
205                                      int *err)
206 {
207         unsigned long offset;
208         unsigned short rec_len;
209         struct page *page;
210         struct ext2_dir_entry_2 * de, * de1;
211         struct super_block * sb;
212
213         ENTRY;
214         *err = -EINVAL;
215         *res_dir = NULL;
216         if (!dir || !dir->i_nlink) {
217                 EXIT;
218                 return NULL;
219         }
220         sb = dir->i_sb;
221
222         if (!namelen) { 
223                 EXIT;
224                 return NULL;
225         }
226         /*
227          * Is this a busy deleted directory?  Can't create new files if so
228          */
229         if (dir->i_size == 0)
230         {
231                 EXIT;
232                 *err = -ENOENT;
233                 return NULL;
234         }
235         page = obdfs_getpage(dir, 0, 0, LOCKED);
236         if (!page) {
237                 EXIT;
238                 return NULL;
239         }
240         rec_len = EXT2_DIR_REC_LEN(namelen);
241         CDEBUG(D_INODE, "reclen: %d\n", rec_len);
242         offset = 0;
243         de = (struct ext2_dir_entry_2 *) page_address(page);
244         *err = -ENOSPC;
245         while (1) {
246                 CDEBUG(D_INODE, "Considering entry at %p, (page at %#lx - %#lx), offset %ld\n",
247                        de, page_address(page), page_address(page) + PAGE_SIZE, offset);
248                 if ((char *)de >= PAGE_SIZE + (char *)page_address(page)) {
249                         UnlockPage(page);
250                         page_cache_release(page);
251                         page = obdfs_getpage(dir, offset, 1, LOCKED);
252                         if (!page) {
253                                 EXIT;
254                                 return NULL;
255                         }
256                         if (dir->i_size <= offset) {
257                                 if (dir->i_size == 0) {
258                                         *err = -ENOENT;
259                                         EXIT;
260                                         return NULL;
261                                 }
262
263                                 CDEBUG(D_INODE, "creating next block\n");
264
265                                 de = (struct ext2_dir_entry_2 *) page_address(page);
266                                 de->inode = 0;
267                                 de->rec_len = le16_to_cpu(PAGE_SIZE);
268                                 dir->i_size = offset + PAGE_SIZE;
269                                 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
270                                 mark_inode_dirty(dir);
271                         } else {
272
273                                 ext2_debug ("skipping to next block\n");
274
275                                 de = (struct ext2_dir_entry_2 *) page_address(page);
276                         }
277                 }
278                 CDEBUG(D_INODE, "\n");
279                 if (!obdfs_check_dir_entry ("ext2_add_entry", dir, de, page,
280                                            offset)) {
281                         *err = -ENOENT;
282                         UnlockPage(page);
283                         page_cache_release(page);
284                         EXIT;
285                         return NULL;
286                 }
287                 CDEBUG(D_INODE, "\n");
288                 if (ext2_match (namelen, name, de)) {
289                                 *err = -EEXIST;
290                                 UnlockPage(page);
291                                 page_cache_release(page);
292                                 EXIT;
293                                 return NULL;
294                 }
295                 CDEBUG(D_INODE, "Testing for enough space at de %p\n", de);
296                 if ( (le32_to_cpu(de->inode) == 0 && le16_to_cpu(de->rec_len) >= rec_len) ||
297                      (le16_to_cpu(de->rec_len) >= EXT2_DIR_REC_LEN(de->name_len) + rec_len)) {
298                         offset += le16_to_cpu(de->rec_len);
299                         CDEBUG(D_INODE, "Found enough space de %p, offset %#lx\n", de, offset);
300                         if (le32_to_cpu(de->inode)) {
301                                 CDEBUG(D_INODE, "Inserting new in %p\n", de);
302                                 de1 = (struct ext2_dir_entry_2 *) ((char *) de +
303                                         EXT2_DIR_REC_LEN(de->name_len));
304                                 CDEBUG(D_INODE, "-- de1 at %p\n", de1);
305                                 de1->rec_len = cpu_to_le16(le16_to_cpu(de->rec_len) -
306                                         EXT2_DIR_REC_LEN(de->name_len));
307                                 de->rec_len = cpu_to_le16(EXT2_DIR_REC_LEN(de->name_len));
308                                 de = de1;
309                         }
310                         CDEBUG(D_INODE, "Reclen adjusted; copy %d bytes to %p, page at %#lx EOP at %#lx\n", namelen, de->name, page_address(page), page_address(page) + PAGE_SIZE);
311                         de->inode = 0;
312                         de->name_len = namelen;
313                         de->file_type = 0;
314                         memcpy (de->name, name, namelen);
315                         CDEBUG(D_INODE, "Copy done\n");
316                         /*
317                          * XXX shouldn't update any times until successful
318                          * completion of syscall, but too many callers depend
319                          * on this.
320                          *
321                          * XXX similarly, too many callers depend on
322                          * ext2_new_inode() setting the times, but error
323                          * recovery deletes the inode, so the worst that can
324                          * happen is that the times are slightly out of date
325                          * and/or different from the directory change time.
326                          */
327                         dir->i_mtime = dir->i_ctime = CURRENT_TIME;
328                         dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
329                         mark_inode_dirty(dir);
330                         dir->i_version = ++event;
331                         *res_dir = de;
332                         *err = 0;
333                         PDEBUG(page, "addentry");
334                         CDEBUG(D_INODE, "Regular exit from add_entry");
335                         EXIT;
336                         return page;
337                 }
338                 CDEBUG(D_INODE, "\n");
339                 offset += le16_to_cpu(de->rec_len);
340                 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
341                 
342         }
343         CDEBUG(D_INODE, "\n");
344
345         UnlockPage(page);
346         page_cache_release(page);
347         PDEBUG(page, "addentry");
348         EXIT;
349         return NULL;
350 }
351
352 /*
353  * ext2_delete_entry deletes a directory entry by merging it with the
354  * previous entry
355  */
356 static int obdfs_delete_entry (struct ext2_dir_entry_2 * dir,
357                               struct page * page)
358 {
359         struct ext2_dir_entry_2 * de, * pde;
360         int i;
361
362         i = 0;
363         pde = NULL;
364         de = (struct ext2_dir_entry_2 *) page_address(page);
365         while (i < PAGE_SIZE) {
366                 if (!obdfs_check_dir_entry ("ext2_delete_entry", NULL, 
367                                            de, page, i))
368                         return -EIO;
369                 if (de == dir)  {
370                         if (pde)
371                                 pde->rec_len =
372                                         cpu_to_le16(le16_to_cpu(pde->rec_len) +
373                                                     le16_to_cpu(dir->rec_len));
374                         else
375                                 dir->inode = 0;
376                         return 0;
377                 }
378                 i += le16_to_cpu(de->rec_len);
379                 pde = de;
380                 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
381         }
382         return -ENOENT;
383 }
384
385
386 static inline void ext2_set_de_type(struct super_block *sb,
387                                 struct ext2_dir_entry_2 *de,
388                                 umode_t mode) {
389         if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
390                 return;
391         if (S_ISCHR(mode))
392                 de->file_type = EXT2_FT_CHRDEV;
393         else if (S_ISBLK(mode))
394                 de->file_type = EXT2_FT_BLKDEV;
395         else if (S_ISFIFO(mode))  
396                 de->file_type = EXT2_FT_FIFO;
397         else if (S_ISLNK(mode))
398                 de->file_type = EXT2_FT_SYMLINK;
399         else if (S_ISREG(mode))
400                 de->file_type = EXT2_FT_REG_FILE;
401         else if (S_ISDIR(mode))  
402                 de->file_type = EXT2_FT_DIR;
403 }
404
405
406 /*
407  * Display all dentries holding the specified inode.
408  */
409 #if 0
410 static void show_dentry(struct list_head * dlist, int subdirs)
411 {
412         struct list_head *tmp = dlist;
413
414         while ((tmp = tmp->next) != dlist) {
415                 struct dentry * dentry;
416                 const char * unhashed = "";
417
418                 if ( subdirs ) 
419                         dentry  = list_entry(tmp, struct dentry, d_child);
420                 else 
421                         dentry  = list_entry(tmp, struct dentry, d_alias);
422
423                 if (list_empty(&dentry->d_hash))
424                         unhashed = "(unhashed)";
425
426                 if ( dentry->d_inode ) 
427                         printk("show_dentry: %s/%s, d_count=%d%s (ino %ld, dev %d, ct %d)\n",
428                                dentry->d_parent->d_name.name,
429                                dentry->d_name.name, dentry->d_count,
430                                unhashed, dentry->d_inode->i_ino, 
431                                dentry->d_inode->i_dev, 
432                                dentry->d_inode->i_count);
433                 else 
434                         printk("show_dentry: %s/%s, d_count=%d%s \n",
435                                dentry->d_parent->d_name.name,
436                                dentry->d_name.name, dentry->d_count,
437                                unhashed);
438         }
439 }
440 #endif
441
442
443 /*
444  * By the time this is called, we already have created
445  * the directory cache entry for the new file, but it
446  * is so far negative - it has no inode.
447  *
448  * If the create succeeds, we fill in the inode information
449  * with d_instantiate(). 
450  */
451 int obdfs_create (struct inode * dir, struct dentry * dentry, int mode)
452 {
453         struct inode * inode;
454         struct page *page;
455         struct ext2_dir_entry_2 * de;
456         int err = -EIO;
457         ino_t ino;
458
459         ENTRY;
460
461         /*
462          * N.B. Several error exits in ext2_new_inode don't set err.
463          */
464         ino = iops(dir)->o_create(iid(dir), 0, &err);
465         if ( err ) 
466                 return err;
467         err = -EIO;
468         inode =  iget(dir->i_sb, ino);
469         if (!inode || !list_empty(&inode->i_dentry)) {
470                 CDEBUG(D_INODE, "No inode, ino %ld\n", ino);
471                 EXIT;
472                 return err;
473         }
474
475         inode->i_op = &obdfs_file_inode_operations;
476         inode->i_mode = mode;
477         mark_inode_dirty(inode);
478         page = obdfs_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
479         if (!page) {
480                 inode->i_nlink--;
481                 mark_inode_dirty(inode);
482                 iput (inode);
483                 EXIT;
484                 return err;
485         }
486         de->inode = cpu_to_le32(inode->i_ino);
487         ext2_set_de_type(dir->i_sb, de, S_IFREG);
488         dir->i_version = ++event;
489         iops(dir)->o_brw(WRITE, iid(dir), dir, page, 0);
490         UnlockPage(page);
491 #if 0
492         if (IS_SYNC(dir)) {
493                 ll_rw_block (WRITE, 1, &bh);
494                 wait_on_buffer (bh);
495         }
496 #endif
497         page_cache_release(page);
498         d_instantiate(dentry, inode);
499         EXIT;
500         return 0;
501 }
502
503 int obdfs_mknod (struct inode * dir, struct dentry *dentry, int mode, int rdev)
504 {
505         struct inode * inode;
506         struct page *page;
507         struct ext2_dir_entry_2 * de;
508         int err = -EIO;
509
510         ino_t ino;
511
512         ENTRY;
513
514         /*
515          * N.B. Several error exits in ext2_new_inode don't set err.
516          */
517         ino = iops(dir)->o_create(iid(dir), 0, &err);
518         if ( ino == -1 ) 
519                 return -1;
520         inode =  iget(dir->i_sb, ino);
521         if (!inode)
522                 return err;
523
524         inode->i_uid = current->fsuid;
525         init_special_inode(inode, mode, rdev);
526         page = obdfs_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
527         if (!page)
528                 goto out_no_entry;
529         de->inode = cpu_to_le32(inode->i_ino);
530         dir->i_version = ++event;
531         ext2_set_de_type(dir->i_sb, de, inode->i_mode);
532         mark_inode_dirty(inode);
533         iops(dir)->o_brw(WRITE, iid(dir), dir, page, 0);
534         UnlockPage(page);
535
536 #if 0
537         if (IS_SYNC(dir)) {
538                 ll_rw_block (WRITE, 1, &bh);
539                 wait_on_buffer (bh);
540         }
541 #endif
542         d_instantiate(dentry, inode);
543         page_cache_release(page);
544         err = 0;
545 out:
546         return err;
547
548 out_no_entry:
549         inode->i_nlink--;
550         mark_inode_dirty(inode);
551         iput(inode);
552         goto out;
553 }
554
555 int obdfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
556 {
557         struct inode * inode;
558         struct page *page, *inode_page;
559         struct ext2_dir_entry_2 * de;
560         int err;
561         ino_t ino;
562
563         ENTRY;
564
565         err = -EMLINK;
566         if (dir->i_nlink >= EXT2_LINK_MAX)
567                 goto out;
568
569         err = -EIO;
570         ino = iops(dir)->o_create(iid(dir), 0, &err);
571         if ( ino == -1 ) 
572                 return -1;
573         inode =  iget(dir->i_sb, ino);
574         if (!inode)
575                 return err;
576
577
578         inode->i_op = &obdfs_dir_inode_operations;
579         inode->i_blocks = 0;    
580         inode_page = obdfs_getpage(inode, 0, 1, LOCKED);
581         if (!inode_page) {
582                 inode->i_nlink--; /* is this nlink == 0? */
583                 mark_inode_dirty(inode);
584                 iput (inode);
585                 return err;
586         }
587
588         /* create . and .. */
589         de = (struct ext2_dir_entry_2 *) page_address(inode_page);
590         de->inode = cpu_to_le32(inode->i_ino);
591         de->name_len = 1;
592         de->rec_len = cpu_to_le16(EXT2_DIR_REC_LEN(de->name_len));
593         strcpy (de->name, ".");
594         ext2_set_de_type(dir->i_sb, de, S_IFDIR);
595         
596         de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
597         de->inode = cpu_to_le32(dir->i_ino);
598         de->rec_len = cpu_to_le16(PAGE_SIZE - EXT2_DIR_REC_LEN(1));
599         de->name_len = 2;
600         strcpy (de->name, "..");
601         ext2_set_de_type(dir->i_sb, de, S_IFDIR);
602         
603         iops(dir)->o_brw(WRITE, iid(dir), inode, inode_page, 1);
604         inode->i_blocks = PAGE_SIZE/inode->i_sb->s_blocksize;
605         inode->i_size = PAGE_SIZE;
606         UnlockPage(inode_page);
607         page_cache_release(inode_page);
608
609         inode->i_nlink = 2;
610         inode->i_mode = S_IFDIR | mode;
611         if (dir->i_mode & S_ISGID)
612                 inode->i_mode |= S_ISGID;
613         mark_inode_dirty(inode);
614
615         /* now deal with the parent */
616         page = obdfs_add_entry(dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
617         if (!page) {
618                 goto out_no_entry;
619         }
620
621         de->inode = cpu_to_le32(inode->i_ino);
622         ext2_set_de_type(dir->i_sb, de, S_IFDIR);
623         dir->i_version = ++event;
624
625 #if 0
626         if (IS_SYNC(dir)) {
627                 ll_rw_block (WRITE, 1, &bh);
628                 wait_on_buffer (bh);
629         }
630 #endif
631         dir->i_nlink++;
632         dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
633         mark_inode_dirty(dir);
634         iops(dir)->o_brw(WRITE, iid(dir), dir, page, 1);
635         UnlockPage(page);
636         page_cache_release(page);
637         d_instantiate(dentry, inode);
638         err = 0;
639 out:
640         EXIT;
641         return err;
642
643 out_no_entry:
644         inode->i_nlink = 0;
645         mark_inode_dirty(inode);
646         iput (inode);
647         EXIT;
648         goto out;
649 }
650
651
652 /*
653  * routine to check that the specified directory is empty (for rmdir)
654  */
655 static int empty_dir (struct inode * inode)
656 {
657         unsigned long offset;
658         struct page *page;
659         struct ext2_dir_entry_2 * de, * de1;
660         struct super_block * sb;
661
662         sb = inode->i_sb;
663         if (inode->i_size < EXT2_DIR_REC_LEN(1) + EXT2_DIR_REC_LEN(2) ||
664             !(page = obdfs_getpage (inode, 0, 0, LOCKED))) {
665                 ext2_warning (inode->i_sb, "empty_dir",
666                               "bad directory (dir #%lu) - no data block",
667                               inode->i_ino);
668                 return 1;
669         }
670         de = (struct ext2_dir_entry_2 *) page_address(page);
671         de1 = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
672         if (le32_to_cpu(de->inode) != inode->i_ino || !le32_to_cpu(de1->inode) || 
673             strcmp (".", de->name) || strcmp ("..", de1->name)) {
674                 ext2_warning (inode->i_sb, "empty_dir",
675                               "bad directory (dir #%lu) - no `.' or `..'",
676                               inode->i_ino);
677                 page_cache_release(page);
678                 return 1;
679         }
680         offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
681         de = (struct ext2_dir_entry_2 *) ((char *) de1 + le16_to_cpu(de1->rec_len));
682         while (offset < inode->i_size ) {
683                 if (!page || (void *) de >= (void *) (page_address(page) + PAGE_SIZE)) {
684                         if (page) {
685                                 UnlockPage(page);
686                                 page_cache_release(page);
687                         }
688                         page = obdfs_getpage(inode, offset, 0, LOCKED);
689                         if (!page) {
690 #if 0
691                                 ext2_error (sb, "empty_dir",
692                                             "directory #%lu contains a hole at offset %lu",
693                                             inode->i_ino, offset);
694 #endif
695                                 offset += sb->s_blocksize;
696                                 continue;
697                         }
698                         de = (struct ext2_dir_entry_2 *) page_address(page);
699                 }
700                 if (!obdfs_check_dir_entry ("empty_dir", inode, de, page,
701                                            offset)) {
702                         UnlockPage(page);
703                         page_cache_release(page);
704                         return 1;
705                 }
706                 if (le32_to_cpu(de->inode)) {
707                         UnlockPage(page);
708                         page_cache_release(page);
709                         return 0;
710                 }
711                 offset += le16_to_cpu(de->rec_len);
712                 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
713         }
714         UnlockPage(page);
715         page_cache_release(page);
716         return 1;
717 }
718
719 int obdfs_rmdir (struct inode * dir, struct dentry *dentry)
720 {
721         int retval;
722         struct inode * inode;
723         struct page *page;
724         struct ext2_dir_entry_2 * de;
725
726         ENTRY;
727
728         retval = -ENOENT;
729         page = obdfs_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, LOCKED);
730         if (!page)
731                 goto end_rmdir;
732
733         inode = dentry->d_inode;
734         DQUOT_INIT(inode);
735
736         retval = -EIO;
737         if (le32_to_cpu(de->inode) != inode->i_ino)
738                 goto end_rmdir;
739
740         retval = -ENOTEMPTY;
741         if (!empty_dir (inode))
742                 goto end_rmdir;
743
744         retval = obdfs_delete_entry (de, page);
745         dir->i_version = ++event;
746         if (retval)
747                 goto end_rmdir;
748         iops(dir)->o_brw(WRITE, iid(dir), dir, page, 0);
749         UnlockPage(page);
750 #if 0
751         if (IS_SYNC(dir)) {
752                 ll_rw_block (WRITE, 1, &bh);
753                 wait_on_buffer (bh);
754         }
755 #endif
756         if (inode->i_nlink != 2)
757                 ext2_warning (inode->i_sb, "ext2_rmdir",
758                               "empty directory has nlink!=2 (%d)",
759                               inode->i_nlink);
760         inode->i_version = ++event;
761         inode->i_nlink = 0;
762         inode->i_size = 0;
763         mark_inode_dirty(inode);
764         dir->i_nlink--;
765         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
766         dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
767         mark_inode_dirty(dir);
768         d_delete(dentry);
769
770 end_rmdir:
771         if ( page )
772                 page_cache_release(page);
773         EXIT;
774         return retval;
775 }
776
777 int obdfs_unlink(struct inode * dir, struct dentry *dentry)
778 {
779         int retval;
780         struct inode * inode;
781         struct page *page;
782         struct ext2_dir_entry_2 * de;
783
784         ENTRY;
785
786         retval = -ENOENT;
787         page = obdfs_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, LOCKED);
788         if (!page)
789                 goto end_unlink;
790
791         inode = dentry->d_inode;
792         DQUOT_INIT(inode);
793
794         retval = -EIO;
795         if (le32_to_cpu(de->inode) != inode->i_ino)
796                 goto end_unlink;
797         
798         if (!inode->i_nlink) {
799                 ext2_warning (inode->i_sb, "ext2_unlink",
800                               "Deleting nonexistent file (%lu), %d",
801                               inode->i_ino, inode->i_nlink);
802                 inode->i_nlink = 1;
803         }
804         retval = obdfs_delete_entry (de, page);
805         if (retval)
806                 goto end_unlink;
807         dir->i_version = ++event;
808         iops(dir)->o_brw(WRITE, iid(dir), dir, page, 0);
809         UnlockPage(page);
810 #if 0
811         if (IS_SYNC(dir)) {
812                 ll_rw_block (WRITE, 1, &bh);
813                 wait_on_buffer (bh);
814         }
815 #endif
816         dir->i_ctime = dir->i_mtime = CURRENT_TIME;
817         dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
818         mark_inode_dirty(dir);
819         inode->i_nlink--;
820         mark_inode_dirty(inode);
821         inode->i_ctime = dir->i_ctime;
822         retval = 0;
823         d_delete(dentry);       /* This also frees the inode */
824
825 end_unlink:
826         if (page)
827                 page_cache_release(page);
828         EXIT;
829         return retval;
830 }
831
832 int obdfs_symlink (struct inode * dir, struct dentry *dentry, const char * symname)
833 {
834         struct ext2_dir_entry_2 * de;
835         struct inode * inode;
836         struct page* page = NULL, * name_page = NULL;
837         char * link;
838         int i, l, err = -EIO;
839         char c;
840         ino_t ino;
841
842         ENTRY;
843         /*
844          * N.B. Several error exits in ext2_new_inode don't set err.
845          */
846         ino = iops(dir)->o_create(iid(dir), 0, &err);
847         if ( ino == -1 )  {
848                 EXIT;
849                 return -1;
850         }
851         inode =  iget(dir->i_sb, ino);
852         if (!inode) {
853                 EXIT;
854                 return err;
855         }
856         inode->i_mode = S_IFLNK | S_IRWXUGO;
857         inode->i_op = &obdfs_symlink_inode_operations;
858         for (l = 0; l < inode->i_sb->s_blocksize - 1 &&
859              symname [l]; l++)
860                 ;
861
862         if (l >= sizeof (inode->u.ext2_i.i_data)) {
863                 CDEBUG(D_INODE, "l=%d, normal symlink\n", l);
864
865                 name_page = obdfs_getpage(inode, 0, 1, LOCKED);
866                 if (!name_page) {
867                         inode->i_nlink--;
868                         mark_inode_dirty(inode);
869                         iput (inode);
870                         EXIT;
871                         return err;
872                 }
873                 link = (char *)page_address(name_page);
874         } else {
875                 link = (char *) inode->u.ext2_i.i_data;
876
877                 CDEBUG(D_INODE, "l=%d, fast symlink\n", l);
878
879         }
880         i = 0;
881         while (i < inode->i_sb->s_blocksize - 1 && (c = *(symname++)))
882                 link[i++] = c;
883         link[i] = 0;
884         if (name_page) {
885                 iops(inode)->o_brw(WRITE, iid(inode), inode, name_page, 1);
886                 PDEBUG(name_page, "symlink");
887                 UnlockPage(name_page);
888                 page_cache_release(name_page);
889         }
890         inode->i_size = i;
891         mark_inode_dirty(inode);
892
893         page = obdfs_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
894         if (!page)
895                 goto out_no_entry;
896         de->inode = cpu_to_le32(inode->i_ino);
897         ext2_set_de_type(dir->i_sb, de, S_IFLNK);
898         dir->i_version = ++event;
899         iops(dir)->o_brw(WRITE, iid(dir), dir, page, 1);
900         UnlockPage(page);
901 #if 0
902         if (IS_SYNC(dir)) {
903                 ll_rw_block (WRITE, 1, &bh);
904                 wait_on_buffer (bh);
905         }
906 #endif
907         d_instantiate(dentry, inode);
908         err = 0;
909 out:
910         EXIT;
911         return err;
912
913 out_no_entry:
914         inode->i_nlink--;
915         mark_inode_dirty(inode);
916         iput (inode);
917         goto out;
918 }
919
920 int obdfs_link (struct dentry * old_dentry,
921                 struct inode * dir, struct dentry *dentry)
922 {
923         struct inode *inode = old_dentry->d_inode;
924         struct ext2_dir_entry_2 * de;
925         struct page *page;
926         int err;
927
928         ENTRY;
929
930         if (S_ISDIR(inode->i_mode))
931                 return -EPERM;
932
933         if (inode->i_nlink >= EXT2_LINK_MAX)
934                 return -EMLINK;
935
936         page = obdfs_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
937         if (!page)
938                 return err;
939
940         de->inode = cpu_to_le32(inode->i_ino);
941         ext2_set_de_type(dir->i_sb, de, inode->i_mode);
942         dir->i_version = ++event;
943         iops(dir)->o_brw(WRITE, iid(dir), dir, page, 0);
944         UnlockPage(page);
945
946 #if 0
947         if (IS_SYNC(dir)) {
948                 ll_rw_block (WRITE, 1, &bh);
949                 wait_on_buffer (bh);
950         }
951 #endif
952         page_cache_release(page);
953         inode->i_nlink++;
954         inode->i_ctime = CURRENT_TIME;
955         mark_inode_dirty(inode);
956         inode->i_count++;
957         d_instantiate(dentry, inode);
958         return 0;
959 }
960
961 #define PARENT_INO(buffer) \
962         ((struct ext2_dir_entry_2 *) ((char *) buffer + \
963         le16_to_cpu(((struct ext2_dir_entry_2 *) buffer)->rec_len)))->inode
964
965 /*
966  * Anybody can rename anything with this: the permission checks are left to the
967  * higher-level routines.
968  */
969 int obdfs_rename (struct inode * old_dir, struct dentry *old_dentry,
970                            struct inode * new_dir, struct dentry *new_dentry)
971 {
972         struct inode * old_inode, * new_inode;
973         struct page * old_page, * new_page, * dir_page;
974         struct ext2_dir_entry_2 * old_de, * new_de;
975         int retval;
976
977         ENTRY;
978
979         new_page = dir_page = NULL;
980
981         /* does the old entry exist? - if not get out */
982         old_page = obdfs_find_entry (old_dir, old_dentry->d_name.name, old_dentry->d_name.len, &old_de, NOLOCK);
983         PDEBUG(old_page, "rename - old page");
984         /*
985          *  Check for inode number is _not_ due to possible IO errors.
986          *  We might rmdir the source, keep it as pwd of some process
987          *  and merrily kill the link to whatever was created under the
988          *  same name. Goodbye sticky bit ;-<
989          */
990         old_inode = old_dentry->d_inode;
991         retval = -ENOENT;
992         if (!old_page || le32_to_cpu(old_de->inode) != old_inode->i_ino)
993                 goto end_rename;
994
995         /* find new inode */
996         new_inode = new_dentry->d_inode;
997         new_page = obdfs_find_entry (new_dir, new_dentry->d_name.name,
998                                 new_dentry->d_name.len, &new_de, NOLOCK);
999         PDEBUG(new_page, "rename - new page ");
1000         if (new_page) {
1001                 if (!new_inode) {
1002                         page_cache_release(new_page);
1003                         new_page = NULL;
1004                 } else {
1005                         DQUOT_INIT(new_inode);
1006                 }
1007         }
1008         /* in this case we to check more ... */
1009         if (S_ISDIR(old_inode->i_mode)) {
1010                 /* can only rename into empty new directory */
1011                 if (new_inode) {
1012                         retval = -ENOTEMPTY;
1013                         if (!empty_dir (new_inode))
1014                                 goto end_rename;
1015                 }
1016                 retval = -EIO;
1017                 dir_page= obdfs_getpage (old_inode, 0, 0, LOCKED);
1018                 PDEBUG(dir_page, "rename dir page");
1019
1020                 if (!dir_page)
1021                         goto end_rename;
1022                 if (le32_to_cpu(PARENT_INO(page_address(dir_page))) != old_dir->i_ino)
1023                         goto end_rename;
1024                 retval = -EMLINK;
1025                 if (!new_inode && new_dir!=old_dir &&
1026                                 new_dir->i_nlink >= EXT2_LINK_MAX)
1027                         goto end_rename;
1028         }
1029         /* create the target dir entry */
1030         if (!new_page) {
1031                 new_page = obdfs_add_entry (new_dir, new_dentry->d_name.name,
1032                                         new_dentry->d_name.len, &new_de,
1033                                         &retval);
1034                 PDEBUG(new_page, "rename new page");
1035                 if (!new_page)
1036                         goto end_rename;
1037         }
1038         new_dir->i_version = ++event;
1039
1040         /*
1041          * remove the old entry
1042          */
1043         new_de->inode = le32_to_cpu(old_inode->i_ino);
1044         if (EXT2_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
1045                                       EXT2_FEATURE_INCOMPAT_FILETYPE))
1046                 new_de->file_type = old_de->file_type;
1047         
1048         obdfs_delete_entry (old_de, old_page);
1049
1050         old_dir->i_version = ++event;
1051         if (new_inode) {
1052                 new_inode->i_nlink--;
1053                 new_inode->i_ctime = CURRENT_TIME;
1054                 mark_inode_dirty(new_inode);
1055         }
1056         old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1057         old_dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
1058         mark_inode_dirty(old_dir);
1059         if (dir_page) {
1060                 PARENT_INO(page_address(dir_page)) = le32_to_cpu(new_dir->i_ino);
1061                 iops(old_inode)->o_brw(WRITE, iid(old_inode), old_inode, dir_page, 0);
1062                 old_dir->i_nlink--;
1063                 mark_inode_dirty(old_dir);
1064                 if (new_inode) {
1065                         new_inode->i_nlink--;
1066                         mark_inode_dirty(new_inode);
1067                 } else {
1068                         new_dir->i_nlink++;
1069                         new_dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
1070                         mark_inode_dirty(new_dir);
1071                 }
1072         }
1073         if ( old_page != new_page ) {
1074                 unsigned long index = old_page->index;
1075                 /* lock the old_page and release unlocked copy */
1076                 CDEBUG(D_INODE, "old_page at %p\n", old_page);
1077                 page_cache_release(old_page);
1078                 old_page = obdfs_getpage(old_dir, index >> PAGE_SHIFT, 0, LOCKED);
1079                 CDEBUG(D_INODE, "old_page at %p\n", old_page);
1080                 iops(old_dir)->o_brw(WRITE, iid(old_dir), old_dir, old_page,0);
1081         }
1082 #if 0
1083         if (IS_SYNC(old_dir)) {
1084                 ll_rw_block (WRITE, 1, &old_bh);
1085                 wait_on_buffer (old_bh);
1086         }
1087 #endif
1088         iops(new_dir)->o_brw(WRITE, iid(new_dir), new_dir, new_page, 0);
1089 #if 0
1090         if (IS_SYNC(new_dir)) {
1091                 ll_rw_block (WRITE, 1, &new_bh);
1092                 wait_on_buffer (new_bh);
1093         }
1094 #endif
1095
1096         retval = 0;
1097
1098 end_rename:
1099         if (old_page && PageLocked(old_page) )
1100                 UnlockPage(old_page);
1101         if (old_page)
1102                 page_cache_release(old_page);
1103         if (new_page && PageLocked(new_page) )
1104                 UnlockPage(new_page);
1105         if (new_page)
1106                 page_cache_release(new_page);
1107         if (dir_page && PageLocked(dir_page) )
1108                 UnlockPage(dir_page);
1109         if (dir_page)
1110                 page_cache_release(dir_page);
1111
1112
1113         return retval;
1114 }