ocfs2: do not put bh when buffer_uptodate failed
[cascardo/linux.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/quotaops.h>
44
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 "xattr.h"
64 #include "acl.h"
65 #include "ocfs2_trace.h"
66
67 #include "buffer_head_io.h"
68
69 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
70                               struct inode *dir,
71                               struct inode *inode,
72                               dev_t dev,
73                               struct buffer_head **new_fe_bh,
74                               struct buffer_head *parent_fe_bh,
75                               handle_t *handle,
76                               struct ocfs2_alloc_context *inode_ac);
77
78 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
79                                     struct inode **ret_orphan_dir,
80                                     u64 blkno,
81                                     char *name,
82                                     struct ocfs2_dir_lookup_result *lookup);
83
84 static int ocfs2_orphan_add(struct ocfs2_super *osb,
85                             handle_t *handle,
86                             struct inode *inode,
87                             struct buffer_head *fe_bh,
88                             char *name,
89                             struct ocfs2_dir_lookup_result *lookup,
90                             struct inode *orphan_dir_inode);
91
92 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
93                                      handle_t *handle,
94                                      struct inode *inode,
95                                      const char *symname);
96
97 /* An orphan dir name is an 8 byte value, printed as a hex string */
98 #define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
99
100 static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
101                                    unsigned int flags)
102 {
103         int status;
104         u64 blkno;
105         struct inode *inode = NULL;
106         struct dentry *ret;
107         struct ocfs2_inode_info *oi;
108
109         trace_ocfs2_lookup(dir, dentry, dentry->d_name.len,
110                            dentry->d_name.name,
111                            (unsigned long long)OCFS2_I(dir)->ip_blkno, 0);
112
113         if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
114                 ret = ERR_PTR(-ENAMETOOLONG);
115                 goto bail;
116         }
117
118         status = ocfs2_inode_lock_nested(dir, NULL, 0, OI_LS_PARENT);
119         if (status < 0) {
120                 if (status != -ENOENT)
121                         mlog_errno(status);
122                 ret = ERR_PTR(status);
123                 goto bail;
124         }
125
126         status = ocfs2_lookup_ino_from_name(dir, dentry->d_name.name,
127                                             dentry->d_name.len, &blkno);
128         if (status < 0)
129                 goto bail_add;
130
131         inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0);
132         if (IS_ERR(inode)) {
133                 ret = ERR_PTR(-EACCES);
134                 goto bail_unlock;
135         }
136
137         oi = OCFS2_I(inode);
138         /* Clear any orphaned state... If we were able to look up the
139          * inode from a directory, it certainly can't be orphaned. We
140          * might have the bad state from a node which intended to
141          * orphan this inode but crashed before it could commit the
142          * unlink. */
143         spin_lock(&oi->ip_lock);
144         oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
145         spin_unlock(&oi->ip_lock);
146
147 bail_add:
148         ret = d_splice_alias(inode, dentry);
149
150         if (inode) {
151                 /*
152                  * If d_splice_alias() finds a DCACHE_DISCONNECTED
153                  * dentry, it will d_move() it on top of ourse. The
154                  * return value will indicate this however, so in
155                  * those cases, we switch them around for the locking
156                  * code.
157                  *
158                  * NOTE: This dentry already has ->d_op set from
159                  * ocfs2_get_parent() and ocfs2_get_dentry()
160                  */
161                 if (ret)
162                         dentry = ret;
163
164                 status = ocfs2_dentry_attach_lock(dentry, inode,
165                                                   OCFS2_I(dir)->ip_blkno);
166                 if (status) {
167                         mlog_errno(status);
168                         ret = ERR_PTR(status);
169                         goto bail_unlock;
170                 }
171         } else
172                 ocfs2_dentry_attach_gen(dentry);
173
174 bail_unlock:
175         /* Don't drop the cluster lock until *after* the d_add --
176          * unlink on another node will message us to remove that
177          * dentry under this lock so otherwise we can race this with
178          * the downconvert thread and have a stale dentry. */
179         ocfs2_inode_unlock(dir, 0);
180
181 bail:
182
183         trace_ocfs2_lookup_ret(ret);
184
185         return ret;
186 }
187
188 static struct inode *ocfs2_get_init_inode(struct inode *dir, umode_t mode)
189 {
190         struct inode *inode;
191
192         inode = new_inode(dir->i_sb);
193         if (!inode) {
194                 mlog(ML_ERROR, "new_inode failed!\n");
195                 return NULL;
196         }
197
198         /* populate as many fields early on as possible - many of
199          * these are used by the support functions here and in
200          * callers. */
201         if (S_ISDIR(mode))
202                 set_nlink(inode, 2);
203         inode_init_owner(inode, dir, mode);
204         dquot_initialize(inode);
205         return inode;
206 }
207
208 static int ocfs2_mknod(struct inode *dir,
209                        struct dentry *dentry,
210                        umode_t mode,
211                        dev_t dev)
212 {
213         int status = 0;
214         struct buffer_head *parent_fe_bh = NULL;
215         handle_t *handle = NULL;
216         struct ocfs2_super *osb;
217         struct ocfs2_dinode *dirfe;
218         struct buffer_head *new_fe_bh = NULL;
219         struct inode *inode = NULL;
220         struct ocfs2_alloc_context *inode_ac = NULL;
221         struct ocfs2_alloc_context *data_ac = NULL;
222         struct ocfs2_alloc_context *meta_ac = NULL;
223         int want_clusters = 0;
224         int want_meta = 0;
225         int xattr_credits = 0;
226         struct ocfs2_security_xattr_info si = {
227                 .enable = 1,
228         };
229         int did_quota_inode = 0;
230         struct ocfs2_dir_lookup_result lookup = { NULL, };
231         sigset_t oldset;
232         int did_block_signals = 0;
233         struct posix_acl *default_acl = NULL, *acl = NULL;
234
235         trace_ocfs2_mknod(dir, dentry, dentry->d_name.len, dentry->d_name.name,
236                           (unsigned long long)OCFS2_I(dir)->ip_blkno,
237                           (unsigned long)dev, mode);
238
239         dquot_initialize(dir);
240
241         /* get our super block */
242         osb = OCFS2_SB(dir->i_sb);
243
244         status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
245         if (status < 0) {
246                 if (status != -ENOENT)
247                         mlog_errno(status);
248                 return status;
249         }
250
251         if (S_ISDIR(mode) && (dir->i_nlink >= ocfs2_link_max(osb))) {
252                 status = -EMLINK;
253                 goto leave;
254         }
255
256         dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
257         if (!ocfs2_read_links_count(dirfe)) {
258                 /* can't make a file in a deleted directory. */
259                 status = -ENOENT;
260                 goto leave;
261         }
262
263         status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
264                                            dentry->d_name.len);
265         if (status)
266                 goto leave;
267
268         /* get a spot inside the dir. */
269         status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
270                                               dentry->d_name.name,
271                                               dentry->d_name.len, &lookup);
272         if (status < 0) {
273                 mlog_errno(status);
274                 goto leave;
275         }
276
277         /* reserve an inode spot */
278         status = ocfs2_reserve_new_inode(osb, &inode_ac);
279         if (status < 0) {
280                 if (status != -ENOSPC)
281                         mlog_errno(status);
282                 goto leave;
283         }
284
285         inode = ocfs2_get_init_inode(dir, mode);
286         if (!inode) {
287                 status = -ENOMEM;
288                 mlog_errno(status);
289                 goto leave;
290         }
291
292         /* get security xattr */
293         status = ocfs2_init_security_get(inode, dir, &dentry->d_name, &si);
294         if (status) {
295                 if (status == -EOPNOTSUPP)
296                         si.enable = 0;
297                 else {
298                         mlog_errno(status);
299                         goto leave;
300                 }
301         }
302
303         /* calculate meta data/clusters for setting security and acl xattr */
304         status = ocfs2_calc_xattr_init(dir, parent_fe_bh, mode,
305                                        &si, &want_clusters,
306                                        &xattr_credits, &want_meta);
307         if (status < 0) {
308                 mlog_errno(status);
309                 goto leave;
310         }
311
312         /* Reserve a cluster if creating an extent based directory. */
313         if (S_ISDIR(mode) && !ocfs2_supports_inline_data(osb)) {
314                 want_clusters += 1;
315
316                 /* Dir indexing requires extra space as well */
317                 if (ocfs2_supports_indexed_dirs(osb))
318                         want_meta++;
319         }
320
321         status = ocfs2_reserve_new_metadata_blocks(osb, want_meta, &meta_ac);
322         if (status < 0) {
323                 if (status != -ENOSPC)
324                         mlog_errno(status);
325                 goto leave;
326         }
327
328         status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
329         if (status < 0) {
330                 if (status != -ENOSPC)
331                         mlog_errno(status);
332                 goto leave;
333         }
334
335         status = posix_acl_create(dir, &mode, &default_acl, &acl);
336         if (status) {
337                 mlog_errno(status);
338                 goto leave;
339         }
340
341         handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb,
342                                                             S_ISDIR(mode),
343                                                             xattr_credits));
344         if (IS_ERR(handle)) {
345                 status = PTR_ERR(handle);
346                 handle = NULL;
347                 mlog_errno(status);
348                 goto leave;
349         }
350
351         /* Starting to change things, restart is no longer possible. */
352         ocfs2_block_signals(&oldset);
353         did_block_signals = 1;
354
355         status = dquot_alloc_inode(inode);
356         if (status)
357                 goto leave;
358         did_quota_inode = 1;
359
360         /* do the real work now. */
361         status = ocfs2_mknod_locked(osb, dir, inode, dev,
362                                     &new_fe_bh, parent_fe_bh, handle,
363                                     inode_ac);
364         if (status < 0) {
365                 mlog_errno(status);
366                 goto leave;
367         }
368
369         if (S_ISDIR(mode)) {
370                 status = ocfs2_fill_new_dir(osb, handle, dir, inode,
371                                             new_fe_bh, data_ac, meta_ac);
372                 if (status < 0) {
373                         mlog_errno(status);
374                         goto leave;
375                 }
376
377                 status = ocfs2_journal_access_di(handle, INODE_CACHE(dir),
378                                                  parent_fe_bh,
379                                                  OCFS2_JOURNAL_ACCESS_WRITE);
380                 if (status < 0) {
381                         mlog_errno(status);
382                         goto leave;
383                 }
384                 ocfs2_add_links_count(dirfe, 1);
385                 ocfs2_journal_dirty(handle, parent_fe_bh);
386                 inc_nlink(dir);
387         }
388
389         if (default_acl) {
390                 status = ocfs2_set_acl(handle, inode, new_fe_bh,
391                                        ACL_TYPE_DEFAULT, default_acl,
392                                        meta_ac, data_ac);
393         }
394         if (!status && acl) {
395                 status = ocfs2_set_acl(handle, inode, new_fe_bh,
396                                        ACL_TYPE_ACCESS, acl,
397                                        meta_ac, data_ac);
398         }
399
400         if (status < 0) {
401                 mlog_errno(status);
402                 goto leave;
403         }
404
405         if (si.enable) {
406                 status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
407                                                  meta_ac, data_ac);
408                 if (status < 0) {
409                         mlog_errno(status);
410                         goto leave;
411                 }
412         }
413
414         /*
415          * Do this before adding the entry to the directory. We add
416          * also set d_op after success so that ->d_iput() will cleanup
417          * the dentry lock even if ocfs2_add_entry() fails below.
418          */
419         status = ocfs2_dentry_attach_lock(dentry, inode,
420                                           OCFS2_I(dir)->ip_blkno);
421         if (status) {
422                 mlog_errno(status);
423                 goto leave;
424         }
425
426         status = ocfs2_add_entry(handle, dentry, inode,
427                                  OCFS2_I(inode)->ip_blkno, parent_fe_bh,
428                                  &lookup);
429         if (status < 0) {
430                 mlog_errno(status);
431                 goto leave;
432         }
433
434         insert_inode_hash(inode);
435         d_instantiate(dentry, inode);
436         status = 0;
437 leave:
438         if (default_acl)
439                 posix_acl_release(default_acl);
440         if (acl)
441                 posix_acl_release(acl);
442         if (status < 0 && did_quota_inode)
443                 dquot_free_inode(inode);
444         if (handle)
445                 ocfs2_commit_trans(osb, handle);
446
447         ocfs2_inode_unlock(dir, 1);
448         if (did_block_signals)
449                 ocfs2_unblock_signals(&oldset);
450
451         brelse(new_fe_bh);
452         brelse(parent_fe_bh);
453         kfree(si.name);
454         kfree(si.value);
455
456         ocfs2_free_dir_lookup_result(&lookup);
457
458         if (inode_ac)
459                 ocfs2_free_alloc_context(inode_ac);
460
461         if (data_ac)
462                 ocfs2_free_alloc_context(data_ac);
463
464         if (meta_ac)
465                 ocfs2_free_alloc_context(meta_ac);
466
467         /*
468          * We should call iput after the i_mutex of the bitmap been
469          * unlocked in ocfs2_free_alloc_context, or the
470          * ocfs2_delete_inode will mutex_lock again.
471          */
472         if ((status < 0) && inode) {
473                 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SKIP_ORPHAN_DIR;
474                 clear_nlink(inode);
475                 iput(inode);
476         }
477
478         if (status)
479                 mlog_errno(status);
480
481         return status;
482 }
483
484 static int __ocfs2_mknod_locked(struct inode *dir,
485                                 struct inode *inode,
486                                 dev_t dev,
487                                 struct buffer_head **new_fe_bh,
488                                 struct buffer_head *parent_fe_bh,
489                                 handle_t *handle,
490                                 struct ocfs2_alloc_context *inode_ac,
491                                 u64 fe_blkno, u64 suballoc_loc, u16 suballoc_bit)
492 {
493         int status = 0;
494         struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
495         struct ocfs2_dinode *fe = NULL;
496         struct ocfs2_extent_list *fel;
497         u16 feat;
498         struct ocfs2_inode_info *oi = OCFS2_I(inode);
499
500         *new_fe_bh = NULL;
501
502         /* populate as many fields early on as possible - many of
503          * these are used by the support functions here and in
504          * callers. */
505         inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
506         OCFS2_I(inode)->ip_blkno = fe_blkno;
507         spin_lock(&osb->osb_lock);
508         inode->i_generation = osb->s_next_generation++;
509         spin_unlock(&osb->osb_lock);
510
511         *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
512         if (!*new_fe_bh) {
513                 status = -ENOMEM;
514                 mlog_errno(status);
515                 goto leave;
516         }
517         ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), *new_fe_bh);
518
519         status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
520                                          *new_fe_bh,
521                                          OCFS2_JOURNAL_ACCESS_CREATE);
522         if (status < 0) {
523                 mlog_errno(status);
524                 goto leave;
525         }
526
527         fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
528         memset(fe, 0, osb->sb->s_blocksize);
529
530         fe->i_generation = cpu_to_le32(inode->i_generation);
531         fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
532         fe->i_blkno = cpu_to_le64(fe_blkno);
533         fe->i_suballoc_loc = cpu_to_le64(suballoc_loc);
534         fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
535         fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot);
536         fe->i_uid = cpu_to_le32(i_uid_read(inode));
537         fe->i_gid = cpu_to_le32(i_gid_read(inode));
538         fe->i_mode = cpu_to_le16(inode->i_mode);
539         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
540                 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
541
542         ocfs2_set_links_count(fe, inode->i_nlink);
543
544         fe->i_last_eb_blk = 0;
545         strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
546         fe->i_flags |= cpu_to_le32(OCFS2_VALID_FL);
547         fe->i_atime = fe->i_ctime = fe->i_mtime =
548                 cpu_to_le64(CURRENT_TIME.tv_sec);
549         fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
550                 cpu_to_le32(CURRENT_TIME.tv_nsec);
551         fe->i_dtime = 0;
552
553         /*
554          * If supported, directories start with inline data. If inline
555          * isn't supported, but indexing is, we start them as indexed.
556          */
557         feat = le16_to_cpu(fe->i_dyn_features);
558         if (S_ISDIR(inode->i_mode) && ocfs2_supports_inline_data(osb)) {
559                 fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL);
560
561                 fe->id2.i_data.id_count = cpu_to_le16(
562                                 ocfs2_max_inline_data_with_xattr(osb->sb, fe));
563         } else {
564                 fel = &fe->id2.i_list;
565                 fel->l_tree_depth = 0;
566                 fel->l_next_free_rec = 0;
567                 fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
568         }
569
570         ocfs2_journal_dirty(handle, *new_fe_bh);
571
572         ocfs2_populate_inode(inode, fe, 1);
573         ocfs2_ci_set_new(osb, INODE_CACHE(inode));
574         if (!ocfs2_mount_local(osb)) {
575                 status = ocfs2_create_new_inode_locks(inode);
576                 if (status < 0)
577                         mlog_errno(status);
578         }
579
580         oi->i_sync_tid = handle->h_transaction->t_tid;
581         oi->i_datasync_tid = handle->h_transaction->t_tid;
582
583 leave:
584         if (status < 0) {
585                 if (*new_fe_bh) {
586                         brelse(*new_fe_bh);
587                         *new_fe_bh = NULL;
588                 }
589         }
590
591         if (status)
592                 mlog_errno(status);
593         return status;
594 }
595
596 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
597                               struct inode *dir,
598                               struct inode *inode,
599                               dev_t dev,
600                               struct buffer_head **new_fe_bh,
601                               struct buffer_head *parent_fe_bh,
602                               handle_t *handle,
603                               struct ocfs2_alloc_context *inode_ac)
604 {
605         int status = 0;
606         u64 suballoc_loc, fe_blkno = 0;
607         u16 suballoc_bit;
608
609         *new_fe_bh = NULL;
610
611         status = ocfs2_claim_new_inode(handle, dir, parent_fe_bh,
612                                        inode_ac, &suballoc_loc,
613                                        &suballoc_bit, &fe_blkno);
614         if (status < 0) {
615                 mlog_errno(status);
616                 return status;
617         }
618
619         return __ocfs2_mknod_locked(dir, inode, dev, new_fe_bh,
620                                     parent_fe_bh, handle, inode_ac,
621                                     fe_blkno, suballoc_loc, suballoc_bit);
622 }
623
624 static int ocfs2_mkdir(struct inode *dir,
625                        struct dentry *dentry,
626                        umode_t mode)
627 {
628         int ret;
629
630         trace_ocfs2_mkdir(dir, dentry, dentry->d_name.len, dentry->d_name.name,
631                           OCFS2_I(dir)->ip_blkno, mode);
632         ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
633         if (ret)
634                 mlog_errno(ret);
635
636         return ret;
637 }
638
639 static int ocfs2_create(struct inode *dir,
640                         struct dentry *dentry,
641                         umode_t mode,
642                         bool excl)
643 {
644         int ret;
645
646         trace_ocfs2_create(dir, dentry, dentry->d_name.len, dentry->d_name.name,
647                            (unsigned long long)OCFS2_I(dir)->ip_blkno, mode);
648         ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
649         if (ret)
650                 mlog_errno(ret);
651
652         return ret;
653 }
654
655 static int ocfs2_link(struct dentry *old_dentry,
656                       struct inode *dir,
657                       struct dentry *dentry)
658 {
659         handle_t *handle;
660         struct inode *inode = old_dentry->d_inode;
661         int err;
662         struct buffer_head *fe_bh = NULL;
663         struct buffer_head *parent_fe_bh = NULL;
664         struct ocfs2_dinode *fe = NULL;
665         struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
666         struct ocfs2_dir_lookup_result lookup = { NULL, };
667         sigset_t oldset;
668         u64 old_de_ino;
669
670         trace_ocfs2_link((unsigned long long)OCFS2_I(inode)->ip_blkno,
671                          old_dentry->d_name.len, old_dentry->d_name.name,
672                          dentry->d_name.len, dentry->d_name.name);
673
674         if (S_ISDIR(inode->i_mode))
675                 return -EPERM;
676
677         dquot_initialize(dir);
678
679         err = ocfs2_inode_lock_nested(dir, &parent_fe_bh, 1, OI_LS_PARENT);
680         if (err < 0) {
681                 if (err != -ENOENT)
682                         mlog_errno(err);
683                 return err;
684         }
685
686         if (!dir->i_nlink) {
687                 err = -ENOENT;
688                 goto out;
689         }
690
691         err = ocfs2_lookup_ino_from_name(dir, old_dentry->d_name.name,
692                         old_dentry->d_name.len, &old_de_ino);
693         if (err) {
694                 err = -ENOENT;
695                 goto out;
696         }
697
698         /*
699          * Check whether another node removed the source inode while we
700          * were in the vfs.
701          */
702         if (old_de_ino != OCFS2_I(inode)->ip_blkno) {
703                 err = -ENOENT;
704                 goto out;
705         }
706
707         err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
708                                         dentry->d_name.len);
709         if (err)
710                 goto out;
711
712         err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
713                                            dentry->d_name.name,
714                                            dentry->d_name.len, &lookup);
715         if (err < 0) {
716                 mlog_errno(err);
717                 goto out;
718         }
719
720         err = ocfs2_inode_lock(inode, &fe_bh, 1);
721         if (err < 0) {
722                 if (err != -ENOENT)
723                         mlog_errno(err);
724                 goto out;
725         }
726
727         fe = (struct ocfs2_dinode *) fe_bh->b_data;
728         if (ocfs2_read_links_count(fe) >= ocfs2_link_max(osb)) {
729                 err = -EMLINK;
730                 goto out_unlock_inode;
731         }
732
733         handle = ocfs2_start_trans(osb, ocfs2_link_credits(osb->sb));
734         if (IS_ERR(handle)) {
735                 err = PTR_ERR(handle);
736                 handle = NULL;
737                 mlog_errno(err);
738                 goto out_unlock_inode;
739         }
740
741         /* Starting to change things, restart is no longer possible. */
742         ocfs2_block_signals(&oldset);
743
744         err = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
745                                       OCFS2_JOURNAL_ACCESS_WRITE);
746         if (err < 0) {
747                 mlog_errno(err);
748                 goto out_commit;
749         }
750
751         inc_nlink(inode);
752         inode->i_ctime = CURRENT_TIME;
753         ocfs2_set_links_count(fe, inode->i_nlink);
754         fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
755         fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
756         ocfs2_journal_dirty(handle, fe_bh);
757
758         err = ocfs2_add_entry(handle, dentry, inode,
759                               OCFS2_I(inode)->ip_blkno,
760                               parent_fe_bh, &lookup);
761         if (err) {
762                 ocfs2_add_links_count(fe, -1);
763                 drop_nlink(inode);
764                 mlog_errno(err);
765                 goto out_commit;
766         }
767
768         err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
769         if (err) {
770                 mlog_errno(err);
771                 goto out_commit;
772         }
773
774         ihold(inode);
775         d_instantiate(dentry, inode);
776
777 out_commit:
778         ocfs2_commit_trans(osb, handle);
779         ocfs2_unblock_signals(&oldset);
780 out_unlock_inode:
781         ocfs2_inode_unlock(inode, 1);
782
783 out:
784         ocfs2_inode_unlock(dir, 1);
785
786         brelse(fe_bh);
787         brelse(parent_fe_bh);
788
789         ocfs2_free_dir_lookup_result(&lookup);
790
791         if (err)
792                 mlog_errno(err);
793
794         return err;
795 }
796
797 /*
798  * Takes and drops an exclusive lock on the given dentry. This will
799  * force other nodes to drop it.
800  */
801 static int ocfs2_remote_dentry_delete(struct dentry *dentry)
802 {
803         int ret;
804
805         ret = ocfs2_dentry_lock(dentry, 1);
806         if (ret)
807                 mlog_errno(ret);
808         else
809                 ocfs2_dentry_unlock(dentry, 1);
810
811         return ret;
812 }
813
814 static inline int ocfs2_inode_is_unlinkable(struct inode *inode)
815 {
816         if (S_ISDIR(inode->i_mode)) {
817                 if (inode->i_nlink == 2)
818                         return 1;
819                 return 0;
820         }
821
822         if (inode->i_nlink == 1)
823                 return 1;
824         return 0;
825 }
826
827 static int ocfs2_unlink(struct inode *dir,
828                         struct dentry *dentry)
829 {
830         int status;
831         int child_locked = 0;
832         bool is_unlinkable = false;
833         struct inode *inode = dentry->d_inode;
834         struct inode *orphan_dir = NULL;
835         struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
836         u64 blkno;
837         struct ocfs2_dinode *fe = NULL;
838         struct buffer_head *fe_bh = NULL;
839         struct buffer_head *parent_node_bh = NULL;
840         handle_t *handle = NULL;
841         char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
842         struct ocfs2_dir_lookup_result lookup = { NULL, };
843         struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
844
845         trace_ocfs2_unlink(dir, dentry, dentry->d_name.len,
846                            dentry->d_name.name,
847                            (unsigned long long)OCFS2_I(dir)->ip_blkno,
848                            (unsigned long long)OCFS2_I(inode)->ip_blkno);
849
850         dquot_initialize(dir);
851
852         BUG_ON(dentry->d_parent->d_inode != dir);
853
854         if (inode == osb->root_inode)
855                 return -EPERM;
856
857         status = ocfs2_inode_lock_nested(dir, &parent_node_bh, 1,
858                                          OI_LS_PARENT);
859         if (status < 0) {
860                 if (status != -ENOENT)
861                         mlog_errno(status);
862                 return status;
863         }
864
865         status = ocfs2_find_files_on_disk(dentry->d_name.name,
866                                           dentry->d_name.len, &blkno, dir,
867                                           &lookup);
868         if (status < 0) {
869                 if (status != -ENOENT)
870                         mlog_errno(status);
871                 goto leave;
872         }
873
874         if (OCFS2_I(inode)->ip_blkno != blkno) {
875                 status = -ENOENT;
876
877                 trace_ocfs2_unlink_noent(
878                                 (unsigned long long)OCFS2_I(inode)->ip_blkno,
879                                 (unsigned long long)blkno,
880                                 OCFS2_I(inode)->ip_flags);
881                 goto leave;
882         }
883
884         status = ocfs2_inode_lock(inode, &fe_bh, 1);
885         if (status < 0) {
886                 if (status != -ENOENT)
887                         mlog_errno(status);
888                 goto leave;
889         }
890         child_locked = 1;
891
892         if (S_ISDIR(inode->i_mode)) {
893                 if (inode->i_nlink != 2 || !ocfs2_empty_dir(inode)) {
894                         status = -ENOTEMPTY;
895                         goto leave;
896                 }
897         }
898
899         status = ocfs2_remote_dentry_delete(dentry);
900         if (status < 0) {
901                 /* This remote delete should succeed under all normal
902                  * circumstances. */
903                 mlog_errno(status);
904                 goto leave;
905         }
906
907         if (ocfs2_inode_is_unlinkable(inode)) {
908                 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
909                                                   OCFS2_I(inode)->ip_blkno,
910                                                   orphan_name, &orphan_insert);
911                 if (status < 0) {
912                         mlog_errno(status);
913                         goto leave;
914                 }
915                 is_unlinkable = true;
916         }
917
918         handle = ocfs2_start_trans(osb, ocfs2_unlink_credits(osb->sb));
919         if (IS_ERR(handle)) {
920                 status = PTR_ERR(handle);
921                 handle = NULL;
922                 mlog_errno(status);
923                 goto leave;
924         }
925
926         status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
927                                          OCFS2_JOURNAL_ACCESS_WRITE);
928         if (status < 0) {
929                 mlog_errno(status);
930                 goto leave;
931         }
932
933         fe = (struct ocfs2_dinode *) fe_bh->b_data;
934
935         /* delete the name from the parent dir */
936         status = ocfs2_delete_entry(handle, dir, &lookup);
937         if (status < 0) {
938                 mlog_errno(status);
939                 goto leave;
940         }
941
942         if (S_ISDIR(inode->i_mode))
943                 drop_nlink(inode);
944         drop_nlink(inode);
945         ocfs2_set_links_count(fe, inode->i_nlink);
946         ocfs2_journal_dirty(handle, fe_bh);
947
948         dir->i_ctime = dir->i_mtime = CURRENT_TIME;
949         if (S_ISDIR(inode->i_mode))
950                 drop_nlink(dir);
951
952         status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
953         if (status < 0) {
954                 mlog_errno(status);
955                 if (S_ISDIR(inode->i_mode))
956                         inc_nlink(dir);
957                 goto leave;
958         }
959
960         if (is_unlinkable) {
961                 status = ocfs2_orphan_add(osb, handle, inode, fe_bh,
962                                 orphan_name, &orphan_insert, orphan_dir);
963                 if (status < 0)
964                         mlog_errno(status);
965         }
966
967 leave:
968         if (handle)
969                 ocfs2_commit_trans(osb, handle);
970
971         if (child_locked)
972                 ocfs2_inode_unlock(inode, 1);
973
974         ocfs2_inode_unlock(dir, 1);
975
976         if (orphan_dir) {
977                 /* This was locked for us in ocfs2_prepare_orphan_dir() */
978                 ocfs2_inode_unlock(orphan_dir, 1);
979                 mutex_unlock(&orphan_dir->i_mutex);
980                 iput(orphan_dir);
981         }
982
983         brelse(fe_bh);
984         brelse(parent_node_bh);
985
986         ocfs2_free_dir_lookup_result(&orphan_insert);
987         ocfs2_free_dir_lookup_result(&lookup);
988
989         if (status && (status != -ENOTEMPTY) && (status != -ENOENT))
990                 mlog_errno(status);
991
992         return status;
993 }
994
995 /*
996  * The only place this should be used is rename!
997  * if they have the same id, then the 1st one is the only one locked.
998  */
999 static int ocfs2_double_lock(struct ocfs2_super *osb,
1000                              struct buffer_head **bh1,
1001                              struct inode *inode1,
1002                              struct buffer_head **bh2,
1003                              struct inode *inode2)
1004 {
1005         int status;
1006         struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
1007         struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
1008         struct buffer_head **tmpbh;
1009         struct inode *tmpinode;
1010
1011         trace_ocfs2_double_lock((unsigned long long)oi1->ip_blkno,
1012                                 (unsigned long long)oi2->ip_blkno);
1013
1014         if (*bh1)
1015                 *bh1 = NULL;
1016         if (*bh2)
1017                 *bh2 = NULL;
1018
1019         /* we always want to lock the one with the lower lockid first. */
1020         if (oi1->ip_blkno != oi2->ip_blkno) {
1021                 if (oi1->ip_blkno < oi2->ip_blkno) {
1022                         /* switch id1 and id2 around */
1023                         tmpbh = bh2;
1024                         bh2 = bh1;
1025                         bh1 = tmpbh;
1026
1027                         tmpinode = inode2;
1028                         inode2 = inode1;
1029                         inode1 = tmpinode;
1030                 }
1031                 /* lock id2 */
1032                 status = ocfs2_inode_lock_nested(inode2, bh2, 1,
1033                                                  OI_LS_RENAME1);
1034                 if (status < 0) {
1035                         if (status != -ENOENT)
1036                                 mlog_errno(status);
1037                         goto bail;
1038                 }
1039         }
1040
1041         /* lock id1 */
1042         status = ocfs2_inode_lock_nested(inode1, bh1, 1, OI_LS_RENAME2);
1043         if (status < 0) {
1044                 /*
1045                  * An error return must mean that no cluster locks
1046                  * were held on function exit.
1047                  */
1048                 if (oi1->ip_blkno != oi2->ip_blkno) {
1049                         ocfs2_inode_unlock(inode2, 1);
1050                         brelse(*bh2);
1051                         *bh2 = NULL;
1052                 }
1053
1054                 if (status != -ENOENT)
1055                         mlog_errno(status);
1056         }
1057
1058         trace_ocfs2_double_lock_end(
1059                         (unsigned long long)OCFS2_I(inode1)->ip_blkno,
1060                         (unsigned long long)OCFS2_I(inode2)->ip_blkno);
1061
1062 bail:
1063         if (status)
1064                 mlog_errno(status);
1065         return status;
1066 }
1067
1068 static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
1069 {
1070         ocfs2_inode_unlock(inode1, 1);
1071
1072         if (inode1 != inode2)
1073                 ocfs2_inode_unlock(inode2, 1);
1074 }
1075
1076 static int ocfs2_rename(struct inode *old_dir,
1077                         struct dentry *old_dentry,
1078                         struct inode *new_dir,
1079                         struct dentry *new_dentry)
1080 {
1081         int status = 0, rename_lock = 0, parents_locked = 0, target_exists = 0;
1082         int old_child_locked = 0, new_child_locked = 0, update_dot_dot = 0;
1083         struct inode *old_inode = old_dentry->d_inode;
1084         struct inode *new_inode = new_dentry->d_inode;
1085         struct inode *orphan_dir = NULL;
1086         struct ocfs2_dinode *newfe = NULL;
1087         char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
1088         struct buffer_head *newfe_bh = NULL;
1089         struct buffer_head *old_inode_bh = NULL;
1090         struct ocfs2_super *osb = NULL;
1091         u64 newfe_blkno, old_de_ino;
1092         handle_t *handle = NULL;
1093         struct buffer_head *old_dir_bh = NULL;
1094         struct buffer_head *new_dir_bh = NULL;
1095         u32 old_dir_nlink = old_dir->i_nlink;
1096         struct ocfs2_dinode *old_di;
1097         struct ocfs2_dir_lookup_result old_inode_dot_dot_res = { NULL, };
1098         struct ocfs2_dir_lookup_result target_lookup_res = { NULL, };
1099         struct ocfs2_dir_lookup_result old_entry_lookup = { NULL, };
1100         struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
1101         struct ocfs2_dir_lookup_result target_insert = { NULL, };
1102
1103         /* At some point it might be nice to break this function up a
1104          * bit. */
1105
1106         trace_ocfs2_rename(old_dir, old_dentry, new_dir, new_dentry,
1107                            old_dentry->d_name.len, old_dentry->d_name.name,
1108                            new_dentry->d_name.len, new_dentry->d_name.name);
1109
1110         dquot_initialize(old_dir);
1111         dquot_initialize(new_dir);
1112
1113         osb = OCFS2_SB(old_dir->i_sb);
1114
1115         if (new_inode) {
1116                 if (!igrab(new_inode))
1117                         BUG();
1118         }
1119
1120         /* Assume a directory hierarchy thusly:
1121          * a/b/c
1122          * a/d
1123          * a,b,c, and d are all directories.
1124          *
1125          * from cwd of 'a' on both nodes:
1126          * node1: mv b/c d
1127          * node2: mv d   b/c
1128          *
1129          * And that's why, just like the VFS, we need a file system
1130          * rename lock. */
1131         if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) {
1132                 status = ocfs2_rename_lock(osb);
1133                 if (status < 0) {
1134                         mlog_errno(status);
1135                         goto bail;
1136                 }
1137                 rename_lock = 1;
1138         }
1139
1140         /* if old and new are the same, this'll just do one lock. */
1141         status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
1142                                    &new_dir_bh, new_dir);
1143         if (status < 0) {
1144                 mlog_errno(status);
1145                 goto bail;
1146         }
1147         parents_locked = 1;
1148
1149         /* make sure both dirs have bhs
1150          * get an extra ref on old_dir_bh if old==new */
1151         if (!new_dir_bh) {
1152                 if (old_dir_bh) {
1153                         new_dir_bh = old_dir_bh;
1154                         get_bh(new_dir_bh);
1155                 } else {
1156                         mlog(ML_ERROR, "no old_dir_bh!\n");
1157                         status = -EIO;
1158                         goto bail;
1159                 }
1160         }
1161
1162         /*
1163          * Aside from allowing a meta data update, the locking here
1164          * also ensures that the downconvert thread on other nodes
1165          * won't have to concurrently downconvert the inode and the
1166          * dentry locks.
1167          */
1168         status = ocfs2_inode_lock_nested(old_inode, &old_inode_bh, 1,
1169                                          OI_LS_PARENT);
1170         if (status < 0) {
1171                 if (status != -ENOENT)
1172                         mlog_errno(status);
1173                 goto bail;
1174         }
1175         old_child_locked = 1;
1176
1177         status = ocfs2_remote_dentry_delete(old_dentry);
1178         if (status < 0) {
1179                 mlog_errno(status);
1180                 goto bail;
1181         }
1182
1183         if (S_ISDIR(old_inode->i_mode)) {
1184                 u64 old_inode_parent;
1185
1186                 update_dot_dot = 1;
1187                 status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent,
1188                                                   old_inode,
1189                                                   &old_inode_dot_dot_res);
1190                 if (status) {
1191                         status = -EIO;
1192                         goto bail;
1193                 }
1194
1195                 if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) {
1196                         status = -EIO;
1197                         goto bail;
1198                 }
1199
1200                 if (!new_inode && new_dir != old_dir &&
1201                     new_dir->i_nlink >= ocfs2_link_max(osb)) {
1202                         status = -EMLINK;
1203                         goto bail;
1204                 }
1205         }
1206
1207         status = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name,
1208                                             old_dentry->d_name.len,
1209                                             &old_de_ino);
1210         if (status) {
1211                 status = -ENOENT;
1212                 goto bail;
1213         }
1214
1215         /*
1216          *  Check for inode number is _not_ due to possible IO errors.
1217          *  We might rmdir the source, keep it as pwd of some process
1218          *  and merrily kill the link to whatever was created under the
1219          *  same name. Goodbye sticky bit ;-<
1220          */
1221         if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) {
1222                 status = -ENOENT;
1223                 goto bail;
1224         }
1225
1226         /* check if the target already exists (in which case we need
1227          * to delete it */
1228         status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
1229                                           new_dentry->d_name.len,
1230                                           &newfe_blkno, new_dir,
1231                                           &target_lookup_res);
1232         /* The only error we allow here is -ENOENT because the new
1233          * file not existing is perfectly valid. */
1234         if ((status < 0) && (status != -ENOENT)) {
1235                 /* If we cannot find the file specified we should just */
1236                 /* return the error... */
1237                 mlog_errno(status);
1238                 goto bail;
1239         }
1240         if (status == 0)
1241                 target_exists = 1;
1242
1243         if (!target_exists && new_inode) {
1244                 /*
1245                  * Target was unlinked by another node while we were
1246                  * waiting to get to ocfs2_rename(). There isn't
1247                  * anything we can do here to help the situation, so
1248                  * bubble up the appropriate error.
1249                  */
1250                 status = -ENOENT;
1251                 goto bail;
1252         }
1253
1254         /* In case we need to overwrite an existing file, we blow it
1255          * away first */
1256         if (target_exists) {
1257                 /* VFS didn't think there existed an inode here, but
1258                  * someone else in the cluster must have raced our
1259                  * rename to create one. Today we error cleanly, in
1260                  * the future we should consider calling iget to build
1261                  * a new struct inode for this entry. */
1262                 if (!new_inode) {
1263                         status = -EACCES;
1264
1265                         trace_ocfs2_rename_target_exists(new_dentry->d_name.len,
1266                                                 new_dentry->d_name.name);
1267                         goto bail;
1268                 }
1269
1270                 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
1271                         status = -EACCES;
1272
1273                         trace_ocfs2_rename_disagree(
1274                              (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
1275                              (unsigned long long)newfe_blkno,
1276                              OCFS2_I(new_inode)->ip_flags);
1277                         goto bail;
1278                 }
1279
1280                 status = ocfs2_inode_lock(new_inode, &newfe_bh, 1);
1281                 if (status < 0) {
1282                         if (status != -ENOENT)
1283                                 mlog_errno(status);
1284                         goto bail;
1285                 }
1286                 new_child_locked = 1;
1287
1288                 status = ocfs2_remote_dentry_delete(new_dentry);
1289                 if (status < 0) {
1290                         mlog_errno(status);
1291                         goto bail;
1292                 }
1293
1294                 newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
1295
1296                 trace_ocfs2_rename_over_existing(
1297                      (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
1298                      (unsigned long long)newfe_bh->b_blocknr : 0ULL);
1299
1300                 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
1301                         status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
1302                                                 OCFS2_I(new_inode)->ip_blkno,
1303                                                 orphan_name, &orphan_insert);
1304                         if (status < 0) {
1305                                 mlog_errno(status);
1306                                 goto bail;
1307                         }
1308                 }
1309         } else {
1310                 BUG_ON(new_dentry->d_parent->d_inode != new_dir);
1311
1312                 status = ocfs2_check_dir_for_entry(new_dir,
1313                                                    new_dentry->d_name.name,
1314                                                    new_dentry->d_name.len);
1315                 if (status)
1316                         goto bail;
1317
1318                 status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
1319                                                       new_dentry->d_name.name,
1320                                                       new_dentry->d_name.len,
1321                                                       &target_insert);
1322                 if (status < 0) {
1323                         mlog_errno(status);
1324                         goto bail;
1325                 }
1326         }
1327
1328         handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb));
1329         if (IS_ERR(handle)) {
1330                 status = PTR_ERR(handle);
1331                 handle = NULL;
1332                 mlog_errno(status);
1333                 goto bail;
1334         }
1335
1336         if (target_exists) {
1337                 if (S_ISDIR(new_inode->i_mode)) {
1338                         if (new_inode->i_nlink != 2 ||
1339                             !ocfs2_empty_dir(new_inode)) {
1340                                 status = -ENOTEMPTY;
1341                                 goto bail;
1342                         }
1343                 }
1344                 status = ocfs2_journal_access_di(handle, INODE_CACHE(new_inode),
1345                                                  newfe_bh,
1346                                                  OCFS2_JOURNAL_ACCESS_WRITE);
1347                 if (status < 0) {
1348                         mlog_errno(status);
1349                         goto bail;
1350                 }
1351
1352                 if (S_ISDIR(new_inode->i_mode) ||
1353                     (ocfs2_read_links_count(newfe) == 1)) {
1354                         status = ocfs2_orphan_add(osb, handle, new_inode,
1355                                                   newfe_bh, orphan_name,
1356                                                   &orphan_insert, orphan_dir);
1357                         if (status < 0) {
1358                                 mlog_errno(status);
1359                                 goto bail;
1360                         }
1361                 }
1362
1363                 /* change the dirent to point to the correct inode */
1364                 status = ocfs2_update_entry(new_dir, handle, &target_lookup_res,
1365                                             old_inode);
1366                 if (status < 0) {
1367                         mlog_errno(status);
1368                         goto bail;
1369                 }
1370                 new_dir->i_version++;
1371
1372                 if (S_ISDIR(new_inode->i_mode))
1373                         ocfs2_set_links_count(newfe, 0);
1374                 else
1375                         ocfs2_add_links_count(newfe, -1);
1376                 ocfs2_journal_dirty(handle, newfe_bh);
1377         } else {
1378                 /* if the name was not found in new_dir, add it now */
1379                 status = ocfs2_add_entry(handle, new_dentry, old_inode,
1380                                          OCFS2_I(old_inode)->ip_blkno,
1381                                          new_dir_bh, &target_insert);
1382         }
1383
1384         old_inode->i_ctime = CURRENT_TIME;
1385         mark_inode_dirty(old_inode);
1386
1387         status = ocfs2_journal_access_di(handle, INODE_CACHE(old_inode),
1388                                          old_inode_bh,
1389                                          OCFS2_JOURNAL_ACCESS_WRITE);
1390         if (status >= 0) {
1391                 old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
1392
1393                 old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
1394                 old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
1395                 ocfs2_journal_dirty(handle, old_inode_bh);
1396         } else
1397                 mlog_errno(status);
1398
1399         /*
1400          * Now that the name has been added to new_dir, remove the old name.
1401          *
1402          * We don't keep any directory entry context around until now
1403          * because the insert might have changed the type of directory
1404          * we're dealing with.
1405          */
1406         status = ocfs2_find_entry(old_dentry->d_name.name,
1407                                   old_dentry->d_name.len, old_dir,
1408                                   &old_entry_lookup);
1409         if (status)
1410                 goto bail;
1411
1412         status = ocfs2_delete_entry(handle, old_dir, &old_entry_lookup);
1413         if (status < 0) {
1414                 mlog_errno(status);
1415                 goto bail;
1416         }
1417
1418         if (new_inode) {
1419                 drop_nlink(new_inode);
1420                 new_inode->i_ctime = CURRENT_TIME;
1421         }
1422         old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1423
1424         if (update_dot_dot) {
1425                 status = ocfs2_update_entry(old_inode, handle,
1426                                             &old_inode_dot_dot_res, new_dir);
1427                 drop_nlink(old_dir);
1428                 if (new_inode) {
1429                         drop_nlink(new_inode);
1430                 } else {
1431                         inc_nlink(new_dir);
1432                         mark_inode_dirty(new_dir);
1433                 }
1434         }
1435         mark_inode_dirty(old_dir);
1436         ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
1437         if (new_inode) {
1438                 mark_inode_dirty(new_inode);
1439                 ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
1440         }
1441
1442         if (old_dir != new_dir) {
1443                 /* Keep the same times on both directories.*/
1444                 new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
1445
1446                 /*
1447                  * This will also pick up the i_nlink change from the
1448                  * block above.
1449                  */
1450                 ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
1451         }
1452
1453         if (old_dir_nlink != old_dir->i_nlink) {
1454                 if (!old_dir_bh) {
1455                         mlog(ML_ERROR, "need to change nlink for old dir "
1456                              "%llu from %d to %d but bh is NULL!\n",
1457                              (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
1458                              (int)old_dir_nlink, old_dir->i_nlink);
1459                 } else {
1460                         struct ocfs2_dinode *fe;
1461                         status = ocfs2_journal_access_di(handle,
1462                                                          INODE_CACHE(old_dir),
1463                                                          old_dir_bh,
1464                                                          OCFS2_JOURNAL_ACCESS_WRITE);
1465                         fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
1466                         ocfs2_set_links_count(fe, old_dir->i_nlink);
1467                         ocfs2_journal_dirty(handle, old_dir_bh);
1468                 }
1469         }
1470         ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
1471         status = 0;
1472 bail:
1473         if (rename_lock)
1474                 ocfs2_rename_unlock(osb);
1475
1476         if (handle)
1477                 ocfs2_commit_trans(osb, handle);
1478
1479         if (parents_locked)
1480                 ocfs2_double_unlock(old_dir, new_dir);
1481
1482         if (old_child_locked)
1483                 ocfs2_inode_unlock(old_inode, 1);
1484
1485         if (new_child_locked)
1486                 ocfs2_inode_unlock(new_inode, 1);
1487
1488         if (orphan_dir) {
1489                 /* This was locked for us in ocfs2_prepare_orphan_dir() */
1490                 ocfs2_inode_unlock(orphan_dir, 1);
1491                 mutex_unlock(&orphan_dir->i_mutex);
1492                 iput(orphan_dir);
1493         }
1494
1495         if (new_inode)
1496                 sync_mapping_buffers(old_inode->i_mapping);
1497
1498         if (new_inode)
1499                 iput(new_inode);
1500
1501         ocfs2_free_dir_lookup_result(&target_lookup_res);
1502         ocfs2_free_dir_lookup_result(&old_entry_lookup);
1503         ocfs2_free_dir_lookup_result(&old_inode_dot_dot_res);
1504         ocfs2_free_dir_lookup_result(&orphan_insert);
1505         ocfs2_free_dir_lookup_result(&target_insert);
1506
1507         brelse(newfe_bh);
1508         brelse(old_inode_bh);
1509         brelse(old_dir_bh);
1510         brelse(new_dir_bh);
1511
1512         if (status)
1513                 mlog_errno(status);
1514
1515         return status;
1516 }
1517
1518 /*
1519  * we expect i_size = strlen(symname). Copy symname into the file
1520  * data, including the null terminator.
1521  */
1522 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
1523                                      handle_t *handle,
1524                                      struct inode *inode,
1525                                      const char *symname)
1526 {
1527         struct buffer_head **bhs = NULL;
1528         const char *c;
1529         struct super_block *sb = osb->sb;
1530         u64 p_blkno, p_blocks;
1531         int virtual, blocks, status, i, bytes_left;
1532
1533         bytes_left = i_size_read(inode) + 1;
1534         /* we can't trust i_blocks because we're actually going to
1535          * write i_size + 1 bytes. */
1536         blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1537
1538         trace_ocfs2_create_symlink_data((unsigned long long)inode->i_blocks,
1539                                         i_size_read(inode), blocks);
1540
1541         /* Sanity check -- make sure we're going to fit. */
1542         if (bytes_left >
1543             ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1544                 status = -EIO;
1545                 mlog_errno(status);
1546                 goto bail;
1547         }
1548
1549         bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
1550         if (!bhs) {
1551                 status = -ENOMEM;
1552                 mlog_errno(status);
1553                 goto bail;
1554         }
1555
1556         status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
1557                                              NULL);
1558         if (status < 0) {
1559                 mlog_errno(status);
1560                 goto bail;
1561         }
1562
1563         /* links can never be larger than one cluster so we know this
1564          * is all going to be contiguous, but do a sanity check
1565          * anyway. */
1566         if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
1567                 status = -EIO;
1568                 mlog_errno(status);
1569                 goto bail;
1570         }
1571
1572         virtual = 0;
1573         while(bytes_left > 0) {
1574                 c = &symname[virtual * sb->s_blocksize];
1575
1576                 bhs[virtual] = sb_getblk(sb, p_blkno);
1577                 if (!bhs[virtual]) {
1578                         status = -ENOMEM;
1579                         mlog_errno(status);
1580                         goto bail;
1581                 }
1582                 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode),
1583                                               bhs[virtual]);
1584
1585                 status = ocfs2_journal_access(handle, INODE_CACHE(inode),
1586                                               bhs[virtual],
1587                                               OCFS2_JOURNAL_ACCESS_CREATE);
1588                 if (status < 0) {
1589                         mlog_errno(status);
1590                         goto bail;
1591                 }
1592
1593                 memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
1594
1595                 memcpy(bhs[virtual]->b_data, c,
1596                        (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
1597                        bytes_left);
1598
1599                 ocfs2_journal_dirty(handle, bhs[virtual]);
1600
1601                 virtual++;
1602                 p_blkno++;
1603                 bytes_left -= sb->s_blocksize;
1604         }
1605
1606         status = 0;
1607 bail:
1608
1609         if (bhs) {
1610                 for(i = 0; i < blocks; i++)
1611                         brelse(bhs[i]);
1612                 kfree(bhs);
1613         }
1614
1615         if (status)
1616                 mlog_errno(status);
1617         return status;
1618 }
1619
1620 static int ocfs2_symlink(struct inode *dir,
1621                          struct dentry *dentry,
1622                          const char *symname)
1623 {
1624         int status, l, credits;
1625         u64 newsize;
1626         struct ocfs2_super *osb = NULL;
1627         struct inode *inode = NULL;
1628         struct super_block *sb;
1629         struct buffer_head *new_fe_bh = NULL;
1630         struct buffer_head *parent_fe_bh = NULL;
1631         struct ocfs2_dinode *fe = NULL;
1632         struct ocfs2_dinode *dirfe;
1633         handle_t *handle = NULL;
1634         struct ocfs2_alloc_context *inode_ac = NULL;
1635         struct ocfs2_alloc_context *data_ac = NULL;
1636         struct ocfs2_alloc_context *xattr_ac = NULL;
1637         int want_clusters = 0;
1638         int xattr_credits = 0;
1639         struct ocfs2_security_xattr_info si = {
1640                 .enable = 1,
1641         };
1642         int did_quota = 0, did_quota_inode = 0;
1643         struct ocfs2_dir_lookup_result lookup = { NULL, };
1644         sigset_t oldset;
1645         int did_block_signals = 0;
1646
1647         trace_ocfs2_symlink_begin(dir, dentry, symname,
1648                                   dentry->d_name.len, dentry->d_name.name);
1649
1650         dquot_initialize(dir);
1651
1652         sb = dir->i_sb;
1653         osb = OCFS2_SB(sb);
1654
1655         l = strlen(symname) + 1;
1656
1657         credits = ocfs2_calc_symlink_credits(sb);
1658
1659         /* lock the parent directory */
1660         status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
1661         if (status < 0) {
1662                 if (status != -ENOENT)
1663                         mlog_errno(status);
1664                 return status;
1665         }
1666
1667         dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1668         if (!ocfs2_read_links_count(dirfe)) {
1669                 /* can't make a file in a deleted directory. */
1670                 status = -ENOENT;
1671                 goto bail;
1672         }
1673
1674         status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
1675                                            dentry->d_name.len);
1676         if (status)
1677                 goto bail;
1678
1679         status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
1680                                               dentry->d_name.name,
1681                                               dentry->d_name.len, &lookup);
1682         if (status < 0) {
1683                 mlog_errno(status);
1684                 goto bail;
1685         }
1686
1687         status = ocfs2_reserve_new_inode(osb, &inode_ac);
1688         if (status < 0) {
1689                 if (status != -ENOSPC)
1690                         mlog_errno(status);
1691                 goto bail;
1692         }
1693
1694         inode = ocfs2_get_init_inode(dir, S_IFLNK | S_IRWXUGO);
1695         if (!inode) {
1696                 status = -ENOMEM;
1697                 mlog_errno(status);
1698                 goto bail;
1699         }
1700
1701         /* get security xattr */
1702         status = ocfs2_init_security_get(inode, dir, &dentry->d_name, &si);
1703         if (status) {
1704                 if (status == -EOPNOTSUPP)
1705                         si.enable = 0;
1706                 else {
1707                         mlog_errno(status);
1708                         goto bail;
1709                 }
1710         }
1711
1712         /* calculate meta data/clusters for setting security xattr */
1713         if (si.enable) {
1714                 status = ocfs2_calc_security_init(dir, &si, &want_clusters,
1715                                                   &xattr_credits, &xattr_ac);
1716                 if (status < 0) {
1717                         mlog_errno(status);
1718                         goto bail;
1719                 }
1720         }
1721
1722         /* don't reserve bitmap space for fast symlinks. */
1723         if (l > ocfs2_fast_symlink_chars(sb))
1724                 want_clusters += 1;
1725
1726         status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
1727         if (status < 0) {
1728                 if (status != -ENOSPC)
1729                         mlog_errno(status);
1730                 goto bail;
1731         }
1732
1733         handle = ocfs2_start_trans(osb, credits + xattr_credits);
1734         if (IS_ERR(handle)) {
1735                 status = PTR_ERR(handle);
1736                 handle = NULL;
1737                 mlog_errno(status);
1738                 goto bail;
1739         }
1740
1741         /* Starting to change things, restart is no longer possible. */
1742         ocfs2_block_signals(&oldset);
1743         did_block_signals = 1;
1744
1745         status = dquot_alloc_inode(inode);
1746         if (status)
1747                 goto bail;
1748         did_quota_inode = 1;
1749
1750         trace_ocfs2_symlink_create(dir, dentry, dentry->d_name.len,
1751                                    dentry->d_name.name,
1752                                    (unsigned long long)OCFS2_I(dir)->ip_blkno,
1753                                    inode->i_mode);
1754
1755         status = ocfs2_mknod_locked(osb, dir, inode,
1756                                     0, &new_fe_bh, parent_fe_bh, handle,
1757                                     inode_ac);
1758         if (status < 0) {
1759                 mlog_errno(status);
1760                 goto bail;
1761         }
1762
1763         fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
1764         inode->i_rdev = 0;
1765         newsize = l - 1;
1766         inode->i_op = &ocfs2_symlink_inode_operations;
1767         if (l > ocfs2_fast_symlink_chars(sb)) {
1768                 u32 offset = 0;
1769
1770                 status = dquot_alloc_space_nodirty(inode,
1771                     ocfs2_clusters_to_bytes(osb->sb, 1));
1772                 if (status)
1773                         goto bail;
1774                 did_quota = 1;
1775                 inode->i_mapping->a_ops = &ocfs2_aops;
1776                 status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0,
1777                                               new_fe_bh,
1778                                               handle, data_ac, NULL,
1779                                               NULL);
1780                 if (status < 0) {
1781                         if (status != -ENOSPC && status != -EINTR) {
1782                                 mlog(ML_ERROR,
1783                                      "Failed to extend file to %llu\n",
1784                                      (unsigned long long)newsize);
1785                                 mlog_errno(status);
1786                                 status = -ENOSPC;
1787                         }
1788                         goto bail;
1789                 }
1790                 i_size_write(inode, newsize);
1791                 inode->i_blocks = ocfs2_inode_sector_count(inode);
1792         } else {
1793                 inode->i_mapping->a_ops = &ocfs2_fast_symlink_aops;
1794                 memcpy((char *) fe->id2.i_symlink, symname, l);
1795                 i_size_write(inode, newsize);
1796                 inode->i_blocks = 0;
1797         }
1798
1799         status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
1800         if (status < 0) {
1801                 mlog_errno(status);
1802                 goto bail;
1803         }
1804
1805         if (!ocfs2_inode_is_fast_symlink(inode)) {
1806                 status = ocfs2_create_symlink_data(osb, handle, inode,
1807                                                    symname);
1808                 if (status < 0) {
1809                         mlog_errno(status);
1810                         goto bail;
1811                 }
1812         }
1813
1814         if (si.enable) {
1815                 status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
1816                                                  xattr_ac, data_ac);
1817                 if (status < 0) {
1818                         mlog_errno(status);
1819                         goto bail;
1820                 }
1821         }
1822
1823         /*
1824          * Do this before adding the entry to the directory. We add
1825          * also set d_op after success so that ->d_iput() will cleanup
1826          * the dentry lock even if ocfs2_add_entry() fails below.
1827          */
1828         status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
1829         if (status) {
1830                 mlog_errno(status);
1831                 goto bail;
1832         }
1833
1834         status = ocfs2_add_entry(handle, dentry, inode,
1835                                  le64_to_cpu(fe->i_blkno), parent_fe_bh,
1836                                  &lookup);
1837         if (status < 0) {
1838                 mlog_errno(status);
1839                 goto bail;
1840         }
1841
1842         insert_inode_hash(inode);
1843         d_instantiate(dentry, inode);
1844 bail:
1845         if (status < 0 && did_quota)
1846                 dquot_free_space_nodirty(inode,
1847                                         ocfs2_clusters_to_bytes(osb->sb, 1));
1848         if (status < 0 && did_quota_inode)
1849                 dquot_free_inode(inode);
1850         if (handle)
1851                 ocfs2_commit_trans(osb, handle);
1852
1853         ocfs2_inode_unlock(dir, 1);
1854         if (did_block_signals)
1855                 ocfs2_unblock_signals(&oldset);
1856
1857         brelse(new_fe_bh);
1858         brelse(parent_fe_bh);
1859         kfree(si.name);
1860         kfree(si.value);
1861         ocfs2_free_dir_lookup_result(&lookup);
1862         if (inode_ac)
1863                 ocfs2_free_alloc_context(inode_ac);
1864         if (data_ac)
1865                 ocfs2_free_alloc_context(data_ac);
1866         if (xattr_ac)
1867                 ocfs2_free_alloc_context(xattr_ac);
1868         if ((status < 0) && inode) {
1869                 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SKIP_ORPHAN_DIR;
1870                 clear_nlink(inode);
1871                 iput(inode);
1872         }
1873
1874         if (status)
1875                 mlog_errno(status);
1876
1877         return status;
1878 }
1879
1880 static int ocfs2_blkno_stringify(u64 blkno, char *name)
1881 {
1882         int status, namelen;
1883
1884         namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
1885                            (long long)blkno);
1886         if (namelen <= 0) {
1887                 if (namelen)
1888                         status = namelen;
1889                 else
1890                         status = -EINVAL;
1891                 mlog_errno(status);
1892                 goto bail;
1893         }
1894         if (namelen != OCFS2_ORPHAN_NAMELEN) {
1895                 status = -EINVAL;
1896                 mlog_errno(status);
1897                 goto bail;
1898         }
1899
1900         trace_ocfs2_blkno_stringify(blkno, name, namelen);
1901
1902         status = 0;
1903 bail:
1904         if (status < 0)
1905                 mlog_errno(status);
1906         return status;
1907 }
1908
1909 static int ocfs2_lookup_lock_orphan_dir(struct ocfs2_super *osb,
1910                                         struct inode **ret_orphan_dir,
1911                                         struct buffer_head **ret_orphan_dir_bh)
1912 {
1913         struct inode *orphan_dir_inode;
1914         struct buffer_head *orphan_dir_bh = NULL;
1915         int ret = 0;
1916
1917         orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1918                                                        ORPHAN_DIR_SYSTEM_INODE,
1919                                                        osb->slot_num);
1920         if (!orphan_dir_inode) {
1921                 ret = -ENOENT;
1922                 mlog_errno(ret);
1923                 return ret;
1924         }
1925
1926         mutex_lock(&orphan_dir_inode->i_mutex);
1927
1928         ret = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
1929         if (ret < 0) {
1930                 mutex_unlock(&orphan_dir_inode->i_mutex);
1931                 iput(orphan_dir_inode);
1932
1933                 mlog_errno(ret);
1934                 return ret;
1935         }
1936
1937         *ret_orphan_dir = orphan_dir_inode;
1938         *ret_orphan_dir_bh = orphan_dir_bh;
1939
1940         return 0;
1941 }
1942
1943 static int __ocfs2_prepare_orphan_dir(struct inode *orphan_dir_inode,
1944                                       struct buffer_head *orphan_dir_bh,
1945                                       u64 blkno,
1946                                       char *name,
1947                                       struct ocfs2_dir_lookup_result *lookup)
1948 {
1949         int ret;
1950         struct ocfs2_super *osb = OCFS2_SB(orphan_dir_inode->i_sb);
1951
1952         ret = ocfs2_blkno_stringify(blkno, name);
1953         if (ret < 0) {
1954                 mlog_errno(ret);
1955                 return ret;
1956         }
1957
1958         ret = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
1959                                            orphan_dir_bh, name,
1960                                            OCFS2_ORPHAN_NAMELEN, lookup);
1961         if (ret < 0) {
1962                 mlog_errno(ret);
1963                 return ret;
1964         }
1965
1966         return 0;
1967 }
1968
1969 /**
1970  * ocfs2_prepare_orphan_dir() - Prepare an orphan directory for
1971  * insertion of an orphan.
1972  * @osb: ocfs2 file system
1973  * @ret_orphan_dir: Orphan dir inode - returned locked!
1974  * @blkno: Actual block number of the inode to be inserted into orphan dir.
1975  * @lookup: dir lookup result, to be passed back into functions like
1976  *          ocfs2_orphan_add
1977  *
1978  * Returns zero on success and the ret_orphan_dir, name and lookup
1979  * fields will be populated.
1980  *
1981  * Returns non-zero on failure. 
1982  */
1983 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
1984                                     struct inode **ret_orphan_dir,
1985                                     u64 blkno,
1986                                     char *name,
1987                                     struct ocfs2_dir_lookup_result *lookup)
1988 {
1989         struct inode *orphan_dir_inode = NULL;
1990         struct buffer_head *orphan_dir_bh = NULL;
1991         int ret = 0;
1992
1993         ret = ocfs2_lookup_lock_orphan_dir(osb, &orphan_dir_inode,
1994                                            &orphan_dir_bh);
1995         if (ret < 0) {
1996                 mlog_errno(ret);
1997                 return ret;
1998         }
1999
2000         ret = __ocfs2_prepare_orphan_dir(orphan_dir_inode, orphan_dir_bh,
2001                                          blkno, name, lookup);
2002         if (ret < 0) {
2003                 mlog_errno(ret);
2004                 goto out;
2005         }
2006
2007         *ret_orphan_dir = orphan_dir_inode;
2008
2009 out:
2010         brelse(orphan_dir_bh);
2011
2012         if (ret) {
2013                 ocfs2_inode_unlock(orphan_dir_inode, 1);
2014                 mutex_unlock(&orphan_dir_inode->i_mutex);
2015                 iput(orphan_dir_inode);
2016         }
2017
2018         if (ret)
2019                 mlog_errno(ret);
2020         return ret;
2021 }
2022
2023 static int ocfs2_orphan_add(struct ocfs2_super *osb,
2024                             handle_t *handle,
2025                             struct inode *inode,
2026                             struct buffer_head *fe_bh,
2027                             char *name,
2028                             struct ocfs2_dir_lookup_result *lookup,
2029                             struct inode *orphan_dir_inode)
2030 {
2031         struct buffer_head *orphan_dir_bh = NULL;
2032         int status = 0;
2033         struct ocfs2_dinode *orphan_fe;
2034         struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
2035
2036         trace_ocfs2_orphan_add_begin(
2037                                 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2038
2039         status = ocfs2_read_inode_block(orphan_dir_inode, &orphan_dir_bh);
2040         if (status < 0) {
2041                 mlog_errno(status);
2042                 goto leave;
2043         }
2044
2045         status = ocfs2_journal_access_di(handle,
2046                                          INODE_CACHE(orphan_dir_inode),
2047                                          orphan_dir_bh,
2048                                          OCFS2_JOURNAL_ACCESS_WRITE);
2049         if (status < 0) {
2050                 mlog_errno(status);
2051                 goto leave;
2052         }
2053
2054         /*
2055          * We're going to journal the change of i_flags and i_orphaned_slot.
2056          * It's safe anyway, though some callers may duplicate the journaling.
2057          * Journaling within the func just make the logic look more
2058          * straightforward.
2059          */
2060         status = ocfs2_journal_access_di(handle,
2061                                          INODE_CACHE(inode),
2062                                          fe_bh,
2063                                          OCFS2_JOURNAL_ACCESS_WRITE);
2064         if (status < 0) {
2065                 mlog_errno(status);
2066                 goto leave;
2067         }
2068
2069         /* we're a cluster, and nlink can change on disk from
2070          * underneath us... */
2071         orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2072         if (S_ISDIR(inode->i_mode))
2073                 ocfs2_add_links_count(orphan_fe, 1);
2074         set_nlink(orphan_dir_inode, ocfs2_read_links_count(orphan_fe));
2075         ocfs2_journal_dirty(handle, orphan_dir_bh);
2076
2077         status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
2078                                    OCFS2_ORPHAN_NAMELEN, inode,
2079                                    OCFS2_I(inode)->ip_blkno,
2080                                    orphan_dir_bh, lookup);
2081         if (status < 0) {
2082                 mlog_errno(status);
2083                 goto rollback;
2084         }
2085
2086         fe->i_flags |= cpu_to_le32(OCFS2_ORPHANED_FL);
2087         OCFS2_I(inode)->ip_flags &= ~OCFS2_INODE_SKIP_ORPHAN_DIR;
2088
2089         /* Record which orphan dir our inode now resides
2090          * in. delete_inode will use this to determine which orphan
2091          * dir to lock. */
2092         fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
2093
2094         ocfs2_journal_dirty(handle, fe_bh);
2095
2096         trace_ocfs2_orphan_add_end((unsigned long long)OCFS2_I(inode)->ip_blkno,
2097                                    osb->slot_num);
2098
2099 rollback:
2100         if (status < 0) {
2101                 if (S_ISDIR(inode->i_mode))
2102                         ocfs2_add_links_count(orphan_fe, -1);
2103                 set_nlink(orphan_dir_inode, ocfs2_read_links_count(orphan_fe));
2104         }
2105
2106 leave:
2107         brelse(orphan_dir_bh);
2108
2109         return status;
2110 }
2111
2112 /* unlike orphan_add, we expect the orphan dir to already be locked here. */
2113 int ocfs2_orphan_del(struct ocfs2_super *osb,
2114                      handle_t *handle,
2115                      struct inode *orphan_dir_inode,
2116                      struct inode *inode,
2117                      struct buffer_head *orphan_dir_bh)
2118 {
2119         char name[OCFS2_ORPHAN_NAMELEN + 1];
2120         struct ocfs2_dinode *orphan_fe;
2121         int status = 0;
2122         struct ocfs2_dir_lookup_result lookup = { NULL, };
2123
2124         status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2125         if (status < 0) {
2126                 mlog_errno(status);
2127                 goto leave;
2128         }
2129
2130         trace_ocfs2_orphan_del(
2131              (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
2132              name, OCFS2_ORPHAN_NAMELEN);
2133
2134         /* find it's spot in the orphan directory */
2135         status = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN, orphan_dir_inode,
2136                                   &lookup);
2137         if (status) {
2138                 mlog_errno(status);
2139                 goto leave;
2140         }
2141
2142         /* remove it from the orphan directory */
2143         status = ocfs2_delete_entry(handle, orphan_dir_inode, &lookup);
2144         if (status < 0) {
2145                 mlog_errno(status);
2146                 goto leave;
2147         }
2148
2149         status = ocfs2_journal_access_di(handle,
2150                                          INODE_CACHE(orphan_dir_inode),
2151                                          orphan_dir_bh,
2152                                          OCFS2_JOURNAL_ACCESS_WRITE);
2153         if (status < 0) {
2154                 mlog_errno(status);
2155                 goto leave;
2156         }
2157
2158         /* do the i_nlink dance! :) */
2159         orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2160         if (S_ISDIR(inode->i_mode))
2161                 ocfs2_add_links_count(orphan_fe, -1);
2162         set_nlink(orphan_dir_inode, ocfs2_read_links_count(orphan_fe));
2163         ocfs2_journal_dirty(handle, orphan_dir_bh);
2164
2165 leave:
2166         ocfs2_free_dir_lookup_result(&lookup);
2167
2168         if (status)
2169                 mlog_errno(status);
2170         return status;
2171 }
2172
2173 /**
2174  * ocfs2_prep_new_orphaned_file() - Prepare the orphan dir to receive a newly
2175  * allocated file. This is different from the typical 'add to orphan dir'
2176  * operation in that the inode does not yet exist. This is a problem because
2177  * the orphan dir stringifies the inode block number to come up with it's
2178  * dirent. Obviously if the inode does not yet exist we have a chicken and egg
2179  * problem. This function works around it by calling deeper into the orphan
2180  * and suballoc code than other callers. Use this only by necessity.
2181  * @dir: The directory which this inode will ultimately wind up under - not the
2182  * orphan dir!
2183  * @dir_bh: buffer_head the @dir inode block
2184  * @orphan_name: string of length (CFS2_ORPHAN_NAMELEN + 1). Will be filled
2185  * with the string to be used for orphan dirent. Pass back to the orphan dir
2186  * code.
2187  * @ret_orphan_dir: orphan dir inode returned to be passed back into orphan
2188  * dir code.
2189  * @ret_di_blkno: block number where the new inode will be allocated.
2190  * @orphan_insert: Dir insert context to be passed back into orphan dir code.
2191  * @ret_inode_ac: Inode alloc context to be passed back to the allocator.
2192  *
2193  * Returns zero on success and the ret_orphan_dir, name and lookup
2194  * fields will be populated.
2195  *
2196  * Returns non-zero on failure. 
2197  */
2198 static int ocfs2_prep_new_orphaned_file(struct inode *dir,
2199                                         struct buffer_head *dir_bh,
2200                                         char *orphan_name,
2201                                         struct inode **ret_orphan_dir,
2202                                         u64 *ret_di_blkno,
2203                                         struct ocfs2_dir_lookup_result *orphan_insert,
2204                                         struct ocfs2_alloc_context **ret_inode_ac)
2205 {
2206         int ret;
2207         u64 di_blkno;
2208         struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2209         struct inode *orphan_dir = NULL;
2210         struct buffer_head *orphan_dir_bh = NULL;
2211         struct ocfs2_alloc_context *inode_ac = NULL;
2212
2213         ret = ocfs2_lookup_lock_orphan_dir(osb, &orphan_dir, &orphan_dir_bh);
2214         if (ret < 0) {
2215                 mlog_errno(ret);
2216                 return ret;
2217         }
2218
2219         /* reserve an inode spot */
2220         ret = ocfs2_reserve_new_inode(osb, &inode_ac);
2221         if (ret < 0) {
2222                 if (ret != -ENOSPC)
2223                         mlog_errno(ret);
2224                 goto out;
2225         }
2226
2227         ret = ocfs2_find_new_inode_loc(dir, dir_bh, inode_ac,
2228                                        &di_blkno);
2229         if (ret) {
2230                 mlog_errno(ret);
2231                 goto out;
2232         }
2233
2234         ret = __ocfs2_prepare_orphan_dir(orphan_dir, orphan_dir_bh,
2235                                          di_blkno, orphan_name, orphan_insert);
2236         if (ret < 0) {
2237                 mlog_errno(ret);
2238                 goto out;
2239         }
2240
2241 out:
2242         if (ret == 0) {
2243                 *ret_orphan_dir = orphan_dir;
2244                 *ret_di_blkno = di_blkno;
2245                 *ret_inode_ac = inode_ac;
2246                 /*
2247                  * orphan_name and orphan_insert are already up to
2248                  * date via prepare_orphan_dir
2249                  */
2250         } else {
2251                 /* Unroll reserve_new_inode* */
2252                 if (inode_ac)
2253                         ocfs2_free_alloc_context(inode_ac);
2254
2255                 /* Unroll orphan dir locking */
2256                 mutex_unlock(&orphan_dir->i_mutex);
2257                 ocfs2_inode_unlock(orphan_dir, 1);
2258                 iput(orphan_dir);
2259         }
2260
2261         brelse(orphan_dir_bh);
2262
2263         return ret;
2264 }
2265
2266 int ocfs2_create_inode_in_orphan(struct inode *dir,
2267                                  int mode,
2268                                  struct inode **new_inode)
2269 {
2270         int status, did_quota_inode = 0;
2271         struct inode *inode = NULL;
2272         struct inode *orphan_dir = NULL;
2273         struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2274         struct ocfs2_dinode *di = NULL;
2275         handle_t *handle = NULL;
2276         char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
2277         struct buffer_head *parent_di_bh = NULL;
2278         struct buffer_head *new_di_bh = NULL;
2279         struct ocfs2_alloc_context *inode_ac = NULL;
2280         struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
2281         u64 uninitialized_var(di_blkno), suballoc_loc;
2282         u16 suballoc_bit;
2283
2284         status = ocfs2_inode_lock(dir, &parent_di_bh, 1);
2285         if (status < 0) {
2286                 if (status != -ENOENT)
2287                         mlog_errno(status);
2288                 return status;
2289         }
2290
2291         status = ocfs2_prep_new_orphaned_file(dir, parent_di_bh,
2292                                               orphan_name, &orphan_dir,
2293                                               &di_blkno, &orphan_insert, &inode_ac);
2294         if (status < 0) {
2295                 if (status != -ENOSPC)
2296                         mlog_errno(status);
2297                 goto leave;
2298         }
2299
2300         inode = ocfs2_get_init_inode(dir, mode);
2301         if (!inode) {
2302                 status = -ENOMEM;
2303                 mlog_errno(status);
2304                 goto leave;
2305         }
2306
2307         handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb, 0, 0));
2308         if (IS_ERR(handle)) {
2309                 status = PTR_ERR(handle);
2310                 handle = NULL;
2311                 mlog_errno(status);
2312                 goto leave;
2313         }
2314
2315         status = dquot_alloc_inode(inode);
2316         if (status)
2317                 goto leave;
2318         did_quota_inode = 1;
2319
2320         status = ocfs2_claim_new_inode_at_loc(handle, dir, inode_ac,
2321                                               &suballoc_loc,
2322                                               &suballoc_bit, di_blkno);
2323         if (status < 0) {
2324                 mlog_errno(status);
2325                 goto leave;
2326         }
2327
2328         clear_nlink(inode);
2329         /* do the real work now. */
2330         status = __ocfs2_mknod_locked(dir, inode,
2331                                       0, &new_di_bh, parent_di_bh, handle,
2332                                       inode_ac, di_blkno, suballoc_loc,
2333                                       suballoc_bit);
2334         if (status < 0) {
2335                 mlog_errno(status);
2336                 goto leave;
2337         }
2338
2339         di = (struct ocfs2_dinode *)new_di_bh->b_data;
2340         status = ocfs2_orphan_add(osb, handle, inode, new_di_bh, orphan_name,
2341                                   &orphan_insert, orphan_dir);
2342         if (status < 0) {
2343                 mlog_errno(status);
2344                 goto leave;
2345         }
2346
2347         /* get open lock so that only nodes can't remove it from orphan dir. */
2348         status = ocfs2_open_lock(inode);
2349         if (status < 0)
2350                 mlog_errno(status);
2351
2352         insert_inode_hash(inode);
2353 leave:
2354         if (status < 0 && did_quota_inode)
2355                 dquot_free_inode(inode);
2356         if (handle)
2357                 ocfs2_commit_trans(osb, handle);
2358
2359         if (orphan_dir) {
2360                 /* This was locked for us in ocfs2_prepare_orphan_dir() */
2361                 ocfs2_inode_unlock(orphan_dir, 1);
2362                 mutex_unlock(&orphan_dir->i_mutex);
2363                 iput(orphan_dir);
2364         }
2365
2366         if ((status < 0) && inode) {
2367                 clear_nlink(inode);
2368                 iput(inode);
2369         }
2370
2371         if (inode_ac)
2372                 ocfs2_free_alloc_context(inode_ac);
2373
2374         brelse(new_di_bh);
2375
2376         if (!status)
2377                 *new_inode = inode;
2378
2379         ocfs2_free_dir_lookup_result(&orphan_insert);
2380
2381         ocfs2_inode_unlock(dir, 1);
2382         brelse(parent_di_bh);
2383         return status;
2384 }
2385
2386 int ocfs2_mv_orphaned_inode_to_new(struct inode *dir,
2387                                    struct inode *inode,
2388                                    struct dentry *dentry)
2389 {
2390         int status = 0;
2391         struct buffer_head *parent_di_bh = NULL;
2392         handle_t *handle = NULL;
2393         struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2394         struct ocfs2_dinode *dir_di, *di;
2395         struct inode *orphan_dir_inode = NULL;
2396         struct buffer_head *orphan_dir_bh = NULL;
2397         struct buffer_head *di_bh = NULL;
2398         struct ocfs2_dir_lookup_result lookup = { NULL, };
2399
2400         trace_ocfs2_mv_orphaned_inode_to_new(dir, dentry,
2401                                 dentry->d_name.len, dentry->d_name.name,
2402                                 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2403                                 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2404
2405         status = ocfs2_inode_lock(dir, &parent_di_bh, 1);
2406         if (status < 0) {
2407                 if (status != -ENOENT)
2408                         mlog_errno(status);
2409                 return status;
2410         }
2411
2412         dir_di = (struct ocfs2_dinode *) parent_di_bh->b_data;
2413         if (!dir_di->i_links_count) {
2414                 /* can't make a file in a deleted directory. */
2415                 status = -ENOENT;
2416                 goto leave;
2417         }
2418
2419         status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
2420                                            dentry->d_name.len);
2421         if (status)
2422                 goto leave;
2423
2424         /* get a spot inside the dir. */
2425         status = ocfs2_prepare_dir_for_insert(osb, dir, parent_di_bh,
2426                                               dentry->d_name.name,
2427                                               dentry->d_name.len, &lookup);
2428         if (status < 0) {
2429                 mlog_errno(status);
2430                 goto leave;
2431         }
2432
2433         orphan_dir_inode = ocfs2_get_system_file_inode(osb,
2434                                                        ORPHAN_DIR_SYSTEM_INODE,
2435                                                        osb->slot_num);
2436         if (!orphan_dir_inode) {
2437                 status = -EEXIST;
2438                 mlog_errno(status);
2439                 goto leave;
2440         }
2441
2442         mutex_lock(&orphan_dir_inode->i_mutex);
2443
2444         status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
2445         if (status < 0) {
2446                 mlog_errno(status);
2447                 mutex_unlock(&orphan_dir_inode->i_mutex);
2448                 iput(orphan_dir_inode);
2449                 goto leave;
2450         }
2451
2452         status = ocfs2_read_inode_block(inode, &di_bh);
2453         if (status < 0) {
2454                 mlog_errno(status);
2455                 goto orphan_unlock;
2456         }
2457
2458         handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb));
2459         if (IS_ERR(handle)) {
2460                 status = PTR_ERR(handle);
2461                 handle = NULL;
2462                 mlog_errno(status);
2463                 goto orphan_unlock;
2464         }
2465
2466         status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
2467                                          di_bh, OCFS2_JOURNAL_ACCESS_WRITE);
2468         if (status < 0) {
2469                 mlog_errno(status);
2470                 goto out_commit;
2471         }
2472
2473         status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
2474                                   orphan_dir_bh);
2475         if (status < 0) {
2476                 mlog_errno(status);
2477                 goto out_commit;
2478         }
2479
2480         di = (struct ocfs2_dinode *)di_bh->b_data;
2481         di->i_flags &= ~cpu_to_le32(OCFS2_ORPHANED_FL);
2482         di->i_orphaned_slot = 0;
2483         set_nlink(inode, 1);
2484         ocfs2_set_links_count(di, inode->i_nlink);
2485         ocfs2_journal_dirty(handle, di_bh);
2486
2487         status = ocfs2_add_entry(handle, dentry, inode,
2488                                  OCFS2_I(inode)->ip_blkno, parent_di_bh,
2489                                  &lookup);
2490         if (status < 0) {
2491                 mlog_errno(status);
2492                 goto out_commit;
2493         }
2494
2495         status = ocfs2_dentry_attach_lock(dentry, inode,
2496                                           OCFS2_I(dir)->ip_blkno);
2497         if (status) {
2498                 mlog_errno(status);
2499                 goto out_commit;
2500         }
2501
2502         d_instantiate(dentry, inode);
2503         status = 0;
2504 out_commit:
2505         ocfs2_commit_trans(osb, handle);
2506 orphan_unlock:
2507         ocfs2_inode_unlock(orphan_dir_inode, 1);
2508         mutex_unlock(&orphan_dir_inode->i_mutex);
2509         iput(orphan_dir_inode);
2510 leave:
2511
2512         ocfs2_inode_unlock(dir, 1);
2513
2514         brelse(di_bh);
2515         brelse(parent_di_bh);
2516         brelse(orphan_dir_bh);
2517
2518         ocfs2_free_dir_lookup_result(&lookup);
2519
2520         if (status)
2521                 mlog_errno(status);
2522
2523         return status;
2524 }
2525
2526 const struct inode_operations ocfs2_dir_iops = {
2527         .create         = ocfs2_create,
2528         .lookup         = ocfs2_lookup,
2529         .link           = ocfs2_link,
2530         .unlink         = ocfs2_unlink,
2531         .rmdir          = ocfs2_unlink,
2532         .symlink        = ocfs2_symlink,
2533         .mkdir          = ocfs2_mkdir,
2534         .mknod          = ocfs2_mknod,
2535         .rename         = ocfs2_rename,
2536         .setattr        = ocfs2_setattr,
2537         .getattr        = ocfs2_getattr,
2538         .permission     = ocfs2_permission,
2539         .setxattr       = generic_setxattr,
2540         .getxattr       = generic_getxattr,
2541         .listxattr      = ocfs2_listxattr,
2542         .removexattr    = generic_removexattr,
2543         .fiemap         = ocfs2_fiemap,
2544         .get_acl        = ocfs2_iop_get_acl,
2545         .set_acl        = ocfs2_iop_set_acl,
2546 };