Whamcloud - gitweb
New direcotory layout:
[fs/lustre-release.git] / lustre / obdfs / namei.c
1 /*
2  *  linux/fs/ext2/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/minix/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 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), page_address(page) + 
112                                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                                 CDEBUG(D_INODE, "Bad entry: at %p len %d\n",
129                                        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 }
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 }
189
190
191 /*
192  *      ext2_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 static struct page *obdfs_add_entry (struct inode * dir,
204                                      const char * name, int namelen,
205                                      struct ext2_dir_entry_2 ** res_dir,
206                                      int *err)
207 {
208         unsigned long offset;
209         unsigned short rec_len;
210         struct page *page;
211         struct ext2_dir_entry_2 * de, * de1;
212         struct super_block * sb;
213
214         ENTRY;
215         *err = -EINVAL;
216         *res_dir = NULL;
217         if (!dir || !dir->i_nlink) {
218                 EXIT;
219                 return NULL;
220         }
221         sb = dir->i_sb;
222
223         if (!namelen) { 
224                 EXIT;
225                 return NULL;
226         }
227         /*
228          * Is this a busy deleted directory?  Can't create new files if so
229          */
230         if (dir->i_size == 0)
231         {
232                 EXIT;
233                 *err = -ENOENT;
234                 return NULL;
235         }
236         page = obdfs_getpage(dir, 0, 0, LOCKED);
237         if (!page) {
238                 EXIT;
239                 return NULL;
240         }
241         rec_len = EXT2_DIR_REC_LEN(namelen);
242         CDEBUG(D_INODE, "reclen: %d\n", rec_len);
243         offset = 0;
244         de = (struct ext2_dir_entry_2 *) page_address(page);
245         *err = -ENOSPC;
246         while (1) {
247                 CDEBUG(D_INODE, "Considering entry at %p, (page at %#lx - %#lx), offset %ld\n",
248                        de, page_address(page), page_address(page) + PAGE_SIZE, offset);
249                 if ((char *)de >= PAGE_SIZE + (char *)page_address(page)) {
250                         UnlockPage(page);
251                         page_cache_release(page);
252                         page = obdfs_getpage(dir, offset, 1, LOCKED);
253                         if (!page) {
254                                 EXIT;
255                                 return NULL;
256                         }
257                         if (dir->i_size <= offset) {
258                                 if (dir->i_size == 0) {
259                                         *err = -ENOENT;
260                                         EXIT;
261                                         return NULL;
262                                 }
263
264                                 CDEBUG(D_INODE, "creating next block\n");
265
266                                 de = (struct ext2_dir_entry_2 *) page_address(page);
267                                 de->inode = 0;
268                                 de->rec_len = le16_to_cpu(PAGE_SIZE);
269                                 dir->i_size = offset + PAGE_SIZE;
270                                 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
271                                 mark_inode_dirty(dir);
272                         } else {
273
274                                 ext2_debug ("skipping to next block\n");
275
276                                 de = (struct ext2_dir_entry_2 *) page_address(page);
277                         }
278                 }
279                 CDEBUG(D_INODE, "\n");
280                 if (!obdfs_check_dir_entry ("ext2_add_entry", dir, de, page,
281                                            offset)) {
282                         *err = -ENOENT;
283                         UnlockPage(page);
284                         page_cache_release(page);
285                         EXIT;
286                         return NULL;
287                 }
288                 CDEBUG(D_INODE, "\n");
289                 if (ext2_match (namelen, name, de)) {
290                                 *err = -EEXIST;
291                                 UnlockPage(page);
292                                 page_cache_release(page);
293                                 EXIT;
294                                 return NULL;
295                 }
296                 CDEBUG(D_INODE, "Testing for enough space at de %p\n", de);
297                 if ( (le32_to_cpu(de->inode) == 0 && 
298                       le16_to_cpu(de->rec_len) >= rec_len) ||
299                      (le16_to_cpu(de->rec_len) >= 
300                       EXT2_DIR_REC_LEN(de->name_len) + rec_len)) {
301                         offset += le16_to_cpu(de->rec_len);
302                         CDEBUG(D_INODE, "Found enough space de %p, offset %#lx\n", de, offset);
303                         if (le32_to_cpu(de->inode)) {
304                                 CDEBUG(D_INODE, "Inserting new in %p\n", de);
305                                 de1 = (struct ext2_dir_entry_2 *) ((char *) de +
306                                         EXT2_DIR_REC_LEN(de->name_len));
307                                 CDEBUG(D_INODE, "-- de1 at %p\n", de1);
308                                 de1->rec_len = cpu_to_le16(le16_to_cpu(de->rec_len) -
309                                         EXT2_DIR_REC_LEN(de->name_len));
310                                 de->rec_len = cpu_to_le16(EXT2_DIR_REC_LEN(de->name_len));
311                                 de = de1;
312                         }
313                         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);
314                         de->inode = 0;
315                         de->name_len = namelen;
316                         de->file_type = 0;
317                         memcpy (de->name, name, namelen);
318                         CDEBUG(D_INODE, "Copy done\n");
319                         /*
320                          * XXX shouldn't update any times until successful
321                          * completion of syscall, but too many callers depend
322                          * on this.
323                          *
324                          * XXX similarly, too many callers depend on
325                          * ext2_new_inode() setting the times, but error
326                          * recovery deletes the inode, so the worst that can
327                          * happen is that the times are slightly out of date
328                          * and/or different from the directory change time.
329                          */
330                         dir->i_mtime = dir->i_ctime = CURRENT_TIME;
331                         dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
332                         mark_inode_dirty(dir);
333                         dir->i_version = ++event;
334                         *res_dir = de;
335                         *err = 0;
336                         PDEBUG(page, "addentry");
337                         CDEBUG(D_INODE, "Regular exit from add_entry");
338                         EXIT;
339                         return page;
340                 }
341                 CDEBUG(D_INODE, "\n");
342                 offset += le16_to_cpu(de->rec_len);
343                 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
344                 
345         }
346         CDEBUG(D_INODE, "\n");
347
348         UnlockPage(page);
349         page_cache_release(page);
350         PDEBUG(page, "addentry");
351         EXIT;
352         return NULL;
353 }
354
355 /*
356  * ext2_delete_entry deletes a directory entry by merging it with the
357  * previous entry
358  */
359 static int obdfs_delete_entry (struct ext2_dir_entry_2 * dir,
360                               struct page * page)
361 {
362         struct ext2_dir_entry_2 * de, * pde;
363         int i;
364
365         i = 0;
366         pde = NULL;
367         de = (struct ext2_dir_entry_2 *) page_address(page);
368         while (i < PAGE_SIZE) {
369                 if (!obdfs_check_dir_entry ("ext2_delete_entry", NULL, 
370                                            de, page, i))
371                         return -EIO;
372                 if (de == dir)  {
373                         if (pde)
374                                 pde->rec_len =
375                                         cpu_to_le16(le16_to_cpu(pde->rec_len) +
376                                                     le16_to_cpu(dir->rec_len));
377                         else
378                                 dir->inode = 0;
379                         return 0;
380                 }
381                 i += le16_to_cpu(de->rec_len);
382                 pde = de;
383                 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
384         }
385         return -ENOENT;
386 }
387
388
389 static inline void ext2_set_de_type(struct super_block *sb,
390                                 struct ext2_dir_entry_2 *de,
391                                 umode_t mode) {
392         if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
393                 return;
394         if (S_ISCHR(mode))
395                 de->file_type = EXT2_FT_CHRDEV;
396         else if (S_ISBLK(mode))
397                 de->file_type = EXT2_FT_BLKDEV;
398         else if (S_ISFIFO(mode))  
399                 de->file_type = EXT2_FT_FIFO;
400         else if (S_ISLNK(mode))
401                 de->file_type = EXT2_FT_SYMLINK;
402         else if (S_ISREG(mode))
403                 de->file_type = EXT2_FT_REG_FILE;
404         else if (S_ISDIR(mode))  
405                 de->file_type = EXT2_FT_DIR;
406 }
407
408
409 /*
410  * Display all dentries holding the specified inode.
411  */
412 static void show_dentry(struct list_head * dlist, int subdirs)
413 {
414         struct list_head *tmp = dlist;
415
416         while ((tmp = tmp->next) != dlist) {
417                 struct dentry * dentry;
418                 const char * unhashed = "";
419
420                 if ( subdirs ) 
421                                 dentry  = list_entry(tmp, struct dentry, d_child);
422                         else 
423                                 dentry  = list_entry(tmp, struct dentry, d_alias);
424
425                 if (list_empty(&dentry->d_hash))
426                         unhashed = "(unhashed)";
427
428                 if ( dentry->d_inode ) 
429                         printk("show_dentry: %s/%s, d_count=%d%s (ino %ld, dev %d, ct %d)\n",
430                                dentry->d_parent->d_name.name,
431                                dentry->d_name.name, dentry->d_count,
432                                unhashed, dentry->d_inode->i_ino, 
433                                dentry->d_inode->i_dev, 
434                                dentry->d_inode->i_count);
435                 else 
436                         printk("show_dentry: %s/%s, d_count=%d%s \n",
437                                dentry->d_parent->d_name.name,
438                                dentry->d_name.name, dentry->d_count,
439                                unhashed);
440         }
441 }
442
443
444 /*
445  * By the time this is called, we already have created
446  * the directory cache entry for the new file, but it
447  * is so far negative - it has no inode.
448  *
449  * If the create succeeds, we fill in the inode information
450  * with d_instantiate(). 
451  */
452 int obdfs_create (struct inode * dir, struct dentry * dentry, int mode)
453 {
454         struct inode * inode;
455         struct page *page;
456         struct ext2_dir_entry_2 * de;
457         int err = -EIO;
458         struct obdfs_sb_info *sbi;
459         ino_t ino;
460
461         ENTRY;
462
463         sbi = dir->i_sb->u.generic_sbp;
464         /*
465          * N.B. Several error exits in ext2_new_inode don't set err.
466          */
467         ino = sbi->osi_ops->o_create(sbi->osi_conn_info.conn_id, 0, &err);
468         if ( err ) 
469                 return err;
470         err = -EIO;
471         inode =  iget(dir->i_sb, ino);
472         if (!inode || !list_empty(&inode->i_dentry)) {
473                 CDEBUG(D_INODE, "No inode, ino %ld\n", ino);
474                 EXIT;
475                 return err;
476         }
477
478         inode->i_op = &obdfs_inode_ops;
479         inode->i_mode = mode;
480         mark_inode_dirty(inode);
481         page = obdfs_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
482         if (!page) {
483                 inode->i_nlink--;
484                 mark_inode_dirty(inode);
485                 iput (inode);
486                 EXIT;
487                 return err;
488         }
489         de->inode = cpu_to_le32(inode->i_ino);
490         ext2_set_de_type(dir->i_sb, de, S_IFREG);
491         dir->i_version = ++event;
492         sbi->osi_ops->o_brw(WRITE, sbi->osi_conn_info.conn_id, 
493                             dir, page, 0);
494         UnlockPage(page);
495 #if 0
496         if (IS_SYNC(dir)) {
497                 ll_rw_block (WRITE, 1, &bh);
498                 wait_on_buffer (bh);
499         }
500 #endif
501         page_cache_release(page);
502         d_instantiate(dentry, inode);
503         EXIT;
504         return 0;
505 }
506
507 int obdfs_mknod (struct inode * dir, struct dentry *dentry, int mode, int rdev)
508 {
509         struct inode * inode;
510         struct page *page;
511         struct ext2_dir_entry_2 * de;
512         int err = -EIO;
513         struct obdfs_sb_info *sbi;
514
515         ino_t ino;
516
517         ENTRY;
518
519         sbi = dir->i_sb->u.generic_sbp;
520         /*
521          * N.B. Several error exits in ext2_new_inode don't set err.
522          */
523         ino = sbi->osi_ops->o_create(sbi->osi_conn_info.conn_id, 0, &err);
524         if ( ino == -1 ) 
525                 return -1;
526         inode =  iget(dir->i_sb, ino);
527         if (!inode)
528                 return err;
529
530         inode->i_uid = current->fsuid;
531         init_special_inode(inode, mode, rdev);
532         page = obdfs_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
533         if (!page)
534                 goto out_no_entry;
535         de->inode = cpu_to_le32(inode->i_ino);
536         dir->i_version = ++event;
537         ext2_set_de_type(dir->i_sb, de, inode->i_mode);
538         mark_inode_dirty(inode);
539         sbi->osi_ops->o_brw(WRITE, sbi->osi_conn_info.conn_id, 
540                             dir, page, 0);
541         UnlockPage(page);
542
543 #if 0
544         if (IS_SYNC(dir)) {
545                 ll_rw_block (WRITE, 1, &bh);
546                 wait_on_buffer (bh);
547         }
548 #endif
549         d_instantiate(dentry, inode);
550         page_cache_release(page);
551         err = 0;
552 out:
553         return err;
554
555 out_no_entry:
556         inode->i_nlink--;
557         mark_inode_dirty(inode);
558         iput(inode);
559         goto out;
560 }
561
562 int obdfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
563 {
564         struct inode * child;
565         struct page *page, *child_page;
566         struct ext2_dir_entry_2 * de;
567         struct obdfs_sb_info *sbi;
568         int err;
569         ino_t ino;
570
571         ENTRY;
572         sbi = dir->i_sb->u.generic_sbp;
573
574         err = -EMLINK;
575         if (dir->i_nlink >= EXT2_LINK_MAX)
576                 goto out;
577
578         err = -EIO;
579         ino = iops(dir)->o_create(sbi->osi_conn_info.conn_id, 0, &err);
580         if ( ino == -1 ) 
581                 return -1;
582         child =  iget(dir->i_sb, ino);
583         if (!child)
584                 return err;
585
586
587         child->i_op = &obdfs_inode_ops;
588         child->i_size = PAGE_SIZE;
589         child->i_blocks = 0;    
590         child_page = obdfs_getpage(child, 0, 1, LOCKED);
591         if (!child_page) {
592                 child->i_nlink--; /* is this nlink == 0? */
593                 mark_inode_dirty(child);
594                 iput (child);
595                 return err;
596         }
597         child->i_blocks = PAGE_SIZE/child->i_sb->s_blocksize;
598
599         /* create . and .. */
600         de = (struct ext2_dir_entry_2 *) page_address(child_page);
601         de->inode = cpu_to_le32(child->i_ino);
602         de->name_len = 1;
603         de->rec_len = cpu_to_le16(EXT2_DIR_REC_LEN(de->name_len));
604         strcpy (de->name, ".");
605         ext2_set_de_type(dir->i_sb, de, S_IFDIR);
606         
607         de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
608         de->inode = cpu_to_le32(dir->i_ino);
609         de->rec_len = cpu_to_le16(PAGE_SIZE - EXT2_DIR_REC_LEN(1));
610         de->name_len = 2;
611         strcpy (de->name, "..");
612         ext2_set_de_type(dir->i_sb, de, S_IFDIR);
613         
614         iops(dir)->o_brw(WRITE, sbi->osi_conn_info.conn_id, child, child_page, 1);
615         UnlockPage(child_page);
616         page_cache_release(child_page);
617
618         child->i_nlink = 2;
619         child->i_mode = S_IFDIR | mode;
620         if (dir->i_mode & S_ISGID)
621                 child->i_mode |= S_ISGID;
622         mark_inode_dirty(child);
623
624         /* now deal with the parent */
625         page = obdfs_add_entry(dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
626         if (!page) {
627                 goto out_no_entry;
628         }
629
630         de->inode = cpu_to_le32(child->i_ino);
631         ext2_set_de_type(dir->i_sb, de, S_IFDIR);
632         dir->i_version = ++event;
633
634 #if 0
635         if (IS_SYNC(dir)) {
636                 ll_rw_block (WRITE, 1, &bh);
637                 wait_on_buffer (bh);
638         }
639 #endif
640         dir->i_nlink++;
641         dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
642         mark_inode_dirty(dir);
643         iops(dir)->o_brw(WRITE, sbi->osi_conn_info.conn_id, dir, page, 1);
644         UnlockPage(page);
645         page_cache_release(page);
646         d_instantiate(dentry, child);
647         err = 0;
648 out:
649         EXIT;
650         return err;
651
652 out_no_entry:
653         child->i_nlink = 0;
654         mark_inode_dirty(child);
655         iput (child);
656         EXIT;
657         goto out;
658 }
659
660
661 /*
662  * routine to check that the specified directory is empty (for rmdir)
663  */
664 static int empty_dir (struct inode * inode)
665 {
666         unsigned long offset;
667         struct page *page;
668         struct ext2_dir_entry_2 * de, * de1;
669         struct super_block * sb;
670
671         sb = inode->i_sb;
672         if (inode->i_size < EXT2_DIR_REC_LEN(1) + EXT2_DIR_REC_LEN(2) ||
673             !(page = obdfs_getpage (inode, 0, 0, LOCKED))) {
674                 ext2_warning (inode->i_sb, "empty_dir",
675                               "bad directory (dir #%lu) - no data block",
676                               inode->i_ino);
677                 return 1;
678         }
679         de = (struct ext2_dir_entry_2 *) page_address(page);
680         de1 = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
681         if (le32_to_cpu(de->inode) != inode->i_ino || !le32_to_cpu(de1->inode) || 
682             strcmp (".", de->name) || strcmp ("..", de1->name)) {
683                 ext2_warning (inode->i_sb, "empty_dir",
684                               "bad directory (dir #%lu) - no `.' or `..'",
685                               inode->i_ino);
686                 page_cache_release(page);
687                 return 1;
688         }
689         offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
690         de = (struct ext2_dir_entry_2 *) ((char *) de1 + le16_to_cpu(de1->rec_len));
691         while (offset < inode->i_size ) {
692                 if (!page || (void *) de >= (void *) (page_address(page) + PAGE_SIZE)) {
693                         if (page) {
694                                 UnlockPage(page);
695                                 page_cache_release(page);
696                         }
697                         page = obdfs_getpage(inode, offset, 0, LOCKED);
698                         if (!page) {
699 #if 0
700                                 ext2_error (sb, "empty_dir",
701                                             "directory #%lu contains a hole at offset %lu",
702                                             inode->i_ino, offset);
703 #endif
704                                 offset += sb->s_blocksize;
705                                 continue;
706                         }
707                         de = (struct ext2_dir_entry_2 *) page_address(page);
708                 }
709                 if (!obdfs_check_dir_entry ("empty_dir", inode, de, page,
710                                            offset)) {
711                         UnlockPage(page);
712                         page_cache_release(page);
713                         return 1;
714                 }
715                 if (le32_to_cpu(de->inode)) {
716                         UnlockPage(page);
717                         page_cache_release(page);
718                         return 0;
719                 }
720                 offset += le16_to_cpu(de->rec_len);
721                 de = (struct ext2_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
722         }
723         UnlockPage(page);
724         page_cache_release(page);
725         return 1;
726 }
727
728 int obdfs_rmdir (struct inode * dir, struct dentry *dentry)
729 {
730         int retval;
731         struct inode * inode;
732         struct page *page;
733         struct ext2_dir_entry_2 * de;
734         struct obdfs_sb_info *sbi;
735
736         ENTRY;
737         sbi = dir->i_sb->u.generic_sbp;
738
739         retval = -ENOENT;
740         page = obdfs_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, LOCKED);
741         if (!page)
742                 goto end_rmdir;
743
744         inode = dentry->d_inode;
745         DQUOT_INIT(inode);
746
747         retval = -EIO;
748         if (le32_to_cpu(de->inode) != inode->i_ino)
749                 goto end_rmdir;
750
751         retval = -ENOTEMPTY;
752         if (!empty_dir (inode))
753                 goto end_rmdir;
754
755         retval = obdfs_delete_entry (de, page);
756         dir->i_version = ++event;
757         if (retval)
758                 goto end_rmdir;
759         sbi->osi_ops->o_brw(WRITE, sbi->osi_conn_info.conn_id, dir, page, 0);
760         UnlockPage(page);
761 #if 0
762         if (IS_SYNC(dir)) {
763                 ll_rw_block (WRITE, 1, &bh);
764                 wait_on_buffer (bh);
765         }
766 #endif
767         if (inode->i_nlink != 2)
768                 ext2_warning (inode->i_sb, "ext2_rmdir",
769                               "empty directory has nlink!=2 (%d)",
770                               inode->i_nlink);
771         inode->i_version = ++event;
772         inode->i_nlink = 0;
773         inode->i_size = 0;
774         mark_inode_dirty(inode);
775         dir->i_nlink--;
776         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
777         dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
778         mark_inode_dirty(dir);
779         d_delete(dentry);
780
781 end_rmdir:
782         if ( page )
783                 page_cache_release(page);
784         EXIT;
785         return retval;
786 }
787
788 int obdfs_unlink(struct inode * dir, struct dentry *dentry)
789 {
790         int retval;
791         struct inode * inode;
792         struct page *page;
793         struct ext2_dir_entry_2 * de;
794         struct obdfs_sb_info *sbi;
795
796         ENTRY;
797         sbi = dir->i_sb->u.generic_sbp;
798
799         retval = -ENOENT;
800         page = obdfs_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, LOCKED);
801         if (!page)
802                 goto end_unlink;
803
804         inode = dentry->d_inode;
805         DQUOT_INIT(inode);
806
807         retval = -EIO;
808         if (le32_to_cpu(de->inode) != inode->i_ino)
809                 goto end_unlink;
810         
811         if (!inode->i_nlink) {
812                 ext2_warning (inode->i_sb, "ext2_unlink",
813                               "Deleting nonexistent file (%lu), %d",
814                               inode->i_ino, inode->i_nlink);
815                 inode->i_nlink = 1;
816         }
817         retval = obdfs_delete_entry (de, page);
818         if (retval)
819                 goto end_unlink;
820         dir->i_version = ++event;
821         sbi->osi_ops->o_brw(WRITE, sbi->osi_conn_info.conn_id, dir, page, 0);
822         UnlockPage(page);
823 #if 0
824         if (IS_SYNC(dir)) {
825                 ll_rw_block (WRITE, 1, &bh);
826                 wait_on_buffer (bh);
827         }
828 #endif
829         dir->i_ctime = dir->i_mtime = CURRENT_TIME;
830         dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
831         mark_inode_dirty(dir);
832         inode->i_nlink--;
833         mark_inode_dirty(inode);
834         inode->i_ctime = dir->i_ctime;
835         retval = 0;
836         d_delete(dentry);       /* This also frees the inode */
837
838 end_unlink:
839         if (page)
840                 page_cache_release(page);
841         EXIT;
842         return retval;
843 }
844
845 int obdfs_symlink (struct inode * dir, struct dentry *dentry, const char * symname)
846 {
847         struct ext2_dir_entry_2 * de;
848         struct inode * inode;
849         struct page* page = NULL, * name_page = NULL;
850         char * link;
851         int i, l, err = -EIO;
852         char c;
853         struct obdfs_sb_info *sbi;
854         ino_t ino;
855
856         ENTRY;
857
858         sbi = dir->i_sb->u.generic_sbp;
859         /*
860          * N.B. Several error exits in ext2_new_inode don't set err.
861          */
862         ino = sbi->osi_ops->o_create(sbi->osi_conn_info.conn_id, 0, &err);
863         if ( ino == -1 ) 
864                 return -1;
865         inode =  iget(dir->i_sb, ino);
866         if (!inode)
867                 return err;
868
869         inode->i_mode = S_IFLNK | S_IRWXUGO;
870         inode->i_op = &obdfs_inode_ops;
871         for (l = 0; l < inode->i_sb->s_blocksize - 1 &&
872              symname [l]; l++)
873                 ;
874         if (l >= sizeof (inode->u.ext2_i.i_data)) {
875
876                 CDEBUG(D_INODE, "l=%d, normal symlink\n", l);
877
878                 name_page = obdfs_getpage(inode, 0, 1, LOCKED);
879                 if (!name_page) {
880                         inode->i_nlink--;
881                         mark_inode_dirty(inode);
882                         iput (inode);
883                         return err;
884                 }
885                 link = (char *)page_address(name_page);
886         } else {
887                 link = (char *) inode->u.ext2_i.i_data;
888
889                 CDEBUG(D_INODE, "l=%d, fast symlink\n", l);
890
891         }
892         i = 0;
893         while (i < inode->i_sb->s_blocksize - 1 && (c = *(symname++)))
894                 link[i++] = c;
895         link[i] = 0;
896         if (name_page) {
897                 sbi->osi_ops->o_brw(WRITE, sbi->osi_conn_info.conn_id, 
898                                     inode, name_page, 1);
899                 PDEBUG(name_page, "symlink");
900                 UnlockPage(name_page);
901                 page_cache_release(name_page);
902         }
903         inode->i_size = i;
904         mark_inode_dirty(inode);
905
906         page = obdfs_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
907         if (!page)
908                 goto out_no_entry;
909         de->inode = cpu_to_le32(inode->i_ino);
910         ext2_set_de_type(dir->i_sb, de, S_IFLNK);
911         dir->i_version = ++event;
912         sbi->osi_ops->o_brw(WRITE, sbi->osi_conn_info.conn_id, 
913                             dir, page, 1);
914         UnlockPage(page);
915 #if 0
916         if (IS_SYNC(dir)) {
917                 ll_rw_block (WRITE, 1, &bh);
918                 wait_on_buffer (bh);
919         }
920 #endif
921         d_instantiate(dentry, inode);
922         err = 0;
923 out:
924         return err;
925
926 out_no_entry:
927         inode->i_nlink--;
928         mark_inode_dirty(inode);
929         iput (inode);
930         goto out;
931 }
932
933 int obdfs_link (struct dentry * old_dentry,
934                 struct inode * dir, struct dentry *dentry)
935 {
936         struct inode *inode = old_dentry->d_inode;
937         struct ext2_dir_entry_2 * de;
938         struct page *page;
939         int err;
940         struct obdfs_sb_info *sbi;
941
942         ENTRY;
943         sbi = dir->i_sb->u.generic_sbp;
944
945         if (S_ISDIR(inode->i_mode))
946                 return -EPERM;
947
948         if (inode->i_nlink >= EXT2_LINK_MAX)
949                 return -EMLINK;
950
951         page = obdfs_add_entry (dir, dentry->d_name.name, dentry->d_name.len, &de, &err);
952         if (!page)
953                 return err;
954
955         de->inode = cpu_to_le32(inode->i_ino);
956         ext2_set_de_type(dir->i_sb, de, inode->i_mode);
957         dir->i_version = ++event;
958         sbi->osi_ops->o_brw(WRITE, sbi->osi_conn_info.conn_id, 
959                             dir, page, 0);
960         UnlockPage(page);
961
962 #if 0
963         if (IS_SYNC(dir)) {
964                 ll_rw_block (WRITE, 1, &bh);
965                 wait_on_buffer (bh);
966         }
967 #endif
968         page_cache_release(page);
969         inode->i_nlink++;
970         inode->i_ctime = CURRENT_TIME;
971         mark_inode_dirty(inode);
972         inode->i_count++;
973         d_instantiate(dentry, inode);
974         return 0;
975 }
976
977 #define PARENT_INO(buffer) \
978         ((struct ext2_dir_entry_2 *) ((char *) buffer + \
979         le16_to_cpu(((struct ext2_dir_entry_2 *) buffer)->rec_len)))->inode
980
981 /*
982  * Anybody can rename anything with this: the permission checks are left to the
983  * higher-level routines.
984  */
985 int obdfs_rename (struct inode * old_dir, struct dentry *old_dentry,
986                            struct inode * new_dir, struct dentry *new_dentry)
987 {
988         struct inode * old_inode, * new_inode;
989         struct page * old_page, * new_page, * dir_page;
990         struct ext2_dir_entry_2 * old_de, * new_de;
991         int retval;
992         struct obdfs_sb_info *sbi;
993
994         ENTRY;
995         sbi = old_dir->i_sb->u.generic_sbp;
996
997         new_page = dir_page = NULL;
998
999         /* does the old entry exist? - if not get out */
1000         old_page = obdfs_find_entry (old_dir, old_dentry->d_name.name, old_dentry->d_name.len, &old_de, NOLOCK);
1001         PDEBUG(old_page, "rename - old page");
1002         /*
1003          *  Check for inode number is _not_ due to possible IO errors.
1004          *  We might rmdir the source, keep it as pwd of some process
1005          *  and merrily kill the link to whatever was created under the
1006          *  same name. Goodbye sticky bit ;-<
1007          */
1008         old_inode = old_dentry->d_inode;
1009         retval = -ENOENT;
1010         if (!old_page || le32_to_cpu(old_de->inode) != old_inode->i_ino)
1011                 goto end_rename;
1012
1013         /* find new inode */
1014         new_inode = new_dentry->d_inode;
1015         new_page = obdfs_find_entry (new_dir, new_dentry->d_name.name,
1016                                 new_dentry->d_name.len, &new_de, NOLOCK);
1017         PDEBUG(new_page, "rename - new page ");
1018         if (new_page) {
1019                 if (!new_inode) {
1020                         page_cache_release(new_page);
1021                         new_page = NULL;
1022                 } else {
1023                         DQUOT_INIT(new_inode);
1024                 }
1025         }
1026         /* in this case we to check more ... */
1027         if (S_ISDIR(old_inode->i_mode)) {
1028                 /* can only rename into empty new directory */
1029                 if (new_inode) {
1030                         retval = -ENOTEMPTY;
1031                         if (!empty_dir (new_inode))
1032                                 goto end_rename;
1033                 }
1034                 retval = -EIO;
1035                 dir_page= obdfs_getpage (old_inode, 0, 0, LOCKED);
1036                 PDEBUG(dir_page, "rename dir page");
1037
1038                 if (!dir_page)
1039                         goto end_rename;
1040                 if (le32_to_cpu(PARENT_INO(page_address(dir_page))) != old_dir->i_ino)
1041                         goto end_rename;
1042                 retval = -EMLINK;
1043                 if (!new_inode && new_dir!=old_dir &&
1044                                 new_dir->i_nlink >= EXT2_LINK_MAX)
1045                         goto end_rename;
1046         }
1047         /* create the target dir entry */
1048         if (!new_page) {
1049                 new_page = obdfs_add_entry (new_dir, new_dentry->d_name.name,
1050                                         new_dentry->d_name.len, &new_de,
1051                                         &retval);
1052                 PDEBUG(new_page, "rename new page");
1053                 if (!new_page)
1054                         goto end_rename;
1055         }
1056         new_dir->i_version = ++event;
1057
1058         /*
1059          * remove the old entry
1060          */
1061         new_de->inode = le32_to_cpu(old_inode->i_ino);
1062         if (EXT2_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
1063                                       EXT2_FEATURE_INCOMPAT_FILETYPE))
1064                 new_de->file_type = old_de->file_type;
1065         
1066         obdfs_delete_entry (old_de, old_page);
1067
1068         old_dir->i_version = ++event;
1069         if (new_inode) {
1070                 new_inode->i_nlink--;
1071                 new_inode->i_ctime = CURRENT_TIME;
1072                 mark_inode_dirty(new_inode);
1073         }
1074         old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1075         old_dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
1076         mark_inode_dirty(old_dir);
1077         if (dir_page) {
1078                 PARENT_INO(page_address(dir_page)) = le32_to_cpu(new_dir->i_ino);
1079                 sbi->osi_ops->o_brw(WRITE, sbi->osi_conn_info.conn_id, 
1080                             old_inode, dir_page, 0);
1081                 old_dir->i_nlink--;
1082                 mark_inode_dirty(old_dir);
1083                 if (new_inode) {
1084                         new_inode->i_nlink--;
1085                         mark_inode_dirty(new_inode);
1086                 } else {
1087                         new_dir->i_nlink++;
1088                         new_dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
1089                         mark_inode_dirty(new_dir);
1090                 }
1091         }
1092         if ( old_page != new_page ) {
1093                 unsigned long offset = old_page->offset;
1094                 /* lock the old_page and release unlocked copy */
1095                 CDEBUG(D_INODE, "old_page at %p\n", old_page);
1096                 page_cache_release(old_page);
1097                 old_page = obdfs_getpage(old_dir, offset, 0, LOCKED);
1098                 CDEBUG(D_INODE, "old_page at %p\n", old_page);
1099                 sbi->osi_ops->o_brw(WRITE, sbi->osi_conn_info.conn_id, 
1100                                     old_dir, old_page, 0);
1101         }
1102 #if 0
1103         if (IS_SYNC(old_dir)) {
1104                 ll_rw_block (WRITE, 1, &old_bh);
1105                 wait_on_buffer (old_bh);
1106         }
1107 #endif
1108         sbi->osi_ops->o_brw(WRITE, sbi->osi_conn_info.conn_id, 
1109                             new_dir, new_page, 0);
1110 #if 0
1111         if (IS_SYNC(new_dir)) {
1112                 ll_rw_block (WRITE, 1, &new_bh);
1113                 wait_on_buffer (new_bh);
1114         }
1115 #endif
1116
1117         retval = 0;
1118
1119 end_rename:
1120         if (old_page && PageLocked(old_page) )
1121                 UnlockPage(old_page);
1122         if (old_page)
1123                 page_cache_release(old_page);
1124         if (new_page && PageLocked(new_page) )
1125                 UnlockPage(new_page);
1126         if (new_page)
1127                 page_cache_release(new_page);
1128         if (dir_page && PageLocked(dir_page) )
1129                 UnlockPage(dir_page);
1130         if (dir_page)
1131                 page_cache_release(dir_page);
1132
1133
1134         return retval;
1135 }