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