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