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