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