blob: 607084b349d4fd424765bddc7447dc9fd11d023c [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- 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>
Jan Karaa90714c2008-10-09 19:38:40 +020043#include <linux/quotaops.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080044
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"
Mark Fashehaa958872006-04-21 13:49:02 -070060#include "super.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080061#include "symlink.h"
62#include "sysfile.h"
63#include "uptodate.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080064#include "xattr.h"
Tiger Yang89c38bd2008-11-14 11:17:41 +080065#include "acl.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080066
67#include "buffer_head_io.h"
68
Mark Fashehccd979b2005-12-15 14:31:24 -080069static int ocfs2_mknod_locked(struct ocfs2_super *osb,
70 struct inode *dir,
Tiger Yangf5d36202008-11-14 11:15:44 +080071 struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -080072 dev_t dev,
73 struct buffer_head **new_fe_bh,
74 struct buffer_head *parent_fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -070075 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080076 struct ocfs2_alloc_context *inode_ac);
77
Mark Fashehccd979b2005-12-15 14:31:24 -080078static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
Mark Fasheh5098c272006-10-05 18:12:57 -070079 struct inode **ret_orphan_dir,
Tao Ma19bd3412009-08-18 11:44:10 +080080 u64 blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -080081 char *name,
Mark Fasheh4a12ca32008-11-12 15:43:34 -080082 struct ocfs2_dir_lookup_result *lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -080083
84static int ocfs2_orphan_add(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -070085 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080086 struct inode *inode,
87 struct ocfs2_dinode *fe,
88 char *name,
Mark Fasheh4a12ca32008-11-12 15:43:34 -080089 struct ocfs2_dir_lookup_result *lookup,
Mark Fasheh5098c272006-10-05 18:12:57 -070090 struct inode *orphan_dir_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -080091
92static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -070093 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080094 struct inode *inode,
95 const char *symname);
96
Mark Fashehccd979b2005-12-15 14:31:24 -080097/* 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;
Mark Fashehccd979b2005-12-15 14:31:24 -0800105 struct inode *inode = NULL;
106 struct dentry *ret;
Mark Fashehccd979b2005-12-15 14:31:24 -0800107 struct ocfs2_inode_info *oi;
108
109 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
110 dentry->d_name.len, dentry->d_name.name);
111
112 if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
113 ret = ERR_PTR(-ENAMETOOLONG);
114 goto bail;
115 }
116
Mark Fashehb0697052006-03-03 10:24:33 -0800117 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);
Mark Fashehccd979b2005-12-15 14:31:24 -0800119
Jan Karacb257972009-06-04 15:26:50 +0200120 status = ocfs2_inode_lock_nested(dir, NULL, 0, OI_LS_PARENT);
Mark Fashehccd979b2005-12-15 14:31:24 -0800121 if (status < 0) {
122 if (status != -ENOENT)
123 mlog_errno(status);
124 ret = ERR_PTR(status);
125 goto bail;
126 }
127
Mark Fashehbe94d112007-09-11 15:22:06 -0700128 status = ocfs2_lookup_ino_from_name(dir, dentry->d_name.name,
129 dentry->d_name.len, &blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800130 if (status < 0)
131 goto bail_add;
132
Jan Kara5fa06132008-01-11 00:11:45 +0100133 inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800134 if (IS_ERR(inode)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800135 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;
Mark Fashehccd979b2005-12-15 14:31:24 -0800147 spin_unlock(&oi->ip_lock);
148
149bail_add:
Mark Fashehccd979b2005-12-15 14:31:24 -0800150 dentry->d_op = &ocfs2_dentry_ops;
151 ret = d_splice_alias(inode, dentry);
152
Mark Fasheh379dfe92006-09-08 14:21:03 -0700153 if (inode) {
154 /*
155 * If d_splice_alias() finds a DCACHE_DISCONNECTED
156 * dentry, it will d_move() it on top of ourse. The
157 * return value will indicate this however, so in
158 * those cases, we switch them around for the locking
159 * code.
160 *
161 * NOTE: This dentry already has ->d_op set from
162 * ocfs2_get_parent() and ocfs2_get_dentry()
163 */
164 if (ret)
165 dentry = ret;
166
167 status = ocfs2_dentry_attach_lock(dentry, inode,
Mark Fasheh0027dd52006-09-21 16:51:28 -0700168 OCFS2_I(dir)->ip_blkno);
Mark Fasheh379dfe92006-09-08 14:21:03 -0700169 if (status) {
170 mlog_errno(status);
171 ret = ERR_PTR(status);
172 goto bail_unlock;
173 }
174 }
175
Mark Fashehccd979b2005-12-15 14:31:24 -0800176bail_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
Mark Fasheh34d024f2007-09-24 15:56:19 -0700180 * the downconvert thread and have a stale dentry. */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700181 ocfs2_inode_unlock(dir, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800182
183bail:
Mark Fashehccd979b2005-12-15 14:31:24 -0800184
185 mlog_exit_ptr(ret);
186
187 return ret;
188}
189
Tiger Yangf5d36202008-11-14 11:15:44 +0800190static 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;
207 inode->i_uid = current_fsuid();
208 if (dir->i_mode & S_ISGID) {
209 inode->i_gid = dir->i_gid;
210 if (S_ISDIR(mode))
211 mode |= S_ISGID;
212 } else
213 inode->i_gid = current_fsgid();
214 inode->i_mode = mode;
Christoph Hellwig871a2932010-03-03 09:05:07 -0500215 dquot_initialize(inode);
Tiger Yangf5d36202008-11-14 11:15:44 +0800216 return inode;
217}
218
Mark Fashehccd979b2005-12-15 14:31:24 -0800219static int ocfs2_mknod(struct inode *dir,
220 struct dentry *dentry,
221 int mode,
222 dev_t dev)
223{
224 int status = 0;
225 struct buffer_head *parent_fe_bh = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700226 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800227 struct ocfs2_super *osb;
228 struct ocfs2_dinode *dirfe;
229 struct buffer_head *new_fe_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800230 struct inode *inode = NULL;
231 struct ocfs2_alloc_context *inode_ac = NULL;
232 struct ocfs2_alloc_context *data_ac = NULL;
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800233 struct ocfs2_alloc_context *meta_ac = NULL;
Tiger Yang534eadd2008-11-14 11:16:41 +0800234 int want_clusters = 0;
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800235 int want_meta = 0;
Tiger Yang534eadd2008-11-14 11:16:41 +0800236 int xattr_credits = 0;
237 struct ocfs2_security_xattr_info si = {
238 .enable = 1,
239 };
Jan Karaa90714c2008-10-09 19:38:40 +0200240 int did_quota_inode = 0;
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800241 struct ocfs2_dir_lookup_result lookup = { NULL, };
Joel Becker547ba7c2010-05-10 11:56:52 -0700242 sigset_t oldset;
243 int did_block_signals = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800244
245 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
246 (unsigned long)dev, dentry->d_name.len,
247 dentry->d_name.name);
248
Christoph Hellwig871a2932010-03-03 09:05:07 -0500249 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500250
Mark Fashehccd979b2005-12-15 14:31:24 -0800251 /* get our super block */
252 osb = OCFS2_SB(dir->i_sb);
253
Mark Fashehe63aecb62007-10-18 15:30:42 -0700254 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
Mark Fashehe3a82132006-10-05 16:04:17 -0700255 if (status < 0) {
256 if (status != -ENOENT)
257 mlog_errno(status);
258 return status;
259 }
260
Mark Fasheh198a1ca2008-11-20 17:54:57 -0800261 if (S_ISDIR(mode) && (dir->i_nlink >= ocfs2_link_max(osb))) {
Mark Fasheha663e302006-08-09 11:45:07 -0700262 status = -EMLINK;
263 goto leave;
264 }
265
Mark Fashehccd979b2005-12-15 14:31:24 -0800266 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
Mark Fasheh198a1ca2008-11-20 17:54:57 -0800267 if (!ocfs2_read_links_count(dirfe)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800268 /* can't make a file in a deleted directory. */
269 status = -ENOENT;
270 goto leave;
271 }
272
273 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
274 dentry->d_name.len);
275 if (status)
276 goto leave;
277
278 /* get a spot inside the dir. */
279 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
280 dentry->d_name.name,
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800281 dentry->d_name.len, &lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -0800282 if (status < 0) {
283 mlog_errno(status);
284 goto leave;
285 }
286
287 /* reserve an inode spot */
Mark Fashehda5cbf22006-10-06 18:34:35 -0700288 status = ocfs2_reserve_new_inode(osb, &inode_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800289 if (status < 0) {
290 if (status != -ENOSPC)
291 mlog_errno(status);
292 goto leave;
293 }
294
Tiger Yangf5d36202008-11-14 11:15:44 +0800295 inode = ocfs2_get_init_inode(dir, mode);
296 if (!inode) {
297 status = -ENOMEM;
298 mlog_errno(status);
299 goto leave;
300 }
301
Tiger Yang534eadd2008-11-14 11:16:41 +0800302 /* get security xattr */
303 status = ocfs2_init_security_get(inode, dir, &si);
304 if (status) {
305 if (status == -EOPNOTSUPP)
306 si.enable = 0;
307 else {
308 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800309 goto leave;
310 }
311 }
312
Tiger Yang89c38bd2008-11-14 11:17:41 +0800313 /* calculate meta data/clusters for setting security and acl xattr */
314 status = ocfs2_calc_xattr_init(dir, parent_fe_bh, mode,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800315 &si, &want_clusters,
316 &xattr_credits, &want_meta);
Tiger Yang89c38bd2008-11-14 11:17:41 +0800317 if (status < 0) {
318 mlog_errno(status);
319 goto leave;
Tiger Yang534eadd2008-11-14 11:16:41 +0800320 }
321
322 /* Reserve a cluster if creating an extent based directory. */
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800323 if (S_ISDIR(mode) && !ocfs2_supports_inline_data(osb)) {
Tiger Yang534eadd2008-11-14 11:16:41 +0800324 want_clusters += 1;
325
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800326 /* Dir indexing requires extra space as well */
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800327 if (ocfs2_supports_indexed_dirs(osb))
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800328 want_meta++;
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800329 }
330
331 status = ocfs2_reserve_new_metadata_blocks(osb, want_meta, &meta_ac);
332 if (status < 0) {
333 if (status != -ENOSPC)
334 mlog_errno(status);
335 goto leave;
336 }
337
Tiger Yang534eadd2008-11-14 11:16:41 +0800338 status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
339 if (status < 0) {
340 if (status != -ENOSPC)
341 mlog_errno(status);
342 goto leave;
343 }
344
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800345 handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb,
346 S_ISDIR(mode),
347 xattr_credits));
Mark Fashehccd979b2005-12-15 14:31:24 -0800348 if (IS_ERR(handle)) {
349 status = PTR_ERR(handle);
350 handle = NULL;
351 mlog_errno(status);
352 goto leave;
353 }
354
Joel Becker547ba7c2010-05-10 11:56:52 -0700355 /* Starting to change things, restart is no longer possible. */
356 ocfs2_block_signals(&oldset);
357 did_block_signals = 1;
358
Christoph Hellwig63936dd2010-03-03 09:05:01 -0500359 status = dquot_alloc_inode(inode);
360 if (status)
Jan Karaa90714c2008-10-09 19:38:40 +0200361 goto leave;
Jan Karaa90714c2008-10-09 19:38:40 +0200362 did_quota_inode = 1;
363
Tao Ma19bd3412009-08-18 11:44:10 +0800364 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry,
365 inode->i_mode, (unsigned long)dev, dentry->d_name.len,
366 dentry->d_name.name);
367
Mark Fashehccd979b2005-12-15 14:31:24 -0800368 /* do the real work now. */
Tao Ma19bd3412009-08-18 11:44:10 +0800369 status = ocfs2_mknod_locked(osb, dir, inode, dev,
Mark Fashehccd979b2005-12-15 14:31:24 -0800370 &new_fe_bh, parent_fe_bh, handle,
Tiger Yangf5d36202008-11-14 11:15:44 +0800371 inode_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800372 if (status < 0) {
373 mlog_errno(status);
374 goto leave;
375 }
376
377 if (S_ISDIR(mode)) {
378 status = ocfs2_fill_new_dir(osb, handle, dir, inode,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800379 new_fe_bh, data_ac, meta_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800380 if (status < 0) {
381 mlog_errno(status);
382 goto leave;
383 }
384
Joel Becker0cf2f762009-02-12 16:41:25 -0800385 status = ocfs2_journal_access_di(handle, INODE_CACHE(dir),
386 parent_fe_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700387 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800388 if (status < 0) {
389 mlog_errno(status);
390 goto leave;
391 }
Mark Fasheh198a1ca2008-11-20 17:54:57 -0800392 ocfs2_add_links_count(dirfe, 1);
Joel Beckerec20cec2010-03-19 14:13:52 -0700393 ocfs2_journal_dirty(handle, parent_fe_bh);
Dave Hansend8c76e62006-09-30 23:29:04 -0700394 inc_nlink(dir);
Mark Fashehccd979b2005-12-15 14:31:24 -0800395 }
396
Tiger Yang89c38bd2008-11-14 11:17:41 +0800397 status = ocfs2_init_acl(handle, inode, dir, new_fe_bh, parent_fe_bh,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800398 meta_ac, data_ac);
Tiger Yang89c38bd2008-11-14 11:17:41 +0800399 if (status < 0) {
400 mlog_errno(status);
401 goto leave;
402 }
403
Tiger Yang534eadd2008-11-14 11:16:41 +0800404 if (si.enable) {
405 status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800406 meta_ac, data_ac);
Tiger Yang534eadd2008-11-14 11:16:41 +0800407 if (status < 0) {
408 mlog_errno(status);
409 goto leave;
410 }
411 }
412
Mark Fashehccd979b2005-12-15 14:31:24 -0800413 status = ocfs2_add_entry(handle, dentry, inode,
414 OCFS2_I(inode)->ip_blkno, parent_fe_bh,
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800415 &lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -0800416 if (status < 0) {
417 mlog_errno(status);
418 goto leave;
419 }
420
Mark Fasheh379dfe92006-09-08 14:21:03 -0700421 status = ocfs2_dentry_attach_lock(dentry, inode,
Mark Fasheh0027dd52006-09-21 16:51:28 -0700422 OCFS2_I(dir)->ip_blkno);
Mark Fasheh379dfe92006-09-08 14:21:03 -0700423 if (status) {
424 mlog_errno(status);
425 goto leave;
426 }
427
Mark Fashehccd979b2005-12-15 14:31:24 -0800428 insert_inode_hash(inode);
429 dentry->d_op = &ocfs2_dentry_ops;
430 d_instantiate(dentry, inode);
431 status = 0;
432leave:
Jan Karaa90714c2008-10-09 19:38:40 +0200433 if (status < 0 && did_quota_inode)
Christoph Hellwig63936dd2010-03-03 09:05:01 -0500434 dquot_free_inode(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800435 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700436 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800437
Mark Fashehe63aecb62007-10-18 15:30:42 -0700438 ocfs2_inode_unlock(dir, 1);
Joel Becker547ba7c2010-05-10 11:56:52 -0700439 if (did_block_signals)
440 ocfs2_unblock_signals(&oldset);
Mark Fashehe3a82132006-10-05 16:04:17 -0700441
Mark Fashehccd979b2005-12-15 14:31:24 -0800442 if (status == -ENOSPC)
443 mlog(0, "Disk is full\n");
444
Mark Fasheha81cb882008-10-07 14:25:16 -0700445 brelse(new_fe_bh);
Mark Fasheha81cb882008-10-07 14:25:16 -0700446 brelse(parent_fe_bh);
Tiger Yang534eadd2008-11-14 11:16:41 +0800447 kfree(si.name);
448 kfree(si.value);
Mark Fashehccd979b2005-12-15 14:31:24 -0800449
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800450 ocfs2_free_dir_lookup_result(&lookup);
451
Tiger Yangf5d36202008-11-14 11:15:44 +0800452 if ((status < 0) && inode) {
453 clear_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800454 iput(inode);
Tiger Yangf5d36202008-11-14 11:15:44 +0800455 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800456
457 if (inode_ac)
458 ocfs2_free_alloc_context(inode_ac);
459
460 if (data_ac)
461 ocfs2_free_alloc_context(data_ac);
462
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800463 if (meta_ac)
464 ocfs2_free_alloc_context(meta_ac);
Tiger Yang534eadd2008-11-14 11:16:41 +0800465
Mark Fashehccd979b2005-12-15 14:31:24 -0800466 mlog_exit(status);
467
468 return status;
469}
470
471static int ocfs2_mknod_locked(struct ocfs2_super *osb,
472 struct inode *dir,
Tiger Yangf5d36202008-11-14 11:15:44 +0800473 struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800474 dev_t dev,
475 struct buffer_head **new_fe_bh,
476 struct buffer_head *parent_fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700477 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800478 struct ocfs2_alloc_context *inode_ac)
479{
480 int status = 0;
481 struct ocfs2_dinode *fe = NULL;
482 struct ocfs2_extent_list *fel;
483 u64 fe_blkno = 0;
484 u16 suballoc_bit;
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800485 u16 feat;
Mark Fashehccd979b2005-12-15 14:31:24 -0800486
Mark Fashehccd979b2005-12-15 14:31:24 -0800487 *new_fe_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800488
Tao Ma13821152009-02-25 00:53:23 +0800489 status = ocfs2_claim_new_inode(osb, handle, dir, parent_fe_bh,
490 inode_ac, &suballoc_bit, &fe_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800491 if (status < 0) {
492 mlog_errno(status);
493 goto leave;
494 }
495
Mark Fashehccd979b2005-12-15 14:31:24 -0800496 /* populate as many fields early on as possible - many of
497 * these are used by the support functions here and in
498 * callers. */
499 inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
500 OCFS2_I(inode)->ip_blkno = fe_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -0800501 spin_lock(&osb->osb_lock);
502 inode->i_generation = osb->s_next_generation++;
503 spin_unlock(&osb->osb_lock);
504
505 *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
506 if (!*new_fe_bh) {
507 status = -EIO;
508 mlog_errno(status);
509 goto leave;
510 }
Joel Becker8cb471e2009-02-10 20:00:41 -0800511 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), *new_fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800512
Joel Becker0cf2f762009-02-12 16:41:25 -0800513 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
514 *new_fe_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700515 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800516 if (status < 0) {
517 mlog_errno(status);
518 goto leave;
519 }
520
521 fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
522 memset(fe, 0, osb->sb->s_blocksize);
523
524 fe->i_generation = cpu_to_le32(inode->i_generation);
525 fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
526 fe->i_blkno = cpu_to_le64(fe_blkno);
527 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800528 fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot);
Tiger Yangf5d36202008-11-14 11:15:44 +0800529 fe->i_uid = cpu_to_le32(inode->i_uid);
530 fe->i_gid = cpu_to_le32(inode->i_gid);
531 fe->i_mode = cpu_to_le16(inode->i_mode);
532 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
Mark Fashehccd979b2005-12-15 14:31:24 -0800533 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
Mark Fasheh198a1ca2008-11-20 17:54:57 -0800534
535 ocfs2_set_links_count(fe, inode->i_nlink);
Mark Fashehccd979b2005-12-15 14:31:24 -0800536
537 fe->i_last_eb_blk = 0;
538 strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
539 le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
540 fe->i_atime = fe->i_ctime = fe->i_mtime =
541 cpu_to_le64(CURRENT_TIME.tv_sec);
542 fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
543 cpu_to_le32(CURRENT_TIME.tv_nsec);
544 fe->i_dtime = 0;
545
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700546 /*
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800547 * If supported, directories start with inline data. If inline
548 * isn't supported, but indexing is, we start them as indexed.
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700549 */
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800550 feat = le16_to_cpu(fe->i_dyn_features);
Tiger Yangf5d36202008-11-14 11:15:44 +0800551 if (S_ISDIR(inode->i_mode) && ocfs2_supports_inline_data(osb)) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700552 fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL);
553
Tiger Yangd9ae49d2009-03-05 11:06:15 +0800554 fe->id2.i_data.id_count = cpu_to_le16(
555 ocfs2_max_inline_data_with_xattr(osb->sb, fe));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700556 } else {
557 fel = &fe->id2.i_list;
558 fel->l_tree_depth = 0;
559 fel->l_next_free_rec = 0;
560 fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
561 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800562
Joel Beckerec20cec2010-03-19 14:13:52 -0700563 ocfs2_journal_dirty(handle, *new_fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800564
Joel Beckerb657c952008-11-13 14:49:11 -0800565 ocfs2_populate_inode(inode, fe, 1);
Joel Becker0cf2f762009-02-12 16:41:25 -0800566 ocfs2_ci_set_new(osb, INODE_CACHE(inode));
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800567 if (!ocfs2_mount_local(osb)) {
568 status = ocfs2_create_new_inode_locks(inode);
569 if (status < 0)
570 mlog_errno(status);
571 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800572
573 status = 0; /* error in ocfs2_create_new_inode_locks is not
574 * critical */
575
Mark Fashehccd979b2005-12-15 14:31:24 -0800576leave:
577 if (status < 0) {
578 if (*new_fe_bh) {
579 brelse(*new_fe_bh);
580 *new_fe_bh = NULL;
581 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800582 }
583
584 mlog_exit(status);
585 return status;
586}
587
588static int ocfs2_mkdir(struct inode *dir,
589 struct dentry *dentry,
590 int mode)
591{
592 int ret;
593
594 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
595 dentry->d_name.len, dentry->d_name.name);
596 ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
597 mlog_exit(ret);
598
599 return ret;
600}
601
602static int ocfs2_create(struct inode *dir,
603 struct dentry *dentry,
604 int mode,
605 struct nameidata *nd)
606{
607 int ret;
608
609 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
610 dentry->d_name.len, dentry->d_name.name);
611 ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
612 mlog_exit(ret);
613
614 return ret;
615}
616
617static int ocfs2_link(struct dentry *old_dentry,
618 struct inode *dir,
619 struct dentry *dentry)
620{
Mark Fasheh1fabe142006-10-09 18:11:45 -0700621 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800622 struct inode *inode = old_dentry->d_inode;
623 int err;
624 struct buffer_head *fe_bh = NULL;
625 struct buffer_head *parent_fe_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800626 struct ocfs2_dinode *fe = NULL;
627 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800628 struct ocfs2_dir_lookup_result lookup = { NULL, };
Joel Becker547ba7c2010-05-10 11:56:52 -0700629 sigset_t oldset;
Mark Fashehccd979b2005-12-15 14:31:24 -0800630
631 mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
632 old_dentry->d_name.len, old_dentry->d_name.name,
633 dentry->d_name.len, dentry->d_name.name);
634
Mark Fasheh123a9642006-10-05 16:48:23 -0700635 if (S_ISDIR(inode->i_mode))
636 return -EPERM;
Mark Fashehccd979b2005-12-15 14:31:24 -0800637
Christoph Hellwig871a2932010-03-03 09:05:07 -0500638 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500639
Jan Karacb257972009-06-04 15:26:50 +0200640 err = ocfs2_inode_lock_nested(dir, &parent_fe_bh, 1, OI_LS_PARENT);
Mark Fashehccd979b2005-12-15 14:31:24 -0800641 if (err < 0) {
642 if (err != -ENOENT)
643 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700644 return err;
Mark Fashehccd979b2005-12-15 14:31:24 -0800645 }
646
Tiger Yang0f62de22006-08-31 20:39:47 -0700647 if (!dir->i_nlink) {
648 err = -ENOENT;
Mark Fasheh123a9642006-10-05 16:48:23 -0700649 goto out;
Tiger Yang0f62de22006-08-31 20:39:47 -0700650 }
651
Mark Fashehccd979b2005-12-15 14:31:24 -0800652 err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
653 dentry->d_name.len);
654 if (err)
Mark Fasheh123a9642006-10-05 16:48:23 -0700655 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800656
657 err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
658 dentry->d_name.name,
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800659 dentry->d_name.len, &lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -0800660 if (err < 0) {
661 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700662 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800663 }
664
Mark Fashehe63aecb62007-10-18 15:30:42 -0700665 err = ocfs2_inode_lock(inode, &fe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800666 if (err < 0) {
667 if (err != -ENOENT)
668 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700669 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800670 }
671
672 fe = (struct ocfs2_dinode *) fe_bh->b_data;
Mark Fasheh198a1ca2008-11-20 17:54:57 -0800673 if (ocfs2_read_links_count(fe) >= ocfs2_link_max(osb)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800674 err = -EMLINK;
Mark Fasheh123a9642006-10-05 16:48:23 -0700675 goto out_unlock_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800676 }
677
Jan Karaa90714c2008-10-09 19:38:40 +0200678 handle = ocfs2_start_trans(osb, ocfs2_link_credits(osb->sb));
Mark Fashehccd979b2005-12-15 14:31:24 -0800679 if (IS_ERR(handle)) {
680 err = PTR_ERR(handle);
681 handle = NULL;
682 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700683 goto out_unlock_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800684 }
685
Joel Becker547ba7c2010-05-10 11:56:52 -0700686 /* Starting to change things, restart is no longer possible. */
687 ocfs2_block_signals(&oldset);
688
Joel Becker0cf2f762009-02-12 16:41:25 -0800689 err = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700690 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800691 if (err < 0) {
692 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700693 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800694 }
695
Dave Hansend8c76e62006-09-30 23:29:04 -0700696 inc_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800697 inode->i_ctime = CURRENT_TIME;
Mark Fasheh198a1ca2008-11-20 17:54:57 -0800698 ocfs2_set_links_count(fe, inode->i_nlink);
Mark Fashehccd979b2005-12-15 14:31:24 -0800699 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
700 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
Joel Beckerec20cec2010-03-19 14:13:52 -0700701 ocfs2_journal_dirty(handle, fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800702
703 err = ocfs2_add_entry(handle, dentry, inode,
704 OCFS2_I(inode)->ip_blkno,
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800705 parent_fe_bh, &lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -0800706 if (err) {
Mark Fasheh198a1ca2008-11-20 17:54:57 -0800707 ocfs2_add_links_count(fe, -1);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700708 drop_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800709 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700710 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800711 }
712
Mark Fasheh0027dd52006-09-21 16:51:28 -0700713 err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
Mark Fasheh379dfe92006-09-08 14:21:03 -0700714 if (err) {
715 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700716 goto out_commit;
Mark Fasheh379dfe92006-09-08 14:21:03 -0700717 }
718
Mark Fashehccd979b2005-12-15 14:31:24 -0800719 atomic_inc(&inode->i_count);
720 dentry->d_op = &ocfs2_dentry_ops;
721 d_instantiate(dentry, inode);
Mark Fasheh123a9642006-10-05 16:48:23 -0700722
723out_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700724 ocfs2_commit_trans(osb, handle);
Joel Becker547ba7c2010-05-10 11:56:52 -0700725 ocfs2_unblock_signals(&oldset);
Mark Fasheh123a9642006-10-05 16:48:23 -0700726out_unlock_inode:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700727 ocfs2_inode_unlock(inode, 1);
Mark Fasheh123a9642006-10-05 16:48:23 -0700728
729out:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700730 ocfs2_inode_unlock(dir, 1);
Mark Fasheh123a9642006-10-05 16:48:23 -0700731
Mark Fasheha81cb882008-10-07 14:25:16 -0700732 brelse(fe_bh);
733 brelse(parent_fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800734
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800735 ocfs2_free_dir_lookup_result(&lookup);
736
Mark Fashehccd979b2005-12-15 14:31:24 -0800737 mlog_exit(err);
738
739 return err;
740}
741
Mark Fasheh379dfe92006-09-08 14:21:03 -0700742/*
743 * Takes and drops an exclusive lock on the given dentry. This will
744 * force other nodes to drop it.
745 */
746static int ocfs2_remote_dentry_delete(struct dentry *dentry)
747{
748 int ret;
749
750 ret = ocfs2_dentry_lock(dentry, 1);
751 if (ret)
752 mlog_errno(ret);
753 else
754 ocfs2_dentry_unlock(dentry, 1);
755
756 return ret;
757}
758
Mark Fasheh17ff7852006-09-30 23:29:05 -0700759static inline int inode_is_unlinkable(struct inode *inode)
760{
761 if (S_ISDIR(inode->i_mode)) {
762 if (inode->i_nlink == 2)
763 return 1;
764 return 0;
765 }
766
767 if (inode->i_nlink == 1)
768 return 1;
769 return 0;
770}
771
Mark Fashehccd979b2005-12-15 14:31:24 -0800772static int ocfs2_unlink(struct inode *dir,
773 struct dentry *dentry)
774{
775 int status;
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700776 int child_locked = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800777 struct inode *inode = dentry->d_inode;
Mark Fasheh5098c272006-10-05 18:12:57 -0700778 struct inode *orphan_dir = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800779 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
780 u64 blkno;
781 struct ocfs2_dinode *fe = NULL;
782 struct buffer_head *fe_bh = NULL;
783 struct buffer_head *parent_node_bh = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700784 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800785 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800786 struct ocfs2_dir_lookup_result lookup = { NULL, };
787 struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
Mark Fashehccd979b2005-12-15 14:31:24 -0800788
789 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
790 dentry->d_name.len, dentry->d_name.name);
791
Christoph Hellwig871a2932010-03-03 09:05:07 -0500792 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500793
Mark Fashehccd979b2005-12-15 14:31:24 -0800794 BUG_ON(dentry->d_parent->d_inode != dir);
795
Mark Fashehb0697052006-03-03 10:24:33 -0800796 mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800797
798 if (inode == osb->root_inode) {
799 mlog(0, "Cannot delete the root directory\n");
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700800 return -EPERM;
Mark Fashehccd979b2005-12-15 14:31:24 -0800801 }
802
Jan Karacb257972009-06-04 15:26:50 +0200803 status = ocfs2_inode_lock_nested(dir, &parent_node_bh, 1,
804 OI_LS_PARENT);
Mark Fashehccd979b2005-12-15 14:31:24 -0800805 if (status < 0) {
806 if (status != -ENOENT)
807 mlog_errno(status);
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700808 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -0800809 }
810
811 status = ocfs2_find_files_on_disk(dentry->d_name.name,
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800812 dentry->d_name.len, &blkno, dir,
813 &lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -0800814 if (status < 0) {
815 if (status != -ENOENT)
816 mlog_errno(status);
817 goto leave;
818 }
819
820 if (OCFS2_I(inode)->ip_blkno != blkno) {
821 status = -ENOENT;
822
Mark Fashehb0697052006-03-03 10:24:33 -0800823 mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
824 (unsigned long long)OCFS2_I(inode)->ip_blkno,
825 (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800826 goto leave;
827 }
828
Mark Fashehe63aecb62007-10-18 15:30:42 -0700829 status = ocfs2_inode_lock(inode, &fe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800830 if (status < 0) {
831 if (status != -ENOENT)
832 mlog_errno(status);
833 goto leave;
834 }
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700835 child_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800836
837 if (S_ISDIR(inode->i_mode)) {
Mark Fashehb80b5492009-02-18 11:41:38 -0800838 if (inode->i_nlink != 2 || !ocfs2_empty_dir(inode)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800839 status = -ENOTEMPTY;
840 goto leave;
841 }
842 }
843
Mark Fasheh379dfe92006-09-08 14:21:03 -0700844 status = ocfs2_remote_dentry_delete(dentry);
Mark Fashehccd979b2005-12-15 14:31:24 -0800845 if (status < 0) {
Mark Fasheh34d024f2007-09-24 15:56:19 -0700846 /* This remote delete should succeed under all normal
Mark Fashehccd979b2005-12-15 14:31:24 -0800847 * circumstances. */
848 mlog_errno(status);
849 goto leave;
850 }
851
Mark Fasheh17ff7852006-09-30 23:29:05 -0700852 if (inode_is_unlinkable(inode)) {
Tao Ma19bd3412009-08-18 11:44:10 +0800853 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
854 OCFS2_I(inode)->ip_blkno,
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800855 orphan_name, &orphan_insert);
Mark Fashehccd979b2005-12-15 14:31:24 -0800856 if (status < 0) {
857 mlog_errno(status);
858 goto leave;
859 }
860 }
861
Jan Karaa90714c2008-10-09 19:38:40 +0200862 handle = ocfs2_start_trans(osb, ocfs2_unlink_credits(osb->sb));
Mark Fashehccd979b2005-12-15 14:31:24 -0800863 if (IS_ERR(handle)) {
864 status = PTR_ERR(handle);
865 handle = NULL;
866 mlog_errno(status);
867 goto leave;
868 }
869
Joel Becker0cf2f762009-02-12 16:41:25 -0800870 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700871 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800872 if (status < 0) {
873 mlog_errno(status);
874 goto leave;
875 }
876
877 fe = (struct ocfs2_dinode *) fe_bh->b_data;
878
Mark Fasheh17ff7852006-09-30 23:29:05 -0700879 if (inode_is_unlinkable(inode)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800880 status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800881 &orphan_insert, orphan_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -0800882 if (status < 0) {
883 mlog_errno(status);
884 goto leave;
885 }
886 }
887
888 /* delete the name from the parent dir */
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800889 status = ocfs2_delete_entry(handle, dir, &lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -0800890 if (status < 0) {
891 mlog_errno(status);
892 goto leave;
893 }
894
Mark Fasheh17ff7852006-09-30 23:29:05 -0700895 if (S_ISDIR(inode->i_mode))
896 drop_nlink(inode);
897 drop_nlink(inode);
Mark Fasheh198a1ca2008-11-20 17:54:57 -0800898 ocfs2_set_links_count(fe, inode->i_nlink);
Joel Beckerec20cec2010-03-19 14:13:52 -0700899 ocfs2_journal_dirty(handle, fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800900
Mark Fasheh592282c2007-01-02 17:59:40 -0800901 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
902 if (S_ISDIR(inode->i_mode))
Mark Fasheh17ff7852006-09-30 23:29:05 -0700903 drop_nlink(dir);
Mark Fasheh592282c2007-01-02 17:59:40 -0800904
905 status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
906 if (status < 0) {
907 mlog_errno(status);
908 if (S_ISDIR(inode->i_mode))
Dave Hansend8c76e62006-09-30 23:29:04 -0700909 inc_nlink(dir);
Mark Fashehccd979b2005-12-15 14:31:24 -0800910 }
911
912leave:
Mark Fashehccd979b2005-12-15 14:31:24 -0800913 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700914 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800915
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700916 if (child_locked)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700917 ocfs2_inode_unlock(inode, 1);
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700918
Mark Fashehe63aecb62007-10-18 15:30:42 -0700919 ocfs2_inode_unlock(dir, 1);
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700920
Mark Fasheh5098c272006-10-05 18:12:57 -0700921 if (orphan_dir) {
922 /* This was locked for us in ocfs2_prepare_orphan_dir() */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700923 ocfs2_inode_unlock(orphan_dir, 1);
Mark Fasheh5098c272006-10-05 18:12:57 -0700924 mutex_unlock(&orphan_dir->i_mutex);
925 iput(orphan_dir);
926 }
927
Mark Fasheha81cb882008-10-07 14:25:16 -0700928 brelse(fe_bh);
Mark Fasheha81cb882008-10-07 14:25:16 -0700929 brelse(parent_node_bh);
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800930
931 ocfs2_free_dir_lookup_result(&orphan_insert);
932 ocfs2_free_dir_lookup_result(&lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -0800933
934 mlog_exit(status);
935
936 return status;
937}
938
939/*
940 * The only place this should be used is rename!
941 * if they have the same id, then the 1st one is the only one locked.
942 */
943static int ocfs2_double_lock(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800944 struct buffer_head **bh1,
945 struct inode *inode1,
946 struct buffer_head **bh2,
947 struct inode *inode2)
948{
949 int status;
950 struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
951 struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
952 struct buffer_head **tmpbh;
953 struct inode *tmpinode;
954
Mark Fashehb0697052006-03-03 10:24:33 -0800955 mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
956 (unsigned long long)oi1->ip_blkno,
957 (unsigned long long)oi2->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800958
Mark Fashehccd979b2005-12-15 14:31:24 -0800959 if (*bh1)
960 *bh1 = NULL;
961 if (*bh2)
962 *bh2 = NULL;
963
964 /* we always want to lock the one with the lower lockid first. */
965 if (oi1->ip_blkno != oi2->ip_blkno) {
966 if (oi1->ip_blkno < oi2->ip_blkno) {
967 /* switch id1 and id2 around */
968 mlog(0, "switching them around...\n");
969 tmpbh = bh2;
970 bh2 = bh1;
971 bh1 = tmpbh;
972
973 tmpinode = inode2;
974 inode2 = inode1;
975 inode1 = tmpinode;
976 }
977 /* lock id2 */
Jan Karacb257972009-06-04 15:26:50 +0200978 status = ocfs2_inode_lock_nested(inode2, bh2, 1,
979 OI_LS_RENAME1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800980 if (status < 0) {
981 if (status != -ENOENT)
982 mlog_errno(status);
983 goto bail;
984 }
985 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700986
Mark Fashehccd979b2005-12-15 14:31:24 -0800987 /* lock id1 */
Jan Karacb257972009-06-04 15:26:50 +0200988 status = ocfs2_inode_lock_nested(inode1, bh1, 1, OI_LS_RENAME2);
Mark Fashehccd979b2005-12-15 14:31:24 -0800989 if (status < 0) {
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700990 /*
991 * An error return must mean that no cluster locks
992 * were held on function exit.
993 */
994 if (oi1->ip_blkno != oi2->ip_blkno)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700995 ocfs2_inode_unlock(inode2, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700996
Mark Fashehccd979b2005-12-15 14:31:24 -0800997 if (status != -ENOENT)
998 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800999 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001000
Mark Fashehccd979b2005-12-15 14:31:24 -08001001bail:
1002 mlog_exit(status);
1003 return status;
1004}
1005
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001006static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
1007{
Mark Fashehe63aecb62007-10-18 15:30:42 -07001008 ocfs2_inode_unlock(inode1, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001009
1010 if (inode1 != inode2)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001011 ocfs2_inode_unlock(inode2, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001012}
1013
Mark Fashehccd979b2005-12-15 14:31:24 -08001014static int ocfs2_rename(struct inode *old_dir,
1015 struct dentry *old_dentry,
1016 struct inode *new_dir,
1017 struct dentry *new_dentry)
1018{
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001019 int status = 0, rename_lock = 0, parents_locked = 0, target_exists = 0;
1020 int old_child_locked = 0, new_child_locked = 0, update_dot_dot = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001021 struct inode *old_inode = old_dentry->d_inode;
1022 struct inode *new_inode = new_dentry->d_inode;
Mark Fasheh5098c272006-10-05 18:12:57 -07001023 struct inode *orphan_dir = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001024 struct ocfs2_dinode *newfe = NULL;
1025 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
Mark Fashehccd979b2005-12-15 14:31:24 -08001026 struct buffer_head *newfe_bh = NULL;
Mark Fasheh592282c2007-01-02 17:59:40 -08001027 struct buffer_head *old_inode_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001028 struct ocfs2_super *osb = NULL;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001029 u64 newfe_blkno, old_de_ino;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001030 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001031 struct buffer_head *old_dir_bh = NULL;
1032 struct buffer_head *new_dir_bh = NULL;
Mark Fasheh592282c2007-01-02 17:59:40 -08001033 nlink_t old_dir_nlink = old_dir->i_nlink;
Sunil Mushran480214d2007-08-06 15:11:56 -07001034 struct ocfs2_dinode *old_di;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001035 struct ocfs2_dir_lookup_result old_inode_dot_dot_res = { NULL, };
1036 struct ocfs2_dir_lookup_result target_lookup_res = { NULL, };
1037 struct ocfs2_dir_lookup_result old_entry_lookup = { NULL, };
1038 struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
1039 struct ocfs2_dir_lookup_result target_insert = { NULL, };
Mark Fashehccd979b2005-12-15 14:31:24 -08001040
1041 /* At some point it might be nice to break this function up a
1042 * bit. */
1043
1044 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
1045 old_dir, old_dentry, new_dir, new_dentry,
1046 old_dentry->d_name.len, old_dentry->d_name.name,
1047 new_dentry->d_name.len, new_dentry->d_name.name);
1048
Christoph Hellwig871a2932010-03-03 09:05:07 -05001049 dquot_initialize(old_dir);
1050 dquot_initialize(new_dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05001051
Mark Fashehccd979b2005-12-15 14:31:24 -08001052 osb = OCFS2_SB(old_dir->i_sb);
1053
1054 if (new_inode) {
1055 if (!igrab(new_inode))
1056 BUG();
1057 }
1058
Uwe Kleine-König1b3c3712007-02-17 19:23:03 +01001059 /* Assume a directory hierarchy thusly:
Mark Fashehccd979b2005-12-15 14:31:24 -08001060 * a/b/c
1061 * a/d
1062 * a,b,c, and d are all directories.
1063 *
1064 * from cwd of 'a' on both nodes:
1065 * node1: mv b/c d
1066 * node2: mv d b/c
1067 *
1068 * And that's why, just like the VFS, we need a file system
1069 * rename lock. */
Jan Kara5dabd692008-02-21 18:00:00 +01001070 if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001071 status = ocfs2_rename_lock(osb);
1072 if (status < 0) {
1073 mlog_errno(status);
1074 goto bail;
1075 }
1076 rename_lock = 1;
1077 }
1078
Mark Fashehccd979b2005-12-15 14:31:24 -08001079 /* if old and new are the same, this'll just do one lock. */
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001080 status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
1081 &new_dir_bh, new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001082 if (status < 0) {
1083 mlog_errno(status);
1084 goto bail;
1085 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001086 parents_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001087
1088 /* make sure both dirs have bhs
1089 * get an extra ref on old_dir_bh if old==new */
1090 if (!new_dir_bh) {
1091 if (old_dir_bh) {
1092 new_dir_bh = old_dir_bh;
1093 get_bh(new_dir_bh);
1094 } else {
1095 mlog(ML_ERROR, "no old_dir_bh!\n");
1096 status = -EIO;
1097 goto bail;
1098 }
1099 }
1100
Mark Fasheh379dfe92006-09-08 14:21:03 -07001101 /*
Mark Fasheh592282c2007-01-02 17:59:40 -08001102 * Aside from allowing a meta data update, the locking here
Mark Fasheh34d024f2007-09-24 15:56:19 -07001103 * also ensures that the downconvert thread on other nodes
1104 * won't have to concurrently downconvert the inode and the
1105 * dentry locks.
Mark Fasheh379dfe92006-09-08 14:21:03 -07001106 */
Jan Karacb257972009-06-04 15:26:50 +02001107 status = ocfs2_inode_lock_nested(old_inode, &old_inode_bh, 1,
1108 OI_LS_PARENT);
Mark Fasheh379dfe92006-09-08 14:21:03 -07001109 if (status < 0) {
1110 if (status != -ENOENT)
Mark Fashehccd979b2005-12-15 14:31:24 -08001111 mlog_errno(status);
Mark Fasheh379dfe92006-09-08 14:21:03 -07001112 goto bail;
1113 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001114 old_child_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001115
Mark Fasheh379dfe92006-09-08 14:21:03 -07001116 status = ocfs2_remote_dentry_delete(old_dentry);
1117 if (status < 0) {
1118 mlog_errno(status);
1119 goto bail;
1120 }
1121
1122 if (S_ISDIR(old_inode->i_mode)) {
Mark Fasheh38760e22007-09-11 17:21:56 -07001123 u64 old_inode_parent;
Mark Fashehccd979b2005-12-15 14:31:24 -08001124
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001125 update_dot_dot = 1;
Mark Fasheh38760e22007-09-11 17:21:56 -07001126 status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001127 old_inode,
1128 &old_inode_dot_dot_res);
Mark Fasheh38760e22007-09-11 17:21:56 -07001129 if (status) {
1130 status = -EIO;
Mark Fashehccd979b2005-12-15 14:31:24 -08001131 goto bail;
Mark Fasheh38760e22007-09-11 17:21:56 -07001132 }
1133
1134 if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) {
1135 status = -EIO;
Mark Fashehccd979b2005-12-15 14:31:24 -08001136 goto bail;
Mark Fasheh38760e22007-09-11 17:21:56 -07001137 }
1138
1139 if (!new_inode && new_dir != old_dir &&
Mark Fasheh198a1ca2008-11-20 17:54:57 -08001140 new_dir->i_nlink >= ocfs2_link_max(osb)) {
Mark Fasheh38760e22007-09-11 17:21:56 -07001141 status = -EMLINK;
1142 goto bail;
1143 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001144 }
1145
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001146 status = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name,
1147 old_dentry->d_name.len,
1148 &old_de_ino);
1149 if (status) {
1150 status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -08001151 goto bail;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001152 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001153
1154 /*
1155 * Check for inode number is _not_ due to possible IO errors.
1156 * We might rmdir the source, keep it as pwd of some process
1157 * and merrily kill the link to whatever was created under the
1158 * same name. Goodbye sticky bit ;-<
1159 */
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001160 if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) {
1161 status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -08001162 goto bail;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001163 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001164
1165 /* check if the target already exists (in which case we need
1166 * to delete it */
1167 status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
1168 new_dentry->d_name.len,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001169 &newfe_blkno, new_dir,
1170 &target_lookup_res);
Mark Fashehccd979b2005-12-15 14:31:24 -08001171 /* The only error we allow here is -ENOENT because the new
1172 * file not existing is perfectly valid. */
1173 if ((status < 0) && (status != -ENOENT)) {
1174 /* If we cannot find the file specified we should just */
1175 /* return the error... */
1176 mlog_errno(status);
1177 goto bail;
1178 }
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001179 if (status == 0)
1180 target_exists = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001181
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001182 if (!target_exists && new_inode) {
Srinivas Eedae325a882007-10-31 16:49:43 -07001183 /*
1184 * Target was unlinked by another node while we were
1185 * waiting to get to ocfs2_rename(). There isn't
1186 * anything we can do here to help the situation, so
1187 * bubble up the appropriate error.
1188 */
1189 status = -ENOENT;
1190 goto bail;
1191 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001192
1193 /* In case we need to overwrite an existing file, we blow it
1194 * away first */
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001195 if (target_exists) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001196 /* VFS didn't think there existed an inode here, but
1197 * someone else in the cluster must have raced our
1198 * rename to create one. Today we error cleanly, in
1199 * the future we should consider calling iget to build
1200 * a new struct inode for this entry. */
1201 if (!new_inode) {
1202 status = -EACCES;
1203
1204 mlog(0, "We found an inode for name %.*s but VFS "
1205 "didn't give us one.\n", new_dentry->d_name.len,
1206 new_dentry->d_name.name);
1207 goto bail;
1208 }
1209
1210 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
1211 status = -EACCES;
1212
Mark Fashehb0697052006-03-03 10:24:33 -08001213 mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
1214 (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
1215 (unsigned long long)newfe_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001216 OCFS2_I(new_inode)->ip_flags);
1217 goto bail;
1218 }
1219
Mark Fashehe63aecb62007-10-18 15:30:42 -07001220 status = ocfs2_inode_lock(new_inode, &newfe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001221 if (status < 0) {
1222 if (status != -ENOENT)
1223 mlog_errno(status);
1224 goto bail;
1225 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001226 new_child_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001227
Mark Fasheh379dfe92006-09-08 14:21:03 -07001228 status = ocfs2_remote_dentry_delete(new_dentry);
Mark Fashehccd979b2005-12-15 14:31:24 -08001229 if (status < 0) {
1230 mlog_errno(status);
1231 goto bail;
1232 }
1233
1234 newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
1235
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001236 mlog(0, "aha rename over existing... new_blkno=%llu "
1237 "newfebh=%p bhblocknr=%llu\n",
Mark Fashehb0697052006-03-03 10:24:33 -08001238 (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
Mark Fashehccd979b2005-12-15 14:31:24 -08001239 (unsigned long long)newfe_bh->b_blocknr : 0ULL);
1240
1241 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
Mark Fasheh5098c272006-10-05 18:12:57 -07001242 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
Tao Ma19bd3412009-08-18 11:44:10 +08001243 OCFS2_I(new_inode)->ip_blkno,
1244 orphan_name, &orphan_insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08001245 if (status < 0) {
1246 mlog_errno(status);
1247 goto bail;
1248 }
1249 }
1250 } else {
1251 BUG_ON(new_dentry->d_parent->d_inode != new_dir);
1252
1253 status = ocfs2_check_dir_for_entry(new_dir,
1254 new_dentry->d_name.name,
1255 new_dentry->d_name.len);
1256 if (status)
1257 goto bail;
1258
1259 status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
1260 new_dentry->d_name.name,
1261 new_dentry->d_name.len,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001262 &target_insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08001263 if (status < 0) {
1264 mlog_errno(status);
1265 goto bail;
1266 }
1267 }
1268
Jan Karaa90714c2008-10-09 19:38:40 +02001269 handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001270 if (IS_ERR(handle)) {
1271 status = PTR_ERR(handle);
1272 handle = NULL;
1273 mlog_errno(status);
1274 goto bail;
1275 }
1276
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001277 if (target_exists) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001278 if (S_ISDIR(new_inode->i_mode)) {
Mark Fashehb80b5492009-02-18 11:41:38 -08001279 if (new_inode->i_nlink != 2 ||
1280 !ocfs2_empty_dir(new_inode)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001281 status = -ENOTEMPTY;
1282 goto bail;
1283 }
1284 }
Joel Becker0cf2f762009-02-12 16:41:25 -08001285 status = ocfs2_journal_access_di(handle, INODE_CACHE(new_inode),
1286 newfe_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001287 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001288 if (status < 0) {
1289 mlog_errno(status);
1290 goto bail;
1291 }
1292
1293 if (S_ISDIR(new_inode->i_mode) ||
Mark Fasheh198a1ca2008-11-20 17:54:57 -08001294 (ocfs2_read_links_count(newfe) == 1)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001295 status = ocfs2_orphan_add(osb, handle, new_inode,
1296 newfe, orphan_name,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001297 &orphan_insert, orphan_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001298 if (status < 0) {
1299 mlog_errno(status);
1300 goto bail;
1301 }
1302 }
1303
1304 /* change the dirent to point to the correct inode */
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001305 status = ocfs2_update_entry(new_dir, handle, &target_lookup_res,
1306 old_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001307 if (status < 0) {
1308 mlog_errno(status);
1309 goto bail;
1310 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001311 new_dir->i_version++;
Mark Fashehccd979b2005-12-15 14:31:24 -08001312
1313 if (S_ISDIR(new_inode->i_mode))
Mark Fasheh198a1ca2008-11-20 17:54:57 -08001314 ocfs2_set_links_count(newfe, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001315 else
Mark Fasheh198a1ca2008-11-20 17:54:57 -08001316 ocfs2_add_links_count(newfe, -1);
Joel Beckerec20cec2010-03-19 14:13:52 -07001317 ocfs2_journal_dirty(handle, newfe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001318 } else {
1319 /* if the name was not found in new_dir, add it now */
1320 status = ocfs2_add_entry(handle, new_dentry, old_inode,
1321 OCFS2_I(old_inode)->ip_blkno,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001322 new_dir_bh, &target_insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08001323 }
1324
1325 old_inode->i_ctime = CURRENT_TIME;
1326 mark_inode_dirty(old_inode);
Sunil Mushran480214d2007-08-06 15:11:56 -07001327
Joel Becker0cf2f762009-02-12 16:41:25 -08001328 status = ocfs2_journal_access_di(handle, INODE_CACHE(old_inode),
1329 old_inode_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001330 OCFS2_JOURNAL_ACCESS_WRITE);
Sunil Mushran480214d2007-08-06 15:11:56 -07001331 if (status >= 0) {
1332 old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
1333
1334 old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
1335 old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
Joel Beckerec20cec2010-03-19 14:13:52 -07001336 ocfs2_journal_dirty(handle, old_inode_bh);
Sunil Mushran480214d2007-08-06 15:11:56 -07001337 } else
1338 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001339
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001340 /*
1341 * Now that the name has been added to new_dir, remove the old name.
1342 *
1343 * We don't keep any directory entry context around until now
1344 * because the insert might have changed the type of directory
1345 * we're dealing with.
1346 */
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001347 status = ocfs2_find_entry(old_dentry->d_name.name,
1348 old_dentry->d_name.len, old_dir,
1349 &old_entry_lookup);
1350 if (status)
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001351 goto bail;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001352
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001353 status = ocfs2_delete_entry(handle, old_dir, &old_entry_lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -08001354 if (status < 0) {
1355 mlog_errno(status);
1356 goto bail;
1357 }
1358
1359 if (new_inode) {
1360 new_inode->i_nlink--;
1361 new_inode->i_ctime = CURRENT_TIME;
1362 }
1363 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001364
1365 if (update_dot_dot) {
1366 status = ocfs2_update_entry(old_inode, handle,
1367 &old_inode_dot_dot_res, new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001368 old_dir->i_nlink--;
1369 if (new_inode) {
1370 new_inode->i_nlink--;
1371 } else {
Dave Hansend8c76e62006-09-30 23:29:04 -07001372 inc_nlink(new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001373 mark_inode_dirty(new_dir);
1374 }
1375 }
1376 mark_inode_dirty(old_dir);
Mark Fasheh592282c2007-01-02 17:59:40 -08001377 ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
1378 if (new_inode) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001379 mark_inode_dirty(new_inode);
Mark Fasheh592282c2007-01-02 17:59:40 -08001380 ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
1381 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001382
Mark Fasheh592282c2007-01-02 17:59:40 -08001383 if (old_dir != new_dir) {
1384 /* Keep the same times on both directories.*/
1385 new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
1386
1387 /*
1388 * This will also pick up the i_nlink change from the
1389 * block above.
1390 */
1391 ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
1392 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001393
1394 if (old_dir_nlink != old_dir->i_nlink) {
1395 if (!old_dir_bh) {
1396 mlog(ML_ERROR, "need to change nlink for old dir "
Mark Fashehb0697052006-03-03 10:24:33 -08001397 "%llu from %d to %d but bh is NULL!\n",
1398 (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
1399 (int)old_dir_nlink, old_dir->i_nlink);
Mark Fashehccd979b2005-12-15 14:31:24 -08001400 } else {
1401 struct ocfs2_dinode *fe;
Joel Becker0cf2f762009-02-12 16:41:25 -08001402 status = ocfs2_journal_access_di(handle,
1403 INODE_CACHE(old_dir),
1404 old_dir_bh,
1405 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001406 fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
Mark Fasheh198a1ca2008-11-20 17:54:57 -08001407 ocfs2_set_links_count(fe, old_dir->i_nlink);
Joel Beckerec20cec2010-03-19 14:13:52 -07001408 ocfs2_journal_dirty(handle, old_dir_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001409 }
1410 }
Mark Fasheh379dfe92006-09-08 14:21:03 -07001411 ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001412 status = 0;
1413bail:
1414 if (rename_lock)
1415 ocfs2_rename_unlock(osb);
1416
1417 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001418 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001419
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001420 if (parents_locked)
1421 ocfs2_double_unlock(old_dir, new_dir);
1422
1423 if (old_child_locked)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001424 ocfs2_inode_unlock(old_inode, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001425
1426 if (new_child_locked)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001427 ocfs2_inode_unlock(new_inode, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001428
Mark Fasheh5098c272006-10-05 18:12:57 -07001429 if (orphan_dir) {
1430 /* This was locked for us in ocfs2_prepare_orphan_dir() */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001431 ocfs2_inode_unlock(orphan_dir, 1);
Mark Fasheh5098c272006-10-05 18:12:57 -07001432 mutex_unlock(&orphan_dir->i_mutex);
1433 iput(orphan_dir);
1434 }
1435
Mark Fashehccd979b2005-12-15 14:31:24 -08001436 if (new_inode)
1437 sync_mapping_buffers(old_inode->i_mapping);
1438
1439 if (new_inode)
1440 iput(new_inode);
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001441
1442 ocfs2_free_dir_lookup_result(&target_lookup_res);
1443 ocfs2_free_dir_lookup_result(&old_entry_lookup);
1444 ocfs2_free_dir_lookup_result(&old_inode_dot_dot_res);
1445 ocfs2_free_dir_lookup_result(&orphan_insert);
1446 ocfs2_free_dir_lookup_result(&target_insert);
1447
Mark Fasheha81cb882008-10-07 14:25:16 -07001448 brelse(newfe_bh);
1449 brelse(old_inode_bh);
1450 brelse(old_dir_bh);
1451 brelse(new_dir_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001452
1453 mlog_exit(status);
1454
1455 return status;
1456}
1457
1458/*
1459 * we expect i_size = strlen(symname). Copy symname into the file
1460 * data, including the null terminator.
1461 */
1462static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001463 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001464 struct inode *inode,
1465 const char *symname)
1466{
1467 struct buffer_head **bhs = NULL;
1468 const char *c;
1469 struct super_block *sb = osb->sb;
Mark Fasheh4f902c32007-03-09 16:26:50 -08001470 u64 p_blkno, p_blocks;
Mark Fashehccd979b2005-12-15 14:31:24 -08001471 int virtual, blocks, status, i, bytes_left;
1472
1473 bytes_left = i_size_read(inode) + 1;
1474 /* we can't trust i_blocks because we're actually going to
1475 * write i_size + 1 bytes. */
1476 blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1477
Andrew Morton5515eff2006-03-26 01:37:53 -08001478 mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
1479 (unsigned long long)inode->i_blocks,
1480 i_size_read(inode), blocks);
Mark Fashehccd979b2005-12-15 14:31:24 -08001481
1482 /* Sanity check -- make sure we're going to fit. */
1483 if (bytes_left >
1484 ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1485 status = -EIO;
1486 mlog_errno(status);
1487 goto bail;
1488 }
1489
1490 bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
1491 if (!bhs) {
1492 status = -ENOMEM;
1493 mlog_errno(status);
1494 goto bail;
1495 }
1496
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001497 status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
1498 NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001499 if (status < 0) {
1500 mlog_errno(status);
1501 goto bail;
1502 }
1503
1504 /* links can never be larger than one cluster so we know this
1505 * is all going to be contiguous, but do a sanity check
1506 * anyway. */
1507 if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
1508 status = -EIO;
1509 mlog_errno(status);
1510 goto bail;
1511 }
1512
1513 virtual = 0;
1514 while(bytes_left > 0) {
1515 c = &symname[virtual * sb->s_blocksize];
1516
1517 bhs[virtual] = sb_getblk(sb, p_blkno);
1518 if (!bhs[virtual]) {
1519 status = -ENOMEM;
1520 mlog_errno(status);
1521 goto bail;
1522 }
Joel Becker8cb471e2009-02-10 20:00:41 -08001523 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode),
1524 bhs[virtual]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001525
Joel Becker0cf2f762009-02-12 16:41:25 -08001526 status = ocfs2_journal_access(handle, INODE_CACHE(inode),
1527 bhs[virtual],
Mark Fashehccd979b2005-12-15 14:31:24 -08001528 OCFS2_JOURNAL_ACCESS_CREATE);
1529 if (status < 0) {
1530 mlog_errno(status);
1531 goto bail;
1532 }
1533
1534 memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
1535
1536 memcpy(bhs[virtual]->b_data, c,
1537 (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
1538 bytes_left);
1539
Joel Beckerec20cec2010-03-19 14:13:52 -07001540 ocfs2_journal_dirty(handle, bhs[virtual]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001541
1542 virtual++;
1543 p_blkno++;
1544 bytes_left -= sb->s_blocksize;
1545 }
1546
1547 status = 0;
1548bail:
1549
1550 if (bhs) {
1551 for(i = 0; i < blocks; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001552 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001553 kfree(bhs);
1554 }
1555
1556 mlog_exit(status);
1557 return status;
1558}
1559
1560static int ocfs2_symlink(struct inode *dir,
1561 struct dentry *dentry,
1562 const char *symname)
1563{
1564 int status, l, credits;
1565 u64 newsize;
1566 struct ocfs2_super *osb = NULL;
1567 struct inode *inode = NULL;
1568 struct super_block *sb;
1569 struct buffer_head *new_fe_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001570 struct buffer_head *parent_fe_bh = NULL;
1571 struct ocfs2_dinode *fe = NULL;
1572 struct ocfs2_dinode *dirfe;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001573 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001574 struct ocfs2_alloc_context *inode_ac = NULL;
1575 struct ocfs2_alloc_context *data_ac = NULL;
Tiger Yang534eadd2008-11-14 11:16:41 +08001576 struct ocfs2_alloc_context *xattr_ac = NULL;
1577 int want_clusters = 0;
1578 int xattr_credits = 0;
1579 struct ocfs2_security_xattr_info si = {
1580 .enable = 1,
1581 };
Jan Karaa90714c2008-10-09 19:38:40 +02001582 int did_quota = 0, did_quota_inode = 0;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001583 struct ocfs2_dir_lookup_result lookup = { NULL, };
Joel Becker547ba7c2010-05-10 11:56:52 -07001584 sigset_t oldset;
1585 int did_block_signals = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001586
1587 mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
1588 dentry, symname, dentry->d_name.len, dentry->d_name.name);
1589
Christoph Hellwig871a2932010-03-03 09:05:07 -05001590 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05001591
Mark Fashehccd979b2005-12-15 14:31:24 -08001592 sb = dir->i_sb;
1593 osb = OCFS2_SB(sb);
1594
1595 l = strlen(symname) + 1;
1596
1597 credits = ocfs2_calc_symlink_credits(sb);
1598
Mark Fashehccd979b2005-12-15 14:31:24 -08001599 /* lock the parent directory */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001600 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001601 if (status < 0) {
1602 if (status != -ENOENT)
1603 mlog_errno(status);
Mark Fasheh6d8fc402006-10-06 11:54:33 -07001604 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001605 }
1606
1607 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
Mark Fasheh198a1ca2008-11-20 17:54:57 -08001608 if (!ocfs2_read_links_count(dirfe)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001609 /* can't make a file in a deleted directory. */
1610 status = -ENOENT;
1611 goto bail;
1612 }
1613
1614 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
1615 dentry->d_name.len);
1616 if (status)
1617 goto bail;
1618
1619 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
1620 dentry->d_name.name,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001621 dentry->d_name.len, &lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -08001622 if (status < 0) {
1623 mlog_errno(status);
1624 goto bail;
1625 }
1626
Mark Fashehda5cbf22006-10-06 18:34:35 -07001627 status = ocfs2_reserve_new_inode(osb, &inode_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001628 if (status < 0) {
1629 if (status != -ENOSPC)
1630 mlog_errno(status);
1631 goto bail;
1632 }
1633
Tiger Yangf5d36202008-11-14 11:15:44 +08001634 inode = ocfs2_get_init_inode(dir, S_IFLNK | S_IRWXUGO);
1635 if (!inode) {
1636 status = -ENOMEM;
1637 mlog_errno(status);
1638 goto bail;
1639 }
1640
Tiger Yang534eadd2008-11-14 11:16:41 +08001641 /* get security xattr */
1642 status = ocfs2_init_security_get(inode, dir, &si);
1643 if (status) {
1644 if (status == -EOPNOTSUPP)
1645 si.enable = 0;
1646 else {
1647 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001648 goto bail;
1649 }
1650 }
1651
Tiger Yang534eadd2008-11-14 11:16:41 +08001652 /* calculate meta data/clusters for setting security xattr */
1653 if (si.enable) {
1654 status = ocfs2_calc_security_init(dir, &si, &want_clusters,
1655 &xattr_credits, &xattr_ac);
1656 if (status < 0) {
1657 mlog_errno(status);
1658 goto bail;
1659 }
1660 }
1661
1662 /* don't reserve bitmap space for fast symlinks. */
1663 if (l > ocfs2_fast_symlink_chars(sb))
1664 want_clusters += 1;
1665
1666 status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
1667 if (status < 0) {
1668 if (status != -ENOSPC)
1669 mlog_errno(status);
1670 goto bail;
1671 }
1672
1673 handle = ocfs2_start_trans(osb, credits + xattr_credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001674 if (IS_ERR(handle)) {
1675 status = PTR_ERR(handle);
1676 handle = NULL;
1677 mlog_errno(status);
1678 goto bail;
1679 }
1680
Joel Becker547ba7c2010-05-10 11:56:52 -07001681 /* Starting to change things, restart is no longer possible. */
1682 ocfs2_block_signals(&oldset);
1683 did_block_signals = 1;
1684
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001685 status = dquot_alloc_inode(inode);
1686 if (status)
Jan Karaa90714c2008-10-09 19:38:40 +02001687 goto bail;
Jan Karaa90714c2008-10-09 19:38:40 +02001688 did_quota_inode = 1;
1689
Tao Ma19bd3412009-08-18 11:44:10 +08001690 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry,
1691 inode->i_mode, dentry->d_name.len,
1692 dentry->d_name.name);
1693
1694 status = ocfs2_mknod_locked(osb, dir, inode,
Tiger Yangf5d36202008-11-14 11:15:44 +08001695 0, &new_fe_bh, parent_fe_bh, handle,
1696 inode_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001697 if (status < 0) {
1698 mlog_errno(status);
1699 goto bail;
1700 }
1701
1702 fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
1703 inode->i_rdev = 0;
1704 newsize = l - 1;
1705 if (l > ocfs2_fast_symlink_chars(sb)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08001706 u32 offset = 0;
1707
Mark Fashehccd979b2005-12-15 14:31:24 -08001708 inode->i_op = &ocfs2_symlink_inode_operations;
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001709 status = dquot_alloc_space_nodirty(inode,
1710 ocfs2_clusters_to_bytes(osb->sb, 1));
1711 if (status)
Jan Karaa90714c2008-10-09 19:38:40 +02001712 goto bail;
Jan Karaa90714c2008-10-09 19:38:40 +02001713 did_quota = 1;
Tao Ma0eb8d472008-08-18 17:38:45 +08001714 status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0,
1715 new_fe_bh,
1716 handle, data_ac, NULL,
1717 NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001718 if (status < 0) {
1719 if (status != -ENOSPC && status != -EINTR) {
Mark Fashehb0697052006-03-03 10:24:33 -08001720 mlog(ML_ERROR,
1721 "Failed to extend file to %llu\n",
1722 (unsigned long long)newsize);
Mark Fashehccd979b2005-12-15 14:31:24 -08001723 mlog_errno(status);
1724 status = -ENOSPC;
1725 }
1726 goto bail;
1727 }
1728 i_size_write(inode, newsize);
Mark Fasheh8110b072007-03-22 16:53:23 -07001729 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001730 } else {
1731 inode->i_op = &ocfs2_fast_symlink_inode_operations;
1732 memcpy((char *) fe->id2.i_symlink, symname, l);
1733 i_size_write(inode, newsize);
1734 inode->i_blocks = 0;
1735 }
1736
1737 status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
1738 if (status < 0) {
1739 mlog_errno(status);
1740 goto bail;
1741 }
1742
1743 if (!ocfs2_inode_is_fast_symlink(inode)) {
1744 status = ocfs2_create_symlink_data(osb, handle, inode,
1745 symname);
1746 if (status < 0) {
1747 mlog_errno(status);
1748 goto bail;
1749 }
1750 }
1751
Tiger Yang534eadd2008-11-14 11:16:41 +08001752 if (si.enable) {
1753 status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
1754 xattr_ac, data_ac);
1755 if (status < 0) {
1756 mlog_errno(status);
1757 goto bail;
1758 }
1759 }
1760
Mark Fashehccd979b2005-12-15 14:31:24 -08001761 status = ocfs2_add_entry(handle, dentry, inode,
1762 le64_to_cpu(fe->i_blkno), parent_fe_bh,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001763 &lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -08001764 if (status < 0) {
1765 mlog_errno(status);
1766 goto bail;
1767 }
1768
Mark Fasheh0027dd52006-09-21 16:51:28 -07001769 status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
Mark Fasheh379dfe92006-09-08 14:21:03 -07001770 if (status) {
1771 mlog_errno(status);
1772 goto bail;
1773 }
1774
Mark Fashehccd979b2005-12-15 14:31:24 -08001775 insert_inode_hash(inode);
1776 dentry->d_op = &ocfs2_dentry_ops;
1777 d_instantiate(dentry, inode);
1778bail:
Jan Karaa90714c2008-10-09 19:38:40 +02001779 if (status < 0 && did_quota)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001780 dquot_free_space_nodirty(inode,
Jan Karaa90714c2008-10-09 19:38:40 +02001781 ocfs2_clusters_to_bytes(osb->sb, 1));
1782 if (status < 0 && did_quota_inode)
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001783 dquot_free_inode(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001784 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001785 ocfs2_commit_trans(osb, handle);
Mark Fasheh6d8fc402006-10-06 11:54:33 -07001786
Mark Fashehe63aecb62007-10-18 15:30:42 -07001787 ocfs2_inode_unlock(dir, 1);
Joel Becker547ba7c2010-05-10 11:56:52 -07001788 if (did_block_signals)
1789 ocfs2_unblock_signals(&oldset);
Mark Fasheh6d8fc402006-10-06 11:54:33 -07001790
Mark Fasheha81cb882008-10-07 14:25:16 -07001791 brelse(new_fe_bh);
1792 brelse(parent_fe_bh);
Tiger Yang534eadd2008-11-14 11:16:41 +08001793 kfree(si.name);
1794 kfree(si.value);
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001795 ocfs2_free_dir_lookup_result(&lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -08001796 if (inode_ac)
1797 ocfs2_free_alloc_context(inode_ac);
1798 if (data_ac)
1799 ocfs2_free_alloc_context(data_ac);
Tiger Yang534eadd2008-11-14 11:16:41 +08001800 if (xattr_ac)
1801 ocfs2_free_alloc_context(xattr_ac);
Tiger Yangf5d36202008-11-14 11:15:44 +08001802 if ((status < 0) && inode) {
1803 clear_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001804 iput(inode);
Tiger Yangf5d36202008-11-14 11:15:44 +08001805 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001806
1807 mlog_exit(status);
1808
1809 return status;
1810}
1811
Mark Fashehccd979b2005-12-15 14:31:24 -08001812static int ocfs2_blkno_stringify(u64 blkno, char *name)
1813{
1814 int status, namelen;
1815
1816 mlog_entry_void();
1817
Mark Fashehb0697052006-03-03 10:24:33 -08001818 namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
1819 (long long)blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001820 if (namelen <= 0) {
1821 if (namelen)
1822 status = namelen;
1823 else
1824 status = -EINVAL;
1825 mlog_errno(status);
1826 goto bail;
1827 }
1828 if (namelen != OCFS2_ORPHAN_NAMELEN) {
1829 status = -EINVAL;
1830 mlog_errno(status);
1831 goto bail;
1832 }
1833
1834 mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
1835 namelen);
1836
1837 status = 0;
1838bail:
1839 mlog_exit(status);
1840 return status;
1841}
1842
1843static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
Mark Fasheh5098c272006-10-05 18:12:57 -07001844 struct inode **ret_orphan_dir,
Tao Ma19bd3412009-08-18 11:44:10 +08001845 u64 blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001846 char *name,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001847 struct ocfs2_dir_lookup_result *lookup)
Mark Fashehccd979b2005-12-15 14:31:24 -08001848{
Mark Fasheh5098c272006-10-05 18:12:57 -07001849 struct inode *orphan_dir_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -08001850 struct buffer_head *orphan_dir_bh = NULL;
1851 int status = 0;
1852
Tao Ma19bd3412009-08-18 11:44:10 +08001853 status = ocfs2_blkno_stringify(blkno, name);
Mark Fashehccd979b2005-12-15 14:31:24 -08001854 if (status < 0) {
1855 mlog_errno(status);
Mark Fasheh5098c272006-10-05 18:12:57 -07001856 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001857 }
1858
1859 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1860 ORPHAN_DIR_SYSTEM_INODE,
1861 osb->slot_num);
1862 if (!orphan_dir_inode) {
1863 status = -ENOENT;
1864 mlog_errno(status);
Mark Fasheh5098c272006-10-05 18:12:57 -07001865 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001866 }
1867
Mark Fasheh5098c272006-10-05 18:12:57 -07001868 mutex_lock(&orphan_dir_inode->i_mutex);
1869
Mark Fashehe63aecb62007-10-18 15:30:42 -07001870 status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001871 if (status < 0) {
1872 mlog_errno(status);
1873 goto leave;
1874 }
1875
1876 status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
1877 orphan_dir_bh, name,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001878 OCFS2_ORPHAN_NAMELEN, lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -08001879 if (status < 0) {
Mark Fashehe63aecb62007-10-18 15:30:42 -07001880 ocfs2_inode_unlock(orphan_dir_inode, 1);
Mark Fasheh5098c272006-10-05 18:12:57 -07001881
Mark Fashehccd979b2005-12-15 14:31:24 -08001882 mlog_errno(status);
1883 goto leave;
1884 }
1885
Mark Fasheh5098c272006-10-05 18:12:57 -07001886 *ret_orphan_dir = orphan_dir_inode;
1887
Mark Fashehccd979b2005-12-15 14:31:24 -08001888leave:
Mark Fasheh5098c272006-10-05 18:12:57 -07001889 if (status) {
1890 mutex_unlock(&orphan_dir_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001891 iput(orphan_dir_inode);
Mark Fasheh5098c272006-10-05 18:12:57 -07001892 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001893
Mark Fasheha81cb882008-10-07 14:25:16 -07001894 brelse(orphan_dir_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001895
1896 mlog_exit(status);
1897 return status;
1898}
1899
1900static int ocfs2_orphan_add(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001901 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001902 struct inode *inode,
1903 struct ocfs2_dinode *fe,
1904 char *name,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001905 struct ocfs2_dir_lookup_result *lookup,
Mark Fasheh5098c272006-10-05 18:12:57 -07001906 struct inode *orphan_dir_inode)
Mark Fashehccd979b2005-12-15 14:31:24 -08001907{
Mark Fashehccd979b2005-12-15 14:31:24 -08001908 struct buffer_head *orphan_dir_bh = NULL;
1909 int status = 0;
1910 struct ocfs2_dinode *orphan_fe;
1911
1912 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
1913
Joel Beckerb657c952008-11-13 14:49:11 -08001914 status = ocfs2_read_inode_block(orphan_dir_inode, &orphan_dir_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001915 if (status < 0) {
1916 mlog_errno(status);
1917 goto leave;
1918 }
1919
Joel Becker0cf2f762009-02-12 16:41:25 -08001920 status = ocfs2_journal_access_di(handle,
1921 INODE_CACHE(orphan_dir_inode),
1922 orphan_dir_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001923 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001924 if (status < 0) {
1925 mlog_errno(status);
1926 goto leave;
1927 }
1928
1929 /* we're a cluster, and nlink can change on disk from
1930 * underneath us... */
1931 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
1932 if (S_ISDIR(inode->i_mode))
Mark Fasheh198a1ca2008-11-20 17:54:57 -08001933 ocfs2_add_links_count(orphan_fe, 1);
1934 orphan_dir_inode->i_nlink = ocfs2_read_links_count(orphan_fe);
Joel Beckerec20cec2010-03-19 14:13:52 -07001935 ocfs2_journal_dirty(handle, orphan_dir_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001936
1937 status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
1938 OCFS2_ORPHAN_NAMELEN, inode,
1939 OCFS2_I(inode)->ip_blkno,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001940 orphan_dir_bh, lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -08001941 if (status < 0) {
1942 mlog_errno(status);
1943 goto leave;
1944 }
1945
1946 le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
1947
1948 /* Record which orphan dir our inode now resides
1949 * in. delete_inode will use this to determine which orphan
1950 * dir to lock. */
Tiger Yang50008632007-03-20 16:01:38 -07001951 fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001952
Mark Fashehb0697052006-03-03 10:24:33 -08001953 mlog(0, "Inode %llu orphaned in slot %d\n",
1954 (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001955
1956leave:
Mark Fasheha81cb882008-10-07 14:25:16 -07001957 brelse(orphan_dir_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001958
1959 mlog_exit(status);
1960 return status;
1961}
1962
1963/* unlike orphan_add, we expect the orphan dir to already be locked here. */
1964int ocfs2_orphan_del(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001965 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001966 struct inode *orphan_dir_inode,
1967 struct inode *inode,
1968 struct buffer_head *orphan_dir_bh)
1969{
1970 char name[OCFS2_ORPHAN_NAMELEN + 1];
1971 struct ocfs2_dinode *orphan_fe;
1972 int status = 0;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001973 struct ocfs2_dir_lookup_result lookup = { NULL, };
Mark Fashehccd979b2005-12-15 14:31:24 -08001974
1975 mlog_entry_void();
1976
1977 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
1978 if (status < 0) {
1979 mlog_errno(status);
1980 goto leave;
1981 }
1982
Mark Fashehb0697052006-03-03 10:24:33 -08001983 mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
1984 name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
1985 OCFS2_ORPHAN_NAMELEN);
Mark Fashehccd979b2005-12-15 14:31:24 -08001986
1987 /* find it's spot in the orphan directory */
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001988 status = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN, orphan_dir_inode,
1989 &lookup);
1990 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001991 mlog_errno(status);
1992 goto leave;
1993 }
1994
1995 /* remove it from the orphan directory */
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001996 status = ocfs2_delete_entry(handle, orphan_dir_inode, &lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -08001997 if (status < 0) {
1998 mlog_errno(status);
1999 goto leave;
2000 }
2001
Joel Becker0cf2f762009-02-12 16:41:25 -08002002 status = ocfs2_journal_access_di(handle,
2003 INODE_CACHE(orphan_dir_inode),
2004 orphan_dir_bh,
Joel Becker13723d02008-10-17 19:25:01 -07002005 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002006 if (status < 0) {
2007 mlog_errno(status);
2008 goto leave;
2009 }
2010
2011 /* do the i_nlink dance! :) */
2012 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2013 if (S_ISDIR(inode->i_mode))
Mark Fasheh198a1ca2008-11-20 17:54:57 -08002014 ocfs2_add_links_count(orphan_fe, -1);
2015 orphan_dir_inode->i_nlink = ocfs2_read_links_count(orphan_fe);
Joel Beckerec20cec2010-03-19 14:13:52 -07002016 ocfs2_journal_dirty(handle, orphan_dir_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002017
2018leave:
Mark Fasheh4a12ca32008-11-12 15:43:34 -08002019 ocfs2_free_dir_lookup_result(&lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -08002020
2021 mlog_exit(status);
2022 return status;
2023}
2024
Tao Mabc13d342009-08-18 11:44:14 +08002025int ocfs2_create_inode_in_orphan(struct inode *dir,
2026 int mode,
2027 struct inode **new_inode)
2028{
2029 int status, did_quota_inode = 0;
2030 struct inode *inode = NULL;
2031 struct inode *orphan_dir = NULL;
2032 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2033 struct ocfs2_dinode *di = NULL;
2034 handle_t *handle = NULL;
2035 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
2036 struct buffer_head *parent_di_bh = NULL;
2037 struct buffer_head *new_di_bh = NULL;
2038 struct ocfs2_alloc_context *inode_ac = NULL;
2039 struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
2040
2041 status = ocfs2_inode_lock(dir, &parent_di_bh, 1);
2042 if (status < 0) {
2043 if (status != -ENOENT)
2044 mlog_errno(status);
2045 return status;
2046 }
2047
2048 /*
2049 * We give the orphan dir the root blkno to fake an orphan name,
2050 * and allocate enough space for our insertion.
2051 */
2052 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
2053 osb->root_blkno,
2054 orphan_name, &orphan_insert);
2055 if (status < 0) {
2056 mlog_errno(status);
2057 goto leave;
2058 }
2059
2060 /* reserve an inode spot */
2061 status = ocfs2_reserve_new_inode(osb, &inode_ac);
2062 if (status < 0) {
2063 if (status != -ENOSPC)
2064 mlog_errno(status);
2065 goto leave;
2066 }
2067
2068 inode = ocfs2_get_init_inode(dir, mode);
2069 if (!inode) {
2070 status = -ENOMEM;
2071 mlog_errno(status);
2072 goto leave;
2073 }
2074
2075 handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb, 0, 0));
2076 if (IS_ERR(handle)) {
2077 status = PTR_ERR(handle);
2078 handle = NULL;
2079 mlog_errno(status);
2080 goto leave;
2081 }
2082
Christoph Hellwig63936dd2010-03-03 09:05:01 -05002083 status = dquot_alloc_inode(inode);
2084 if (status)
Tao Mabc13d342009-08-18 11:44:14 +08002085 goto leave;
Tao Mabc13d342009-08-18 11:44:14 +08002086 did_quota_inode = 1;
2087
Tao Ma10cf1a02009-12-18 10:24:55 +08002088 inode->i_nlink = 0;
Tao Mabc13d342009-08-18 11:44:14 +08002089 /* do the real work now. */
2090 status = ocfs2_mknod_locked(osb, dir, inode,
2091 0, &new_di_bh, parent_di_bh, handle,
2092 inode_ac);
2093 if (status < 0) {
2094 mlog_errno(status);
2095 goto leave;
2096 }
2097
2098 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, orphan_name);
2099 if (status < 0) {
2100 mlog_errno(status);
2101 goto leave;
2102 }
2103
2104 di = (struct ocfs2_dinode *)new_di_bh->b_data;
2105 status = ocfs2_orphan_add(osb, handle, inode, di, orphan_name,
2106 &orphan_insert, orphan_dir);
2107 if (status < 0) {
2108 mlog_errno(status);
2109 goto leave;
2110 }
2111
2112 /* get open lock so that only nodes can't remove it from orphan dir. */
2113 status = ocfs2_open_lock(inode);
2114 if (status < 0)
2115 mlog_errno(status);
2116
Tao Mac7d260a2009-12-18 10:24:54 +08002117 insert_inode_hash(inode);
Tao Mabc13d342009-08-18 11:44:14 +08002118leave:
2119 if (status < 0 && did_quota_inode)
Christoph Hellwig63936dd2010-03-03 09:05:01 -05002120 dquot_free_inode(inode);
Tao Mabc13d342009-08-18 11:44:14 +08002121 if (handle)
2122 ocfs2_commit_trans(osb, handle);
2123
2124 if (orphan_dir) {
2125 /* This was locked for us in ocfs2_prepare_orphan_dir() */
2126 ocfs2_inode_unlock(orphan_dir, 1);
2127 mutex_unlock(&orphan_dir->i_mutex);
2128 iput(orphan_dir);
2129 }
2130
2131 if (status == -ENOSPC)
2132 mlog(0, "Disk is full\n");
2133
2134 if ((status < 0) && inode) {
2135 clear_nlink(inode);
2136 iput(inode);
2137 }
2138
2139 if (inode_ac)
2140 ocfs2_free_alloc_context(inode_ac);
2141
2142 brelse(new_di_bh);
2143
2144 if (!status)
2145 *new_inode = inode;
2146
2147 ocfs2_free_dir_lookup_result(&orphan_insert);
2148
2149 ocfs2_inode_unlock(dir, 1);
2150 brelse(parent_di_bh);
2151 return status;
2152}
2153
2154int ocfs2_mv_orphaned_inode_to_new(struct inode *dir,
2155 struct inode *inode,
2156 struct dentry *dentry)
2157{
2158 int status = 0;
2159 struct buffer_head *parent_di_bh = NULL;
2160 handle_t *handle = NULL;
2161 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2162 struct ocfs2_dinode *dir_di, *di;
2163 struct inode *orphan_dir_inode = NULL;
2164 struct buffer_head *orphan_dir_bh = NULL;
2165 struct buffer_head *di_bh = NULL;
2166 struct ocfs2_dir_lookup_result lookup = { NULL, };
2167
2168 mlog_entry("(0x%p, 0x%p, %.*s')\n", dir, dentry,
2169 dentry->d_name.len, dentry->d_name.name);
2170
2171 status = ocfs2_inode_lock(dir, &parent_di_bh, 1);
2172 if (status < 0) {
2173 if (status != -ENOENT)
2174 mlog_errno(status);
2175 return status;
2176 }
2177
2178 dir_di = (struct ocfs2_dinode *) parent_di_bh->b_data;
2179 if (!dir_di->i_links_count) {
2180 /* can't make a file in a deleted directory. */
2181 status = -ENOENT;
2182 goto leave;
2183 }
2184
2185 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
2186 dentry->d_name.len);
2187 if (status)
2188 goto leave;
2189
2190 /* get a spot inside the dir. */
2191 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_di_bh,
2192 dentry->d_name.name,
2193 dentry->d_name.len, &lookup);
2194 if (status < 0) {
2195 mlog_errno(status);
2196 goto leave;
2197 }
2198
2199 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
2200 ORPHAN_DIR_SYSTEM_INODE,
2201 osb->slot_num);
2202 if (!orphan_dir_inode) {
2203 status = -EEXIST;
2204 mlog_errno(status);
2205 goto leave;
2206 }
2207
2208 mutex_lock(&orphan_dir_inode->i_mutex);
2209
2210 status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
2211 if (status < 0) {
2212 mlog_errno(status);
2213 mutex_unlock(&orphan_dir_inode->i_mutex);
2214 iput(orphan_dir_inode);
2215 goto leave;
2216 }
2217
2218 status = ocfs2_read_inode_block(inode, &di_bh);
2219 if (status < 0) {
2220 mlog_errno(status);
2221 goto orphan_unlock;
2222 }
2223
2224 handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb));
2225 if (IS_ERR(handle)) {
2226 status = PTR_ERR(handle);
2227 handle = NULL;
2228 mlog_errno(status);
2229 goto orphan_unlock;
2230 }
2231
2232 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
2233 di_bh, OCFS2_JOURNAL_ACCESS_WRITE);
2234 if (status < 0) {
2235 mlog_errno(status);
2236 goto out_commit;
2237 }
2238
2239 status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
2240 orphan_dir_bh);
2241 if (status < 0) {
2242 mlog_errno(status);
2243 goto out_commit;
2244 }
2245
2246 di = (struct ocfs2_dinode *)di_bh->b_data;
2247 le32_add_cpu(&di->i_flags, -OCFS2_ORPHANED_FL);
2248 di->i_orphaned_slot = 0;
Tao Ma10cf1a02009-12-18 10:24:55 +08002249 inode->i_nlink = 1;
2250 ocfs2_set_links_count(di, inode->i_nlink);
Tao Mabc13d342009-08-18 11:44:14 +08002251 ocfs2_journal_dirty(handle, di_bh);
2252
2253 status = ocfs2_add_entry(handle, dentry, inode,
2254 OCFS2_I(inode)->ip_blkno, parent_di_bh,
2255 &lookup);
2256 if (status < 0) {
2257 mlog_errno(status);
2258 goto out_commit;
2259 }
2260
2261 status = ocfs2_dentry_attach_lock(dentry, inode,
2262 OCFS2_I(dir)->ip_blkno);
2263 if (status) {
2264 mlog_errno(status);
2265 goto out_commit;
2266 }
2267
Tao Mabc13d342009-08-18 11:44:14 +08002268 dentry->d_op = &ocfs2_dentry_ops;
2269 d_instantiate(dentry, inode);
2270 status = 0;
2271out_commit:
2272 ocfs2_commit_trans(osb, handle);
2273orphan_unlock:
2274 ocfs2_inode_unlock(orphan_dir_inode, 1);
2275 mutex_unlock(&orphan_dir_inode->i_mutex);
2276 iput(orphan_dir_inode);
2277leave:
2278
2279 ocfs2_inode_unlock(dir, 1);
2280
2281 brelse(di_bh);
2282 brelse(parent_di_bh);
2283 brelse(orphan_dir_bh);
2284
2285 ocfs2_free_dir_lookup_result(&lookup);
2286
2287 mlog_exit(status);
2288
2289 return status;
2290}
2291
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002292const struct inode_operations ocfs2_dir_iops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002293 .create = ocfs2_create,
2294 .lookup = ocfs2_lookup,
2295 .link = ocfs2_link,
2296 .unlink = ocfs2_unlink,
2297 .rmdir = ocfs2_unlink,
2298 .symlink = ocfs2_symlink,
2299 .mkdir = ocfs2_mkdir,
2300 .mknod = ocfs2_mknod,
2301 .rename = ocfs2_rename,
2302 .setattr = ocfs2_setattr,
2303 .getattr = ocfs2_getattr,
Tiger Yangd38eb8d2006-11-27 09:59:21 +08002304 .permission = ocfs2_permission,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002305 .setxattr = generic_setxattr,
2306 .getxattr = generic_getxattr,
2307 .listxattr = ocfs2_listxattr,
2308 .removexattr = generic_removexattr,
Tristan Ye55f49462009-12-17 18:42:16 +08002309 .fiemap = ocfs2_fiemap,
Mark Fashehccd979b2005-12-15 14:31:24 -08002310};