Whamcloud - gitweb
Fixed packing in obdcontrol
[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         obd_id id;
458
459         ENTRY;
460
461         /*
462          * N.B. Several error exits in ext2_new_inode don't set err.
463          */
464         err = iops(dir)->o_create(iid(dir), 0, &id);
465         if ( err ) 
466                 return err;
467         inode =  iget(dir->i_sb, (ino_t)id);
468         if (!inode || !list_empty(&inode->i_dentry)) {
469                 CDEBUG(D_INODE, "No inode, ino %ld\n", id);
470                 EXIT;
471                 return -EIO;
472         }
473
474         inode->i_op = &obdfs_file_inode_operations;
475         inode->i_mode = mode;
476         mark_inode_dirty(inode);
477         page = obdfs_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
478         if (!page) {
479                 inode->i_nlink--;
480                 mark_inode_dirty(inode);
481                 iput (inode);
482                 EXIT;
483                 return err;
484         }
485         de->inode = cpu_to_le32(inode->i_ino);
486         ext2_set_de_type(dir->i_sb, de, S_IFREG);
487         dir->i_version = ++event;
488         iops(dir)->o_brw(WRITE, iid(dir), dir, page, 0);
489         UnlockPage(page);
490 #if 0
491         if (IS_SYNC(dir)) {
492                 ll_rw_block (WRITE, 1, &bh);
493                 wait_on_buffer (bh);
494         }
495 #endif
496         page_cache_release(page);
497         d_instantiate(dentry, inode);
498         EXIT;
499         return 0;
500 }
501
502 int obdfs_mknod (struct inode * dir, struct dentry *dentry, int mode, int rdev)
503 {
504         struct inode * inode;
505         struct page *page;
506         struct ext2_dir_entry_2 * de;
507         int err;
508
509         obd_id id;
510
511         ENTRY;
512
513         /*
514          * N.B. Several error exits in ext2_new_inode don't set err.
515          */
516         err = iops(dir)->o_create(iid(dir), 0, &id);
517         if ( err ) 
518                 return err;
519         inode =  iget(dir->i_sb, (ino_t)id);
520         if (!inode)
521                 return -EIO;
522
523         inode->i_uid = current->fsuid;
524         init_special_inode(inode, mode, rdev);
525         page = obdfs_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
526         if (!page)
527                 goto out_no_entry;
528         de->inode = cpu_to_le32(inode->i_ino);
529         dir->i_version = ++event;
530         ext2_set_de_type(dir->i_sb, de, inode->i_mode);
531         mark_inode_dirty(inode);
532         iops(dir)->o_brw(WRITE, iid(dir), dir, page, 0);
533         UnlockPage(page);
534
535 #if 0
536         if (IS_SYNC(dir)) {
537                 ll_rw_block (WRITE, 1, &bh);
538                 wait_on_buffer (bh);
539         }
540 #endif
541         d_instantiate(dentry, inode);
542         page_cache_release(page);
543         err = 0;
544 out:
545         return err;
546
547 out_no_entry:
548         inode->i_nlink--;
549         mark_inode_dirty(inode);
550         iput(inode);
551         goto out;
552 }
553
554 int obdfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
555 {
556         struct inode * inode;
557         struct page *page, *inode_page;
558         struct ext2_dir_entry_2 * de;
559         int err;
560         obd_id id;
561
562         ENTRY;
563
564         err = -EMLINK;
565         if (dir->i_nlink >= EXT2_LINK_MAX)
566                 goto out;
567
568         err = iops(dir)->o_create(iid(dir), 0, &id);
569         if ( err ) 
570                 return err;
571         inode =  iget(dir->i_sb, (ino_t)id);
572         if (!inode)
573                 return -EIO;
574
575
576         inode->i_op = &obdfs_dir_inode_operations;
577         inode->i_blocks = 0;    
578         inode_page = obdfs_getpage(inode, 0, 1, LOCKED);
579         if (!inode_page) {
580                 inode->i_nlink--; /* is this nlink == 0? */
581                 mark_inode_dirty(inode);
582                 iput (inode);
583                 return err;
584         }
585
586         /* create . and .. */
587         de = (struct ext2_dir_entry_2 *) page_address(inode_page);
588         de->inode = cpu_to_le32(inode->i_ino);
589         de->name_len = 1;
590         de->rec_len = cpu_to_le16(EXT2_DIR_REC_LEN(de->name_len));
591         strcpy (de->name, ".");
592         ext2_set_de_type(dir->i_sb, de, S_IFDIR);
593         
594         de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
595         de->inode = cpu_to_le32(dir->i_ino);
596         de->rec_len = cpu_to_le16(PAGE_SIZE - EXT2_DIR_REC_LEN(1));
597         de->name_len = 2;
598         strcpy (de->name, "..");
599         ext2_set_de_type(dir->i_sb, de, S_IFDIR);
600         
601         iops(dir)->o_brw(WRITE, iid(dir), inode, inode_page, 1);
602         inode->i_blocks = PAGE_SIZE/inode->i_sb->s_blocksize;
603         inode->i_size = PAGE_SIZE;
604         UnlockPage(inode_page);
605         page_cache_release(inode_page);
606
607         inode->i_nlink = 2;
608         inode->i_mode = S_IFDIR | mode;
609         if (dir->i_mode & S_ISGID)
610                 inode->i_mode |= S_ISGID;
611         mark_inode_dirty(inode);
612
613         /* now deal with the parent */
614         page = obdfs_add_entry(dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
615         if (!page) {
616                 goto out_no_entry;
617         }
618
619         de->inode = cpu_to_le32(inode->i_ino);
620         ext2_set_de_type(dir->i_sb, de, S_IFDIR);
621         dir->i_version = ++event;
622
623 #if 0
624         if (IS_SYNC(dir)) {
625                 ll_rw_block (WRITE, 1, &bh);
626                 wait_on_buffer (bh);
627         }
628 #endif
629         dir->i_nlink++;
630         dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
631         mark_inode_dirty(dir);
632         iops(dir)->o_brw(WRITE, iid(dir), dir, page, 1);
633         UnlockPage(page);
634         page_cache_release(page);
635         d_instantiate(dentry, inode);
636         err = 0;
637 out:
638         EXIT;
639         return err;
640
641 out_no_entry:
642         inode->i_nlink = 0;
643         mark_inode_dirty(inode);
644         iput (inode);
645         EXIT;
646         goto out;
647 }
648
649
650 /*
651  * routine to check that the specified directory is empty (for rmdir)
652  */
653 static int empty_dir (struct inode * inode)
654 {
655         unsigned long offset;
656         struct page *page;
657         struct ext2_dir_entry_2 * de, * de1;
658         struct super_block * sb;
659
660         sb = inode->i_sb;
661         if (inode->i_size < EXT2_DIR_REC_LEN(1) + EXT2_DIR_REC_LEN(2) ||
662             !(page = obdfs_getpage (inode, 0, 0, LOCKED))) {
663                 ext2_warning (inode->i_sb, "empty_dir",
664                               "bad directory (dir #%lu) - no data block",
665                               inode->i_ino);
666                 return 1;
667         }
668         de = (struct ext2_dir_entry_2 *) page_address(page);
669         de1 = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
670         if (le32_to_cpu(de->inode) != inode->i_ino || !le32_to_cpu(de1->inode) || 
671             strcmp (".", de->name) || strcmp ("..", de1->name)) {
672                 ext2_warning (inode->i_sb, "empty_dir",
673                               "bad directory (dir #%lu) - no `.' or `..'",
674                               inode->i_ino);
675                 page_cache_release(page);
676                 return 1;
677         }
678         offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
679         de = (struct ext2_dir_entry_2 *) ((char *) de1 + le16_to_cpu(de1->rec_len));
680         while (offset < inode->i_size ) {
681                 if (!page || (void *) de >= (void *) (page_address(page) + PAGE_SIZE)) {
682                         if (page) {
683                                 UnlockPage(page);
684                                 page_cache_release(page);
685                         }
686                         page = obdfs_getpage(inode, offset, 0, LOCKED);
687                         if (!page) {
688 #if 0
689                                 ext2_error (sb, "empty_dir",
690                                             "directory #%lu contains a hole at offset %lu",
691                                             inode->i_ino, offset);
692 #endif
693                                 offset += sb->s_blocksize;
694                                 continue;
695                         }
696                         de = (struct ext2_dir_entry_2 *) page_address(page);
697                 }
698                 if (!obdfs_check_dir_entry ("empty_dir", inode, de, page,
699                                            offset)) {
700                         UnlockPage(page);
701                         page_cache_release(page);
702                         return 1;
703                 }
704                 if (le32_to_cpu(de->inode)) {
705                         UnlockPage(page);
706                         page_cache_release(page);
707                         return 0;
708                 }
709                 offset += le16_to_cpu(de->rec_len);
710                 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
711         }
712         UnlockPage(page);
713         page_cache_release(page);
714         return 1;
715 }
716
717 int obdfs_rmdir (struct inode * dir, struct dentry *dentry)
718 {
719         int retval;
720         struct inode * inode;
721         struct page *page;
722         struct ext2_dir_entry_2 * de;
723
724         ENTRY;
725
726         retval = -ENOENT;
727         page = obdfs_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, LOCKED);
728         if (!page)
729                 goto end_rmdir;
730
731         inode = dentry->d_inode;
732         DQUOT_INIT(inode);
733
734         retval = -EIO;
735         if (le32_to_cpu(de->inode) != inode->i_ino)
736                 goto end_rmdir;
737
738         retval = -ENOTEMPTY;
739         if (!empty_dir (inode))
740                 goto end_rmdir;
741
742         retval = obdfs_delete_entry (de, page);
743         dir->i_version = ++event;
744         if (retval)
745                 goto end_rmdir;
746         iops(dir)->o_brw(WRITE, iid(dir), dir, page, 0);
747         UnlockPage(page);
748 #if 0
749         if (IS_SYNC(dir)) {
750                 ll_rw_block (WRITE, 1, &bh);
751                 wait_on_buffer (bh);
752         }
753 #endif
754         if (inode->i_nlink != 2)
755                 ext2_warning (inode->i_sb, "ext2_rmdir",
756                               "empty directory has nlink!=2 (%d)",
757                               inode->i_nlink);
758         inode->i_version = ++event;
759         inode->i_nlink = 0;
760         inode->i_size = 0;
761         mark_inode_dirty(inode);
762         dir->i_nlink--;
763         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
764         dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
765         mark_inode_dirty(dir);
766         d_delete(dentry);
767
768 end_rmdir:
769         if ( page )
770                 page_cache_release(page);
771         EXIT;
772         return retval;
773 }
774
775 int obdfs_unlink(struct inode * dir, struct dentry *dentry)
776 {
777         int retval;
778         struct inode * inode;
779         struct page *page;
780         struct ext2_dir_entry_2 * de;
781
782         ENTRY;
783
784         retval = -ENOENT;
785         page = obdfs_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, LOCKED);
786         if (!page)
787                 goto end_unlink;
788
789         inode = dentry->d_inode;
790         DQUOT_INIT(inode);
791
792         retval = -EIO;
793         if (le32_to_cpu(de->inode) != inode->i_ino)
794                 goto end_unlink;
795         
796         if (!inode->i_nlink) {
797                 ext2_warning (inode->i_sb, "ext2_unlink",
798                               "Deleting nonexistent file (%lu), %d",
799                               inode->i_ino, inode->i_nlink);
800                 inode->i_nlink = 1;
801         }
802         retval = obdfs_delete_entry (de, page);
803         if (retval)
804                 goto end_unlink;
805         dir->i_version = ++event;
806         iops(dir)->o_brw(WRITE, iid(dir), dir, page, 0);
807         UnlockPage(page);
808 #if 0
809         if (IS_SYNC(dir)) {
810                 ll_rw_block (WRITE, 1, &bh);
811                 wait_on_buffer (bh);
812         }
813 #endif
814         dir->i_ctime = dir->i_mtime = CURRENT_TIME;
815         dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
816         mark_inode_dirty(dir);
817         inode->i_nlink--;
818         mark_inode_dirty(inode);
819         inode->i_ctime = dir->i_ctime;
820         retval = 0;
821         d_delete(dentry);       /* This also frees the inode */
822
823 end_unlink:
824         if (page)
825                 page_cache_release(page);
826         EXIT;
827         return retval;
828 }
829
830 int obdfs_symlink (struct inode * dir, struct dentry *dentry, const char * symname)
831 {
832         struct ext2_dir_entry_2 * de;
833         struct inode * inode;
834         struct page* page = NULL, * name_page = NULL;
835         char * link;
836         int i, l, err = -EIO;
837         char c;
838         obd_id id;
839
840         ENTRY;
841         /*
842          * N.B. Several error exits in ext2_new_inode don't set err.
843          */
844         err = iops(dir)->o_create(iid(dir), 0, &id);
845         if ( err )  {
846                 EXIT;
847                 return err;
848         }
849         inode =  iget(dir->i_sb, (ino_t)id);
850         if (!inode) {
851                 EXIT;
852                 return err;
853         }
854         inode->i_mode = S_IFLNK | S_IRWXUGO;
855         inode->i_op = &obdfs_symlink_inode_operations;
856         for (l = 0; l < inode->i_sb->s_blocksize - 1 &&
857              symname [l]; l++)
858                 ;
859
860         if (l >= sizeof (inode->u.ext2_i.i_data)) {
861                 CDEBUG(D_INODE, "l=%d, normal symlink\n", l);
862
863                 name_page = obdfs_getpage(inode, 0, 1, LOCKED);
864                 if (!name_page) {
865                         inode->i_nlink--;
866                         mark_inode_dirty(inode);
867                         iput (inode);
868                         EXIT;
869                         return err;
870                 }
871                 link = (char *)page_address(name_page);
872         } else {
873                 link = (char *) inode->u.ext2_i.i_data;
874
875                 CDEBUG(D_INODE, "l=%d, fast symlink\n", l);
876
877         }
878         i = 0;
879         while (i < inode->i_sb->s_blocksize - 1 && (c = *(symname++)))
880                 link[i++] = c;
881         link[i] = 0;
882         if (name_page) {
883                 iops(inode)->o_brw(WRITE, iid(inode), inode, name_page, 1);
884                 PDEBUG(name_page, "symlink");
885                 UnlockPage(name_page);
886                 page_cache_release(name_page);
887         }
888         inode->i_size = i;
889         mark_inode_dirty(inode);
890
891         page = obdfs_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
892         if (!page)
893                 goto out_no_entry;
894         de->inode = cpu_to_le32(inode->i_ino);
895         ext2_set_de_type(dir->i_sb, de, S_IFLNK);
896         dir->i_version = ++event;
897         iops(dir)->o_brw(WRITE, iid(dir), dir, page, 1);
898         UnlockPage(page);
899 #if 0
900         if (IS_SYNC(dir)) {
901                 ll_rw_block (WRITE, 1, &bh);
902                 wait_on_buffer (bh);
903         }
904 #endif
905         d_instantiate(dentry, inode);
906         err = 0;
907 out:
908         EXIT;
909         return err;
910
911 out_no_entry:
912         inode->i_nlink--;
913         mark_inode_dirty(inode);
914         iput (inode);
915         goto out;
916 }
917
918 int obdfs_link (struct dentry * old_dentry,
919                 struct inode * dir, struct dentry *dentry)
920 {
921         struct inode *inode = old_dentry->d_inode;
922         struct ext2_dir_entry_2 * de;
923         struct page *page;
924         int err;
925
926         ENTRY;
927
928         if (S_ISDIR(inode->i_mode))
929                 return -EPERM;
930
931         if (inode->i_nlink >= EXT2_LINK_MAX)
932                 return -EMLINK;
933
934         page = obdfs_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
935         if (!page)
936                 return err;
937
938         de->inode = cpu_to_le32(inode->i_ino);
939         ext2_set_de_type(dir->i_sb, de, inode->i_mode);
940         dir->i_version = ++event;
941         iops(dir)->o_brw(WRITE, iid(dir), dir, page, 0);
942         UnlockPage(page);
943
944 #if 0
945         if (IS_SYNC(dir)) {
946                 ll_rw_block (WRITE, 1, &bh);
947                 wait_on_buffer (bh);
948         }
949 #endif
950         page_cache_release(page);
951         inode->i_nlink++;
952         inode->i_ctime = CURRENT_TIME;
953         mark_inode_dirty(inode);
954         inode->i_count++;
955         d_instantiate(dentry, inode);
956         return 0;
957 }
958
959 #define PARENT_INO(buffer) \
960         ((struct ext2_dir_entry_2 *) ((char *) buffer + \
961         le16_to_cpu(((struct ext2_dir_entry_2 *) buffer)->rec_len)))->inode
962
963 /*
964  * Anybody can rename anything with this: the permission checks are left to the
965  * higher-level routines.
966  */
967 int obdfs_rename (struct inode * old_dir, struct dentry *old_dentry,
968                            struct inode * new_dir, struct dentry *new_dentry)
969 {
970         struct inode * old_inode, * new_inode;
971         struct page * old_page, * new_page, * dir_page;
972         struct ext2_dir_entry_2 * old_de, * new_de;
973         int retval;
974
975         ENTRY;
976
977         new_page = dir_page = NULL;
978
979         /* does the old entry exist? - if not get out */
980         old_page = obdfs_find_entry (old_dir, old_dentry->d_name.name, old_dentry->d_name.len, &old_de, NOLOCK);
981         PDEBUG(old_page, "rename - old page");
982         /*
983          *  Check for inode number is _not_ due to possible IO errors.
984          *  We might rmdir the source, keep it as pwd of some process
985          *  and merrily kill the link to whatever was created under the
986          *  same name. Goodbye sticky bit ;-<
987          */
988         old_inode = old_dentry->d_inode;
989         retval = -ENOENT;
990         if (!old_page || le32_to_cpu(old_de->inode) != old_inode->i_ino)
991                 goto end_rename;
992
993         /* find new inode */
994         new_inode = new_dentry->d_inode;
995         new_page = obdfs_find_entry (new_dir, new_dentry->d_name.name,
996                                 new_dentry->d_name.len, &new_de, NOLOCK);
997         PDEBUG(new_page, "rename - new page ");
998         if (new_page) {
999                 if (!new_inode) {
1000                         page_cache_release(new_page);
1001                         new_page = NULL;
1002                 } else {
1003                         DQUOT_INIT(new_inode);
1004                 }
1005         }
1006         /* in this case we to check more ... */
1007         if (S_ISDIR(old_inode->i_mode)) {
1008                 /* can only rename into empty new directory */
1009                 if (new_inode) {
1010                         retval = -ENOTEMPTY;
1011                         if (!empty_dir (new_inode))
1012                                 goto end_rename;
1013                 }
1014                 retval = -EIO;
1015                 dir_page= obdfs_getpage (old_inode, 0, 0, LOCKED);
1016                 PDEBUG(dir_page, "rename dir page");
1017
1018                 if (!dir_page)
1019                         goto end_rename;
1020                 if (le32_to_cpu(PARENT_INO(page_address(dir_page))) != old_dir->i_ino)
1021                         goto end_rename;
1022                 retval = -EMLINK;
1023                 if (!new_inode && new_dir!=old_dir &&
1024                                 new_dir->i_nlink >= EXT2_LINK_MAX)
1025                         goto end_rename;
1026         }
1027         /* create the target dir entry */
1028         if (!new_page) {
1029                 new_page = obdfs_add_entry (new_dir, new_dentry->d_name.name,
1030                                         new_dentry->d_name.len, &new_de,
1031                                         &retval);
1032                 PDEBUG(new_page, "rename new page");
1033                 if (!new_page)
1034                         goto end_rename;
1035         }
1036         new_dir->i_version = ++event;
1037
1038         /*
1039          * remove the old entry
1040          */
1041         new_de->inode = le32_to_cpu(old_inode->i_ino);
1042         if (EXT2_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
1043                                       EXT2_FEATURE_INCOMPAT_FILETYPE))
1044                 new_de->file_type = old_de->file_type;
1045         
1046         obdfs_delete_entry (old_de, old_page);
1047
1048         old_dir->i_version = ++event;
1049         if (new_inode) {
1050                 new_inode->i_nlink--;
1051                 new_inode->i_ctime = CURRENT_TIME;
1052                 mark_inode_dirty(new_inode);
1053         }
1054         old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1055         old_dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
1056         mark_inode_dirty(old_dir);
1057         if (dir_page) {
1058                 PARENT_INO(page_address(dir_page)) = le32_to_cpu(new_dir->i_ino);
1059                 iops(old_inode)->o_brw(WRITE, iid(old_inode), old_inode, dir_page, 0);
1060                 old_dir->i_nlink--;
1061                 mark_inode_dirty(old_dir);
1062                 if (new_inode) {
1063                         new_inode->i_nlink--;
1064                         mark_inode_dirty(new_inode);
1065                 } else {
1066                         new_dir->i_nlink++;
1067                         new_dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
1068                         mark_inode_dirty(new_dir);
1069                 }
1070         }
1071         if ( old_page != new_page ) {
1072                 unsigned long index = old_page->index;
1073                 /* lock the old_page and release unlocked copy */
1074                 CDEBUG(D_INODE, "old_page at %p\n", old_page);
1075                 page_cache_release(old_page);
1076                 old_page = obdfs_getpage(old_dir, index >> PAGE_SHIFT, 0, LOCKED);
1077                 CDEBUG(D_INODE, "old_page at %p\n", old_page);
1078                 iops(old_dir)->o_brw(WRITE, iid(old_dir), old_dir, old_page,0);
1079         }
1080 #if 0
1081         if (IS_SYNC(old_dir)) {
1082                 ll_rw_block (WRITE, 1, &old_bh);
1083                 wait_on_buffer (old_bh);
1084         }
1085 #endif
1086         iops(new_dir)->o_brw(WRITE, iid(new_dir), new_dir, new_page, 0);
1087 #if 0
1088         if (IS_SYNC(new_dir)) {
1089                 ll_rw_block (WRITE, 1, &new_bh);
1090                 wait_on_buffer (new_bh);
1091         }
1092 #endif
1093
1094         retval = 0;
1095
1096 end_rename:
1097         if (old_page && PageLocked(old_page) )
1098                 UnlockPage(old_page);
1099         if (old_page)
1100                 page_cache_release(old_page);
1101         if (new_page && PageLocked(new_page) )
1102                 UnlockPage(new_page);
1103         if (new_page)
1104                 page_cache_release(new_page);
1105         if (dir_page && PageLocked(dir_page) )
1106                 UnlockPage(dir_page);
1107         if (dir_page)
1108                 page_cache_release(dir_page);
1109
1110
1111         return retval;
1112 }