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