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