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