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