ocfs2: Remove mlog(0) from fs/ocfs2/dir.c
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / ocfs2 / namei.c
CommitLineData
ccd979bd
MF
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * namei.c
5 *
6 * Create and rename file, directory, symlinks
7 *
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
9 *
10 * Portions of this code from linux/fs/ext3/dir.c
11 *
12 * Copyright (C) 1992, 1993, 1994, 1995
13 * Remy Card (card@masi.ibp.fr)
14 * Laboratoire MASI - Institut Blaise pascal
15 * Universite Pierre et Marie Curie (Paris VI)
16 *
17 * from
18 *
19 * linux/fs/minix/dir.c
20 *
21 * Copyright (C) 1991, 1992 Linux Torvalds
22 *
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public
25 * License as published by the Free Software Foundation; either
26 * version 2 of the License, or (at your option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 * General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public
34 * License along with this program; if not, write to the
35 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
36 * Boston, MA 021110-1307, USA.
37 */
38
39#include <linux/fs.h>
40#include <linux/types.h>
41#include <linux/slab.h>
42#include <linux/highmem.h>
a90714c1 43#include <linux/quotaops.h>
ccd979bd
MF
44
45#define MLOG_MASK_PREFIX ML_NAMEI
46#include <cluster/masklog.h>
47
48#include "ocfs2.h"
49
50#include "alloc.h"
51#include "dcache.h"
52#include "dir.h"
53#include "dlmglue.h"
54#include "extent_map.h"
55#include "file.h"
56#include "inode.h"
57#include "journal.h"
58#include "namei.h"
59#include "suballoc.h"
aa958874 60#include "super.h"
ccd979bd
MF
61#include "symlink.h"
62#include "sysfile.h"
63#include "uptodate.h"
cf1d6c76 64#include "xattr.h"
89c38bd0 65#include "acl.h"
ccd979bd
MF
66
67#include "buffer_head_io.h"
68
ccd979bd
MF
69static int ocfs2_mknod_locked(struct ocfs2_super *osb,
70 struct inode *dir,
f5d36202 71 struct inode *inode,
ccd979bd
MF
72 dev_t dev,
73 struct buffer_head **new_fe_bh,
74 struct buffer_head *parent_fe_bh,
1fabe148 75 handle_t *handle,
ccd979bd
MF
76 struct ocfs2_alloc_context *inode_ac);
77
ccd979bd 78static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
5098c27b 79 struct inode **ret_orphan_dir,
19bd341f 80 u64 blkno,
ccd979bd 81 char *name,
4a12ca3a 82 struct ocfs2_dir_lookup_result *lookup);
ccd979bd
MF
83
84static int ocfs2_orphan_add(struct ocfs2_super *osb,
1fabe148 85 handle_t *handle,
ccd979bd 86 struct inode *inode,
3939fda4 87 struct buffer_head *fe_bh,
ccd979bd 88 char *name,
4a12ca3a 89 struct ocfs2_dir_lookup_result *lookup,
5098c27b 90 struct inode *orphan_dir_inode);
ccd979bd
MF
91
92static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
1fabe148 93 handle_t *handle,
ccd979bd
MF
94 struct inode *inode,
95 const char *symname);
96
ccd979bd
MF
97/* An orphan dir name is an 8 byte value, printed as a hex string */
98#define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
99
100static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
101 struct nameidata *nd)
102{
103 int status;
104 u64 blkno;
ccd979bd
MF
105 struct inode *inode = NULL;
106 struct dentry *ret;
ccd979bd
MF
107 struct ocfs2_inode_info *oi;
108
ef6b689b
TM
109 mlog(0, "(0x%p, 0x%p, '%.*s')\n", dir, dentry,
110 dentry->d_name.len, dentry->d_name.name);
ccd979bd
MF
111
112 if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
113 ret = ERR_PTR(-ENAMETOOLONG);
114 goto bail;
115 }
116
b0697053
MF
117 mlog(0, "find name %.*s in directory %llu\n", dentry->d_name.len,
118 dentry->d_name.name, (unsigned long long)OCFS2_I(dir)->ip_blkno);
ccd979bd 119
cb25797d 120 status = ocfs2_inode_lock_nested(dir, NULL, 0, OI_LS_PARENT);
ccd979bd
MF
121 if (status < 0) {
122 if (status != -ENOENT)
123 mlog_errno(status);
124 ret = ERR_PTR(status);
125 goto bail;
126 }
127
be94d117
MF
128 status = ocfs2_lookup_ino_from_name(dir, dentry->d_name.name,
129 dentry->d_name.len, &blkno);
ccd979bd
MF
130 if (status < 0)
131 goto bail_add;
132
5fa0613e 133 inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0);
ccd979bd 134 if (IS_ERR(inode)) {
ccd979bd
MF
135 ret = ERR_PTR(-EACCES);
136 goto bail_unlock;
137 }
138
139 oi = OCFS2_I(inode);
140 /* Clear any orphaned state... If we were able to look up the
141 * inode from a directory, it certainly can't be orphaned. We
142 * might have the bad state from a node which intended to
143 * orphan this inode but crashed before it could commit the
144 * unlink. */
145 spin_lock(&oi->ip_lock);
146 oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
ccd979bd
MF
147 spin_unlock(&oi->ip_lock);
148
149bail_add:
ccd979bd
MF
150 ret = d_splice_alias(inode, dentry);
151
379dfe9d
MF
152 if (inode) {
153 /*
154 * If d_splice_alias() finds a DCACHE_DISCONNECTED
155 * dentry, it will d_move() it on top of ourse. The
156 * return value will indicate this however, so in
157 * those cases, we switch them around for the locking
158 * code.
159 *
160 * NOTE: This dentry already has ->d_op set from
161 * ocfs2_get_parent() and ocfs2_get_dentry()
162 */
163 if (ret)
164 dentry = ret;
165
166 status = ocfs2_dentry_attach_lock(dentry, inode,
0027dd5b 167 OCFS2_I(dir)->ip_blkno);
379dfe9d
MF
168 if (status) {
169 mlog_errno(status);
170 ret = ERR_PTR(status);
171 goto bail_unlock;
172 }
5e98d492
GR
173 } else
174 ocfs2_dentry_attach_gen(dentry);
379dfe9d 175
ccd979bd
MF
176bail_unlock:
177 /* Don't drop the cluster lock until *after* the d_add --
178 * unlink on another node will message us to remove that
179 * dentry under this lock so otherwise we can race this with
34d024f8 180 * the downconvert thread and have a stale dentry. */
e63aecb6 181 ocfs2_inode_unlock(dir, 0);
ccd979bd
MF
182
183bail:
ccd979bd 184
c1e8d35e 185 mlog(0, "%p\n", ret);
ccd979bd
MF
186
187 return ret;
188}
189
f5d36202
TY
190static struct inode *ocfs2_get_init_inode(struct inode *dir, int mode)
191{
192 struct inode *inode;
193
194 inode = new_inode(dir->i_sb);
195 if (!inode) {
196 mlog(ML_ERROR, "new_inode failed!\n");
197 return NULL;
198 }
199
200 /* populate as many fields early on as possible - many of
201 * these are used by the support functions here and in
202 * callers. */
203 if (S_ISDIR(mode))
204 inode->i_nlink = 2;
205 else
206 inode->i_nlink = 1;
75fe0a24 207 inode_init_owner(inode, dir, mode);
871a2931 208 dquot_initialize(inode);
f5d36202
TY
209 return inode;
210}
211
ccd979bd
MF
212static int ocfs2_mknod(struct inode *dir,
213 struct dentry *dentry,
214 int mode,
215 dev_t dev)
216{
217 int status = 0;
218 struct buffer_head *parent_fe_bh = NULL;
1fabe148 219 handle_t *handle = NULL;
ccd979bd
MF
220 struct ocfs2_super *osb;
221 struct ocfs2_dinode *dirfe;
222 struct buffer_head *new_fe_bh = NULL;
ccd979bd
MF
223 struct inode *inode = NULL;
224 struct ocfs2_alloc_context *inode_ac = NULL;
225 struct ocfs2_alloc_context *data_ac = NULL;
9b7895ef 226 struct ocfs2_alloc_context *meta_ac = NULL;
534eaddd 227 int want_clusters = 0;
9b7895ef 228 int want_meta = 0;
534eaddd
TY
229 int xattr_credits = 0;
230 struct ocfs2_security_xattr_info si = {
231 .enable = 1,
232 };
a90714c1 233 int did_quota_inode = 0;
4a12ca3a 234 struct ocfs2_dir_lookup_result lookup = { NULL, };
547ba7c8
JB
235 sigset_t oldset;
236 int did_block_signals = 0;
ccd979bd 237
ef6b689b
TM
238 mlog(0, "(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
239 (unsigned long)dev, dentry->d_name.len,
240 dentry->d_name.name);
ccd979bd 241
871a2931 242 dquot_initialize(dir);
907f4554 243
ccd979bd
MF
244 /* get our super block */
245 osb = OCFS2_SB(dir->i_sb);
246
e63aecb6 247 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
e3a82138
MF
248 if (status < 0) {
249 if (status != -ENOENT)
250 mlog_errno(status);
251 return status;
252 }
253
198a1ca3 254 if (S_ISDIR(mode) && (dir->i_nlink >= ocfs2_link_max(osb))) {
a663e305
MF
255 status = -EMLINK;
256 goto leave;
257 }
258
ccd979bd 259 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
198a1ca3 260 if (!ocfs2_read_links_count(dirfe)) {
ccd979bd
MF
261 /* can't make a file in a deleted directory. */
262 status = -ENOENT;
263 goto leave;
264 }
265
266 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
267 dentry->d_name.len);
268 if (status)
269 goto leave;
270
271 /* get a spot inside the dir. */
272 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
273 dentry->d_name.name,
4a12ca3a 274 dentry->d_name.len, &lookup);
ccd979bd
MF
275 if (status < 0) {
276 mlog_errno(status);
277 goto leave;
278 }
279
280 /* reserve an inode spot */
da5cbf2f 281 status = ocfs2_reserve_new_inode(osb, &inode_ac);
ccd979bd
MF
282 if (status < 0) {
283 if (status != -ENOSPC)
284 mlog_errno(status);
285 goto leave;
286 }
287
f5d36202
TY
288 inode = ocfs2_get_init_inode(dir, mode);
289 if (!inode) {
290 status = -ENOMEM;
291 mlog_errno(status);
292 goto leave;
293 }
294
534eaddd
TY
295 /* get security xattr */
296 status = ocfs2_init_security_get(inode, dir, &si);
297 if (status) {
298 if (status == -EOPNOTSUPP)
299 si.enable = 0;
300 else {
301 mlog_errno(status);
302 goto leave;
303 }
304 }
305
89c38bd0
TY
306 /* calculate meta data/clusters for setting security and acl xattr */
307 status = ocfs2_calc_xattr_init(dir, parent_fe_bh, mode,
9b7895ef
MF
308 &si, &want_clusters,
309 &xattr_credits, &want_meta);
89c38bd0
TY
310 if (status < 0) {
311 mlog_errno(status);
312 goto leave;
ccd979bd
MF
313 }
314
534eaddd 315 /* Reserve a cluster if creating an extent based directory. */
9b7895ef 316 if (S_ISDIR(mode) && !ocfs2_supports_inline_data(osb)) {
534eaddd
TY
317 want_clusters += 1;
318
9b7895ef 319 /* Dir indexing requires extra space as well */
4ed8a6bb 320 if (ocfs2_supports_indexed_dirs(osb))
9b7895ef 321 want_meta++;
9b7895ef
MF
322 }
323
324 status = ocfs2_reserve_new_metadata_blocks(osb, want_meta, &meta_ac);
325 if (status < 0) {
326 if (status != -ENOSPC)
327 mlog_errno(status);
328 goto leave;
329 }
330
534eaddd
TY
331 status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
332 if (status < 0) {
333 if (status != -ENOSPC)
334 mlog_errno(status);
335 goto leave;
336 }
337
9b7895ef
MF
338 handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb,
339 S_ISDIR(mode),
340 xattr_credits));
ccd979bd
MF
341 if (IS_ERR(handle)) {
342 status = PTR_ERR(handle);
343 handle = NULL;
344 mlog_errno(status);
345 goto leave;
346 }
347
547ba7c8
JB
348 /* Starting to change things, restart is no longer possible. */
349 ocfs2_block_signals(&oldset);
350 did_block_signals = 1;
351
63936dda
CH
352 status = dquot_alloc_inode(inode);
353 if (status)
a90714c1 354 goto leave;
a90714c1
JK
355 did_quota_inode = 1;
356
ef6b689b
TM
357 mlog(0, "(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry,
358 inode->i_mode, (unsigned long)dev, dentry->d_name.len,
359 dentry->d_name.name);
19bd341f 360
ccd979bd 361 /* do the real work now. */
19bd341f 362 status = ocfs2_mknod_locked(osb, dir, inode, dev,
ccd979bd 363 &new_fe_bh, parent_fe_bh, handle,
f5d36202 364 inode_ac);
ccd979bd
MF
365 if (status < 0) {
366 mlog_errno(status);
367 goto leave;
368 }
369
370 if (S_ISDIR(mode)) {
371 status = ocfs2_fill_new_dir(osb, handle, dir, inode,
9b7895ef 372 new_fe_bh, data_ac, meta_ac);
ccd979bd
MF
373 if (status < 0) {
374 mlog_errno(status);
375 goto leave;
376 }
377
0cf2f763
JB
378 status = ocfs2_journal_access_di(handle, INODE_CACHE(dir),
379 parent_fe_bh,
13723d00 380 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
381 if (status < 0) {
382 mlog_errno(status);
383 goto leave;
384 }
198a1ca3 385 ocfs2_add_links_count(dirfe, 1);
ec20cec7 386 ocfs2_journal_dirty(handle, parent_fe_bh);
d8c76e6f 387 inc_nlink(dir);
ccd979bd
MF
388 }
389
89c38bd0 390 status = ocfs2_init_acl(handle, inode, dir, new_fe_bh, parent_fe_bh,
9b7895ef 391 meta_ac, data_ac);
89c38bd0
TY
392 if (status < 0) {
393 mlog_errno(status);
394 goto leave;
395 }
396
534eaddd
TY
397 if (si.enable) {
398 status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
9b7895ef 399 meta_ac, data_ac);
534eaddd
TY
400 if (status < 0) {
401 mlog_errno(status);
402 goto leave;
403 }
404 }
405
a9743fcd
MF
406 /*
407 * Do this before adding the entry to the directory. We add
408 * also set d_op after success so that ->d_iput() will cleanup
409 * the dentry lock even if ocfs2_add_entry() fails below.
410 */
411 status = ocfs2_dentry_attach_lock(dentry, inode,
412 OCFS2_I(dir)->ip_blkno);
413 if (status) {
ccd979bd
MF
414 mlog_errno(status);
415 goto leave;
416 }
417
a9743fcd
MF
418 status = ocfs2_add_entry(handle, dentry, inode,
419 OCFS2_I(inode)->ip_blkno, parent_fe_bh,
420 &lookup);
421 if (status < 0) {
379dfe9d
MF
422 mlog_errno(status);
423 goto leave;
424 }
425
ccd979bd 426 insert_inode_hash(inode);
ccd979bd
MF
427 d_instantiate(dentry, inode);
428 status = 0;
429leave:
a90714c1 430 if (status < 0 && did_quota_inode)
63936dda 431 dquot_free_inode(inode);
ccd979bd 432 if (handle)
02dc1af4 433 ocfs2_commit_trans(osb, handle);
ccd979bd 434
e63aecb6 435 ocfs2_inode_unlock(dir, 1);
547ba7c8
JB
436 if (did_block_signals)
437 ocfs2_unblock_signals(&oldset);
e3a82138 438
ccd979bd
MF
439 if (status == -ENOSPC)
440 mlog(0, "Disk is full\n");
441
a81cb88b 442 brelse(new_fe_bh);
a81cb88b 443 brelse(parent_fe_bh);
534eaddd
TY
444 kfree(si.name);
445 kfree(si.value);
ccd979bd 446
4a12ca3a
MF
447 ocfs2_free_dir_lookup_result(&lookup);
448
ccd979bd
MF
449 if (inode_ac)
450 ocfs2_free_alloc_context(inode_ac);
451
452 if (data_ac)
453 ocfs2_free_alloc_context(data_ac);
454
9b7895ef
MF
455 if (meta_ac)
456 ocfs2_free_alloc_context(meta_ac);
534eaddd 457
062d3403
LD
458 /*
459 * We should call iput after the i_mutex of the bitmap been
460 * unlocked in ocfs2_free_alloc_context, or the
461 * ocfs2_delete_inode will mutex_lock again.
462 */
463 if ((status < 0) && inode) {
464 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SKIP_ORPHAN_DIR;
465 clear_nlink(inode);
466 iput(inode);
467 }
468
c1e8d35e
TM
469 if (status)
470 mlog_errno(status);
ccd979bd
MF
471
472 return status;
473}
474
021960ca
MF
475static int __ocfs2_mknod_locked(struct inode *dir,
476 struct inode *inode,
477 dev_t dev,
478 struct buffer_head **new_fe_bh,
479 struct buffer_head *parent_fe_bh,
480 handle_t *handle,
481 struct ocfs2_alloc_context *inode_ac,
482 u64 fe_blkno, u64 suballoc_loc, u16 suballoc_bit)
ccd979bd
MF
483{
484 int status = 0;
021960ca 485 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
ccd979bd
MF
486 struct ocfs2_dinode *fe = NULL;
487 struct ocfs2_extent_list *fel;
9b7895ef 488 u16 feat;
ccd979bd 489
ccd979bd 490 *new_fe_bh = NULL;
ccd979bd 491
ccd979bd
MF
492 /* populate as many fields early on as possible - many of
493 * these are used by the support functions here and in
494 * callers. */
495 inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
496 OCFS2_I(inode)->ip_blkno = fe_blkno;
ccd979bd
MF
497 spin_lock(&osb->osb_lock);
498 inode->i_generation = osb->s_next_generation++;
499 spin_unlock(&osb->osb_lock);
500
501 *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
502 if (!*new_fe_bh) {
503 status = -EIO;
504 mlog_errno(status);
505 goto leave;
506 }
8cb471e8 507 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), *new_fe_bh);
ccd979bd 508
0cf2f763
JB
509 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
510 *new_fe_bh,
13723d00 511 OCFS2_JOURNAL_ACCESS_CREATE);
ccd979bd
MF
512 if (status < 0) {
513 mlog_errno(status);
514 goto leave;
515 }
516
517 fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
518 memset(fe, 0, osb->sb->s_blocksize);
519
520 fe->i_generation = cpu_to_le32(inode->i_generation);
521 fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
522 fe->i_blkno = cpu_to_le64(fe_blkno);
2b6cb576 523 fe->i_suballoc_loc = cpu_to_le64(suballoc_loc);
ccd979bd 524 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
4d0ddb2c 525 fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot);
f5d36202
TY
526 fe->i_uid = cpu_to_le32(inode->i_uid);
527 fe->i_gid = cpu_to_le32(inode->i_gid);
528 fe->i_mode = cpu_to_le16(inode->i_mode);
529 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
ccd979bd 530 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
198a1ca3
MF
531
532 ocfs2_set_links_count(fe, inode->i_nlink);
ccd979bd
MF
533
534 fe->i_last_eb_blk = 0;
535 strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
536 le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
537 fe->i_atime = fe->i_ctime = fe->i_mtime =
538 cpu_to_le64(CURRENT_TIME.tv_sec);
539 fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
540 cpu_to_le32(CURRENT_TIME.tv_nsec);
541 fe->i_dtime = 0;
542
5b6a3a2b 543 /*
9b7895ef
MF
544 * If supported, directories start with inline data. If inline
545 * isn't supported, but indexing is, we start them as indexed.
5b6a3a2b 546 */
9b7895ef 547 feat = le16_to_cpu(fe->i_dyn_features);
f5d36202 548 if (S_ISDIR(inode->i_mode) && ocfs2_supports_inline_data(osb)) {
5b6a3a2b
MF
549 fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL);
550
d9ae49d6
TY
551 fe->id2.i_data.id_count = cpu_to_le16(
552 ocfs2_max_inline_data_with_xattr(osb->sb, fe));
5b6a3a2b
MF
553 } else {
554 fel = &fe->id2.i_list;
555 fel->l_tree_depth = 0;
556 fel->l_next_free_rec = 0;
557 fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
558 }
ccd979bd 559
ec20cec7 560 ocfs2_journal_dirty(handle, *new_fe_bh);
ccd979bd 561
b657c95c 562 ocfs2_populate_inode(inode, fe, 1);
0cf2f763 563 ocfs2_ci_set_new(osb, INODE_CACHE(inode));
c271c5c2
SM
564 if (!ocfs2_mount_local(osb)) {
565 status = ocfs2_create_new_inode_locks(inode);
566 if (status < 0)
567 mlog_errno(status);
568 }
ccd979bd
MF
569
570 status = 0; /* error in ocfs2_create_new_inode_locks is not
571 * critical */
572
ccd979bd
MF
573leave:
574 if (status < 0) {
575 if (*new_fe_bh) {
576 brelse(*new_fe_bh);
577 *new_fe_bh = NULL;
578 }
ccd979bd
MF
579 }
580
c1e8d35e
TM
581 if (status)
582 mlog_errno(status);
ccd979bd
MF
583 return status;
584}
585
021960ca
MF
586static int ocfs2_mknod_locked(struct ocfs2_super *osb,
587 struct inode *dir,
588 struct inode *inode,
589 dev_t dev,
590 struct buffer_head **new_fe_bh,
591 struct buffer_head *parent_fe_bh,
592 handle_t *handle,
593 struct ocfs2_alloc_context *inode_ac)
594{
595 int status = 0;
596 u64 suballoc_loc, fe_blkno = 0;
597 u16 suballoc_bit;
598
599 *new_fe_bh = NULL;
600
601 status = ocfs2_claim_new_inode(handle, dir, parent_fe_bh,
602 inode_ac, &suballoc_loc,
603 &suballoc_bit, &fe_blkno);
604 if (status < 0) {
605 mlog_errno(status);
606 return status;
607 }
608
609 return __ocfs2_mknod_locked(dir, inode, dev, new_fe_bh,
610 parent_fe_bh, handle, inode_ac,
611 fe_blkno, suballoc_loc, suballoc_bit);
612}
613
ccd979bd
MF
614static int ocfs2_mkdir(struct inode *dir,
615 struct dentry *dentry,
616 int mode)
617{
618 int ret;
619
ef6b689b
TM
620 mlog(0, "(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
621 dentry->d_name.len, dentry->d_name.name);
ccd979bd 622 ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
c1e8d35e
TM
623 if (ret)
624 mlog_errno(ret);
ccd979bd
MF
625
626 return ret;
627}
628
629static int ocfs2_create(struct inode *dir,
630 struct dentry *dentry,
631 int mode,
632 struct nameidata *nd)
633{
634 int ret;
635
ef6b689b
TM
636 mlog(0, "(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
637 dentry->d_name.len, dentry->d_name.name);
ccd979bd 638 ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
c1e8d35e
TM
639 if (ret)
640 mlog_errno(ret);
ccd979bd
MF
641
642 return ret;
643}
644
645static int ocfs2_link(struct dentry *old_dentry,
646 struct inode *dir,
647 struct dentry *dentry)
648{
1fabe148 649 handle_t *handle;
ccd979bd
MF
650 struct inode *inode = old_dentry->d_inode;
651 int err;
652 struct buffer_head *fe_bh = NULL;
653 struct buffer_head *parent_fe_bh = NULL;
ccd979bd
MF
654 struct ocfs2_dinode *fe = NULL;
655 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4a12ca3a 656 struct ocfs2_dir_lookup_result lookup = { NULL, };
547ba7c8 657 sigset_t oldset;
ccd979bd 658
ef6b689b
TM
659 mlog(0, "(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
660 old_dentry->d_name.len, old_dentry->d_name.name,
661 dentry->d_name.len, dentry->d_name.name);
ccd979bd 662
123a9643
MF
663 if (S_ISDIR(inode->i_mode))
664 return -EPERM;
ccd979bd 665
871a2931 666 dquot_initialize(dir);
907f4554 667
cb25797d 668 err = ocfs2_inode_lock_nested(dir, &parent_fe_bh, 1, OI_LS_PARENT);
ccd979bd
MF
669 if (err < 0) {
670 if (err != -ENOENT)
671 mlog_errno(err);
123a9643 672 return err;
ccd979bd
MF
673 }
674
0f62de2c
TY
675 if (!dir->i_nlink) {
676 err = -ENOENT;
123a9643 677 goto out;
0f62de2c
TY
678 }
679
ccd979bd
MF
680 err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
681 dentry->d_name.len);
682 if (err)
123a9643 683 goto out;
ccd979bd
MF
684
685 err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
686 dentry->d_name.name,
4a12ca3a 687 dentry->d_name.len, &lookup);
ccd979bd
MF
688 if (err < 0) {
689 mlog_errno(err);
123a9643 690 goto out;
ccd979bd
MF
691 }
692
e63aecb6 693 err = ocfs2_inode_lock(inode, &fe_bh, 1);
ccd979bd
MF
694 if (err < 0) {
695 if (err != -ENOENT)
696 mlog_errno(err);
123a9643 697 goto out;
ccd979bd
MF
698 }
699
700 fe = (struct ocfs2_dinode *) fe_bh->b_data;
198a1ca3 701 if (ocfs2_read_links_count(fe) >= ocfs2_link_max(osb)) {
ccd979bd 702 err = -EMLINK;
123a9643 703 goto out_unlock_inode;
ccd979bd
MF
704 }
705
a90714c1 706 handle = ocfs2_start_trans(osb, ocfs2_link_credits(osb->sb));
ccd979bd
MF
707 if (IS_ERR(handle)) {
708 err = PTR_ERR(handle);
709 handle = NULL;
710 mlog_errno(err);
123a9643 711 goto out_unlock_inode;
ccd979bd
MF
712 }
713
547ba7c8
JB
714 /* Starting to change things, restart is no longer possible. */
715 ocfs2_block_signals(&oldset);
716
0cf2f763 717 err = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
13723d00 718 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
719 if (err < 0) {
720 mlog_errno(err);
123a9643 721 goto out_commit;
ccd979bd
MF
722 }
723
d8c76e6f 724 inc_nlink(inode);
ccd979bd 725 inode->i_ctime = CURRENT_TIME;
198a1ca3 726 ocfs2_set_links_count(fe, inode->i_nlink);
ccd979bd
MF
727 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
728 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
ec20cec7 729 ocfs2_journal_dirty(handle, fe_bh);
ccd979bd
MF
730
731 err = ocfs2_add_entry(handle, dentry, inode,
732 OCFS2_I(inode)->ip_blkno,
4a12ca3a 733 parent_fe_bh, &lookup);
ccd979bd 734 if (err) {
198a1ca3 735 ocfs2_add_links_count(fe, -1);
9a53c3a7 736 drop_nlink(inode);
ccd979bd 737 mlog_errno(err);
123a9643 738 goto out_commit;
ccd979bd
MF
739 }
740
0027dd5b 741 err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
379dfe9d
MF
742 if (err) {
743 mlog_errno(err);
123a9643 744 goto out_commit;
379dfe9d
MF
745 }
746
7de9c6ee 747 ihold(inode);
ccd979bd 748 d_instantiate(dentry, inode);
123a9643
MF
749
750out_commit:
02dc1af4 751 ocfs2_commit_trans(osb, handle);
547ba7c8 752 ocfs2_unblock_signals(&oldset);
123a9643 753out_unlock_inode:
e63aecb6 754 ocfs2_inode_unlock(inode, 1);
123a9643
MF
755
756out:
e63aecb6 757 ocfs2_inode_unlock(dir, 1);
123a9643 758
a81cb88b
MF
759 brelse(fe_bh);
760 brelse(parent_fe_bh);
ccd979bd 761
4a12ca3a
MF
762 ocfs2_free_dir_lookup_result(&lookup);
763
c1e8d35e
TM
764 if (err)
765 mlog_errno(err);
ccd979bd
MF
766
767 return err;
768}
769
379dfe9d
MF
770/*
771 * Takes and drops an exclusive lock on the given dentry. This will
772 * force other nodes to drop it.
773 */
774static int ocfs2_remote_dentry_delete(struct dentry *dentry)
775{
776 int ret;
777
778 ret = ocfs2_dentry_lock(dentry, 1);
779 if (ret)
780 mlog_errno(ret);
781 else
782 ocfs2_dentry_unlock(dentry, 1);
783
784 return ret;
785}
786
17ff7856
MF
787static inline int inode_is_unlinkable(struct inode *inode)
788{
789 if (S_ISDIR(inode->i_mode)) {
790 if (inode->i_nlink == 2)
791 return 1;
792 return 0;
793 }
794
795 if (inode->i_nlink == 1)
796 return 1;
797 return 0;
798}
799
ccd979bd
MF
800static int ocfs2_unlink(struct inode *dir,
801 struct dentry *dentry)
802{
803 int status;
30a4f5e8 804 int child_locked = 0;
ccd979bd 805 struct inode *inode = dentry->d_inode;
5098c27b 806 struct inode *orphan_dir = NULL;
ccd979bd
MF
807 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
808 u64 blkno;
809 struct ocfs2_dinode *fe = NULL;
810 struct buffer_head *fe_bh = NULL;
811 struct buffer_head *parent_node_bh = NULL;
1fabe148 812 handle_t *handle = NULL;
ccd979bd 813 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
4a12ca3a
MF
814 struct ocfs2_dir_lookup_result lookup = { NULL, };
815 struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
ccd979bd 816
ef6b689b
TM
817 mlog(0, "(0x%p, 0x%p, '%.*s')\n", dir, dentry,
818 dentry->d_name.len, dentry->d_name.name);
ccd979bd 819
871a2931 820 dquot_initialize(dir);
907f4554 821
ccd979bd
MF
822 BUG_ON(dentry->d_parent->d_inode != dir);
823
b0697053 824 mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
ccd979bd
MF
825
826 if (inode == osb->root_inode) {
827 mlog(0, "Cannot delete the root directory\n");
30a4f5e8 828 return -EPERM;
ccd979bd
MF
829 }
830
cb25797d
JK
831 status = ocfs2_inode_lock_nested(dir, &parent_node_bh, 1,
832 OI_LS_PARENT);
ccd979bd
MF
833 if (status < 0) {
834 if (status != -ENOENT)
835 mlog_errno(status);
30a4f5e8 836 return status;
ccd979bd
MF
837 }
838
839 status = ocfs2_find_files_on_disk(dentry->d_name.name,
4a12ca3a
MF
840 dentry->d_name.len, &blkno, dir,
841 &lookup);
ccd979bd
MF
842 if (status < 0) {
843 if (status != -ENOENT)
844 mlog_errno(status);
845 goto leave;
846 }
847
848 if (OCFS2_I(inode)->ip_blkno != blkno) {
849 status = -ENOENT;
850
b0697053
MF
851 mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
852 (unsigned long long)OCFS2_I(inode)->ip_blkno,
853 (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
ccd979bd
MF
854 goto leave;
855 }
856
e63aecb6 857 status = ocfs2_inode_lock(inode, &fe_bh, 1);
ccd979bd
MF
858 if (status < 0) {
859 if (status != -ENOENT)
860 mlog_errno(status);
861 goto leave;
862 }
30a4f5e8 863 child_locked = 1;
ccd979bd
MF
864
865 if (S_ISDIR(inode->i_mode)) {
b80b549c 866 if (inode->i_nlink != 2 || !ocfs2_empty_dir(inode)) {
ccd979bd
MF
867 status = -ENOTEMPTY;
868 goto leave;
869 }
870 }
871
379dfe9d 872 status = ocfs2_remote_dentry_delete(dentry);
ccd979bd 873 if (status < 0) {
34d024f8 874 /* This remote delete should succeed under all normal
ccd979bd
MF
875 * circumstances. */
876 mlog_errno(status);
877 goto leave;
878 }
879
17ff7856 880 if (inode_is_unlinkable(inode)) {
19bd341f
TM
881 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
882 OCFS2_I(inode)->ip_blkno,
4a12ca3a 883 orphan_name, &orphan_insert);
ccd979bd
MF
884 if (status < 0) {
885 mlog_errno(status);
886 goto leave;
887 }
888 }
889
a90714c1 890 handle = ocfs2_start_trans(osb, ocfs2_unlink_credits(osb->sb));
ccd979bd
MF
891 if (IS_ERR(handle)) {
892 status = PTR_ERR(handle);
893 handle = NULL;
894 mlog_errno(status);
895 goto leave;
896 }
897
0cf2f763 898 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
13723d00 899 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
900 if (status < 0) {
901 mlog_errno(status);
902 goto leave;
903 }
904
905 fe = (struct ocfs2_dinode *) fe_bh->b_data;
906
17ff7856 907 if (inode_is_unlinkable(inode)) {
3939fda4 908 status = ocfs2_orphan_add(osb, handle, inode, fe_bh, orphan_name,
4a12ca3a 909 &orphan_insert, orphan_dir);
ccd979bd
MF
910 if (status < 0) {
911 mlog_errno(status);
912 goto leave;
913 }
914 }
915
916 /* delete the name from the parent dir */
4a12ca3a 917 status = ocfs2_delete_entry(handle, dir, &lookup);
ccd979bd
MF
918 if (status < 0) {
919 mlog_errno(status);
920 goto leave;
921 }
922
17ff7856
MF
923 if (S_ISDIR(inode->i_mode))
924 drop_nlink(inode);
925 drop_nlink(inode);
198a1ca3 926 ocfs2_set_links_count(fe, inode->i_nlink);
ec20cec7 927 ocfs2_journal_dirty(handle, fe_bh);
ccd979bd 928
592282cf
MF
929 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
930 if (S_ISDIR(inode->i_mode))
17ff7856 931 drop_nlink(dir);
592282cf
MF
932
933 status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
934 if (status < 0) {
935 mlog_errno(status);
936 if (S_ISDIR(inode->i_mode))
d8c76e6f 937 inc_nlink(dir);
ccd979bd
MF
938 }
939
940leave:
ccd979bd 941 if (handle)
02dc1af4 942 ocfs2_commit_trans(osb, handle);
ccd979bd 943
30a4f5e8 944 if (child_locked)
e63aecb6 945 ocfs2_inode_unlock(inode, 1);
30a4f5e8 946
e63aecb6 947 ocfs2_inode_unlock(dir, 1);
30a4f5e8 948
5098c27b
MF
949 if (orphan_dir) {
950 /* This was locked for us in ocfs2_prepare_orphan_dir() */
e63aecb6 951 ocfs2_inode_unlock(orphan_dir, 1);
5098c27b
MF
952 mutex_unlock(&orphan_dir->i_mutex);
953 iput(orphan_dir);
954 }
955
a81cb88b 956 brelse(fe_bh);
a81cb88b 957 brelse(parent_node_bh);
4a12ca3a
MF
958
959 ocfs2_free_dir_lookup_result(&orphan_insert);
960 ocfs2_free_dir_lookup_result(&lookup);
ccd979bd 961
c1e8d35e
TM
962 if (status)
963 mlog_errno(status);
ccd979bd
MF
964
965 return status;
966}
967
968/*
969 * The only place this should be used is rename!
970 * if they have the same id, then the 1st one is the only one locked.
971 */
972static int ocfs2_double_lock(struct ocfs2_super *osb,
ccd979bd
MF
973 struct buffer_head **bh1,
974 struct inode *inode1,
975 struct buffer_head **bh2,
976 struct inode *inode2)
977{
978 int status;
979 struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
980 struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
981 struct buffer_head **tmpbh;
982 struct inode *tmpinode;
983
ef6b689b
TM
984 mlog(0, "(inode1 = %llu, inode2 = %llu)\n",
985 (unsigned long long)oi1->ip_blkno,
986 (unsigned long long)oi2->ip_blkno);
ccd979bd 987
ccd979bd
MF
988 if (*bh1)
989 *bh1 = NULL;
990 if (*bh2)
991 *bh2 = NULL;
992
993 /* we always want to lock the one with the lower lockid first. */
994 if (oi1->ip_blkno != oi2->ip_blkno) {
995 if (oi1->ip_blkno < oi2->ip_blkno) {
996 /* switch id1 and id2 around */
997 mlog(0, "switching them around...\n");
998 tmpbh = bh2;
999 bh2 = bh1;
1000 bh1 = tmpbh;
1001
1002 tmpinode = inode2;
1003 inode2 = inode1;
1004 inode1 = tmpinode;
1005 }
1006 /* lock id2 */
cb25797d
JK
1007 status = ocfs2_inode_lock_nested(inode2, bh2, 1,
1008 OI_LS_RENAME1);
ccd979bd
MF
1009 if (status < 0) {
1010 if (status != -ENOENT)
1011 mlog_errno(status);
1012 goto bail;
1013 }
1014 }
8d5596c6 1015
ccd979bd 1016 /* lock id1 */
cb25797d 1017 status = ocfs2_inode_lock_nested(inode1, bh1, 1, OI_LS_RENAME2);
ccd979bd 1018 if (status < 0) {
8d5596c6
MF
1019 /*
1020 * An error return must mean that no cluster locks
1021 * were held on function exit.
1022 */
1e6d9153 1023 if (oi1->ip_blkno != oi2->ip_blkno) {
e63aecb6 1024 ocfs2_inode_unlock(inode2, 1);
1e6d9153
TM
1025 brelse(*bh2);
1026 *bh2 = NULL;
1027 }
8d5596c6 1028
ccd979bd
MF
1029 if (status != -ENOENT)
1030 mlog_errno(status);
ccd979bd 1031 }
8d5596c6 1032
ccd979bd 1033bail:
c1e8d35e
TM
1034 if (status)
1035 mlog_errno(status);
ccd979bd
MF
1036 return status;
1037}
1038
8d5596c6
MF
1039static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
1040{
e63aecb6 1041 ocfs2_inode_unlock(inode1, 1);
8d5596c6
MF
1042
1043 if (inode1 != inode2)
e63aecb6 1044 ocfs2_inode_unlock(inode2, 1);
8d5596c6
MF
1045}
1046
ccd979bd
MF
1047static int ocfs2_rename(struct inode *old_dir,
1048 struct dentry *old_dentry,
1049 struct inode *new_dir,
1050 struct dentry *new_dentry)
1051{
4a12ca3a
MF
1052 int status = 0, rename_lock = 0, parents_locked = 0, target_exists = 0;
1053 int old_child_locked = 0, new_child_locked = 0, update_dot_dot = 0;
ccd979bd
MF
1054 struct inode *old_inode = old_dentry->d_inode;
1055 struct inode *new_inode = new_dentry->d_inode;
5098c27b 1056 struct inode *orphan_dir = NULL;
ccd979bd
MF
1057 struct ocfs2_dinode *newfe = NULL;
1058 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
ccd979bd 1059 struct buffer_head *newfe_bh = NULL;
592282cf 1060 struct buffer_head *old_inode_bh = NULL;
ccd979bd 1061 struct ocfs2_super *osb = NULL;
5b6a3a2b 1062 u64 newfe_blkno, old_de_ino;
1fabe148 1063 handle_t *handle = NULL;
ccd979bd
MF
1064 struct buffer_head *old_dir_bh = NULL;
1065 struct buffer_head *new_dir_bh = NULL;
592282cf 1066 nlink_t old_dir_nlink = old_dir->i_nlink;
480214d7 1067 struct ocfs2_dinode *old_di;
4a12ca3a
MF
1068 struct ocfs2_dir_lookup_result old_inode_dot_dot_res = { NULL, };
1069 struct ocfs2_dir_lookup_result target_lookup_res = { NULL, };
1070 struct ocfs2_dir_lookup_result old_entry_lookup = { NULL, };
1071 struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
1072 struct ocfs2_dir_lookup_result target_insert = { NULL, };
ccd979bd
MF
1073
1074 /* At some point it might be nice to break this function up a
1075 * bit. */
1076
ef6b689b
TM
1077 mlog(0, "(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
1078 old_dir, old_dentry, new_dir, new_dentry,
1079 old_dentry->d_name.len, old_dentry->d_name.name,
1080 new_dentry->d_name.len, new_dentry->d_name.name);
ccd979bd 1081
871a2931
CH
1082 dquot_initialize(old_dir);
1083 dquot_initialize(new_dir);
907f4554 1084
ccd979bd
MF
1085 osb = OCFS2_SB(old_dir->i_sb);
1086
1087 if (new_inode) {
1088 if (!igrab(new_inode))
1089 BUG();
1090 }
1091
1b3c3714 1092 /* Assume a directory hierarchy thusly:
ccd979bd
MF
1093 * a/b/c
1094 * a/d
1095 * a,b,c, and d are all directories.
1096 *
1097 * from cwd of 'a' on both nodes:
1098 * node1: mv b/c d
1099 * node2: mv d b/c
1100 *
1101 * And that's why, just like the VFS, we need a file system
1102 * rename lock. */
5dabd695 1103 if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) {
ccd979bd
MF
1104 status = ocfs2_rename_lock(osb);
1105 if (status < 0) {
1106 mlog_errno(status);
1107 goto bail;
1108 }
1109 rename_lock = 1;
1110 }
1111
ccd979bd 1112 /* if old and new are the same, this'll just do one lock. */
8d5596c6
MF
1113 status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
1114 &new_dir_bh, new_dir);
ccd979bd
MF
1115 if (status < 0) {
1116 mlog_errno(status);
1117 goto bail;
1118 }
8d5596c6 1119 parents_locked = 1;
ccd979bd
MF
1120
1121 /* make sure both dirs have bhs
1122 * get an extra ref on old_dir_bh if old==new */
1123 if (!new_dir_bh) {
1124 if (old_dir_bh) {
1125 new_dir_bh = old_dir_bh;
1126 get_bh(new_dir_bh);
1127 } else {
1128 mlog(ML_ERROR, "no old_dir_bh!\n");
1129 status = -EIO;
1130 goto bail;
1131 }
1132 }
1133
379dfe9d 1134 /*
592282cf 1135 * Aside from allowing a meta data update, the locking here
34d024f8
MF
1136 * also ensures that the downconvert thread on other nodes
1137 * won't have to concurrently downconvert the inode and the
1138 * dentry locks.
379dfe9d 1139 */
cb25797d
JK
1140 status = ocfs2_inode_lock_nested(old_inode, &old_inode_bh, 1,
1141 OI_LS_PARENT);
379dfe9d
MF
1142 if (status < 0) {
1143 if (status != -ENOENT)
ccd979bd 1144 mlog_errno(status);
379dfe9d
MF
1145 goto bail;
1146 }
8d5596c6 1147 old_child_locked = 1;
ccd979bd 1148
379dfe9d
MF
1149 status = ocfs2_remote_dentry_delete(old_dentry);
1150 if (status < 0) {
1151 mlog_errno(status);
1152 goto bail;
1153 }
1154
1155 if (S_ISDIR(old_inode->i_mode)) {
38760e24
MF
1156 u64 old_inode_parent;
1157
4a12ca3a 1158 update_dot_dot = 1;
38760e24 1159 status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent,
4a12ca3a
MF
1160 old_inode,
1161 &old_inode_dot_dot_res);
38760e24
MF
1162 if (status) {
1163 status = -EIO;
ccd979bd 1164 goto bail;
38760e24 1165 }
ccd979bd 1166
38760e24
MF
1167 if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) {
1168 status = -EIO;
ccd979bd 1169 goto bail;
38760e24
MF
1170 }
1171
1172 if (!new_inode && new_dir != old_dir &&
198a1ca3 1173 new_dir->i_nlink >= ocfs2_link_max(osb)) {
38760e24 1174 status = -EMLINK;
ccd979bd 1175 goto bail;
38760e24 1176 }
ccd979bd
MF
1177 }
1178
5b6a3a2b
MF
1179 status = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name,
1180 old_dentry->d_name.len,
1181 &old_de_ino);
1182 if (status) {
1183 status = -ENOENT;
ccd979bd 1184 goto bail;
5b6a3a2b 1185 }
ccd979bd
MF
1186
1187 /*
1188 * Check for inode number is _not_ due to possible IO errors.
1189 * We might rmdir the source, keep it as pwd of some process
1190 * and merrily kill the link to whatever was created under the
1191 * same name. Goodbye sticky bit ;-<
1192 */
5b6a3a2b
MF
1193 if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) {
1194 status = -ENOENT;
ccd979bd 1195 goto bail;
5b6a3a2b 1196 }
ccd979bd
MF
1197
1198 /* check if the target already exists (in which case we need
1199 * to delete it */
1200 status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
1201 new_dentry->d_name.len,
4a12ca3a
MF
1202 &newfe_blkno, new_dir,
1203 &target_lookup_res);
ccd979bd
MF
1204 /* The only error we allow here is -ENOENT because the new
1205 * file not existing is perfectly valid. */
1206 if ((status < 0) && (status != -ENOENT)) {
1207 /* If we cannot find the file specified we should just */
1208 /* return the error... */
1209 mlog_errno(status);
1210 goto bail;
1211 }
4a12ca3a
MF
1212 if (status == 0)
1213 target_exists = 1;
ccd979bd 1214
4a12ca3a 1215 if (!target_exists && new_inode) {
e325a88f
SE
1216 /*
1217 * Target was unlinked by another node while we were
1218 * waiting to get to ocfs2_rename(). There isn't
1219 * anything we can do here to help the situation, so
1220 * bubble up the appropriate error.
1221 */
1222 status = -ENOENT;
1223 goto bail;
1224 }
ccd979bd
MF
1225
1226 /* In case we need to overwrite an existing file, we blow it
1227 * away first */
4a12ca3a 1228 if (target_exists) {
ccd979bd
MF
1229 /* VFS didn't think there existed an inode here, but
1230 * someone else in the cluster must have raced our
1231 * rename to create one. Today we error cleanly, in
1232 * the future we should consider calling iget to build
1233 * a new struct inode for this entry. */
1234 if (!new_inode) {
1235 status = -EACCES;
1236
1237 mlog(0, "We found an inode for name %.*s but VFS "
1238 "didn't give us one.\n", new_dentry->d_name.len,
1239 new_dentry->d_name.name);
1240 goto bail;
1241 }
1242
1243 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
1244 status = -EACCES;
1245
b0697053
MF
1246 mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
1247 (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
1248 (unsigned long long)newfe_blkno,
ccd979bd
MF
1249 OCFS2_I(new_inode)->ip_flags);
1250 goto bail;
1251 }
1252
e63aecb6 1253 status = ocfs2_inode_lock(new_inode, &newfe_bh, 1);
ccd979bd
MF
1254 if (status < 0) {
1255 if (status != -ENOENT)
1256 mlog_errno(status);
1257 goto bail;
1258 }
8d5596c6 1259 new_child_locked = 1;
ccd979bd 1260
379dfe9d 1261 status = ocfs2_remote_dentry_delete(new_dentry);
ccd979bd
MF
1262 if (status < 0) {
1263 mlog_errno(status);
1264 goto bail;
1265 }
1266
1267 newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
1268
4a12ca3a
MF
1269 mlog(0, "aha rename over existing... new_blkno=%llu "
1270 "newfebh=%p bhblocknr=%llu\n",
b0697053 1271 (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
ccd979bd
MF
1272 (unsigned long long)newfe_bh->b_blocknr : 0ULL);
1273
1274 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
5098c27b 1275 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
19bd341f
TM
1276 OCFS2_I(new_inode)->ip_blkno,
1277 orphan_name, &orphan_insert);
ccd979bd
MF
1278 if (status < 0) {
1279 mlog_errno(status);
1280 goto bail;
1281 }
1282 }
1283 } else {
1284 BUG_ON(new_dentry->d_parent->d_inode != new_dir);
1285
1286 status = ocfs2_check_dir_for_entry(new_dir,
1287 new_dentry->d_name.name,
1288 new_dentry->d_name.len);
1289 if (status)
1290 goto bail;
1291
1292 status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
1293 new_dentry->d_name.name,
1294 new_dentry->d_name.len,
4a12ca3a 1295 &target_insert);
ccd979bd
MF
1296 if (status < 0) {
1297 mlog_errno(status);
1298 goto bail;
1299 }
1300 }
1301
a90714c1 1302 handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb));
ccd979bd
MF
1303 if (IS_ERR(handle)) {
1304 status = PTR_ERR(handle);
1305 handle = NULL;
1306 mlog_errno(status);
1307 goto bail;
1308 }
1309
4a12ca3a 1310 if (target_exists) {
ccd979bd 1311 if (S_ISDIR(new_inode->i_mode)) {
b80b549c
MF
1312 if (new_inode->i_nlink != 2 ||
1313 !ocfs2_empty_dir(new_inode)) {
ccd979bd
MF
1314 status = -ENOTEMPTY;
1315 goto bail;
1316 }
1317 }
0cf2f763
JB
1318 status = ocfs2_journal_access_di(handle, INODE_CACHE(new_inode),
1319 newfe_bh,
13723d00 1320 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
1321 if (status < 0) {
1322 mlog_errno(status);
1323 goto bail;
1324 }
1325
1326 if (S_ISDIR(new_inode->i_mode) ||
198a1ca3 1327 (ocfs2_read_links_count(newfe) == 1)) {
ccd979bd 1328 status = ocfs2_orphan_add(osb, handle, new_inode,
3939fda4 1329 newfe_bh, orphan_name,
4a12ca3a 1330 &orphan_insert, orphan_dir);
ccd979bd
MF
1331 if (status < 0) {
1332 mlog_errno(status);
1333 goto bail;
1334 }
1335 }
1336
1337 /* change the dirent to point to the correct inode */
4a12ca3a
MF
1338 status = ocfs2_update_entry(new_dir, handle, &target_lookup_res,
1339 old_inode);
ccd979bd
MF
1340 if (status < 0) {
1341 mlog_errno(status);
1342 goto bail;
1343 }
ccd979bd 1344 new_dir->i_version++;
ccd979bd
MF
1345
1346 if (S_ISDIR(new_inode->i_mode))
198a1ca3 1347 ocfs2_set_links_count(newfe, 0);
ccd979bd 1348 else
198a1ca3 1349 ocfs2_add_links_count(newfe, -1);
ec20cec7 1350 ocfs2_journal_dirty(handle, newfe_bh);
ccd979bd
MF
1351 } else {
1352 /* if the name was not found in new_dir, add it now */
1353 status = ocfs2_add_entry(handle, new_dentry, old_inode,
1354 OCFS2_I(old_inode)->ip_blkno,
4a12ca3a 1355 new_dir_bh, &target_insert);
ccd979bd
MF
1356 }
1357
1358 old_inode->i_ctime = CURRENT_TIME;
1359 mark_inode_dirty(old_inode);
480214d7 1360
0cf2f763
JB
1361 status = ocfs2_journal_access_di(handle, INODE_CACHE(old_inode),
1362 old_inode_bh,
13723d00 1363 OCFS2_JOURNAL_ACCESS_WRITE);
480214d7
SM
1364 if (status >= 0) {
1365 old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
1366
1367 old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
1368 old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
ec20cec7 1369 ocfs2_journal_dirty(handle, old_inode_bh);
480214d7
SM
1370 } else
1371 mlog_errno(status);
ccd979bd 1372
5b6a3a2b
MF
1373 /*
1374 * Now that the name has been added to new_dir, remove the old name.
1375 *
1376 * We don't keep any directory entry context around until now
1377 * because the insert might have changed the type of directory
1378 * we're dealing with.
1379 */
4a12ca3a
MF
1380 status = ocfs2_find_entry(old_dentry->d_name.name,
1381 old_dentry->d_name.len, old_dir,
1382 &old_entry_lookup);
1383 if (status)
5b6a3a2b 1384 goto bail;
5b6a3a2b 1385
4a12ca3a 1386 status = ocfs2_delete_entry(handle, old_dir, &old_entry_lookup);
ccd979bd
MF
1387 if (status < 0) {
1388 mlog_errno(status);
1389 goto bail;
1390 }
1391
1392 if (new_inode) {
1393 new_inode->i_nlink--;
1394 new_inode->i_ctime = CURRENT_TIME;
1395 }
1396 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
4a12ca3a
MF
1397
1398 if (update_dot_dot) {
1399 status = ocfs2_update_entry(old_inode, handle,
1400 &old_inode_dot_dot_res, new_dir);
ccd979bd
MF
1401 old_dir->i_nlink--;
1402 if (new_inode) {
1403 new_inode->i_nlink--;
1404 } else {
d8c76e6f 1405 inc_nlink(new_dir);
ccd979bd
MF
1406 mark_inode_dirty(new_dir);
1407 }
1408 }
1409 mark_inode_dirty(old_dir);
592282cf
MF
1410 ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
1411 if (new_inode) {
ccd979bd 1412 mark_inode_dirty(new_inode);
592282cf
MF
1413 ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
1414 }
ccd979bd 1415
592282cf
MF
1416 if (old_dir != new_dir) {
1417 /* Keep the same times on both directories.*/
1418 new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
1419
1420 /*
1421 * This will also pick up the i_nlink change from the
1422 * block above.
1423 */
1424 ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
1425 }
ccd979bd
MF
1426
1427 if (old_dir_nlink != old_dir->i_nlink) {
1428 if (!old_dir_bh) {
1429 mlog(ML_ERROR, "need to change nlink for old dir "
b0697053
MF
1430 "%llu from %d to %d but bh is NULL!\n",
1431 (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
1432 (int)old_dir_nlink, old_dir->i_nlink);
ccd979bd
MF
1433 } else {
1434 struct ocfs2_dinode *fe;
0cf2f763
JB
1435 status = ocfs2_journal_access_di(handle,
1436 INODE_CACHE(old_dir),
1437 old_dir_bh,
1438 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd 1439 fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
198a1ca3 1440 ocfs2_set_links_count(fe, old_dir->i_nlink);
ec20cec7 1441 ocfs2_journal_dirty(handle, old_dir_bh);
ccd979bd
MF
1442 }
1443 }
379dfe9d 1444 ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
ccd979bd
MF
1445 status = 0;
1446bail:
1447 if (rename_lock)
1448 ocfs2_rename_unlock(osb);
1449
1450 if (handle)
02dc1af4 1451 ocfs2_commit_trans(osb, handle);
ccd979bd 1452
8d5596c6
MF
1453 if (parents_locked)
1454 ocfs2_double_unlock(old_dir, new_dir);
1455
1456 if (old_child_locked)
e63aecb6 1457 ocfs2_inode_unlock(old_inode, 1);
8d5596c6
MF
1458
1459 if (new_child_locked)
e63aecb6 1460 ocfs2_inode_unlock(new_inode, 1);
8d5596c6 1461
5098c27b
MF
1462 if (orphan_dir) {
1463 /* This was locked for us in ocfs2_prepare_orphan_dir() */
e63aecb6 1464 ocfs2_inode_unlock(orphan_dir, 1);
5098c27b
MF
1465 mutex_unlock(&orphan_dir->i_mutex);
1466 iput(orphan_dir);
1467 }
1468
ccd979bd
MF
1469 if (new_inode)
1470 sync_mapping_buffers(old_inode->i_mapping);
1471
1472 if (new_inode)
1473 iput(new_inode);
4a12ca3a
MF
1474
1475 ocfs2_free_dir_lookup_result(&target_lookup_res);
1476 ocfs2_free_dir_lookup_result(&old_entry_lookup);
1477 ocfs2_free_dir_lookup_result(&old_inode_dot_dot_res);
1478 ocfs2_free_dir_lookup_result(&orphan_insert);
1479 ocfs2_free_dir_lookup_result(&target_insert);
1480
a81cb88b
MF
1481 brelse(newfe_bh);
1482 brelse(old_inode_bh);
1483 brelse(old_dir_bh);
1484 brelse(new_dir_bh);
ccd979bd 1485
c1e8d35e
TM
1486 if (status)
1487 mlog_errno(status);
ccd979bd
MF
1488
1489 return status;
1490}
1491
1492/*
1493 * we expect i_size = strlen(symname). Copy symname into the file
1494 * data, including the null terminator.
1495 */
1496static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
1fabe148 1497 handle_t *handle,
ccd979bd
MF
1498 struct inode *inode,
1499 const char *symname)
1500{
1501 struct buffer_head **bhs = NULL;
1502 const char *c;
1503 struct super_block *sb = osb->sb;
4f902c37 1504 u64 p_blkno, p_blocks;
ccd979bd
MF
1505 int virtual, blocks, status, i, bytes_left;
1506
1507 bytes_left = i_size_read(inode) + 1;
1508 /* we can't trust i_blocks because we're actually going to
1509 * write i_size + 1 bytes. */
1510 blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1511
ef6b689b
TM
1512 mlog(0, "i_blocks = %llu, i_size = %llu, blocks = %d\n",
1513 (unsigned long long)inode->i_blocks,
1514 i_size_read(inode), blocks);
ccd979bd
MF
1515
1516 /* Sanity check -- make sure we're going to fit. */
1517 if (bytes_left >
1518 ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1519 status = -EIO;
1520 mlog_errno(status);
1521 goto bail;
1522 }
1523
1524 bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
1525 if (!bhs) {
1526 status = -ENOMEM;
1527 mlog_errno(status);
1528 goto bail;
1529 }
1530
49cb8d2d
MF
1531 status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
1532 NULL);
ccd979bd
MF
1533 if (status < 0) {
1534 mlog_errno(status);
1535 goto bail;
1536 }
1537
1538 /* links can never be larger than one cluster so we know this
1539 * is all going to be contiguous, but do a sanity check
1540 * anyway. */
1541 if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
1542 status = -EIO;
1543 mlog_errno(status);
1544 goto bail;
1545 }
1546
1547 virtual = 0;
1548 while(bytes_left > 0) {
1549 c = &symname[virtual * sb->s_blocksize];
1550
1551 bhs[virtual] = sb_getblk(sb, p_blkno);
1552 if (!bhs[virtual]) {
1553 status = -ENOMEM;
1554 mlog_errno(status);
1555 goto bail;
1556 }
8cb471e8
JB
1557 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode),
1558 bhs[virtual]);
ccd979bd 1559
0cf2f763
JB
1560 status = ocfs2_journal_access(handle, INODE_CACHE(inode),
1561 bhs[virtual],
ccd979bd
MF
1562 OCFS2_JOURNAL_ACCESS_CREATE);
1563 if (status < 0) {
1564 mlog_errno(status);
1565 goto bail;
1566 }
1567
1568 memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
1569
1570 memcpy(bhs[virtual]->b_data, c,
1571 (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
1572 bytes_left);
1573
ec20cec7 1574 ocfs2_journal_dirty(handle, bhs[virtual]);
ccd979bd
MF
1575
1576 virtual++;
1577 p_blkno++;
1578 bytes_left -= sb->s_blocksize;
1579 }
1580
1581 status = 0;
1582bail:
1583
1584 if (bhs) {
1585 for(i = 0; i < blocks; i++)
a81cb88b 1586 brelse(bhs[i]);
ccd979bd
MF
1587 kfree(bhs);
1588 }
1589
c1e8d35e
TM
1590 if (status)
1591 mlog_errno(status);
ccd979bd
MF
1592 return status;
1593}
1594
1595static int ocfs2_symlink(struct inode *dir,
1596 struct dentry *dentry,
1597 const char *symname)
1598{
1599 int status, l, credits;
1600 u64 newsize;
1601 struct ocfs2_super *osb = NULL;
1602 struct inode *inode = NULL;
1603 struct super_block *sb;
1604 struct buffer_head *new_fe_bh = NULL;
ccd979bd
MF
1605 struct buffer_head *parent_fe_bh = NULL;
1606 struct ocfs2_dinode *fe = NULL;
1607 struct ocfs2_dinode *dirfe;
1fabe148 1608 handle_t *handle = NULL;
ccd979bd
MF
1609 struct ocfs2_alloc_context *inode_ac = NULL;
1610 struct ocfs2_alloc_context *data_ac = NULL;
534eaddd
TY
1611 struct ocfs2_alloc_context *xattr_ac = NULL;
1612 int want_clusters = 0;
1613 int xattr_credits = 0;
1614 struct ocfs2_security_xattr_info si = {
1615 .enable = 1,
1616 };
a90714c1 1617 int did_quota = 0, did_quota_inode = 0;
4a12ca3a 1618 struct ocfs2_dir_lookup_result lookup = { NULL, };
547ba7c8
JB
1619 sigset_t oldset;
1620 int did_block_signals = 0;
ccd979bd 1621
ef6b689b
TM
1622 mlog(0, "(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
1623 dentry, symname, dentry->d_name.len, dentry->d_name.name);
ccd979bd 1624
871a2931 1625 dquot_initialize(dir);
907f4554 1626
ccd979bd
MF
1627 sb = dir->i_sb;
1628 osb = OCFS2_SB(sb);
1629
1630 l = strlen(symname) + 1;
1631
1632 credits = ocfs2_calc_symlink_credits(sb);
1633
ccd979bd 1634 /* lock the parent directory */
e63aecb6 1635 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
ccd979bd
MF
1636 if (status < 0) {
1637 if (status != -ENOENT)
1638 mlog_errno(status);
6d8fc40e 1639 return status;
ccd979bd
MF
1640 }
1641
1642 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
198a1ca3 1643 if (!ocfs2_read_links_count(dirfe)) {
ccd979bd
MF
1644 /* can't make a file in a deleted directory. */
1645 status = -ENOENT;
1646 goto bail;
1647 }
1648
1649 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
1650 dentry->d_name.len);
1651 if (status)
1652 goto bail;
1653
1654 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
1655 dentry->d_name.name,
4a12ca3a 1656 dentry->d_name.len, &lookup);
ccd979bd
MF
1657 if (status < 0) {
1658 mlog_errno(status);
1659 goto bail;
1660 }
1661
da5cbf2f 1662 status = ocfs2_reserve_new_inode(osb, &inode_ac);
ccd979bd
MF
1663 if (status < 0) {
1664 if (status != -ENOSPC)
1665 mlog_errno(status);
1666 goto bail;
1667 }
1668
f5d36202
TY
1669 inode = ocfs2_get_init_inode(dir, S_IFLNK | S_IRWXUGO);
1670 if (!inode) {
1671 status = -ENOMEM;
1672 mlog_errno(status);
1673 goto bail;
1674 }
1675
534eaddd
TY
1676 /* get security xattr */
1677 status = ocfs2_init_security_get(inode, dir, &si);
1678 if (status) {
1679 if (status == -EOPNOTSUPP)
1680 si.enable = 0;
1681 else {
1682 mlog_errno(status);
1683 goto bail;
1684 }
1685 }
1686
1687 /* calculate meta data/clusters for setting security xattr */
1688 if (si.enable) {
1689 status = ocfs2_calc_security_init(dir, &si, &want_clusters,
1690 &xattr_credits, &xattr_ac);
ccd979bd 1691 if (status < 0) {
534eaddd 1692 mlog_errno(status);
ccd979bd
MF
1693 goto bail;
1694 }
1695 }
1696
534eaddd
TY
1697 /* don't reserve bitmap space for fast symlinks. */
1698 if (l > ocfs2_fast_symlink_chars(sb))
1699 want_clusters += 1;
1700
1701 status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
1702 if (status < 0) {
1703 if (status != -ENOSPC)
1704 mlog_errno(status);
1705 goto bail;
1706 }
1707
1708 handle = ocfs2_start_trans(osb, credits + xattr_credits);
ccd979bd
MF
1709 if (IS_ERR(handle)) {
1710 status = PTR_ERR(handle);
1711 handle = NULL;
1712 mlog_errno(status);
1713 goto bail;
1714 }
1715
547ba7c8
JB
1716 /* Starting to change things, restart is no longer possible. */
1717 ocfs2_block_signals(&oldset);
1718 did_block_signals = 1;
1719
63936dda
CH
1720 status = dquot_alloc_inode(inode);
1721 if (status)
a90714c1 1722 goto bail;
a90714c1
JK
1723 did_quota_inode = 1;
1724
ef6b689b
TM
1725 mlog(0, "(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry,
1726 inode->i_mode, dentry->d_name.len,
1727 dentry->d_name.name);
19bd341f
TM
1728
1729 status = ocfs2_mknod_locked(osb, dir, inode,
f5d36202
TY
1730 0, &new_fe_bh, parent_fe_bh, handle,
1731 inode_ac);
ccd979bd
MF
1732 if (status < 0) {
1733 mlog_errno(status);
1734 goto bail;
1735 }
1736
1737 fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
1738 inode->i_rdev = 0;
1739 newsize = l - 1;
1740 if (l > ocfs2_fast_symlink_chars(sb)) {
dcd0538f
MF
1741 u32 offset = 0;
1742
ccd979bd 1743 inode->i_op = &ocfs2_symlink_inode_operations;
5dd4056d
CH
1744 status = dquot_alloc_space_nodirty(inode,
1745 ocfs2_clusters_to_bytes(osb->sb, 1));
1746 if (status)
a90714c1 1747 goto bail;
a90714c1 1748 did_quota = 1;
0eb8d47e
TM
1749 status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0,
1750 new_fe_bh,
1751 handle, data_ac, NULL,
1752 NULL);
ccd979bd
MF
1753 if (status < 0) {
1754 if (status != -ENOSPC && status != -EINTR) {
b0697053
MF
1755 mlog(ML_ERROR,
1756 "Failed to extend file to %llu\n",
1757 (unsigned long long)newsize);
ccd979bd
MF
1758 mlog_errno(status);
1759 status = -ENOSPC;
1760 }
1761 goto bail;
1762 }
1763 i_size_write(inode, newsize);
8110b073 1764 inode->i_blocks = ocfs2_inode_sector_count(inode);
ccd979bd
MF
1765 } else {
1766 inode->i_op = &ocfs2_fast_symlink_inode_operations;
1767 memcpy((char *) fe->id2.i_symlink, symname, l);
1768 i_size_write(inode, newsize);
1769 inode->i_blocks = 0;
1770 }
1771
1772 status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
1773 if (status < 0) {
1774 mlog_errno(status);
1775 goto bail;
1776 }
1777
1778 if (!ocfs2_inode_is_fast_symlink(inode)) {
1779 status = ocfs2_create_symlink_data(osb, handle, inode,
1780 symname);
1781 if (status < 0) {
1782 mlog_errno(status);
1783 goto bail;
1784 }
1785 }
1786
534eaddd
TY
1787 if (si.enable) {
1788 status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
1789 xattr_ac, data_ac);
1790 if (status < 0) {
1791 mlog_errno(status);
1792 goto bail;
1793 }
1794 }
1795
a9743fcd
MF
1796 /*
1797 * Do this before adding the entry to the directory. We add
1798 * also set d_op after success so that ->d_iput() will cleanup
1799 * the dentry lock even if ocfs2_add_entry() fails below.
1800 */
1801 status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
1802 if (status) {
ccd979bd
MF
1803 mlog_errno(status);
1804 goto bail;
1805 }
1806
a9743fcd
MF
1807 status = ocfs2_add_entry(handle, dentry, inode,
1808 le64_to_cpu(fe->i_blkno), parent_fe_bh,
1809 &lookup);
1810 if (status < 0) {
379dfe9d
MF
1811 mlog_errno(status);
1812 goto bail;
1813 }
1814
ccd979bd 1815 insert_inode_hash(inode);
ccd979bd
MF
1816 d_instantiate(dentry, inode);
1817bail:
a90714c1 1818 if (status < 0 && did_quota)
5dd4056d 1819 dquot_free_space_nodirty(inode,
a90714c1
JK
1820 ocfs2_clusters_to_bytes(osb->sb, 1));
1821 if (status < 0 && did_quota_inode)
63936dda 1822 dquot_free_inode(inode);
ccd979bd 1823 if (handle)
02dc1af4 1824 ocfs2_commit_trans(osb, handle);
6d8fc40e 1825
e63aecb6 1826 ocfs2_inode_unlock(dir, 1);
547ba7c8
JB
1827 if (did_block_signals)
1828 ocfs2_unblock_signals(&oldset);
6d8fc40e 1829
a81cb88b
MF
1830 brelse(new_fe_bh);
1831 brelse(parent_fe_bh);
534eaddd
TY
1832 kfree(si.name);
1833 kfree(si.value);
4a12ca3a 1834 ocfs2_free_dir_lookup_result(&lookup);
ccd979bd
MF
1835 if (inode_ac)
1836 ocfs2_free_alloc_context(inode_ac);
1837 if (data_ac)
1838 ocfs2_free_alloc_context(data_ac);
534eaddd
TY
1839 if (xattr_ac)
1840 ocfs2_free_alloc_context(xattr_ac);
f5d36202 1841 if ((status < 0) && inode) {
ab41fdc8 1842 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SKIP_ORPHAN_DIR;
f5d36202 1843 clear_nlink(inode);
ccd979bd 1844 iput(inode);
f5d36202 1845 }
ccd979bd 1846
c1e8d35e
TM
1847 if (status)
1848 mlog_errno(status);
ccd979bd
MF
1849
1850 return status;
1851}
1852
ccd979bd
MF
1853static int ocfs2_blkno_stringify(u64 blkno, char *name)
1854{
1855 int status, namelen;
1856
b0697053
MF
1857 namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
1858 (long long)blkno);
ccd979bd
MF
1859 if (namelen <= 0) {
1860 if (namelen)
1861 status = namelen;
1862 else
1863 status = -EINVAL;
1864 mlog_errno(status);
1865 goto bail;
1866 }
1867 if (namelen != OCFS2_ORPHAN_NAMELEN) {
1868 status = -EINVAL;
1869 mlog_errno(status);
1870 goto bail;
1871 }
1872
1873 mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
1874 namelen);
1875
1876 status = 0;
1877bail:
c1e8d35e
TM
1878 if (status < 0)
1879 mlog_errno(status);
ccd979bd
MF
1880 return status;
1881}
1882
dd43bcde
MF
1883static int ocfs2_lookup_lock_orphan_dir(struct ocfs2_super *osb,
1884 struct inode **ret_orphan_dir,
1885 struct buffer_head **ret_orphan_dir_bh)
ccd979bd 1886{
5098c27b 1887 struct inode *orphan_dir_inode;
ccd979bd 1888 struct buffer_head *orphan_dir_bh = NULL;
dd43bcde 1889 int ret = 0;
ccd979bd
MF
1890
1891 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1892 ORPHAN_DIR_SYSTEM_INODE,
1893 osb->slot_num);
1894 if (!orphan_dir_inode) {
dd43bcde
MF
1895 ret = -ENOENT;
1896 mlog_errno(ret);
1897 return ret;
ccd979bd
MF
1898 }
1899
5098c27b
MF
1900 mutex_lock(&orphan_dir_inode->i_mutex);
1901
dd43bcde
MF
1902 ret = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
1903 if (ret < 0) {
1904 mutex_unlock(&orphan_dir_inode->i_mutex);
1905 iput(orphan_dir_inode);
1906
1907 mlog_errno(ret);
1908 return ret;
ccd979bd
MF
1909 }
1910
dd43bcde
MF
1911 *ret_orphan_dir = orphan_dir_inode;
1912 *ret_orphan_dir_bh = orphan_dir_bh;
5098c27b 1913
dd43bcde
MF
1914 return 0;
1915}
1916
1917static int __ocfs2_prepare_orphan_dir(struct inode *orphan_dir_inode,
1918 struct buffer_head *orphan_dir_bh,
1919 u64 blkno,
1920 char *name,
1921 struct ocfs2_dir_lookup_result *lookup)
1922{
1923 int ret;
1924 struct ocfs2_super *osb = OCFS2_SB(orphan_dir_inode->i_sb);
1925
1926 ret = ocfs2_blkno_stringify(blkno, name);
1927 if (ret < 0) {
1928 mlog_errno(ret);
1929 return ret;
1930 }
1931
1932 ret = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
1933 orphan_dir_bh, name,
1934 OCFS2_ORPHAN_NAMELEN, lookup);
1935 if (ret < 0) {
1936 mlog_errno(ret);
1937 return ret;
1938 }
1939
1940 return 0;
1941}
1942
1943/**
1944 * ocfs2_prepare_orphan_dir() - Prepare an orphan directory for
1945 * insertion of an orphan.
1946 * @osb: ocfs2 file system
1947 * @ret_orphan_dir: Orphan dir inode - returned locked!
1948 * @blkno: Actual block number of the inode to be inserted into orphan dir.
1949 * @lookup: dir lookup result, to be passed back into functions like
1950 * ocfs2_orphan_add
1951 *
1952 * Returns zero on success and the ret_orphan_dir, name and lookup
1953 * fields will be populated.
1954 *
1955 * Returns non-zero on failure.
1956 */
1957static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
1958 struct inode **ret_orphan_dir,
1959 u64 blkno,
1960 char *name,
1961 struct ocfs2_dir_lookup_result *lookup)
1962{
1963 struct inode *orphan_dir_inode = NULL;
1964 struct buffer_head *orphan_dir_bh = NULL;
1965 int ret = 0;
1966
1967 ret = ocfs2_lookup_lock_orphan_dir(osb, &orphan_dir_inode,
1968 &orphan_dir_bh);
1969 if (ret < 0) {
1970 mlog_errno(ret);
1971 return ret;
1972 }
1973
1974 ret = __ocfs2_prepare_orphan_dir(orphan_dir_inode, orphan_dir_bh,
1975 blkno, name, lookup);
1976 if (ret < 0) {
1977 mlog_errno(ret);
1978 goto out;
ccd979bd
MF
1979 }
1980
5098c27b
MF
1981 *ret_orphan_dir = orphan_dir_inode;
1982
dd43bcde
MF
1983out:
1984 brelse(orphan_dir_bh);
1985
1986 if (ret) {
1987 ocfs2_inode_unlock(orphan_dir_inode, 1);
5098c27b 1988 mutex_unlock(&orphan_dir_inode->i_mutex);
ccd979bd 1989 iput(orphan_dir_inode);
5098c27b 1990 }
ccd979bd 1991
c1e8d35e
TM
1992 if (ret)
1993 mlog_errno(ret);
dd43bcde 1994 return ret;
ccd979bd
MF
1995}
1996
1997static int ocfs2_orphan_add(struct ocfs2_super *osb,
1fabe148 1998 handle_t *handle,
ccd979bd 1999 struct inode *inode,
3939fda4 2000 struct buffer_head *fe_bh,
ccd979bd 2001 char *name,
4a12ca3a 2002 struct ocfs2_dir_lookup_result *lookup,
5098c27b 2003 struct inode *orphan_dir_inode)
ccd979bd 2004{
ccd979bd
MF
2005 struct buffer_head *orphan_dir_bh = NULL;
2006 int status = 0;
2007 struct ocfs2_dinode *orphan_fe;
3939fda4 2008 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
ccd979bd 2009
ef6b689b 2010 mlog(0, "(inode->i_ino = %lu)\n", inode->i_ino);
ccd979bd 2011
b657c95c 2012 status = ocfs2_read_inode_block(orphan_dir_inode, &orphan_dir_bh);
ccd979bd
MF
2013 if (status < 0) {
2014 mlog_errno(status);
2015 goto leave;
2016 }
2017
0cf2f763
JB
2018 status = ocfs2_journal_access_di(handle,
2019 INODE_CACHE(orphan_dir_inode),
2020 orphan_dir_bh,
13723d00 2021 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
2022 if (status < 0) {
2023 mlog_errno(status);
2024 goto leave;
2025 }
2026
2027 /* we're a cluster, and nlink can change on disk from
2028 * underneath us... */
2029 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2030 if (S_ISDIR(inode->i_mode))
198a1ca3
MF
2031 ocfs2_add_links_count(orphan_fe, 1);
2032 orphan_dir_inode->i_nlink = ocfs2_read_links_count(orphan_fe);
ec20cec7 2033 ocfs2_journal_dirty(handle, orphan_dir_bh);
ccd979bd
MF
2034
2035 status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
2036 OCFS2_ORPHAN_NAMELEN, inode,
2037 OCFS2_I(inode)->ip_blkno,
4a12ca3a 2038 orphan_dir_bh, lookup);
ccd979bd
MF
2039 if (status < 0) {
2040 mlog_errno(status);
2041 goto leave;
2042 }
2043
3939fda4
TY
2044 /*
2045 * We're going to journal the change of i_flags and i_orphaned_slot.
2046 * It's safe anyway, though some callers may duplicate the journaling.
2047 * Journaling within the func just make the logic look more
2048 * straightforward.
2049 */
2050 status = ocfs2_journal_access_di(handle,
2051 INODE_CACHE(inode),
2052 fe_bh,
2053 OCFS2_JOURNAL_ACCESS_WRITE);
2054 if (status < 0) {
2055 mlog_errno(status);
2056 goto leave;
2057 }
2058
ccd979bd 2059 le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
d4cd1871 2060 OCFS2_I(inode)->ip_flags &= ~OCFS2_INODE_SKIP_ORPHAN_DIR;
ccd979bd
MF
2061
2062 /* Record which orphan dir our inode now resides
2063 * in. delete_inode will use this to determine which orphan
2064 * dir to lock. */
50008630 2065 fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
ccd979bd 2066
3939fda4
TY
2067 ocfs2_journal_dirty(handle, fe_bh);
2068
b0697053
MF
2069 mlog(0, "Inode %llu orphaned in slot %d\n",
2070 (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
ccd979bd
MF
2071
2072leave:
a81cb88b 2073 brelse(orphan_dir_bh);
ccd979bd 2074
c1e8d35e
TM
2075 if (status)
2076 mlog_errno(status);
ccd979bd
MF
2077 return status;
2078}
2079
2080/* unlike orphan_add, we expect the orphan dir to already be locked here. */
2081int ocfs2_orphan_del(struct ocfs2_super *osb,
1fabe148 2082 handle_t *handle,
ccd979bd
MF
2083 struct inode *orphan_dir_inode,
2084 struct inode *inode,
2085 struct buffer_head *orphan_dir_bh)
2086{
2087 char name[OCFS2_ORPHAN_NAMELEN + 1];
2088 struct ocfs2_dinode *orphan_fe;
2089 int status = 0;
4a12ca3a 2090 struct ocfs2_dir_lookup_result lookup = { NULL, };
ccd979bd 2091
ccd979bd
MF
2092 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2093 if (status < 0) {
2094 mlog_errno(status);
2095 goto leave;
2096 }
2097
b0697053
MF
2098 mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
2099 name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
2100 OCFS2_ORPHAN_NAMELEN);
ccd979bd
MF
2101
2102 /* find it's spot in the orphan directory */
4a12ca3a
MF
2103 status = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN, orphan_dir_inode,
2104 &lookup);
2105 if (status) {
ccd979bd
MF
2106 mlog_errno(status);
2107 goto leave;
2108 }
2109
2110 /* remove it from the orphan directory */
4a12ca3a 2111 status = ocfs2_delete_entry(handle, orphan_dir_inode, &lookup);
ccd979bd
MF
2112 if (status < 0) {
2113 mlog_errno(status);
2114 goto leave;
2115 }
2116
0cf2f763
JB
2117 status = ocfs2_journal_access_di(handle,
2118 INODE_CACHE(orphan_dir_inode),
2119 orphan_dir_bh,
13723d00 2120 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
2121 if (status < 0) {
2122 mlog_errno(status);
2123 goto leave;
2124 }
2125
2126 /* do the i_nlink dance! :) */
2127 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2128 if (S_ISDIR(inode->i_mode))
198a1ca3
MF
2129 ocfs2_add_links_count(orphan_fe, -1);
2130 orphan_dir_inode->i_nlink = ocfs2_read_links_count(orphan_fe);
ec20cec7 2131 ocfs2_journal_dirty(handle, orphan_dir_bh);
ccd979bd
MF
2132
2133leave:
4a12ca3a 2134 ocfs2_free_dir_lookup_result(&lookup);
ccd979bd 2135
c1e8d35e
TM
2136 if (status)
2137 mlog_errno(status);
ccd979bd
MF
2138 return status;
2139}
2140
97b8f4a9
MF
2141/**
2142 * ocfs2_prep_new_orphaned_file() - Prepare the orphan dir to recieve a newly
2143 * allocated file. This is different from the typical 'add to orphan dir'
2144 * operation in that the inode does not yet exist. This is a problem because
2145 * the orphan dir stringifies the inode block number to come up with it's
2146 * dirent. Obviously if the inode does not yet exist we have a chicken and egg
2147 * problem. This function works around it by calling deeper into the orphan
2148 * and suballoc code than other callers. Use this only by necessity.
2149 * @dir: The directory which this inode will ultimately wind up under - not the
2150 * orphan dir!
2151 * @dir_bh: buffer_head the @dir inode block
2152 * @orphan_name: string of length (CFS2_ORPHAN_NAMELEN + 1). Will be filled
2153 * with the string to be used for orphan dirent. Pass back to the orphan dir
2154 * code.
2155 * @ret_orphan_dir: orphan dir inode returned to be passed back into orphan
2156 * dir code.
2157 * @ret_di_blkno: block number where the new inode will be allocated.
2158 * @orphan_insert: Dir insert context to be passed back into orphan dir code.
2159 * @ret_inode_ac: Inode alloc context to be passed back to the allocator.
2160 *
2161 * Returns zero on success and the ret_orphan_dir, name and lookup
2162 * fields will be populated.
2163 *
2164 * Returns non-zero on failure.
2165 */
2166static int ocfs2_prep_new_orphaned_file(struct inode *dir,
2167 struct buffer_head *dir_bh,
2168 char *orphan_name,
2169 struct inode **ret_orphan_dir,
2170 u64 *ret_di_blkno,
2171 struct ocfs2_dir_lookup_result *orphan_insert,
2172 struct ocfs2_alloc_context **ret_inode_ac)
2173{
2174 int ret;
2175 u64 di_blkno;
2176 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2177 struct inode *orphan_dir = NULL;
2178 struct buffer_head *orphan_dir_bh = NULL;
2179 struct ocfs2_alloc_context *inode_ac = NULL;
2180
2181 ret = ocfs2_lookup_lock_orphan_dir(osb, &orphan_dir, &orphan_dir_bh);
2182 if (ret < 0) {
2183 mlog_errno(ret);
2184 return ret;
2185 }
2186
2187 /* reserve an inode spot */
2188 ret = ocfs2_reserve_new_inode(osb, &inode_ac);
2189 if (ret < 0) {
2190 if (ret != -ENOSPC)
2191 mlog_errno(ret);
2192 goto out;
2193 }
2194
2195 ret = ocfs2_find_new_inode_loc(dir, dir_bh, inode_ac,
2196 &di_blkno);
2197 if (ret) {
2198 mlog_errno(ret);
2199 goto out;
2200 }
2201
2202 ret = __ocfs2_prepare_orphan_dir(orphan_dir, orphan_dir_bh,
2203 di_blkno, orphan_name, orphan_insert);
2204 if (ret < 0) {
2205 mlog_errno(ret);
2206 goto out;
2207 }
2208
2209out:
2210 if (ret == 0) {
2211 *ret_orphan_dir = orphan_dir;
2212 *ret_di_blkno = di_blkno;
2213 *ret_inode_ac = inode_ac;
2214 /*
2215 * orphan_name and orphan_insert are already up to
2216 * date via prepare_orphan_dir
2217 */
2218 } else {
2219 /* Unroll reserve_new_inode* */
2220 if (inode_ac)
2221 ocfs2_free_alloc_context(inode_ac);
2222
2223 /* Unroll orphan dir locking */
2224 mutex_unlock(&orphan_dir->i_mutex);
2225 ocfs2_inode_unlock(orphan_dir, 1);
2226 iput(orphan_dir);
2227 }
2228
2229 brelse(orphan_dir_bh);
2230
2231 return 0;
2232}
2233
bc13d347
TM
2234int ocfs2_create_inode_in_orphan(struct inode *dir,
2235 int mode,
2236 struct inode **new_inode)
2237{
2238 int status, did_quota_inode = 0;
2239 struct inode *inode = NULL;
2240 struct inode *orphan_dir = NULL;
2241 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2242 struct ocfs2_dinode *di = NULL;
2243 handle_t *handle = NULL;
2244 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
2245 struct buffer_head *parent_di_bh = NULL;
2246 struct buffer_head *new_di_bh = NULL;
2247 struct ocfs2_alloc_context *inode_ac = NULL;
2248 struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
97b8f4a9
MF
2249 u64 uninitialized_var(di_blkno), suballoc_loc;
2250 u16 suballoc_bit;
bc13d347
TM
2251
2252 status = ocfs2_inode_lock(dir, &parent_di_bh, 1);
2253 if (status < 0) {
2254 if (status != -ENOENT)
2255 mlog_errno(status);
2256 return status;
2257 }
2258
97b8f4a9
MF
2259 status = ocfs2_prep_new_orphaned_file(dir, parent_di_bh,
2260 orphan_name, &orphan_dir,
2261 &di_blkno, &orphan_insert, &inode_ac);
bc13d347
TM
2262 if (status < 0) {
2263 if (status != -ENOSPC)
2264 mlog_errno(status);
2265 goto leave;
2266 }
2267
2268 inode = ocfs2_get_init_inode(dir, mode);
2269 if (!inode) {
2270 status = -ENOMEM;
2271 mlog_errno(status);
2272 goto leave;
2273 }
2274
2275 handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb, 0, 0));
2276 if (IS_ERR(handle)) {
2277 status = PTR_ERR(handle);
2278 handle = NULL;
2279 mlog_errno(status);
2280 goto leave;
2281 }
2282
63936dda
CH
2283 status = dquot_alloc_inode(inode);
2284 if (status)
bc13d347 2285 goto leave;
bc13d347
TM
2286 did_quota_inode = 1;
2287
97b8f4a9
MF
2288 status = ocfs2_claim_new_inode_at_loc(handle, dir, inode_ac,
2289 &suballoc_loc,
2290 &suballoc_bit, di_blkno);
bc13d347
TM
2291 if (status < 0) {
2292 mlog_errno(status);
2293 goto leave;
2294 }
2295
97b8f4a9
MF
2296 inode->i_nlink = 0;
2297 /* do the real work now. */
2298 status = __ocfs2_mknod_locked(dir, inode,
2299 0, &new_di_bh, parent_di_bh, handle,
2300 inode_ac, di_blkno, suballoc_loc,
2301 suballoc_bit);
bc13d347
TM
2302 if (status < 0) {
2303 mlog_errno(status);
2304 goto leave;
2305 }
2306
2307 di = (struct ocfs2_dinode *)new_di_bh->b_data;
3939fda4 2308 status = ocfs2_orphan_add(osb, handle, inode, new_di_bh, orphan_name,
bc13d347
TM
2309 &orphan_insert, orphan_dir);
2310 if (status < 0) {
2311 mlog_errno(status);
2312 goto leave;
2313 }
2314
2315 /* get open lock so that only nodes can't remove it from orphan dir. */
2316 status = ocfs2_open_lock(inode);
2317 if (status < 0)
2318 mlog_errno(status);
2319
c7d260af 2320 insert_inode_hash(inode);
bc13d347
TM
2321leave:
2322 if (status < 0 && did_quota_inode)
63936dda 2323 dquot_free_inode(inode);
bc13d347
TM
2324 if (handle)
2325 ocfs2_commit_trans(osb, handle);
2326
2327 if (orphan_dir) {
2328 /* This was locked for us in ocfs2_prepare_orphan_dir() */
2329 ocfs2_inode_unlock(orphan_dir, 1);
2330 mutex_unlock(&orphan_dir->i_mutex);
2331 iput(orphan_dir);
2332 }
2333
2334 if (status == -ENOSPC)
2335 mlog(0, "Disk is full\n");
2336
2337 if ((status < 0) && inode) {
2338 clear_nlink(inode);
2339 iput(inode);
2340 }
2341
2342 if (inode_ac)
2343 ocfs2_free_alloc_context(inode_ac);
2344
2345 brelse(new_di_bh);
2346
2347 if (!status)
2348 *new_inode = inode;
2349
2350 ocfs2_free_dir_lookup_result(&orphan_insert);
2351
2352 ocfs2_inode_unlock(dir, 1);
2353 brelse(parent_di_bh);
2354 return status;
2355}
2356
2357int ocfs2_mv_orphaned_inode_to_new(struct inode *dir,
2358 struct inode *inode,
2359 struct dentry *dentry)
2360{
2361 int status = 0;
2362 struct buffer_head *parent_di_bh = NULL;
2363 handle_t *handle = NULL;
2364 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2365 struct ocfs2_dinode *dir_di, *di;
2366 struct inode *orphan_dir_inode = NULL;
2367 struct buffer_head *orphan_dir_bh = NULL;
2368 struct buffer_head *di_bh = NULL;
2369 struct ocfs2_dir_lookup_result lookup = { NULL, };
2370
ef6b689b
TM
2371 mlog(0, "(0x%p, 0x%p, %.*s')\n", dir, dentry,
2372 dentry->d_name.len, dentry->d_name.name);
bc13d347
TM
2373
2374 status = ocfs2_inode_lock(dir, &parent_di_bh, 1);
2375 if (status < 0) {
2376 if (status != -ENOENT)
2377 mlog_errno(status);
2378 return status;
2379 }
2380
2381 dir_di = (struct ocfs2_dinode *) parent_di_bh->b_data;
2382 if (!dir_di->i_links_count) {
2383 /* can't make a file in a deleted directory. */
2384 status = -ENOENT;
2385 goto leave;
2386 }
2387
2388 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
2389 dentry->d_name.len);
2390 if (status)
2391 goto leave;
2392
2393 /* get a spot inside the dir. */
2394 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_di_bh,
2395 dentry->d_name.name,
2396 dentry->d_name.len, &lookup);
2397 if (status < 0) {
2398 mlog_errno(status);
2399 goto leave;
2400 }
2401
2402 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
2403 ORPHAN_DIR_SYSTEM_INODE,
2404 osb->slot_num);
2405 if (!orphan_dir_inode) {
2406 status = -EEXIST;
2407 mlog_errno(status);
2408 goto leave;
2409 }
2410
2411 mutex_lock(&orphan_dir_inode->i_mutex);
2412
2413 status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
2414 if (status < 0) {
2415 mlog_errno(status);
2416 mutex_unlock(&orphan_dir_inode->i_mutex);
2417 iput(orphan_dir_inode);
2418 goto leave;
2419 }
2420
2421 status = ocfs2_read_inode_block(inode, &di_bh);
2422 if (status < 0) {
2423 mlog_errno(status);
2424 goto orphan_unlock;
2425 }
2426
2427 handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb));
2428 if (IS_ERR(handle)) {
2429 status = PTR_ERR(handle);
2430 handle = NULL;
2431 mlog_errno(status);
2432 goto orphan_unlock;
2433 }
2434
2435 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
2436 di_bh, OCFS2_JOURNAL_ACCESS_WRITE);
2437 if (status < 0) {
2438 mlog_errno(status);
2439 goto out_commit;
2440 }
2441
2442 status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
2443 orphan_dir_bh);
2444 if (status < 0) {
2445 mlog_errno(status);
2446 goto out_commit;
2447 }
2448
2449 di = (struct ocfs2_dinode *)di_bh->b_data;
2450 le32_add_cpu(&di->i_flags, -OCFS2_ORPHANED_FL);
2451 di->i_orphaned_slot = 0;
10cf1a02
TM
2452 inode->i_nlink = 1;
2453 ocfs2_set_links_count(di, inode->i_nlink);
bc13d347
TM
2454 ocfs2_journal_dirty(handle, di_bh);
2455
2456 status = ocfs2_add_entry(handle, dentry, inode,
2457 OCFS2_I(inode)->ip_blkno, parent_di_bh,
2458 &lookup);
2459 if (status < 0) {
2460 mlog_errno(status);
2461 goto out_commit;
2462 }
2463
2464 status = ocfs2_dentry_attach_lock(dentry, inode,
2465 OCFS2_I(dir)->ip_blkno);
2466 if (status) {
2467 mlog_errno(status);
2468 goto out_commit;
2469 }
2470
bc13d347
TM
2471 d_instantiate(dentry, inode);
2472 status = 0;
2473out_commit:
2474 ocfs2_commit_trans(osb, handle);
2475orphan_unlock:
2476 ocfs2_inode_unlock(orphan_dir_inode, 1);
2477 mutex_unlock(&orphan_dir_inode->i_mutex);
2478 iput(orphan_dir_inode);
2479leave:
2480
2481 ocfs2_inode_unlock(dir, 1);
2482
2483 brelse(di_bh);
2484 brelse(parent_di_bh);
2485 brelse(orphan_dir_bh);
2486
2487 ocfs2_free_dir_lookup_result(&lookup);
2488
c1e8d35e
TM
2489 if (status)
2490 mlog_errno(status);
bc13d347
TM
2491
2492 return status;
2493}
2494
92e1d5be 2495const struct inode_operations ocfs2_dir_iops = {
ccd979bd
MF
2496 .create = ocfs2_create,
2497 .lookup = ocfs2_lookup,
2498 .link = ocfs2_link,
2499 .unlink = ocfs2_unlink,
2500 .rmdir = ocfs2_unlink,
2501 .symlink = ocfs2_symlink,
2502 .mkdir = ocfs2_mkdir,
2503 .mknod = ocfs2_mknod,
2504 .rename = ocfs2_rename,
2505 .setattr = ocfs2_setattr,
2506 .getattr = ocfs2_getattr,
d38eb8db 2507 .permission = ocfs2_permission,
cf1d6c76
TY
2508 .setxattr = generic_setxattr,
2509 .getxattr = generic_getxattr,
2510 .listxattr = ocfs2_listxattr,
2511 .removexattr = generic_removexattr,
55f4946e 2512 .fiemap = ocfs2_fiemap,
ccd979bd 2513};