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