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