ocfs2: Remove delete inode vote
[pandora-kernel.git] / fs / ocfs2 / namei.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * namei.c
5  *
6  * Create and rename file, directory, symlinks
7  *
8  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
9  *
10  *  Portions of this code from linux/fs/ext3/dir.c
11  *
12  *  Copyright (C) 1992, 1993, 1994, 1995
13  *  Remy Card (card@masi.ibp.fr)
14  *  Laboratoire MASI - Institut Blaise pascal
15  *  Universite Pierre et Marie Curie (Paris VI)
16  *
17  *   from
18  *
19  *   linux/fs/minix/dir.c
20  *
21  *   Copyright (C) 1991, 1992 Linux Torvalds
22  *
23  * This program is free software; you can redistribute it and/or
24  * modify it under the terms of the GNU General Public
25  * License as published by the Free Software Foundation; either
26  * version 2 of the License, or (at your option) any later version.
27  *
28  * This program is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
31  * General Public License for more details.
32  *
33  * You should have received a copy of the GNU General Public
34  * License along with this program; if not, write to the
35  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
36  * Boston, MA 021110-1307, USA.
37  */
38
39 #include <linux/fs.h>
40 #include <linux/types.h>
41 #include <linux/slab.h>
42 #include <linux/highmem.h>
43
44 #define MLOG_MASK_PREFIX ML_NAMEI
45 #include <cluster/masklog.h>
46
47 #include "ocfs2.h"
48
49 #include "alloc.h"
50 #include "dcache.h"
51 #include "dir.h"
52 #include "dlmglue.h"
53 #include "extent_map.h"
54 #include "file.h"
55 #include "inode.h"
56 #include "journal.h"
57 #include "namei.h"
58 #include "suballoc.h"
59 #include "super.h"
60 #include "symlink.h"
61 #include "sysfile.h"
62 #include "uptodate.h"
63 #include "vote.h"
64
65 #include "buffer_head_io.h"
66
67 #define NAMEI_RA_CHUNKS  2
68 #define NAMEI_RA_BLOCKS  4
69 #define NAMEI_RA_SIZE        (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
70 #define NAMEI_RA_INDEX(c,b)  (((c) * NAMEI_RA_BLOCKS) + (b))
71
72 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
73                                         struct inode *dir,
74                                         const char *name, int namelen,
75                                         unsigned long offset,
76                                         struct ocfs2_dir_entry **res_dir);
77
78 static int ocfs2_delete_entry(handle_t *handle,
79                               struct inode *dir,
80                               struct ocfs2_dir_entry *de_del,
81                               struct buffer_head *bh);
82
83 static int __ocfs2_add_entry(handle_t *handle,
84                              struct inode *dir,
85                              const char *name, int namelen,
86                              struct inode *inode, u64 blkno,
87                              struct buffer_head *parent_fe_bh,
88                              struct buffer_head *insert_bh);
89
90 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
91                               struct inode *dir,
92                               struct dentry *dentry, int mode,
93                               dev_t dev,
94                               struct buffer_head **new_fe_bh,
95                               struct buffer_head *parent_fe_bh,
96                               handle_t *handle,
97                               struct inode **ret_inode,
98                               struct ocfs2_alloc_context *inode_ac);
99
100 static int ocfs2_fill_new_dir(struct ocfs2_super *osb,
101                               handle_t *handle,
102                               struct inode *parent,
103                               struct inode *inode,
104                               struct buffer_head *fe_bh,
105                               struct ocfs2_alloc_context *data_ac);
106
107 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
108                                     struct inode **ret_orphan_dir,
109                                     struct inode *inode,
110                                     char *name,
111                                     struct buffer_head **de_bh);
112
113 static int ocfs2_orphan_add(struct ocfs2_super *osb,
114                             handle_t *handle,
115                             struct inode *inode,
116                             struct ocfs2_dinode *fe,
117                             char *name,
118                             struct buffer_head *de_bh,
119                             struct inode *orphan_dir_inode);
120
121 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
122                                      handle_t *handle,
123                                      struct inode *inode,
124                                      const char *symname);
125
126 static inline int ocfs2_add_entry(handle_t *handle,
127                                   struct dentry *dentry,
128                                   struct inode *inode, u64 blkno,
129                                   struct buffer_head *parent_fe_bh,
130                                   struct buffer_head *insert_bh)
131 {
132         return __ocfs2_add_entry(handle, dentry->d_parent->d_inode,
133                                  dentry->d_name.name, dentry->d_name.len,
134                                  inode, blkno, parent_fe_bh, insert_bh);
135 }
136
137 /* An orphan dir name is an 8 byte value, printed as a hex string */
138 #define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
139
140 static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
141                                    struct nameidata *nd)
142 {
143         int status;
144         u64 blkno;
145         struct buffer_head *dirent_bh = NULL;
146         struct inode *inode = NULL;
147         struct dentry *ret;
148         struct ocfs2_dir_entry *dirent;
149         struct ocfs2_inode_info *oi;
150
151         mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
152                    dentry->d_name.len, dentry->d_name.name);
153
154         if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
155                 ret = ERR_PTR(-ENAMETOOLONG);
156                 goto bail;
157         }
158
159         mlog(0, "find name %.*s in directory %llu\n", dentry->d_name.len,
160              dentry->d_name.name, (unsigned long long)OCFS2_I(dir)->ip_blkno);
161
162         status = ocfs2_meta_lock(dir, NULL, 0);
163         if (status < 0) {
164                 if (status != -ENOENT)
165                         mlog_errno(status);
166                 ret = ERR_PTR(status);
167                 goto bail;
168         }
169
170         status = ocfs2_find_files_on_disk(dentry->d_name.name,
171                                           dentry->d_name.len, &blkno,
172                                           dir, &dirent_bh, &dirent);
173         if (status < 0)
174                 goto bail_add;
175
176         inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0);
177         if (IS_ERR(inode)) {
178                 ret = ERR_PTR(-EACCES);
179                 goto bail_unlock;
180         }
181
182         oi = OCFS2_I(inode);
183         /* Clear any orphaned state... If we were able to look up the
184          * inode from a directory, it certainly can't be orphaned. We
185          * might have the bad state from a node which intended to
186          * orphan this inode but crashed before it could commit the
187          * unlink. */
188         spin_lock(&oi->ip_lock);
189         oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
190         spin_unlock(&oi->ip_lock);
191
192 bail_add:
193         dentry->d_op = &ocfs2_dentry_ops;
194         ret = d_splice_alias(inode, dentry);
195
196         if (inode) {
197                 /*
198                  * If d_splice_alias() finds a DCACHE_DISCONNECTED
199                  * dentry, it will d_move() it on top of ourse. The
200                  * return value will indicate this however, so in
201                  * those cases, we switch them around for the locking
202                  * code.
203                  *
204                  * NOTE: This dentry already has ->d_op set from
205                  * ocfs2_get_parent() and ocfs2_get_dentry()
206                  */
207                 if (ret)
208                         dentry = ret;
209
210                 status = ocfs2_dentry_attach_lock(dentry, inode,
211                                                   OCFS2_I(dir)->ip_blkno);
212                 if (status) {
213                         mlog_errno(status);
214                         ret = ERR_PTR(status);
215                         goto bail_unlock;
216                 }
217         }
218
219 bail_unlock:
220         /* Don't drop the cluster lock until *after* the d_add --
221          * unlink on another node will message us to remove that
222          * dentry under this lock so otherwise we can race this with
223          * the vote thread and have a stale dentry. */
224         ocfs2_meta_unlock(dir, 0);
225
226 bail:
227         if (dirent_bh)
228                 brelse(dirent_bh);
229
230         mlog_exit_ptr(ret);
231
232         return ret;
233 }
234
235 static int ocfs2_fill_new_dir(struct ocfs2_super *osb,
236                               handle_t *handle,
237                               struct inode *parent,
238                               struct inode *inode,
239                               struct buffer_head *fe_bh,
240                               struct ocfs2_alloc_context *data_ac)
241 {
242         int status;
243         struct buffer_head *new_bh = NULL;
244         struct ocfs2_dir_entry *de = NULL;
245
246         mlog_entry_void();
247
248         status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
249                                      data_ac, NULL, &new_bh);
250         if (status < 0) {
251                 mlog_errno(status);
252                 goto bail;
253         }
254
255         ocfs2_set_new_buffer_uptodate(inode, new_bh);
256
257         status = ocfs2_journal_access(handle, inode, new_bh,
258                                       OCFS2_JOURNAL_ACCESS_CREATE);
259         if (status < 0) {
260                 mlog_errno(status);
261                 goto bail;
262         }
263         memset(new_bh->b_data, 0, osb->sb->s_blocksize);
264
265         de = (struct ocfs2_dir_entry *) new_bh->b_data;
266         de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
267         de->name_len = 1;
268         de->rec_len =
269                 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
270         strcpy(de->name, ".");
271         ocfs2_set_de_type(de, S_IFDIR);
272         de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
273         de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
274         de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize -
275                                   OCFS2_DIR_REC_LEN(1));
276         de->name_len = 2;
277         strcpy(de->name, "..");
278         ocfs2_set_de_type(de, S_IFDIR);
279
280         status = ocfs2_journal_dirty(handle, new_bh);
281         if (status < 0) {
282                 mlog_errno(status);
283                 goto bail;
284         }
285
286         i_size_write(inode, inode->i_sb->s_blocksize);
287         inode->i_nlink = 2;
288         inode->i_blocks = ocfs2_align_bytes_to_sectors(inode->i_sb->s_blocksize);
289         status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
290         if (status < 0) {
291                 mlog_errno(status);
292                 goto bail;
293         }
294
295         status = 0;
296 bail:
297         if (new_bh)
298                 brelse(new_bh);
299
300         mlog_exit(status);
301         return status;
302 }
303
304 static int ocfs2_mknod(struct inode *dir,
305                        struct dentry *dentry,
306                        int mode,
307                        dev_t dev)
308 {
309         int status = 0;
310         struct buffer_head *parent_fe_bh = NULL;
311         handle_t *handle = NULL;
312         struct ocfs2_super *osb;
313         struct ocfs2_dinode *dirfe;
314         struct buffer_head *new_fe_bh = NULL;
315         struct buffer_head *de_bh = NULL;
316         struct inode *inode = NULL;
317         struct ocfs2_alloc_context *inode_ac = NULL;
318         struct ocfs2_alloc_context *data_ac = NULL;
319
320         mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
321                    (unsigned long)dev, dentry->d_name.len,
322                    dentry->d_name.name);
323
324         /* get our super block */
325         osb = OCFS2_SB(dir->i_sb);
326
327         status = ocfs2_meta_lock(dir, &parent_fe_bh, 1);
328         if (status < 0) {
329                 if (status != -ENOENT)
330                         mlog_errno(status);
331                 return status;
332         }
333
334         if (S_ISDIR(mode) && (dir->i_nlink >= OCFS2_LINK_MAX)) {
335                 status = -EMLINK;
336                 goto leave;
337         }
338
339         dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
340         if (!dirfe->i_links_count) {
341                 /* can't make a file in a deleted directory. */
342                 status = -ENOENT;
343                 goto leave;
344         }
345
346         status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
347                                            dentry->d_name.len);
348         if (status)
349                 goto leave;
350
351         /* get a spot inside the dir. */
352         status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
353                                               dentry->d_name.name,
354                                               dentry->d_name.len, &de_bh);
355         if (status < 0) {
356                 mlog_errno(status);
357                 goto leave;
358         }
359
360         /* reserve an inode spot */
361         status = ocfs2_reserve_new_inode(osb, &inode_ac);
362         if (status < 0) {
363                 if (status != -ENOSPC)
364                         mlog_errno(status);
365                 goto leave;
366         }
367
368         /* are we making a directory? If so, reserve a cluster for his
369          * 1st extent. */
370         if (S_ISDIR(mode)) {
371                 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
372                 if (status < 0) {
373                         if (status != -ENOSPC)
374                                 mlog_errno(status);
375                         goto leave;
376                 }
377         }
378
379         handle = ocfs2_start_trans(osb, OCFS2_MKNOD_CREDITS);
380         if (IS_ERR(handle)) {
381                 status = PTR_ERR(handle);
382                 handle = NULL;
383                 mlog_errno(status);
384                 goto leave;
385         }
386
387         /* do the real work now. */
388         status = ocfs2_mknod_locked(osb, dir, dentry, mode, dev,
389                                     &new_fe_bh, parent_fe_bh, handle,
390                                     &inode, inode_ac);
391         if (status < 0) {
392                 mlog_errno(status);
393                 goto leave;
394         }
395
396         if (S_ISDIR(mode)) {
397                 status = ocfs2_fill_new_dir(osb, handle, dir, inode,
398                                             new_fe_bh, data_ac);
399                 if (status < 0) {
400                         mlog_errno(status);
401                         goto leave;
402                 }
403
404                 status = ocfs2_journal_access(handle, dir, parent_fe_bh,
405                                               OCFS2_JOURNAL_ACCESS_WRITE);
406                 if (status < 0) {
407                         mlog_errno(status);
408                         goto leave;
409                 }
410                 le16_add_cpu(&dirfe->i_links_count, 1);
411                 status = ocfs2_journal_dirty(handle, parent_fe_bh);
412                 if (status < 0) {
413                         mlog_errno(status);
414                         goto leave;
415                 }
416                 inc_nlink(dir);
417         }
418
419         status = ocfs2_add_entry(handle, dentry, inode,
420                                  OCFS2_I(inode)->ip_blkno, parent_fe_bh,
421                                  de_bh);
422         if (status < 0) {
423                 mlog_errno(status);
424                 goto leave;
425         }
426
427         status = ocfs2_dentry_attach_lock(dentry, inode,
428                                           OCFS2_I(dir)->ip_blkno);
429         if (status) {
430                 mlog_errno(status);
431                 goto leave;
432         }
433
434         insert_inode_hash(inode);
435         dentry->d_op = &ocfs2_dentry_ops;
436         d_instantiate(dentry, inode);
437         status = 0;
438 leave:
439         if (handle)
440                 ocfs2_commit_trans(osb, handle);
441
442         ocfs2_meta_unlock(dir, 1);
443
444         if (status == -ENOSPC)
445                 mlog(0, "Disk is full\n");
446
447         if (new_fe_bh)
448                 brelse(new_fe_bh);
449
450         if (de_bh)
451                 brelse(de_bh);
452
453         if (parent_fe_bh)
454                 brelse(parent_fe_bh);
455
456         if ((status < 0) && inode)
457                 iput(inode);
458
459         if (inode_ac)
460                 ocfs2_free_alloc_context(inode_ac);
461
462         if (data_ac)
463                 ocfs2_free_alloc_context(data_ac);
464
465         mlog_exit(status);
466
467         return status;
468 }
469
470 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
471                               struct inode *dir,
472                               struct dentry *dentry, int mode,
473                               dev_t dev,
474                               struct buffer_head **new_fe_bh,
475                               struct buffer_head *parent_fe_bh,
476                               handle_t *handle,
477                               struct inode **ret_inode,
478                               struct ocfs2_alloc_context *inode_ac)
479 {
480         int status = 0;
481         struct ocfs2_dinode *fe = NULL;
482         struct ocfs2_extent_list *fel;
483         u64 fe_blkno = 0;
484         u16 suballoc_bit;
485         struct inode *inode = NULL;
486
487         mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
488                    (unsigned long)dev, dentry->d_name.len,
489                    dentry->d_name.name);
490
491         *new_fe_bh = NULL;
492         *ret_inode = NULL;
493
494         status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit,
495                                        &fe_blkno);
496         if (status < 0) {
497                 mlog_errno(status);
498                 goto leave;
499         }
500
501         inode = new_inode(dir->i_sb);
502         if (IS_ERR(inode)) {
503                 status = PTR_ERR(inode);
504                 mlog(ML_ERROR, "new_inode failed!\n");
505                 goto leave;
506         }
507
508         /* populate as many fields early on as possible - many of
509          * these are used by the support functions here and in
510          * callers. */
511         inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
512         OCFS2_I(inode)->ip_blkno = fe_blkno;
513         if (S_ISDIR(mode))
514                 inode->i_nlink = 2;
515         else
516                 inode->i_nlink = 1;
517         inode->i_mode = mode;
518         spin_lock(&osb->osb_lock);
519         inode->i_generation = osb->s_next_generation++;
520         spin_unlock(&osb->osb_lock);
521
522         *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
523         if (!*new_fe_bh) {
524                 status = -EIO;
525                 mlog_errno(status);
526                 goto leave;
527         }
528         ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh);
529
530         status = ocfs2_journal_access(handle, inode, *new_fe_bh,
531                                       OCFS2_JOURNAL_ACCESS_CREATE);
532         if (status < 0) {
533                 mlog_errno(status);
534                 goto leave;
535         }
536
537         fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
538         memset(fe, 0, osb->sb->s_blocksize);
539
540         fe->i_generation = cpu_to_le32(inode->i_generation);
541         fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
542         fe->i_blkno = cpu_to_le64(fe_blkno);
543         fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
544         fe->i_suballoc_slot = cpu_to_le16(osb->slot_num);
545         fe->i_uid = cpu_to_le32(current->fsuid);
546         if (dir->i_mode & S_ISGID) {
547                 fe->i_gid = cpu_to_le32(dir->i_gid);
548                 if (S_ISDIR(mode))
549                         mode |= S_ISGID;
550         } else
551                 fe->i_gid = cpu_to_le32(current->fsgid);
552         fe->i_mode = cpu_to_le16(mode);
553         if (S_ISCHR(mode) || S_ISBLK(mode))
554                 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
555
556         fe->i_links_count = cpu_to_le16(inode->i_nlink);
557
558         fe->i_last_eb_blk = 0;
559         strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
560         le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
561         fe->i_atime = fe->i_ctime = fe->i_mtime =
562                 cpu_to_le64(CURRENT_TIME.tv_sec);
563         fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
564                 cpu_to_le32(CURRENT_TIME.tv_nsec);
565         fe->i_dtime = 0;
566
567         fel = &fe->id2.i_list;
568         fel->l_tree_depth = 0;
569         fel->l_next_free_rec = 0;
570         fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
571
572         status = ocfs2_journal_dirty(handle, *new_fe_bh);
573         if (status < 0) {
574                 mlog_errno(status);
575                 goto leave;
576         }
577
578         if (ocfs2_populate_inode(inode, fe, 1) < 0) {
579                 mlog(ML_ERROR, "populate inode failed! bh->b_blocknr=%llu, "
580                      "i_blkno=%llu, i_ino=%lu\n",
581                      (unsigned long long) (*new_fe_bh)->b_blocknr,
582                      (unsigned long long)fe->i_blkno, inode->i_ino);
583                 BUG();
584         }
585
586         ocfs2_inode_set_new(osb, inode);
587         if (!ocfs2_mount_local(osb)) {
588                 status = ocfs2_create_new_inode_locks(inode);
589                 if (status < 0)
590                         mlog_errno(status);
591         }
592
593         status = 0; /* error in ocfs2_create_new_inode_locks is not
594                      * critical */
595
596         *ret_inode = inode;
597 leave:
598         if (status < 0) {
599                 if (*new_fe_bh) {
600                         brelse(*new_fe_bh);
601                         *new_fe_bh = NULL;
602                 }
603                 if (inode)
604                         iput(inode);
605         }
606
607         mlog_exit(status);
608         return status;
609 }
610
611 static int ocfs2_mkdir(struct inode *dir,
612                        struct dentry *dentry,
613                        int mode)
614 {
615         int ret;
616
617         mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
618                    dentry->d_name.len, dentry->d_name.name);
619         ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
620         mlog_exit(ret);
621
622         return ret;
623 }
624
625 static int ocfs2_create(struct inode *dir,
626                         struct dentry *dentry,
627                         int mode,
628                         struct nameidata *nd)
629 {
630         int ret;
631
632         mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
633                    dentry->d_name.len, dentry->d_name.name);
634         ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
635         mlog_exit(ret);
636
637         return ret;
638 }
639
640 static int ocfs2_link(struct dentry *old_dentry,
641                       struct inode *dir,
642                       struct dentry *dentry)
643 {
644         handle_t *handle;
645         struct inode *inode = old_dentry->d_inode;
646         int err;
647         struct buffer_head *fe_bh = NULL;
648         struct buffer_head *parent_fe_bh = NULL;
649         struct buffer_head *de_bh = NULL;
650         struct ocfs2_dinode *fe = NULL;
651         struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
652
653         mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
654                    old_dentry->d_name.len, old_dentry->d_name.name,
655                    dentry->d_name.len, dentry->d_name.name);
656
657         if (S_ISDIR(inode->i_mode))
658                 return -EPERM;
659
660         err = ocfs2_meta_lock(dir, &parent_fe_bh, 1);
661         if (err < 0) {
662                 if (err != -ENOENT)
663                         mlog_errno(err);
664                 return err;
665         }
666
667         if (!dir->i_nlink) {
668                 err = -ENOENT;
669                 goto out;
670         }
671
672         err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
673                                         dentry->d_name.len);
674         if (err)
675                 goto out;
676
677         err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
678                                            dentry->d_name.name,
679                                            dentry->d_name.len, &de_bh);
680         if (err < 0) {
681                 mlog_errno(err);
682                 goto out;
683         }
684
685         err = ocfs2_meta_lock(inode, &fe_bh, 1);
686         if (err < 0) {
687                 if (err != -ENOENT)
688                         mlog_errno(err);
689                 goto out;
690         }
691
692         fe = (struct ocfs2_dinode *) fe_bh->b_data;
693         if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) {
694                 err = -EMLINK;
695                 goto out_unlock_inode;
696         }
697
698         handle = ocfs2_start_trans(osb, OCFS2_LINK_CREDITS);
699         if (IS_ERR(handle)) {
700                 err = PTR_ERR(handle);
701                 handle = NULL;
702                 mlog_errno(err);
703                 goto out_unlock_inode;
704         }
705
706         err = ocfs2_journal_access(handle, inode, fe_bh,
707                                    OCFS2_JOURNAL_ACCESS_WRITE);
708         if (err < 0) {
709                 mlog_errno(err);
710                 goto out_commit;
711         }
712
713         inc_nlink(inode);
714         inode->i_ctime = CURRENT_TIME;
715         fe->i_links_count = cpu_to_le16(inode->i_nlink);
716         fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
717         fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
718
719         err = ocfs2_journal_dirty(handle, fe_bh);
720         if (err < 0) {
721                 le16_add_cpu(&fe->i_links_count, -1);
722                 drop_nlink(inode);
723                 mlog_errno(err);
724                 goto out_commit;
725         }
726
727         err = ocfs2_add_entry(handle, dentry, inode,
728                               OCFS2_I(inode)->ip_blkno,
729                               parent_fe_bh, de_bh);
730         if (err) {
731                 le16_add_cpu(&fe->i_links_count, -1);
732                 drop_nlink(inode);
733                 mlog_errno(err);
734                 goto out_commit;
735         }
736
737         err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
738         if (err) {
739                 mlog_errno(err);
740                 goto out_commit;
741         }
742
743         atomic_inc(&inode->i_count);
744         dentry->d_op = &ocfs2_dentry_ops;
745         d_instantiate(dentry, inode);
746
747 out_commit:
748         ocfs2_commit_trans(osb, handle);
749 out_unlock_inode:
750         ocfs2_meta_unlock(inode, 1);
751
752 out:
753         ocfs2_meta_unlock(dir, 1);
754
755         if (de_bh)
756                 brelse(de_bh);
757         if (fe_bh)
758                 brelse(fe_bh);
759         if (parent_fe_bh)
760                 brelse(parent_fe_bh);
761
762         mlog_exit(err);
763
764         return err;
765 }
766
767 /*
768  * Takes and drops an exclusive lock on the given dentry. This will
769  * force other nodes to drop it.
770  */
771 static int ocfs2_remote_dentry_delete(struct dentry *dentry)
772 {
773         int ret;
774
775         ret = ocfs2_dentry_lock(dentry, 1);
776         if (ret)
777                 mlog_errno(ret);
778         else
779                 ocfs2_dentry_unlock(dentry, 1);
780
781         return ret;
782 }
783
784 static inline int inode_is_unlinkable(struct inode *inode)
785 {
786         if (S_ISDIR(inode->i_mode)) {
787                 if (inode->i_nlink == 2)
788                         return 1;
789                 return 0;
790         }
791
792         if (inode->i_nlink == 1)
793                 return 1;
794         return 0;
795 }
796
797 static int ocfs2_unlink(struct inode *dir,
798                         struct dentry *dentry)
799 {
800         int status;
801         int child_locked = 0;
802         struct inode *inode = dentry->d_inode;
803         struct inode *orphan_dir = NULL;
804         struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
805         u64 blkno;
806         struct ocfs2_dinode *fe = NULL;
807         struct buffer_head *fe_bh = NULL;
808         struct buffer_head *parent_node_bh = NULL;
809         handle_t *handle = NULL;
810         struct ocfs2_dir_entry *dirent = NULL;
811         struct buffer_head *dirent_bh = NULL;
812         char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
813         struct buffer_head *orphan_entry_bh = NULL;
814
815         mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
816                    dentry->d_name.len, dentry->d_name.name);
817
818         BUG_ON(dentry->d_parent->d_inode != dir);
819
820         mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
821
822         if (inode == osb->root_inode) {
823                 mlog(0, "Cannot delete the root directory\n");
824                 return -EPERM;
825         }
826
827         status = ocfs2_meta_lock(dir, &parent_node_bh, 1);
828         if (status < 0) {
829                 if (status != -ENOENT)
830                         mlog_errno(status);
831                 return status;
832         }
833
834         status = ocfs2_find_files_on_disk(dentry->d_name.name,
835                                           dentry->d_name.len, &blkno,
836                                           dir, &dirent_bh, &dirent);
837         if (status < 0) {
838                 if (status != -ENOENT)
839                         mlog_errno(status);
840                 goto leave;
841         }
842
843         if (OCFS2_I(inode)->ip_blkno != blkno) {
844                 status = -ENOENT;
845
846                 mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
847                      (unsigned long long)OCFS2_I(inode)->ip_blkno,
848                      (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
849                 goto leave;
850         }
851
852         status = ocfs2_meta_lock(inode, &fe_bh, 1);
853         if (status < 0) {
854                 if (status != -ENOENT)
855                         mlog_errno(status);
856                 goto leave;
857         }
858         child_locked = 1;
859
860         if (S_ISDIR(inode->i_mode)) {
861                 if (!ocfs2_empty_dir(inode)) {
862                         status = -ENOTEMPTY;
863                         goto leave;
864                 } else if (inode->i_nlink != 2) {
865                         status = -ENOTEMPTY;
866                         goto leave;
867                 }
868         }
869
870         status = ocfs2_remote_dentry_delete(dentry);
871         if (status < 0) {
872                 /* This vote should succeed under all normal
873                  * circumstances. */
874                 mlog_errno(status);
875                 goto leave;
876         }
877
878         if (inode_is_unlinkable(inode)) {
879                 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode,
880                                                   orphan_name,
881                                                   &orphan_entry_bh);
882                 if (status < 0) {
883                         mlog_errno(status);
884                         goto leave;
885                 }
886         }
887
888         handle = ocfs2_start_trans(osb, OCFS2_UNLINK_CREDITS);
889         if (IS_ERR(handle)) {
890                 status = PTR_ERR(handle);
891                 handle = NULL;
892                 mlog_errno(status);
893                 goto leave;
894         }
895
896         status = ocfs2_journal_access(handle, inode, fe_bh,
897                                       OCFS2_JOURNAL_ACCESS_WRITE);
898         if (status < 0) {
899                 mlog_errno(status);
900                 goto leave;
901         }
902
903         fe = (struct ocfs2_dinode *) fe_bh->b_data;
904
905         if (inode_is_unlinkable(inode)) {
906                 status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
907                                           orphan_entry_bh, orphan_dir);
908                 if (status < 0) {
909                         mlog_errno(status);
910                         goto leave;
911                 }
912         }
913
914         /* delete the name from the parent dir */
915         status = ocfs2_delete_entry(handle, dir, dirent, dirent_bh);
916         if (status < 0) {
917                 mlog_errno(status);
918                 goto leave;
919         }
920
921         if (S_ISDIR(inode->i_mode))
922                 drop_nlink(inode);
923         drop_nlink(inode);
924         fe->i_links_count = cpu_to_le16(inode->i_nlink);
925
926         status = ocfs2_journal_dirty(handle, fe_bh);
927         if (status < 0) {
928                 mlog_errno(status);
929                 goto leave;
930         }
931
932         dir->i_ctime = dir->i_mtime = CURRENT_TIME;
933         if (S_ISDIR(inode->i_mode))
934                 drop_nlink(dir);
935
936         status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
937         if (status < 0) {
938                 mlog_errno(status);
939                 if (S_ISDIR(inode->i_mode))
940                         inc_nlink(dir);
941         }
942
943 leave:
944         if (handle)
945                 ocfs2_commit_trans(osb, handle);
946
947         if (child_locked)
948                 ocfs2_meta_unlock(inode, 1);
949
950         ocfs2_meta_unlock(dir, 1);
951
952         if (orphan_dir) {
953                 /* This was locked for us in ocfs2_prepare_orphan_dir() */
954                 ocfs2_meta_unlock(orphan_dir, 1);
955                 mutex_unlock(&orphan_dir->i_mutex);
956                 iput(orphan_dir);
957         }
958
959         if (fe_bh)
960                 brelse(fe_bh);
961
962         if (dirent_bh)
963                 brelse(dirent_bh);
964
965         if (parent_node_bh)
966                 brelse(parent_node_bh);
967
968         if (orphan_entry_bh)
969                 brelse(orphan_entry_bh);
970
971         mlog_exit(status);
972
973         return status;
974 }
975
976 /*
977  * The only place this should be used is rename!
978  * if they have the same id, then the 1st one is the only one locked.
979  */
980 static int ocfs2_double_lock(struct ocfs2_super *osb,
981                              struct buffer_head **bh1,
982                              struct inode *inode1,
983                              struct buffer_head **bh2,
984                              struct inode *inode2)
985 {
986         int status;
987         struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
988         struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
989         struct buffer_head **tmpbh;
990         struct inode *tmpinode;
991
992         mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
993                    (unsigned long long)oi1->ip_blkno,
994                    (unsigned long long)oi2->ip_blkno);
995
996         if (*bh1)
997                 *bh1 = NULL;
998         if (*bh2)
999                 *bh2 = NULL;
1000
1001         /* we always want to lock the one with the lower lockid first. */
1002         if (oi1->ip_blkno != oi2->ip_blkno) {
1003                 if (oi1->ip_blkno < oi2->ip_blkno) {
1004                         /* switch id1 and id2 around */
1005                         mlog(0, "switching them around...\n");
1006                         tmpbh = bh2;
1007                         bh2 = bh1;
1008                         bh1 = tmpbh;
1009
1010                         tmpinode = inode2;
1011                         inode2 = inode1;
1012                         inode1 = tmpinode;
1013                 }
1014                 /* lock id2 */
1015                 status = ocfs2_meta_lock(inode2, bh2, 1);
1016                 if (status < 0) {
1017                         if (status != -ENOENT)
1018                                 mlog_errno(status);
1019                         goto bail;
1020                 }
1021         }
1022
1023         /* lock id1 */
1024         status = ocfs2_meta_lock(inode1, bh1, 1);
1025         if (status < 0) {
1026                 /*
1027                  * An error return must mean that no cluster locks
1028                  * were held on function exit.
1029                  */
1030                 if (oi1->ip_blkno != oi2->ip_blkno)
1031                         ocfs2_meta_unlock(inode2, 1);
1032
1033                 if (status != -ENOENT)
1034                         mlog_errno(status);
1035         }
1036
1037 bail:
1038         mlog_exit(status);
1039         return status;
1040 }
1041
1042 static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
1043 {
1044         ocfs2_meta_unlock(inode1, 1);
1045
1046         if (inode1 != inode2)
1047                 ocfs2_meta_unlock(inode2, 1);
1048 }
1049
1050 #define PARENT_INO(buffer) \
1051         ((struct ocfs2_dir_entry *) \
1052          ((char *)buffer + \
1053           le16_to_cpu(((struct ocfs2_dir_entry *)buffer)->rec_len)))->inode
1054
1055 static int ocfs2_rename(struct inode *old_dir,
1056                         struct dentry *old_dentry,
1057                         struct inode *new_dir,
1058                         struct dentry *new_dentry)
1059 {
1060         int status = 0, rename_lock = 0, parents_locked = 0;
1061         int old_child_locked = 0, new_child_locked = 0;
1062         struct inode *old_inode = old_dentry->d_inode;
1063         struct inode *new_inode = new_dentry->d_inode;
1064         struct inode *orphan_dir = NULL;
1065         struct ocfs2_dinode *newfe = NULL;
1066         char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
1067         struct buffer_head *orphan_entry_bh = NULL;
1068         struct buffer_head *newfe_bh = NULL;
1069         struct buffer_head *old_inode_bh = NULL;
1070         struct buffer_head *insert_entry_bh = NULL;
1071         struct ocfs2_super *osb = NULL;
1072         u64 newfe_blkno;
1073         handle_t *handle = NULL;
1074         struct buffer_head *old_dir_bh = NULL;
1075         struct buffer_head *new_dir_bh = NULL;
1076         struct ocfs2_dir_entry *old_de = NULL, *new_de = NULL; // dirent for old_dentry
1077                                                                // and new_dentry
1078         struct buffer_head *new_de_bh = NULL, *old_de_bh = NULL; // bhs for above
1079         struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir,
1080                                                     // this is the 1st dirent bh
1081         nlink_t old_dir_nlink = old_dir->i_nlink;
1082
1083         /* At some point it might be nice to break this function up a
1084          * bit. */
1085
1086         mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
1087                    old_dir, old_dentry, new_dir, new_dentry,
1088                    old_dentry->d_name.len, old_dentry->d_name.name,
1089                    new_dentry->d_name.len, new_dentry->d_name.name);
1090
1091         osb = OCFS2_SB(old_dir->i_sb);
1092
1093         if (new_inode) {
1094                 if (!igrab(new_inode))
1095                         BUG();
1096         }
1097
1098         /* Assume a directory hierarchy thusly:
1099          * a/b/c
1100          * a/d
1101          * a,b,c, and d are all directories.
1102          *
1103          * from cwd of 'a' on both nodes:
1104          * node1: mv b/c d
1105          * node2: mv d   b/c
1106          *
1107          * And that's why, just like the VFS, we need a file system
1108          * rename lock. */
1109         if (old_dentry != new_dentry) {
1110                 status = ocfs2_rename_lock(osb);
1111                 if (status < 0) {
1112                         mlog_errno(status);
1113                         goto bail;
1114                 }
1115                 rename_lock = 1;
1116         }
1117
1118         /* if old and new are the same, this'll just do one lock. */
1119         status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
1120                                    &new_dir_bh, new_dir);
1121         if (status < 0) {
1122                 mlog_errno(status);
1123                 goto bail;
1124         }
1125         parents_locked = 1;
1126
1127         /* make sure both dirs have bhs
1128          * get an extra ref on old_dir_bh if old==new */
1129         if (!new_dir_bh) {
1130                 if (old_dir_bh) {
1131                         new_dir_bh = old_dir_bh;
1132                         get_bh(new_dir_bh);
1133                 } else {
1134                         mlog(ML_ERROR, "no old_dir_bh!\n");
1135                         status = -EIO;
1136                         goto bail;
1137                 }
1138         }
1139
1140         /*
1141          * Aside from allowing a meta data update, the locking here
1142          * also ensures that the vote thread on other nodes won't have
1143          * to concurrently downconvert the inode and the dentry locks.
1144          */
1145         status = ocfs2_meta_lock(old_inode, &old_inode_bh, 1);
1146         if (status < 0) {
1147                 if (status != -ENOENT)
1148                         mlog_errno(status);
1149                 goto bail;
1150         }
1151         old_child_locked = 1;
1152
1153         status = ocfs2_remote_dentry_delete(old_dentry);
1154         if (status < 0) {
1155                 mlog_errno(status);
1156                 goto bail;
1157         }
1158
1159         if (S_ISDIR(old_inode->i_mode)) {
1160                 status = -EIO;
1161                 old_inode_de_bh = ocfs2_bread(old_inode, 0, &status, 0);
1162                 if (!old_inode_de_bh)
1163                         goto bail;
1164
1165                 status = -EIO;
1166                 if (le64_to_cpu(PARENT_INO(old_inode_de_bh->b_data)) !=
1167                     OCFS2_I(old_dir)->ip_blkno)
1168                         goto bail;
1169                 status = -EMLINK;
1170                 if (!new_inode && new_dir!=old_dir &&
1171                     new_dir->i_nlink >= OCFS2_LINK_MAX)
1172                         goto bail;
1173         }
1174
1175         status = -ENOENT;
1176         old_de_bh = ocfs2_find_entry(old_dentry->d_name.name,
1177                                      old_dentry->d_name.len,
1178                                      old_dir, &old_de);
1179         if (!old_de_bh)
1180                 goto bail;
1181
1182         /*
1183          *  Check for inode number is _not_ due to possible IO errors.
1184          *  We might rmdir the source, keep it as pwd of some process
1185          *  and merrily kill the link to whatever was created under the
1186          *  same name. Goodbye sticky bit ;-<
1187          */
1188         if (le64_to_cpu(old_de->inode) != OCFS2_I(old_inode)->ip_blkno)
1189                 goto bail;
1190
1191         /* check if the target already exists (in which case we need
1192          * to delete it */
1193         status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
1194                                           new_dentry->d_name.len,
1195                                           &newfe_blkno, new_dir, &new_de_bh,
1196                                           &new_de);
1197         /* The only error we allow here is -ENOENT because the new
1198          * file not existing is perfectly valid. */
1199         if ((status < 0) && (status != -ENOENT)) {
1200                 /* If we cannot find the file specified we should just */
1201                 /* return the error... */
1202                 mlog_errno(status);
1203                 goto bail;
1204         }
1205
1206         if (!new_de && new_inode)
1207                 mlog(ML_ERROR, "inode %lu does not exist in it's parent "
1208                      "directory!", new_inode->i_ino);
1209
1210         /* In case we need to overwrite an existing file, we blow it
1211          * away first */
1212         if (new_de) {
1213                 /* VFS didn't think there existed an inode here, but
1214                  * someone else in the cluster must have raced our
1215                  * rename to create one. Today we error cleanly, in
1216                  * the future we should consider calling iget to build
1217                  * a new struct inode for this entry. */
1218                 if (!new_inode) {
1219                         status = -EACCES;
1220
1221                         mlog(0, "We found an inode for name %.*s but VFS "
1222                              "didn't give us one.\n", new_dentry->d_name.len,
1223                              new_dentry->d_name.name);
1224                         goto bail;
1225                 }
1226
1227                 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
1228                         status = -EACCES;
1229
1230                         mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
1231                              (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
1232                              (unsigned long long)newfe_blkno,
1233                              OCFS2_I(new_inode)->ip_flags);
1234                         goto bail;
1235                 }
1236
1237                 status = ocfs2_meta_lock(new_inode, &newfe_bh, 1);
1238                 if (status < 0) {
1239                         if (status != -ENOENT)
1240                                 mlog_errno(status);
1241                         goto bail;
1242                 }
1243                 new_child_locked = 1;
1244
1245                 status = ocfs2_remote_dentry_delete(new_dentry);
1246                 if (status < 0) {
1247                         mlog_errno(status);
1248                         goto bail;
1249                 }
1250
1251                 newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
1252
1253                 mlog(0, "aha rename over existing... new_de=%p new_blkno=%llu "
1254                      "newfebh=%p bhblocknr=%llu\n", new_de,
1255                      (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
1256                      (unsigned long long)newfe_bh->b_blocknr : 0ULL);
1257
1258                 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
1259                         status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
1260                                                           new_inode,
1261                                                           orphan_name,
1262                                                           &orphan_entry_bh);
1263                         if (status < 0) {
1264                                 mlog_errno(status);
1265                                 goto bail;
1266                         }
1267                 }
1268         } else {
1269                 BUG_ON(new_dentry->d_parent->d_inode != new_dir);
1270
1271                 status = ocfs2_check_dir_for_entry(new_dir,
1272                                                    new_dentry->d_name.name,
1273                                                    new_dentry->d_name.len);
1274                 if (status)
1275                         goto bail;
1276
1277                 status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
1278                                                       new_dentry->d_name.name,
1279                                                       new_dentry->d_name.len,
1280                                                       &insert_entry_bh);
1281                 if (status < 0) {
1282                         mlog_errno(status);
1283                         goto bail;
1284                 }
1285         }
1286
1287         handle = ocfs2_start_trans(osb, OCFS2_RENAME_CREDITS);
1288         if (IS_ERR(handle)) {
1289                 status = PTR_ERR(handle);
1290                 handle = NULL;
1291                 mlog_errno(status);
1292                 goto bail;
1293         }
1294
1295         if (new_de) {
1296                 if (S_ISDIR(new_inode->i_mode)) {
1297                         if (!ocfs2_empty_dir(new_inode) ||
1298                             new_inode->i_nlink != 2) {
1299                                 status = -ENOTEMPTY;
1300                                 goto bail;
1301                         }
1302                 }
1303                 status = ocfs2_journal_access(handle, new_inode, newfe_bh,
1304                                               OCFS2_JOURNAL_ACCESS_WRITE);
1305                 if (status < 0) {
1306                         mlog_errno(status);
1307                         goto bail;
1308                 }
1309
1310                 if (S_ISDIR(new_inode->i_mode) ||
1311                     (newfe->i_links_count == cpu_to_le16(1))){
1312                         status = ocfs2_orphan_add(osb, handle, new_inode,
1313                                                   newfe, orphan_name,
1314                                                   orphan_entry_bh, orphan_dir);
1315                         if (status < 0) {
1316                                 mlog_errno(status);
1317                                 goto bail;
1318                         }
1319                 }
1320
1321                 /* change the dirent to point to the correct inode */
1322                 status = ocfs2_journal_access(handle, new_dir, new_de_bh,
1323                                               OCFS2_JOURNAL_ACCESS_WRITE);
1324                 if (status < 0) {
1325                         mlog_errno(status);
1326                         goto bail;
1327                 }
1328                 new_de->inode = cpu_to_le64(OCFS2_I(old_inode)->ip_blkno);
1329                 new_de->file_type = old_de->file_type;
1330                 new_dir->i_version++;
1331                 status = ocfs2_journal_dirty(handle, new_de_bh);
1332                 if (status < 0) {
1333                         mlog_errno(status);
1334                         goto bail;
1335                 }
1336
1337                 if (S_ISDIR(new_inode->i_mode))
1338                         newfe->i_links_count = 0;
1339                 else
1340                         le16_add_cpu(&newfe->i_links_count, -1);
1341
1342                 status = ocfs2_journal_dirty(handle, newfe_bh);
1343                 if (status < 0) {
1344                         mlog_errno(status);
1345                         goto bail;
1346                 }
1347         } else {
1348                 /* if the name was not found in new_dir, add it now */
1349                 status = ocfs2_add_entry(handle, new_dentry, old_inode,
1350                                          OCFS2_I(old_inode)->ip_blkno,
1351                                          new_dir_bh, insert_entry_bh);
1352         }
1353
1354         old_inode->i_ctime = CURRENT_TIME;
1355         mark_inode_dirty(old_inode);
1356         ocfs2_mark_inode_dirty(handle, old_inode, old_inode_bh);
1357
1358         /* now that the name has been added to new_dir, remove the old name */
1359         status = ocfs2_delete_entry(handle, old_dir, old_de, old_de_bh);
1360         if (status < 0) {
1361                 mlog_errno(status);
1362                 goto bail;
1363         }
1364
1365         if (new_inode) {
1366                 new_inode->i_nlink--;
1367                 new_inode->i_ctime = CURRENT_TIME;
1368         }
1369         old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1370         if (old_inode_de_bh) {
1371                 status = ocfs2_journal_access(handle, old_inode,
1372                                              old_inode_de_bh,
1373                                              OCFS2_JOURNAL_ACCESS_WRITE);
1374                 PARENT_INO(old_inode_de_bh->b_data) =
1375                         cpu_to_le64(OCFS2_I(new_dir)->ip_blkno);
1376                 status = ocfs2_journal_dirty(handle, old_inode_de_bh);
1377                 old_dir->i_nlink--;
1378                 if (new_inode) {
1379                         new_inode->i_nlink--;
1380                 } else {
1381                         inc_nlink(new_dir);
1382                         mark_inode_dirty(new_dir);
1383                 }
1384         }
1385         mark_inode_dirty(old_dir);
1386         ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
1387         if (new_inode) {
1388                 mark_inode_dirty(new_inode);
1389                 ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
1390         }
1391
1392         if (old_dir != new_dir) {
1393                 /* Keep the same times on both directories.*/
1394                 new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
1395
1396                 /*
1397                  * This will also pick up the i_nlink change from the
1398                  * block above.
1399                  */
1400                 ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
1401         }
1402
1403         if (old_dir_nlink != old_dir->i_nlink) {
1404                 if (!old_dir_bh) {
1405                         mlog(ML_ERROR, "need to change nlink for old dir "
1406                              "%llu from %d to %d but bh is NULL!\n",
1407                              (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
1408                              (int)old_dir_nlink, old_dir->i_nlink);
1409                 } else {
1410                         struct ocfs2_dinode *fe;
1411                         status = ocfs2_journal_access(handle, old_dir,
1412                                                       old_dir_bh,
1413                                                       OCFS2_JOURNAL_ACCESS_WRITE);
1414                         fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
1415                         fe->i_links_count = cpu_to_le16(old_dir->i_nlink);
1416                         status = ocfs2_journal_dirty(handle, old_dir_bh);
1417                 }
1418         }
1419
1420         ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
1421         status = 0;
1422 bail:
1423         if (rename_lock)
1424                 ocfs2_rename_unlock(osb);
1425
1426         if (handle)
1427                 ocfs2_commit_trans(osb, handle);
1428
1429         if (parents_locked)
1430                 ocfs2_double_unlock(old_dir, new_dir);
1431
1432         if (old_child_locked)
1433                 ocfs2_meta_unlock(old_inode, 1);
1434
1435         if (new_child_locked)
1436                 ocfs2_meta_unlock(new_inode, 1);
1437
1438         if (orphan_dir) {
1439                 /* This was locked for us in ocfs2_prepare_orphan_dir() */
1440                 ocfs2_meta_unlock(orphan_dir, 1);
1441                 mutex_unlock(&orphan_dir->i_mutex);
1442                 iput(orphan_dir);
1443         }
1444
1445         if (new_inode)
1446                 sync_mapping_buffers(old_inode->i_mapping);
1447
1448         if (new_inode)
1449                 iput(new_inode);
1450         if (newfe_bh)
1451                 brelse(newfe_bh);
1452         if (old_inode_bh)
1453                 brelse(old_inode_bh);
1454         if (old_dir_bh)
1455                 brelse(old_dir_bh);
1456         if (new_dir_bh)
1457                 brelse(new_dir_bh);
1458         if (new_de_bh)
1459                 brelse(new_de_bh);
1460         if (old_de_bh)
1461                 brelse(old_de_bh);
1462         if (old_inode_de_bh)
1463                 brelse(old_inode_de_bh);
1464         if (orphan_entry_bh)
1465                 brelse(orphan_entry_bh);
1466         if (insert_entry_bh)
1467                 brelse(insert_entry_bh);
1468
1469         mlog_exit(status);
1470
1471         return status;
1472 }
1473
1474 /*
1475  * we expect i_size = strlen(symname). Copy symname into the file
1476  * data, including the null terminator.
1477  */
1478 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
1479                                      handle_t *handle,
1480                                      struct inode *inode,
1481                                      const char *symname)
1482 {
1483         struct buffer_head **bhs = NULL;
1484         const char *c;
1485         struct super_block *sb = osb->sb;
1486         u64 p_blkno;
1487         int p_blocks;
1488         int virtual, blocks, status, i, bytes_left;
1489
1490         bytes_left = i_size_read(inode) + 1;
1491         /* we can't trust i_blocks because we're actually going to
1492          * write i_size + 1 bytes. */
1493         blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1494
1495         mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
1496                         (unsigned long long)inode->i_blocks,
1497                         i_size_read(inode), blocks);
1498
1499         /* Sanity check -- make sure we're going to fit. */
1500         if (bytes_left >
1501             ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1502                 status = -EIO;
1503                 mlog_errno(status);
1504                 goto bail;
1505         }
1506
1507         bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
1508         if (!bhs) {
1509                 status = -ENOMEM;
1510                 mlog_errno(status);
1511                 goto bail;
1512         }
1513
1514         status = ocfs2_extent_map_get_blocks(inode, 0, 1, &p_blkno,
1515                                              &p_blocks);
1516         if (status < 0) {
1517                 mlog_errno(status);
1518                 goto bail;
1519         }
1520
1521         /* links can never be larger than one cluster so we know this
1522          * is all going to be contiguous, but do a sanity check
1523          * anyway. */
1524         if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
1525                 status = -EIO;
1526                 mlog_errno(status);
1527                 goto bail;
1528         }
1529
1530         virtual = 0;
1531         while(bytes_left > 0) {
1532                 c = &symname[virtual * sb->s_blocksize];
1533
1534                 bhs[virtual] = sb_getblk(sb, p_blkno);
1535                 if (!bhs[virtual]) {
1536                         status = -ENOMEM;
1537                         mlog_errno(status);
1538                         goto bail;
1539                 }
1540                 ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
1541
1542                 status = ocfs2_journal_access(handle, inode, bhs[virtual],
1543                                               OCFS2_JOURNAL_ACCESS_CREATE);
1544                 if (status < 0) {
1545                         mlog_errno(status);
1546                         goto bail;
1547                 }
1548
1549                 memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
1550
1551                 memcpy(bhs[virtual]->b_data, c,
1552                        (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
1553                        bytes_left);
1554
1555                 status = ocfs2_journal_dirty(handle, bhs[virtual]);
1556                 if (status < 0) {
1557                         mlog_errno(status);
1558                         goto bail;
1559                 }
1560
1561                 virtual++;
1562                 p_blkno++;
1563                 bytes_left -= sb->s_blocksize;
1564         }
1565
1566         status = 0;
1567 bail:
1568
1569         if (bhs) {
1570                 for(i = 0; i < blocks; i++)
1571                         if (bhs[i])
1572                                 brelse(bhs[i]);
1573                 kfree(bhs);
1574         }
1575
1576         mlog_exit(status);
1577         return status;
1578 }
1579
1580 static int ocfs2_symlink(struct inode *dir,
1581                          struct dentry *dentry,
1582                          const char *symname)
1583 {
1584         int status, l, credits;
1585         u64 newsize;
1586         struct ocfs2_super *osb = NULL;
1587         struct inode *inode = NULL;
1588         struct super_block *sb;
1589         struct buffer_head *new_fe_bh = NULL;
1590         struct buffer_head *de_bh = NULL;
1591         struct buffer_head *parent_fe_bh = NULL;
1592         struct ocfs2_dinode *fe = NULL;
1593         struct ocfs2_dinode *dirfe;
1594         handle_t *handle = NULL;
1595         struct ocfs2_alloc_context *inode_ac = NULL;
1596         struct ocfs2_alloc_context *data_ac = NULL;
1597
1598         mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
1599                    dentry, symname, dentry->d_name.len, dentry->d_name.name);
1600
1601         sb = dir->i_sb;
1602         osb = OCFS2_SB(sb);
1603
1604         l = strlen(symname) + 1;
1605
1606         credits = ocfs2_calc_symlink_credits(sb);
1607
1608         /* lock the parent directory */
1609         status = ocfs2_meta_lock(dir, &parent_fe_bh, 1);
1610         if (status < 0) {
1611                 if (status != -ENOENT)
1612                         mlog_errno(status);
1613                 return status;
1614         }
1615
1616         dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1617         if (!dirfe->i_links_count) {
1618                 /* can't make a file in a deleted directory. */
1619                 status = -ENOENT;
1620                 goto bail;
1621         }
1622
1623         status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
1624                                            dentry->d_name.len);
1625         if (status)
1626                 goto bail;
1627
1628         status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
1629                                               dentry->d_name.name,
1630                                               dentry->d_name.len, &de_bh);
1631         if (status < 0) {
1632                 mlog_errno(status);
1633                 goto bail;
1634         }
1635
1636         status = ocfs2_reserve_new_inode(osb, &inode_ac);
1637         if (status < 0) {
1638                 if (status != -ENOSPC)
1639                         mlog_errno(status);
1640                 goto bail;
1641         }
1642
1643         /* don't reserve bitmap space for fast symlinks. */
1644         if (l > ocfs2_fast_symlink_chars(sb)) {
1645                 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
1646                 if (status < 0) {
1647                         if (status != -ENOSPC)
1648                                 mlog_errno(status);
1649                         goto bail;
1650                 }
1651         }
1652
1653         handle = ocfs2_start_trans(osb, credits);
1654         if (IS_ERR(handle)) {
1655                 status = PTR_ERR(handle);
1656                 handle = NULL;
1657                 mlog_errno(status);
1658                 goto bail;
1659         }
1660
1661         status = ocfs2_mknod_locked(osb, dir, dentry,
1662                                     S_IFLNK | S_IRWXUGO, 0,
1663                                     &new_fe_bh, parent_fe_bh, handle,
1664                                     &inode, inode_ac);
1665         if (status < 0) {
1666                 mlog_errno(status);
1667                 goto bail;
1668         }
1669
1670         fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
1671         inode->i_rdev = 0;
1672         newsize = l - 1;
1673         if (l > ocfs2_fast_symlink_chars(sb)) {
1674                 inode->i_op = &ocfs2_symlink_inode_operations;
1675                 status = ocfs2_do_extend_allocation(osb, inode, 1, new_fe_bh,
1676                                                     handle, data_ac, NULL,
1677                                                     NULL);
1678                 if (status < 0) {
1679                         if (status != -ENOSPC && status != -EINTR) {
1680                                 mlog(ML_ERROR,
1681                                      "Failed to extend file to %llu\n",
1682                                      (unsigned long long)newsize);
1683                                 mlog_errno(status);
1684                                 status = -ENOSPC;
1685                         }
1686                         goto bail;
1687                 }
1688                 i_size_write(inode, newsize);
1689                 inode->i_blocks = ocfs2_align_bytes_to_sectors(newsize);
1690         } else {
1691                 inode->i_op = &ocfs2_fast_symlink_inode_operations;
1692                 memcpy((char *) fe->id2.i_symlink, symname, l);
1693                 i_size_write(inode, newsize);
1694                 inode->i_blocks = 0;
1695         }
1696
1697         status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
1698         if (status < 0) {
1699                 mlog_errno(status);
1700                 goto bail;
1701         }
1702
1703         if (!ocfs2_inode_is_fast_symlink(inode)) {
1704                 status = ocfs2_create_symlink_data(osb, handle, inode,
1705                                                    symname);
1706                 if (status < 0) {
1707                         mlog_errno(status);
1708                         goto bail;
1709                 }
1710         }
1711
1712         status = ocfs2_add_entry(handle, dentry, inode,
1713                                  le64_to_cpu(fe->i_blkno), parent_fe_bh,
1714                                  de_bh);
1715         if (status < 0) {
1716                 mlog_errno(status);
1717                 goto bail;
1718         }
1719
1720         status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
1721         if (status) {
1722                 mlog_errno(status);
1723                 goto bail;
1724         }
1725
1726         insert_inode_hash(inode);
1727         dentry->d_op = &ocfs2_dentry_ops;
1728         d_instantiate(dentry, inode);
1729 bail:
1730         if (handle)
1731                 ocfs2_commit_trans(osb, handle);
1732
1733         ocfs2_meta_unlock(dir, 1);
1734
1735         if (new_fe_bh)
1736                 brelse(new_fe_bh);
1737         if (parent_fe_bh)
1738                 brelse(parent_fe_bh);
1739         if (de_bh)
1740                 brelse(de_bh);
1741         if (inode_ac)
1742                 ocfs2_free_alloc_context(inode_ac);
1743         if (data_ac)
1744                 ocfs2_free_alloc_context(data_ac);
1745         if ((status < 0) && inode)
1746                 iput(inode);
1747
1748         mlog_exit(status);
1749
1750         return status;
1751 }
1752
1753 int ocfs2_check_dir_entry(struct inode * dir,
1754                           struct ocfs2_dir_entry * de,
1755                           struct buffer_head * bh,
1756                           unsigned long offset)
1757 {
1758         const char *error_msg = NULL;
1759         const int rlen = le16_to_cpu(de->rec_len);
1760
1761         if (rlen < OCFS2_DIR_REC_LEN(1))
1762                 error_msg = "rec_len is smaller than minimal";
1763         else if (rlen % 4 != 0)
1764                 error_msg = "rec_len % 4 != 0";
1765         else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
1766                 error_msg = "rec_len is too small for name_len";
1767         else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
1768                 error_msg = "directory entry across blocks";
1769
1770         if (error_msg != NULL)
1771                 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
1772                      "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
1773                      (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
1774                      offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
1775                      de->name_len);
1776         return error_msg == NULL ? 1 : 0;
1777 }
1778
1779 /* we don't always have a dentry for what we want to add, so people
1780  * like orphan dir can call this instead.
1781  *
1782  * If you pass me insert_bh, I'll skip the search of the other dir
1783  * blocks and put the record in there.
1784  */
1785 static int __ocfs2_add_entry(handle_t *handle,
1786                              struct inode *dir,
1787                              const char *name, int namelen,
1788                              struct inode *inode, u64 blkno,
1789                              struct buffer_head *parent_fe_bh,
1790                              struct buffer_head *insert_bh)
1791 {
1792         unsigned long offset;
1793         unsigned short rec_len;
1794         struct ocfs2_dir_entry *de, *de1;
1795         struct super_block *sb;
1796         int retval, status;
1797
1798         mlog_entry_void();
1799
1800         sb = dir->i_sb;
1801
1802         if (!namelen)
1803                 return -EINVAL;
1804
1805         rec_len = OCFS2_DIR_REC_LEN(namelen);
1806         offset = 0;
1807         de = (struct ocfs2_dir_entry *) insert_bh->b_data;
1808         while (1) {
1809                 BUG_ON((char *)de >= sb->s_blocksize + insert_bh->b_data);
1810                 /* These checks should've already been passed by the
1811                  * prepare function, but I guess we can leave them
1812                  * here anyway. */
1813                 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
1814                         retval = -ENOENT;
1815                         goto bail;
1816                 }
1817                 if (ocfs2_match(namelen, name, de)) {
1818                         retval = -EEXIST;
1819                         goto bail;
1820                 }
1821                 if (((le64_to_cpu(de->inode) == 0) &&
1822                      (le16_to_cpu(de->rec_len) >= rec_len)) ||
1823                     (le16_to_cpu(de->rec_len) >=
1824                      (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
1825                         dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1826                         retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1827                         if (retval < 0) {
1828                                 mlog_errno(retval);
1829                                 goto bail;
1830                         }
1831
1832                         status = ocfs2_journal_access(handle, dir, insert_bh,
1833                                                       OCFS2_JOURNAL_ACCESS_WRITE);
1834                         /* By now the buffer is marked for journaling */
1835                         offset += le16_to_cpu(de->rec_len);
1836                         if (le64_to_cpu(de->inode)) {
1837                                 de1 = (struct ocfs2_dir_entry *)((char *) de +
1838                                         OCFS2_DIR_REC_LEN(de->name_len));
1839                                 de1->rec_len =
1840                                         cpu_to_le16(le16_to_cpu(de->rec_len) -
1841                                         OCFS2_DIR_REC_LEN(de->name_len));
1842                                 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1843                                 de = de1;
1844                         }
1845                         de->file_type = OCFS2_FT_UNKNOWN;
1846                         if (blkno) {
1847                                 de->inode = cpu_to_le64(blkno);
1848                                 ocfs2_set_de_type(de, inode->i_mode);
1849                         } else
1850                                 de->inode = 0;
1851                         de->name_len = namelen;
1852                         memcpy(de->name, name, namelen);
1853
1854                         dir->i_version++;
1855                         status = ocfs2_journal_dirty(handle, insert_bh);
1856                         retval = 0;
1857                         goto bail;
1858                 }
1859                 offset += le16_to_cpu(de->rec_len);
1860                 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
1861         }
1862
1863         /* when you think about it, the assert above should prevent us
1864          * from ever getting here. */
1865         retval = -ENOSPC;
1866 bail:
1867
1868         mlog_exit(retval);
1869         return retval;
1870 }
1871
1872
1873 /*
1874  * ocfs2_delete_entry deletes a directory entry by merging it with the
1875  * previous entry
1876  */
1877 static int ocfs2_delete_entry(handle_t *handle,
1878                               struct inode *dir,
1879                               struct ocfs2_dir_entry *de_del,
1880                               struct buffer_head *bh)
1881 {
1882         struct ocfs2_dir_entry *de, *pde;
1883         int i, status = -ENOENT;
1884
1885         mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
1886
1887         i = 0;
1888         pde = NULL;
1889         de = (struct ocfs2_dir_entry *) bh->b_data;
1890         while (i < bh->b_size) {
1891                 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
1892                         status = -EIO;
1893                         mlog_errno(status);
1894                         goto bail;
1895                 }
1896                 if (de == de_del)  {
1897                         status = ocfs2_journal_access(handle, dir, bh,
1898                                                       OCFS2_JOURNAL_ACCESS_WRITE);
1899                         if (status < 0) {
1900                                 status = -EIO;
1901                                 mlog_errno(status);
1902                                 goto bail;
1903                         }
1904                         if (pde)
1905                                 pde->rec_len =
1906                                         cpu_to_le16(le16_to_cpu(pde->rec_len) +
1907                                                     le16_to_cpu(de->rec_len));
1908                         else
1909                                 de->inode = 0;
1910                         dir->i_version++;
1911                         status = ocfs2_journal_dirty(handle, bh);
1912                         goto bail;
1913                 }
1914                 i += le16_to_cpu(de->rec_len);
1915                 pde = de;
1916                 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
1917         }
1918 bail:
1919         mlog_exit(status);
1920         return status;
1921 }
1922
1923 /*
1924  * Returns 0 if not found, -1 on failure, and 1 on success
1925  */
1926 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
1927                                         struct inode *dir,
1928                                         const char *name, int namelen,
1929                                         unsigned long offset,
1930                                         struct ocfs2_dir_entry **res_dir)
1931 {
1932         struct ocfs2_dir_entry *de;
1933         char *dlimit, *de_buf;
1934         int de_len;
1935         int ret = 0;
1936
1937         mlog_entry_void();
1938
1939         de_buf = bh->b_data;
1940         dlimit = de_buf + dir->i_sb->s_blocksize;
1941
1942         while (de_buf < dlimit) {
1943                 /* this code is executed quadratically often */
1944                 /* do minimal checking `by hand' */
1945
1946                 de = (struct ocfs2_dir_entry *) de_buf;
1947
1948                 if (de_buf + namelen <= dlimit &&
1949                     ocfs2_match(namelen, name, de)) {
1950                         /* found a match - just to be sure, do a full check */
1951                         if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1952                                 ret = -1;
1953                                 goto bail;
1954                         }
1955                         *res_dir = de;
1956                         ret = 1;
1957                         goto bail;
1958                 }
1959
1960                 /* prevent looping on a bad block */
1961                 de_len = le16_to_cpu(de->rec_len);
1962                 if (de_len <= 0) {
1963                         ret = -1;
1964                         goto bail;
1965                 }
1966
1967                 de_buf += de_len;
1968                 offset += de_len;
1969         }
1970
1971 bail:
1972         mlog_exit(ret);
1973         return ret;
1974 }
1975
1976 struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
1977                                      struct inode *dir,
1978                                      struct ocfs2_dir_entry **res_dir)
1979 {
1980         struct super_block *sb;
1981         struct buffer_head *bh_use[NAMEI_RA_SIZE];
1982         struct buffer_head *bh, *ret = NULL;
1983         unsigned long start, block, b;
1984         int ra_max = 0;         /* Number of bh's in the readahead
1985                                    buffer, bh_use[] */
1986         int ra_ptr = 0;         /* Current index into readahead
1987                                    buffer */
1988         int num = 0;
1989         int nblocks, i, err;
1990
1991         mlog_entry_void();
1992
1993         *res_dir = NULL;
1994         sb = dir->i_sb;
1995
1996         nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
1997         start = OCFS2_I(dir)->ip_dir_start_lookup;
1998         if (start >= nblocks)
1999                 start = 0;
2000         block = start;
2001
2002 restart:
2003         do {
2004                 /*
2005                  * We deal with the read-ahead logic here.
2006                  */
2007                 if (ra_ptr >= ra_max) {
2008                         /* Refill the readahead buffer */
2009                         ra_ptr = 0;
2010                         b = block;
2011                         for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
2012                                 /*
2013                                  * Terminate if we reach the end of the
2014                                  * directory and must wrap, or if our
2015                                  * search has finished at this block.
2016                                  */
2017                                 if (b >= nblocks || (num && block == start)) {
2018                                         bh_use[ra_max] = NULL;
2019                                         break;
2020                                 }
2021                                 num++;
2022
2023                                 bh = ocfs2_bread(dir, b++, &err, 1);
2024                                 bh_use[ra_max] = bh;
2025                         }
2026                 }
2027                 if ((bh = bh_use[ra_ptr++]) == NULL)
2028                         goto next;
2029                 wait_on_buffer(bh);
2030                 if (!buffer_uptodate(bh)) {
2031                         /* read error, skip block & hope for the best */
2032                         ocfs2_error(dir->i_sb, "reading directory %llu, "
2033                                     "offset %lu\n",
2034                                     (unsigned long long)OCFS2_I(dir)->ip_blkno,
2035                                     block);
2036                         brelse(bh);
2037                         goto next;
2038                 }
2039                 i = ocfs2_search_dirblock(bh, dir, name, namelen,
2040                                           block << sb->s_blocksize_bits,
2041                                           res_dir);
2042                 if (i == 1) {
2043                         OCFS2_I(dir)->ip_dir_start_lookup = block;
2044                         ret = bh;
2045                         goto cleanup_and_exit;
2046                 } else {
2047                         brelse(bh);
2048                         if (i < 0)
2049                                 goto cleanup_and_exit;
2050                 }
2051         next:
2052                 if (++block >= nblocks)
2053                         block = 0;
2054         } while (block != start);
2055
2056         /*
2057          * If the directory has grown while we were searching, then
2058          * search the last part of the directory before giving up.
2059          */
2060         block = nblocks;
2061         nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
2062         if (block < nblocks) {
2063                 start = 0;
2064                 goto restart;
2065         }
2066
2067 cleanup_and_exit:
2068         /* Clean up the read-ahead blocks */
2069         for (; ra_ptr < ra_max; ra_ptr++)
2070                 brelse(bh_use[ra_ptr]);
2071
2072         mlog_exit_ptr(ret);
2073         return ret;
2074 }
2075
2076 static int ocfs2_blkno_stringify(u64 blkno, char *name)
2077 {
2078         int status, namelen;
2079
2080         mlog_entry_void();
2081
2082         namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
2083                            (long long)blkno);
2084         if (namelen <= 0) {
2085                 if (namelen)
2086                         status = namelen;
2087                 else
2088                         status = -EINVAL;
2089                 mlog_errno(status);
2090                 goto bail;
2091         }
2092         if (namelen != OCFS2_ORPHAN_NAMELEN) {
2093                 status = -EINVAL;
2094                 mlog_errno(status);
2095                 goto bail;
2096         }
2097
2098         mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
2099              namelen);
2100
2101         status = 0;
2102 bail:
2103         mlog_exit(status);
2104         return status;
2105 }
2106
2107 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
2108                                     struct inode **ret_orphan_dir,
2109                                     struct inode *inode,
2110                                     char *name,
2111                                     struct buffer_head **de_bh)
2112 {
2113         struct inode *orphan_dir_inode;
2114         struct buffer_head *orphan_dir_bh = NULL;
2115         int status = 0;
2116
2117         status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2118         if (status < 0) {
2119                 mlog_errno(status);
2120                 return status;
2121         }
2122
2123         orphan_dir_inode = ocfs2_get_system_file_inode(osb,
2124                                                        ORPHAN_DIR_SYSTEM_INODE,
2125                                                        osb->slot_num);
2126         if (!orphan_dir_inode) {
2127                 status = -ENOENT;
2128                 mlog_errno(status);
2129                 return status;
2130         }
2131
2132         mutex_lock(&orphan_dir_inode->i_mutex);
2133
2134         status = ocfs2_meta_lock(orphan_dir_inode, &orphan_dir_bh, 1);
2135         if (status < 0) {
2136                 mlog_errno(status);
2137                 goto leave;
2138         }
2139
2140         status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
2141                                               orphan_dir_bh, name,
2142                                               OCFS2_ORPHAN_NAMELEN, de_bh);
2143         if (status < 0) {
2144                 ocfs2_meta_unlock(orphan_dir_inode, 1);
2145
2146                 mlog_errno(status);
2147                 goto leave;
2148         }
2149
2150         *ret_orphan_dir = orphan_dir_inode;
2151
2152 leave:
2153         if (status) {
2154                 mutex_unlock(&orphan_dir_inode->i_mutex);
2155                 iput(orphan_dir_inode);
2156         }
2157
2158         if (orphan_dir_bh)
2159                 brelse(orphan_dir_bh);
2160
2161         mlog_exit(status);
2162         return status;
2163 }
2164
2165 static int ocfs2_orphan_add(struct ocfs2_super *osb,
2166                             handle_t *handle,
2167                             struct inode *inode,
2168                             struct ocfs2_dinode *fe,
2169                             char *name,
2170                             struct buffer_head *de_bh,
2171                             struct inode *orphan_dir_inode)
2172 {
2173         struct buffer_head *orphan_dir_bh = NULL;
2174         int status = 0;
2175         struct ocfs2_dinode *orphan_fe;
2176
2177         mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
2178
2179         status = ocfs2_read_block(osb,
2180                                   OCFS2_I(orphan_dir_inode)->ip_blkno,
2181                                   &orphan_dir_bh, OCFS2_BH_CACHED,
2182                                   orphan_dir_inode);
2183         if (status < 0) {
2184                 mlog_errno(status);
2185                 goto leave;
2186         }
2187
2188         status = ocfs2_journal_access(handle, orphan_dir_inode, orphan_dir_bh,
2189                                       OCFS2_JOURNAL_ACCESS_WRITE);
2190         if (status < 0) {
2191                 mlog_errno(status);
2192                 goto leave;
2193         }
2194
2195         /* we're a cluster, and nlink can change on disk from
2196          * underneath us... */
2197         orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2198         if (S_ISDIR(inode->i_mode))
2199                 le16_add_cpu(&orphan_fe->i_links_count, 1);
2200         orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
2201
2202         status = ocfs2_journal_dirty(handle, orphan_dir_bh);
2203         if (status < 0) {
2204                 mlog_errno(status);
2205                 goto leave;
2206         }
2207
2208         status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
2209                                    OCFS2_ORPHAN_NAMELEN, inode,
2210                                    OCFS2_I(inode)->ip_blkno,
2211                                    orphan_dir_bh, de_bh);
2212         if (status < 0) {
2213                 mlog_errno(status);
2214                 goto leave;
2215         }
2216
2217         le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
2218
2219         /* Record which orphan dir our inode now resides
2220          * in. delete_inode will use this to determine which orphan
2221          * dir to lock. */
2222         fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
2223
2224         mlog(0, "Inode %llu orphaned in slot %d\n",
2225              (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
2226
2227 leave:
2228         if (orphan_dir_bh)
2229                 brelse(orphan_dir_bh);
2230
2231         mlog_exit(status);
2232         return status;
2233 }
2234
2235 /* unlike orphan_add, we expect the orphan dir to already be locked here. */
2236 int ocfs2_orphan_del(struct ocfs2_super *osb,
2237                      handle_t *handle,
2238                      struct inode *orphan_dir_inode,
2239                      struct inode *inode,
2240                      struct buffer_head *orphan_dir_bh)
2241 {
2242         char name[OCFS2_ORPHAN_NAMELEN + 1];
2243         struct ocfs2_dinode *orphan_fe;
2244         int status = 0;
2245         struct buffer_head *target_de_bh = NULL;
2246         struct ocfs2_dir_entry *target_de = NULL;
2247
2248         mlog_entry_void();
2249
2250         status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2251         if (status < 0) {
2252                 mlog_errno(status);
2253                 goto leave;
2254         }
2255
2256         mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
2257              name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
2258              OCFS2_ORPHAN_NAMELEN);
2259
2260         /* find it's spot in the orphan directory */
2261         target_de_bh = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN,
2262                                         orphan_dir_inode, &target_de);
2263         if (!target_de_bh) {
2264                 status = -ENOENT;
2265                 mlog_errno(status);
2266                 goto leave;
2267         }
2268
2269         /* remove it from the orphan directory */
2270         status = ocfs2_delete_entry(handle, orphan_dir_inode, target_de,
2271                                     target_de_bh);
2272         if (status < 0) {
2273                 mlog_errno(status);
2274                 goto leave;
2275         }
2276
2277         status = ocfs2_journal_access(handle,orphan_dir_inode,  orphan_dir_bh,
2278                                       OCFS2_JOURNAL_ACCESS_WRITE);
2279         if (status < 0) {
2280                 mlog_errno(status);
2281                 goto leave;
2282         }
2283
2284         /* do the i_nlink dance! :) */
2285         orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2286         if (S_ISDIR(inode->i_mode))
2287                 le16_add_cpu(&orphan_fe->i_links_count, -1);
2288         orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
2289
2290         status = ocfs2_journal_dirty(handle, orphan_dir_bh);
2291         if (status < 0) {
2292                 mlog_errno(status);
2293                 goto leave;
2294         }
2295
2296 leave:
2297         if (target_de_bh)
2298                 brelse(target_de_bh);
2299
2300         mlog_exit(status);
2301         return status;
2302 }
2303
2304 const struct inode_operations ocfs2_dir_iops = {
2305         .create         = ocfs2_create,
2306         .lookup         = ocfs2_lookup,
2307         .link           = ocfs2_link,
2308         .unlink         = ocfs2_unlink,
2309         .rmdir          = ocfs2_unlink,
2310         .symlink        = ocfs2_symlink,
2311         .mkdir          = ocfs2_mkdir,
2312         .mknod          = ocfs2_mknod,
2313         .rename         = ocfs2_rename,
2314         .setattr        = ocfs2_setattr,
2315         .getattr        = ocfs2_getattr,
2316         .permission     = ocfs2_permission,
2317 };