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