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

source code of linux/fs/ocfs2/namei.c