blob: 8a13226eb1a25b92dd850c1925e7a65a446eca54 [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,
Tristan Ye3939fda2010-03-19 09:21:09 +080087 struct buffer_head *fe_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -080088 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
Tao Maef6b6892011-02-21 11:10:44 +0800109 mlog(0, "(0x%p, 0x%p, '%.*s')\n", dir, dentry,
110 dentry->d_name.len, dentry->d_name.name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800111
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 ret = d_splice_alias(inode, dentry);
151
Mark Fasheh379dfe92006-09-08 14:21:03 -0700152 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,
Mark Fasheh0027dd52006-09-21 16:51:28 -0700167 OCFS2_I(dir)->ip_blkno);
Mark Fasheh379dfe92006-09-08 14:21:03 -0700168 if (status) {
169 mlog_errno(status);
170 ret = ERR_PTR(status);
171 goto bail_unlock;
172 }
Goldwyn Rodrigues5e98d492010-06-28 10:04:32 -0500173 } else
174 ocfs2_dentry_attach_gen(dentry);
Mark Fasheh379dfe92006-09-08 14:21:03 -0700175
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
Tao Mac1e8d352011-03-07 16:43:21 +0800185 mlog(0, "%p\n", ret);
Mark Fashehccd979b2005-12-15 14:31:24 -0800186
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;
Dmitry Monakhov75fe0a22010-03-04 17:32:16 +0300207 inode_init_owner(inode, dir, mode);
Christoph Hellwig871a2932010-03-03 09:05:07 -0500208 dquot_initialize(inode);
Tiger Yangf5d36202008-11-14 11:15:44 +0800209 return inode;
210}
211
Mark Fashehccd979b2005-12-15 14:31:24 -0800212static 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;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700219 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800220 struct ocfs2_super *osb;
221 struct ocfs2_dinode *dirfe;
222 struct buffer_head *new_fe_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800223 struct inode *inode = NULL;
224 struct ocfs2_alloc_context *inode_ac = NULL;
225 struct ocfs2_alloc_context *data_ac = NULL;
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800226 struct ocfs2_alloc_context *meta_ac = NULL;
Tiger Yang534eadd2008-11-14 11:16:41 +0800227 int want_clusters = 0;
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800228 int want_meta = 0;
Tiger Yang534eadd2008-11-14 11:16:41 +0800229 int xattr_credits = 0;
230 struct ocfs2_security_xattr_info si = {
231 .enable = 1,
232 };
Jan Karaa90714c2008-10-09 19:38:40 +0200233 int did_quota_inode = 0;
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800234 struct ocfs2_dir_lookup_result lookup = { NULL, };
Joel Becker547ba7c2010-05-10 11:56:52 -0700235 sigset_t oldset;
236 int did_block_signals = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800237
Tao Maef6b6892011-02-21 11:10:44 +0800238 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);
Mark Fashehccd979b2005-12-15 14:31:24 -0800241
Christoph Hellwig871a2932010-03-03 09:05:07 -0500242 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500243
Mark Fashehccd979b2005-12-15 14:31:24 -0800244 /* get our super block */
245 osb = OCFS2_SB(dir->i_sb);
246
Mark Fashehe63aecb62007-10-18 15:30:42 -0700247 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
Mark Fashehe3a82132006-10-05 16:04:17 -0700248 if (status < 0) {
249 if (status != -ENOENT)
250 mlog_errno(status);
251 return status;
252 }
253
Mark Fasheh198a1ca2008-11-20 17:54:57 -0800254 if (S_ISDIR(mode) && (dir->i_nlink >= ocfs2_link_max(osb))) {
Mark Fasheha663e302006-08-09 11:45:07 -0700255 status = -EMLINK;
256 goto leave;
257 }
258
Mark Fashehccd979b2005-12-15 14:31:24 -0800259 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
Mark Fasheh198a1ca2008-11-20 17:54:57 -0800260 if (!ocfs2_read_links_count(dirfe)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800261 /* 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,
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800274 dentry->d_name.len, &lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -0800275 if (status < 0) {
276 mlog_errno(status);
277 goto leave;
278 }
279
280 /* reserve an inode spot */
Mark Fashehda5cbf22006-10-06 18:34:35 -0700281 status = ocfs2_reserve_new_inode(osb, &inode_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800282 if (status < 0) {
283 if (status != -ENOSPC)
284 mlog_errno(status);
285 goto leave;
286 }
287
Tiger Yangf5d36202008-11-14 11:15:44 +0800288 inode = ocfs2_get_init_inode(dir, mode);
289 if (!inode) {
290 status = -ENOMEM;
291 mlog_errno(status);
292 goto leave;
293 }
294
Tiger Yang534eadd2008-11-14 11:16:41 +0800295 /* 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);
Mark Fashehccd979b2005-12-15 14:31:24 -0800302 goto leave;
303 }
304 }
305
Tiger Yang89c38bd2008-11-14 11:17:41 +0800306 /* calculate meta data/clusters for setting security and acl xattr */
307 status = ocfs2_calc_xattr_init(dir, parent_fe_bh, mode,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800308 &si, &want_clusters,
309 &xattr_credits, &want_meta);
Tiger Yang89c38bd2008-11-14 11:17:41 +0800310 if (status < 0) {
311 mlog_errno(status);
312 goto leave;
Tiger Yang534eadd2008-11-14 11:16:41 +0800313 }
314
315 /* Reserve a cluster if creating an extent based directory. */
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800316 if (S_ISDIR(mode) && !ocfs2_supports_inline_data(osb)) {
Tiger Yang534eadd2008-11-14 11:16:41 +0800317 want_clusters += 1;
318
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800319 /* Dir indexing requires extra space as well */
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800320 if (ocfs2_supports_indexed_dirs(osb))
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800321 want_meta++;
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800322 }
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
Tiger Yang534eadd2008-11-14 11:16:41 +0800331 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
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800338 handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb,
339 S_ISDIR(mode),
340 xattr_credits));
Mark Fashehccd979b2005-12-15 14:31:24 -0800341 if (IS_ERR(handle)) {
342 status = PTR_ERR(handle);
343 handle = NULL;
344 mlog_errno(status);
345 goto leave;
346 }
347
Joel Becker547ba7c2010-05-10 11:56:52 -0700348 /* Starting to change things, restart is no longer possible. */
349 ocfs2_block_signals(&oldset);
350 did_block_signals = 1;
351
Christoph Hellwig63936dd2010-03-03 09:05:01 -0500352 status = dquot_alloc_inode(inode);
353 if (status)
Jan Karaa90714c2008-10-09 19:38:40 +0200354 goto leave;
Jan Karaa90714c2008-10-09 19:38:40 +0200355 did_quota_inode = 1;
356
Tao Maef6b6892011-02-21 11:10:44 +0800357 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);
Tao Ma19bd3412009-08-18 11:44:10 +0800360
Mark Fashehccd979b2005-12-15 14:31:24 -0800361 /* do the real work now. */
Tao Ma19bd3412009-08-18 11:44:10 +0800362 status = ocfs2_mknod_locked(osb, dir, inode, dev,
Mark Fashehccd979b2005-12-15 14:31:24 -0800363 &new_fe_bh, parent_fe_bh, handle,
Tiger Yangf5d36202008-11-14 11:15:44 +0800364 inode_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800365 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,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800372 new_fe_bh, data_ac, meta_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800373 if (status < 0) {
374 mlog_errno(status);
375 goto leave;
376 }
377
Joel Becker0cf2f762009-02-12 16:41:25 -0800378 status = ocfs2_journal_access_di(handle, INODE_CACHE(dir),
379 parent_fe_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700380 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800381 if (status < 0) {
382 mlog_errno(status);
383 goto leave;
384 }
Mark Fasheh198a1ca2008-11-20 17:54:57 -0800385 ocfs2_add_links_count(dirfe, 1);
Joel Beckerec20cec2010-03-19 14:13:52 -0700386 ocfs2_journal_dirty(handle, parent_fe_bh);
Dave Hansend8c76e62006-09-30 23:29:04 -0700387 inc_nlink(dir);
Mark Fashehccd979b2005-12-15 14:31:24 -0800388 }
389
Tiger Yang89c38bd2008-11-14 11:17:41 +0800390 status = ocfs2_init_acl(handle, inode, dir, new_fe_bh, parent_fe_bh,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800391 meta_ac, data_ac);
Tiger Yang89c38bd2008-11-14 11:17:41 +0800392 if (status < 0) {
393 mlog_errno(status);
394 goto leave;
395 }
396
Tiger Yang534eadd2008-11-14 11:16:41 +0800397 if (si.enable) {
398 status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800399 meta_ac, data_ac);
Tiger Yang534eadd2008-11-14 11:16:41 +0800400 if (status < 0) {
401 mlog_errno(status);
402 goto leave;
403 }
404 }
405
Mark Fasheha9743fc2010-04-23 11:42:22 -0700406 /*
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) {
414 mlog_errno(status);
415 goto leave;
416 }
Mark Fasheha9743fc2010-04-23 11:42:22 -0700417
Mark Fashehccd979b2005-12-15 14:31:24 -0800418 status = ocfs2_add_entry(handle, dentry, inode,
419 OCFS2_I(inode)->ip_blkno, parent_fe_bh,
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800420 &lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -0800421 if (status < 0) {
422 mlog_errno(status);
423 goto leave;
424 }
425
426 insert_inode_hash(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800427 d_instantiate(dentry, inode);
428 status = 0;
429leave:
Jan Karaa90714c2008-10-09 19:38:40 +0200430 if (status < 0 && did_quota_inode)
Christoph Hellwig63936dd2010-03-03 09:05:01 -0500431 dquot_free_inode(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800432 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700433 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800434
Mark Fashehe63aecb62007-10-18 15:30:42 -0700435 ocfs2_inode_unlock(dir, 1);
Joel Becker547ba7c2010-05-10 11:56:52 -0700436 if (did_block_signals)
437 ocfs2_unblock_signals(&oldset);
Mark Fashehe3a82132006-10-05 16:04:17 -0700438
Mark Fashehccd979b2005-12-15 14:31:24 -0800439 if (status == -ENOSPC)
440 mlog(0, "Disk is full\n");
441
Mark Fasheha81cb882008-10-07 14:25:16 -0700442 brelse(new_fe_bh);
Mark Fasheha81cb882008-10-07 14:25:16 -0700443 brelse(parent_fe_bh);
Tiger Yang534eadd2008-11-14 11:16:41 +0800444 kfree(si.name);
445 kfree(si.value);
Mark Fashehccd979b2005-12-15 14:31:24 -0800446
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800447 ocfs2_free_dir_lookup_result(&lookup);
448
Mark Fashehccd979b2005-12-15 14:31:24 -0800449 if (inode_ac)
450 ocfs2_free_alloc_context(inode_ac);
451
452 if (data_ac)
453 ocfs2_free_alloc_context(data_ac);
454
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800455 if (meta_ac)
456 ocfs2_free_alloc_context(meta_ac);
Tiger Yang534eadd2008-11-14 11:16:41 +0800457
Li Dongyang062d3402010-04-22 16:11:29 +0800458 /*
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
Tao Mac1e8d352011-03-07 16:43:21 +0800469 if (status)
470 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800471
472 return status;
473}
474
Mark Fasheh021960c2010-08-13 15:15:15 -0700475static 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)
Mark Fashehccd979b2005-12-15 14:31:24 -0800483{
484 int status = 0;
Mark Fasheh021960c2010-08-13 15:15:15 -0700485 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800486 struct ocfs2_dinode *fe = NULL;
487 struct ocfs2_extent_list *fel;
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800488 u16 feat;
Mark Fashehccd979b2005-12-15 14:31:24 -0800489
Mark Fashehccd979b2005-12-15 14:31:24 -0800490 *new_fe_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800491
Mark Fashehccd979b2005-12-15 14:31:24 -0800492 /* 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;
Mark Fashehccd979b2005-12-15 14:31:24 -0800497 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 }
Joel Becker8cb471e2009-02-10 20:00:41 -0800507 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), *new_fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800508
Joel Becker0cf2f762009-02-12 16:41:25 -0800509 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
510 *new_fe_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700511 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800512 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);
Joel Becker2b6cb572010-03-26 10:09:15 +0800523 fe->i_suballoc_loc = cpu_to_le64(suballoc_loc);
Mark Fashehccd979b2005-12-15 14:31:24 -0800524 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800525 fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot);
Tiger Yangf5d36202008-11-14 11:15:44 +0800526 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))
Mark Fashehccd979b2005-12-15 14:31:24 -0800530 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
Mark Fasheh198a1ca2008-11-20 17:54:57 -0800531
532 ocfs2_set_links_count(fe, inode->i_nlink);
Mark Fashehccd979b2005-12-15 14:31:24 -0800533
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
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700543 /*
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800544 * If supported, directories start with inline data. If inline
545 * isn't supported, but indexing is, we start them as indexed.
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700546 */
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800547 feat = le16_to_cpu(fe->i_dyn_features);
Tiger Yangf5d36202008-11-14 11:15:44 +0800548 if (S_ISDIR(inode->i_mode) && ocfs2_supports_inline_data(osb)) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700549 fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL);
550
Tiger Yangd9ae49d2009-03-05 11:06:15 +0800551 fe->id2.i_data.id_count = cpu_to_le16(
552 ocfs2_max_inline_data_with_xattr(osb->sb, fe));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700553 } 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 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800559
Joel Beckerec20cec2010-03-19 14:13:52 -0700560 ocfs2_journal_dirty(handle, *new_fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800561
Joel Beckerb657c952008-11-13 14:49:11 -0800562 ocfs2_populate_inode(inode, fe, 1);
Joel Becker0cf2f762009-02-12 16:41:25 -0800563 ocfs2_ci_set_new(osb, INODE_CACHE(inode));
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800564 if (!ocfs2_mount_local(osb)) {
565 status = ocfs2_create_new_inode_locks(inode);
566 if (status < 0)
567 mlog_errno(status);
568 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800569
570 status = 0; /* error in ocfs2_create_new_inode_locks is not
571 * critical */
572
Mark Fashehccd979b2005-12-15 14:31:24 -0800573leave:
574 if (status < 0) {
575 if (*new_fe_bh) {
576 brelse(*new_fe_bh);
577 *new_fe_bh = NULL;
578 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800579 }
580
Tao Mac1e8d352011-03-07 16:43:21 +0800581 if (status)
582 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800583 return status;
584}
585
Mark Fasheh021960c2010-08-13 15:15:15 -0700586static 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
Mark Fashehccd979b2005-12-15 14:31:24 -0800614static int ocfs2_mkdir(struct inode *dir,
615 struct dentry *dentry,
616 int mode)
617{
618 int ret;
619
Tao Maef6b6892011-02-21 11:10:44 +0800620 mlog(0, "(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
621 dentry->d_name.len, dentry->d_name.name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800622 ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
Tao Mac1e8d352011-03-07 16:43:21 +0800623 if (ret)
624 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -0800625
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
Tao Maef6b6892011-02-21 11:10:44 +0800636 mlog(0, "(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
637 dentry->d_name.len, dentry->d_name.name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800638 ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
Tao Mac1e8d352011-03-07 16:43:21 +0800639 if (ret)
640 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -0800641
642 return ret;
643}
644
645static int ocfs2_link(struct dentry *old_dentry,
646 struct inode *dir,
647 struct dentry *dentry)
648{
Mark Fasheh1fabe142006-10-09 18:11:45 -0700649 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800650 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;
Mark Fashehccd979b2005-12-15 14:31:24 -0800654 struct ocfs2_dinode *fe = NULL;
655 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800656 struct ocfs2_dir_lookup_result lookup = { NULL, };
Joel Becker547ba7c2010-05-10 11:56:52 -0700657 sigset_t oldset;
Mark Fashehccd979b2005-12-15 14:31:24 -0800658
Tao Maef6b6892011-02-21 11:10:44 +0800659 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);
Mark Fashehccd979b2005-12-15 14:31:24 -0800662
Mark Fasheh123a9642006-10-05 16:48:23 -0700663 if (S_ISDIR(inode->i_mode))
664 return -EPERM;
Mark Fashehccd979b2005-12-15 14:31:24 -0800665
Christoph Hellwig871a2932010-03-03 09:05:07 -0500666 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500667
Jan Karacb257972009-06-04 15:26:50 +0200668 err = ocfs2_inode_lock_nested(dir, &parent_fe_bh, 1, OI_LS_PARENT);
Mark Fashehccd979b2005-12-15 14:31:24 -0800669 if (err < 0) {
670 if (err != -ENOENT)
671 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700672 return err;
Mark Fashehccd979b2005-12-15 14:31:24 -0800673 }
674
Tiger Yang0f62de22006-08-31 20:39:47 -0700675 if (!dir->i_nlink) {
676 err = -ENOENT;
Mark Fasheh123a9642006-10-05 16:48:23 -0700677 goto out;
Tiger Yang0f62de22006-08-31 20:39:47 -0700678 }
679
Mark Fashehccd979b2005-12-15 14:31:24 -0800680 err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
681 dentry->d_name.len);
682 if (err)
Mark Fasheh123a9642006-10-05 16:48:23 -0700683 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800684
685 err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
686 dentry->d_name.name,
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800687 dentry->d_name.len, &lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -0800688 if (err < 0) {
689 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700690 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800691 }
692
Mark Fashehe63aecb62007-10-18 15:30:42 -0700693 err = ocfs2_inode_lock(inode, &fe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800694 if (err < 0) {
695 if (err != -ENOENT)
696 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700697 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800698 }
699
700 fe = (struct ocfs2_dinode *) fe_bh->b_data;
Mark Fasheh198a1ca2008-11-20 17:54:57 -0800701 if (ocfs2_read_links_count(fe) >= ocfs2_link_max(osb)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800702 err = -EMLINK;
Mark Fasheh123a9642006-10-05 16:48:23 -0700703 goto out_unlock_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800704 }
705
Jan Karaa90714c2008-10-09 19:38:40 +0200706 handle = ocfs2_start_trans(osb, ocfs2_link_credits(osb->sb));
Mark Fashehccd979b2005-12-15 14:31:24 -0800707 if (IS_ERR(handle)) {
708 err = PTR_ERR(handle);
709 handle = NULL;
710 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700711 goto out_unlock_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800712 }
713
Joel Becker547ba7c2010-05-10 11:56:52 -0700714 /* Starting to change things, restart is no longer possible. */
715 ocfs2_block_signals(&oldset);
716
Joel Becker0cf2f762009-02-12 16:41:25 -0800717 err = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700718 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800719 if (err < 0) {
720 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700721 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800722 }
723
Dave Hansend8c76e62006-09-30 23:29:04 -0700724 inc_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800725 inode->i_ctime = CURRENT_TIME;
Mark Fasheh198a1ca2008-11-20 17:54:57 -0800726 ocfs2_set_links_count(fe, inode->i_nlink);
Mark Fashehccd979b2005-12-15 14:31:24 -0800727 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
728 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
Joel Beckerec20cec2010-03-19 14:13:52 -0700729 ocfs2_journal_dirty(handle, fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800730
731 err = ocfs2_add_entry(handle, dentry, inode,
732 OCFS2_I(inode)->ip_blkno,
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800733 parent_fe_bh, &lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -0800734 if (err) {
Mark Fasheh198a1ca2008-11-20 17:54:57 -0800735 ocfs2_add_links_count(fe, -1);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700736 drop_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800737 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700738 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800739 }
740
Mark Fasheh0027dd52006-09-21 16:51:28 -0700741 err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
Mark Fasheh379dfe92006-09-08 14:21:03 -0700742 if (err) {
743 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700744 goto out_commit;
Mark Fasheh379dfe92006-09-08 14:21:03 -0700745 }
746
Al Viro7de9c6e2010-10-23 11:11:40 -0400747 ihold(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800748 d_instantiate(dentry, inode);
Mark Fasheh123a9642006-10-05 16:48:23 -0700749
750out_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700751 ocfs2_commit_trans(osb, handle);
Joel Becker547ba7c2010-05-10 11:56:52 -0700752 ocfs2_unblock_signals(&oldset);
Mark Fasheh123a9642006-10-05 16:48:23 -0700753out_unlock_inode:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700754 ocfs2_inode_unlock(inode, 1);
Mark Fasheh123a9642006-10-05 16:48:23 -0700755
756out:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700757 ocfs2_inode_unlock(dir, 1);
Mark Fasheh123a9642006-10-05 16:48:23 -0700758
Mark Fasheha81cb882008-10-07 14:25:16 -0700759 brelse(fe_bh);
760 brelse(parent_fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800761
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800762 ocfs2_free_dir_lookup_result(&lookup);
763
Tao Mac1e8d352011-03-07 16:43:21 +0800764 if (err)
765 mlog_errno(err);
Mark Fashehccd979b2005-12-15 14:31:24 -0800766
767 return err;
768}
769
Mark Fasheh379dfe92006-09-08 14:21:03 -0700770/*
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
Mark Fasheh17ff7852006-09-30 23:29:05 -0700787static 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
Mark Fashehccd979b2005-12-15 14:31:24 -0800800static int ocfs2_unlink(struct inode *dir,
801 struct dentry *dentry)
802{
803 int status;
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700804 int child_locked = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800805 struct inode *inode = dentry->d_inode;
Mark Fasheh5098c272006-10-05 18:12:57 -0700806 struct inode *orphan_dir = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800807 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;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700812 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800813 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800814 struct ocfs2_dir_lookup_result lookup = { NULL, };
815 struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
Mark Fashehccd979b2005-12-15 14:31:24 -0800816
Tao Maef6b6892011-02-21 11:10:44 +0800817 mlog(0, "(0x%p, 0x%p, '%.*s')\n", dir, dentry,
818 dentry->d_name.len, dentry->d_name.name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800819
Christoph Hellwig871a2932010-03-03 09:05:07 -0500820 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500821
Mark Fashehccd979b2005-12-15 14:31:24 -0800822 BUG_ON(dentry->d_parent->d_inode != dir);
823
Mark Fashehb0697052006-03-03 10:24:33 -0800824 mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800825
826 if (inode == osb->root_inode) {
827 mlog(0, "Cannot delete the root directory\n");
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700828 return -EPERM;
Mark Fashehccd979b2005-12-15 14:31:24 -0800829 }
830
Jan Karacb257972009-06-04 15:26:50 +0200831 status = ocfs2_inode_lock_nested(dir, &parent_node_bh, 1,
832 OI_LS_PARENT);
Mark Fashehccd979b2005-12-15 14:31:24 -0800833 if (status < 0) {
834 if (status != -ENOENT)
835 mlog_errno(status);
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700836 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -0800837 }
838
839 status = ocfs2_find_files_on_disk(dentry->d_name.name,
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800840 dentry->d_name.len, &blkno, dir,
841 &lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -0800842 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
Mark Fashehb0697052006-03-03 10:24:33 -0800851 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);
Mark Fashehccd979b2005-12-15 14:31:24 -0800854 goto leave;
855 }
856
Mark Fashehe63aecb62007-10-18 15:30:42 -0700857 status = ocfs2_inode_lock(inode, &fe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800858 if (status < 0) {
859 if (status != -ENOENT)
860 mlog_errno(status);
861 goto leave;
862 }
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700863 child_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800864
865 if (S_ISDIR(inode->i_mode)) {
Mark Fashehb80b5492009-02-18 11:41:38 -0800866 if (inode->i_nlink != 2 || !ocfs2_empty_dir(inode)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800867 status = -ENOTEMPTY;
868 goto leave;
869 }
870 }
871
Mark Fasheh379dfe92006-09-08 14:21:03 -0700872 status = ocfs2_remote_dentry_delete(dentry);
Mark Fashehccd979b2005-12-15 14:31:24 -0800873 if (status < 0) {
Mark Fasheh34d024f2007-09-24 15:56:19 -0700874 /* This remote delete should succeed under all normal
Mark Fashehccd979b2005-12-15 14:31:24 -0800875 * circumstances. */
876 mlog_errno(status);
877 goto leave;
878 }
879
Mark Fasheh17ff7852006-09-30 23:29:05 -0700880 if (inode_is_unlinkable(inode)) {
Tao Ma19bd3412009-08-18 11:44:10 +0800881 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
882 OCFS2_I(inode)->ip_blkno,
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800883 orphan_name, &orphan_insert);
Mark Fashehccd979b2005-12-15 14:31:24 -0800884 if (status < 0) {
885 mlog_errno(status);
886 goto leave;
887 }
888 }
889
Jan Karaa90714c2008-10-09 19:38:40 +0200890 handle = ocfs2_start_trans(osb, ocfs2_unlink_credits(osb->sb));
Mark Fashehccd979b2005-12-15 14:31:24 -0800891 if (IS_ERR(handle)) {
892 status = PTR_ERR(handle);
893 handle = NULL;
894 mlog_errno(status);
895 goto leave;
896 }
897
Joel Becker0cf2f762009-02-12 16:41:25 -0800898 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700899 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800900 if (status < 0) {
901 mlog_errno(status);
902 goto leave;
903 }
904
905 fe = (struct ocfs2_dinode *) fe_bh->b_data;
906
Mark Fasheh17ff7852006-09-30 23:29:05 -0700907 if (inode_is_unlinkable(inode)) {
Tristan Ye3939fda2010-03-19 09:21:09 +0800908 status = ocfs2_orphan_add(osb, handle, inode, fe_bh, orphan_name,
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800909 &orphan_insert, orphan_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -0800910 if (status < 0) {
911 mlog_errno(status);
912 goto leave;
913 }
914 }
915
916 /* delete the name from the parent dir */
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800917 status = ocfs2_delete_entry(handle, dir, &lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -0800918 if (status < 0) {
919 mlog_errno(status);
920 goto leave;
921 }
922
Mark Fasheh17ff7852006-09-30 23:29:05 -0700923 if (S_ISDIR(inode->i_mode))
924 drop_nlink(inode);
925 drop_nlink(inode);
Mark Fasheh198a1ca2008-11-20 17:54:57 -0800926 ocfs2_set_links_count(fe, inode->i_nlink);
Joel Beckerec20cec2010-03-19 14:13:52 -0700927 ocfs2_journal_dirty(handle, fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800928
Mark Fasheh592282c2007-01-02 17:59:40 -0800929 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
930 if (S_ISDIR(inode->i_mode))
Mark Fasheh17ff7852006-09-30 23:29:05 -0700931 drop_nlink(dir);
Mark Fasheh592282c2007-01-02 17:59:40 -0800932
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))
Dave Hansend8c76e62006-09-30 23:29:04 -0700937 inc_nlink(dir);
Mark Fashehccd979b2005-12-15 14:31:24 -0800938 }
939
940leave:
Mark Fashehccd979b2005-12-15 14:31:24 -0800941 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700942 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800943
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700944 if (child_locked)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700945 ocfs2_inode_unlock(inode, 1);
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700946
Mark Fashehe63aecb62007-10-18 15:30:42 -0700947 ocfs2_inode_unlock(dir, 1);
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700948
Mark Fasheh5098c272006-10-05 18:12:57 -0700949 if (orphan_dir) {
950 /* This was locked for us in ocfs2_prepare_orphan_dir() */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700951 ocfs2_inode_unlock(orphan_dir, 1);
Mark Fasheh5098c272006-10-05 18:12:57 -0700952 mutex_unlock(&orphan_dir->i_mutex);
953 iput(orphan_dir);
954 }
955
Mark Fasheha81cb882008-10-07 14:25:16 -0700956 brelse(fe_bh);
Mark Fasheha81cb882008-10-07 14:25:16 -0700957 brelse(parent_node_bh);
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800958
959 ocfs2_free_dir_lookup_result(&orphan_insert);
960 ocfs2_free_dir_lookup_result(&lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -0800961
Tao Mac1e8d352011-03-07 16:43:21 +0800962 if (status)
963 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800964
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,
Mark Fashehccd979b2005-12-15 14:31:24 -0800973 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
Tao Maef6b6892011-02-21 11:10:44 +0800984 mlog(0, "(inode1 = %llu, inode2 = %llu)\n",
985 (unsigned long long)oi1->ip_blkno,
986 (unsigned long long)oi2->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800987
Mark Fashehccd979b2005-12-15 14:31:24 -0800988 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 */
Jan Karacb257972009-06-04 15:26:50 +02001007 status = ocfs2_inode_lock_nested(inode2, bh2, 1,
1008 OI_LS_RENAME1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001009 if (status < 0) {
1010 if (status != -ENOENT)
1011 mlog_errno(status);
1012 goto bail;
1013 }
1014 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001015
Mark Fashehccd979b2005-12-15 14:31:24 -08001016 /* lock id1 */
Jan Karacb257972009-06-04 15:26:50 +02001017 status = ocfs2_inode_lock_nested(inode1, bh1, 1, OI_LS_RENAME2);
Mark Fashehccd979b2005-12-15 14:31:24 -08001018 if (status < 0) {
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001019 /*
1020 * An error return must mean that no cluster locks
1021 * were held on function exit.
1022 */
Tao Ma1e6d9152010-12-20 16:21:11 +08001023 if (oi1->ip_blkno != oi2->ip_blkno) {
Mark Fashehe63aecb62007-10-18 15:30:42 -07001024 ocfs2_inode_unlock(inode2, 1);
Tao Ma1e6d9152010-12-20 16:21:11 +08001025 brelse(*bh2);
1026 *bh2 = NULL;
1027 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001028
Mark Fashehccd979b2005-12-15 14:31:24 -08001029 if (status != -ENOENT)
1030 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001031 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001032
Mark Fashehccd979b2005-12-15 14:31:24 -08001033bail:
Tao Mac1e8d352011-03-07 16:43:21 +08001034 if (status)
1035 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001036 return status;
1037}
1038
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001039static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
1040{
Mark Fashehe63aecb62007-10-18 15:30:42 -07001041 ocfs2_inode_unlock(inode1, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001042
1043 if (inode1 != inode2)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001044 ocfs2_inode_unlock(inode2, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001045}
1046
Mark Fashehccd979b2005-12-15 14:31:24 -08001047static int ocfs2_rename(struct inode *old_dir,
1048 struct dentry *old_dentry,
1049 struct inode *new_dir,
1050 struct dentry *new_dentry)
1051{
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001052 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;
Mark Fashehccd979b2005-12-15 14:31:24 -08001054 struct inode *old_inode = old_dentry->d_inode;
1055 struct inode *new_inode = new_dentry->d_inode;
Mark Fasheh5098c272006-10-05 18:12:57 -07001056 struct inode *orphan_dir = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001057 struct ocfs2_dinode *newfe = NULL;
1058 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
Mark Fashehccd979b2005-12-15 14:31:24 -08001059 struct buffer_head *newfe_bh = NULL;
Mark Fasheh592282c2007-01-02 17:59:40 -08001060 struct buffer_head *old_inode_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001061 struct ocfs2_super *osb = NULL;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001062 u64 newfe_blkno, old_de_ino;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001063 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001064 struct buffer_head *old_dir_bh = NULL;
1065 struct buffer_head *new_dir_bh = NULL;
Mark Fasheh592282c2007-01-02 17:59:40 -08001066 nlink_t old_dir_nlink = old_dir->i_nlink;
Sunil Mushran480214d2007-08-06 15:11:56 -07001067 struct ocfs2_dinode *old_di;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001068 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, };
Mark Fashehccd979b2005-12-15 14:31:24 -08001073
1074 /* At some point it might be nice to break this function up a
1075 * bit. */
1076
Tao Maef6b6892011-02-21 11:10:44 +08001077 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);
Mark Fashehccd979b2005-12-15 14:31:24 -08001081
Christoph Hellwig871a2932010-03-03 09:05:07 -05001082 dquot_initialize(old_dir);
1083 dquot_initialize(new_dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05001084
Mark Fashehccd979b2005-12-15 14:31:24 -08001085 osb = OCFS2_SB(old_dir->i_sb);
1086
1087 if (new_inode) {
1088 if (!igrab(new_inode))
1089 BUG();
1090 }
1091
Uwe Kleine-König1b3c3712007-02-17 19:23:03 +01001092 /* Assume a directory hierarchy thusly:
Mark Fashehccd979b2005-12-15 14:31:24 -08001093 * 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. */
Jan Kara5dabd692008-02-21 18:00:00 +01001103 if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001104 status = ocfs2_rename_lock(osb);
1105 if (status < 0) {
1106 mlog_errno(status);
1107 goto bail;
1108 }
1109 rename_lock = 1;
1110 }
1111
Mark Fashehccd979b2005-12-15 14:31:24 -08001112 /* if old and new are the same, this'll just do one lock. */
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001113 status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
1114 &new_dir_bh, new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001115 if (status < 0) {
1116 mlog_errno(status);
1117 goto bail;
1118 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001119 parents_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001120
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
Mark Fasheh379dfe92006-09-08 14:21:03 -07001134 /*
Mark Fasheh592282c2007-01-02 17:59:40 -08001135 * Aside from allowing a meta data update, the locking here
Mark Fasheh34d024f2007-09-24 15:56:19 -07001136 * also ensures that the downconvert thread on other nodes
1137 * won't have to concurrently downconvert the inode and the
1138 * dentry locks.
Mark Fasheh379dfe92006-09-08 14:21:03 -07001139 */
Jan Karacb257972009-06-04 15:26:50 +02001140 status = ocfs2_inode_lock_nested(old_inode, &old_inode_bh, 1,
1141 OI_LS_PARENT);
Mark Fasheh379dfe92006-09-08 14:21:03 -07001142 if (status < 0) {
1143 if (status != -ENOENT)
Mark Fashehccd979b2005-12-15 14:31:24 -08001144 mlog_errno(status);
Mark Fasheh379dfe92006-09-08 14:21:03 -07001145 goto bail;
1146 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001147 old_child_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001148
Mark Fasheh379dfe92006-09-08 14:21:03 -07001149 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)) {
Mark Fasheh38760e22007-09-11 17:21:56 -07001156 u64 old_inode_parent;
Mark Fashehccd979b2005-12-15 14:31:24 -08001157
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001158 update_dot_dot = 1;
Mark Fasheh38760e22007-09-11 17:21:56 -07001159 status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001160 old_inode,
1161 &old_inode_dot_dot_res);
Mark Fasheh38760e22007-09-11 17:21:56 -07001162 if (status) {
1163 status = -EIO;
Mark Fashehccd979b2005-12-15 14:31:24 -08001164 goto bail;
Mark Fasheh38760e22007-09-11 17:21:56 -07001165 }
1166
1167 if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) {
1168 status = -EIO;
Mark Fashehccd979b2005-12-15 14:31:24 -08001169 goto bail;
Mark Fasheh38760e22007-09-11 17:21:56 -07001170 }
1171
1172 if (!new_inode && new_dir != old_dir &&
Mark Fasheh198a1ca2008-11-20 17:54:57 -08001173 new_dir->i_nlink >= ocfs2_link_max(osb)) {
Mark Fasheh38760e22007-09-11 17:21:56 -07001174 status = -EMLINK;
1175 goto bail;
1176 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001177 }
1178
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001179 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;
Mark Fashehccd979b2005-12-15 14:31:24 -08001184 goto bail;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001185 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001186
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 */
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001193 if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) {
1194 status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -08001195 goto bail;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001196 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001197
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,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001202 &newfe_blkno, new_dir,
1203 &target_lookup_res);
Mark Fashehccd979b2005-12-15 14:31:24 -08001204 /* 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 }
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001212 if (status == 0)
1213 target_exists = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001214
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001215 if (!target_exists && new_inode) {
Srinivas Eedae325a882007-10-31 16:49:43 -07001216 /*
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 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001225
1226 /* In case we need to overwrite an existing file, we blow it
1227 * away first */
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001228 if (target_exists) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001229 /* 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
Mark Fashehb0697052006-03-03 10:24:33 -08001246 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,
Mark Fashehccd979b2005-12-15 14:31:24 -08001249 OCFS2_I(new_inode)->ip_flags);
1250 goto bail;
1251 }
1252
Mark Fashehe63aecb62007-10-18 15:30:42 -07001253 status = ocfs2_inode_lock(new_inode, &newfe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001254 if (status < 0) {
1255 if (status != -ENOENT)
1256 mlog_errno(status);
1257 goto bail;
1258 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001259 new_child_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001260
Mark Fasheh379dfe92006-09-08 14:21:03 -07001261 status = ocfs2_remote_dentry_delete(new_dentry);
Mark Fashehccd979b2005-12-15 14:31:24 -08001262 if (status < 0) {
1263 mlog_errno(status);
1264 goto bail;
1265 }
1266
1267 newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
1268
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001269 mlog(0, "aha rename over existing... new_blkno=%llu "
1270 "newfebh=%p bhblocknr=%llu\n",
Mark Fashehb0697052006-03-03 10:24:33 -08001271 (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
Mark Fashehccd979b2005-12-15 14:31:24 -08001272 (unsigned long long)newfe_bh->b_blocknr : 0ULL);
1273
1274 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
Mark Fasheh5098c272006-10-05 18:12:57 -07001275 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
Tao Ma19bd3412009-08-18 11:44:10 +08001276 OCFS2_I(new_inode)->ip_blkno,
1277 orphan_name, &orphan_insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08001278 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,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001295 &target_insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08001296 if (status < 0) {
1297 mlog_errno(status);
1298 goto bail;
1299 }
1300 }
1301
Jan Karaa90714c2008-10-09 19:38:40 +02001302 handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001303 if (IS_ERR(handle)) {
1304 status = PTR_ERR(handle);
1305 handle = NULL;
1306 mlog_errno(status);
1307 goto bail;
1308 }
1309
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001310 if (target_exists) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001311 if (S_ISDIR(new_inode->i_mode)) {
Mark Fashehb80b5492009-02-18 11:41:38 -08001312 if (new_inode->i_nlink != 2 ||
1313 !ocfs2_empty_dir(new_inode)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001314 status = -ENOTEMPTY;
1315 goto bail;
1316 }
1317 }
Joel Becker0cf2f762009-02-12 16:41:25 -08001318 status = ocfs2_journal_access_di(handle, INODE_CACHE(new_inode),
1319 newfe_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001320 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001321 if (status < 0) {
1322 mlog_errno(status);
1323 goto bail;
1324 }
1325
1326 if (S_ISDIR(new_inode->i_mode) ||
Mark Fasheh198a1ca2008-11-20 17:54:57 -08001327 (ocfs2_read_links_count(newfe) == 1)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001328 status = ocfs2_orphan_add(osb, handle, new_inode,
Tristan Ye3939fda2010-03-19 09:21:09 +08001329 newfe_bh, orphan_name,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001330 &orphan_insert, orphan_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001331 if (status < 0) {
1332 mlog_errno(status);
1333 goto bail;
1334 }
1335 }
1336
1337 /* change the dirent to point to the correct inode */
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001338 status = ocfs2_update_entry(new_dir, handle, &target_lookup_res,
1339 old_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001340 if (status < 0) {
1341 mlog_errno(status);
1342 goto bail;
1343 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001344 new_dir->i_version++;
Mark Fashehccd979b2005-12-15 14:31:24 -08001345
1346 if (S_ISDIR(new_inode->i_mode))
Mark Fasheh198a1ca2008-11-20 17:54:57 -08001347 ocfs2_set_links_count(newfe, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001348 else
Mark Fasheh198a1ca2008-11-20 17:54:57 -08001349 ocfs2_add_links_count(newfe, -1);
Joel Beckerec20cec2010-03-19 14:13:52 -07001350 ocfs2_journal_dirty(handle, newfe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001351 } 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,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001355 new_dir_bh, &target_insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08001356 }
1357
1358 old_inode->i_ctime = CURRENT_TIME;
1359 mark_inode_dirty(old_inode);
Sunil Mushran480214d2007-08-06 15:11:56 -07001360
Joel Becker0cf2f762009-02-12 16:41:25 -08001361 status = ocfs2_journal_access_di(handle, INODE_CACHE(old_inode),
1362 old_inode_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001363 OCFS2_JOURNAL_ACCESS_WRITE);
Sunil Mushran480214d2007-08-06 15:11:56 -07001364 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);
Joel Beckerec20cec2010-03-19 14:13:52 -07001369 ocfs2_journal_dirty(handle, old_inode_bh);
Sunil Mushran480214d2007-08-06 15:11:56 -07001370 } else
1371 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001372
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001373 /*
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 */
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001380 status = ocfs2_find_entry(old_dentry->d_name.name,
1381 old_dentry->d_name.len, old_dir,
1382 &old_entry_lookup);
1383 if (status)
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001384 goto bail;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001385
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001386 status = ocfs2_delete_entry(handle, old_dir, &old_entry_lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -08001387 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;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001397
1398 if (update_dot_dot) {
1399 status = ocfs2_update_entry(old_inode, handle,
1400 &old_inode_dot_dot_res, new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001401 old_dir->i_nlink--;
1402 if (new_inode) {
1403 new_inode->i_nlink--;
1404 } else {
Dave Hansend8c76e62006-09-30 23:29:04 -07001405 inc_nlink(new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001406 mark_inode_dirty(new_dir);
1407 }
1408 }
1409 mark_inode_dirty(old_dir);
Mark Fasheh592282c2007-01-02 17:59:40 -08001410 ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
1411 if (new_inode) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001412 mark_inode_dirty(new_inode);
Mark Fasheh592282c2007-01-02 17:59:40 -08001413 ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
1414 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001415
Mark Fasheh592282c2007-01-02 17:59:40 -08001416 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 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001426
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 "
Mark Fashehb0697052006-03-03 10:24:33 -08001430 "%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);
Mark Fashehccd979b2005-12-15 14:31:24 -08001433 } else {
1434 struct ocfs2_dinode *fe;
Joel Becker0cf2f762009-02-12 16:41:25 -08001435 status = ocfs2_journal_access_di(handle,
1436 INODE_CACHE(old_dir),
1437 old_dir_bh,
1438 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001439 fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
Mark Fasheh198a1ca2008-11-20 17:54:57 -08001440 ocfs2_set_links_count(fe, old_dir->i_nlink);
Joel Beckerec20cec2010-03-19 14:13:52 -07001441 ocfs2_journal_dirty(handle, old_dir_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001442 }
1443 }
Mark Fasheh379dfe92006-09-08 14:21:03 -07001444 ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001445 status = 0;
1446bail:
1447 if (rename_lock)
1448 ocfs2_rename_unlock(osb);
1449
1450 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001451 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001452
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001453 if (parents_locked)
1454 ocfs2_double_unlock(old_dir, new_dir);
1455
1456 if (old_child_locked)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001457 ocfs2_inode_unlock(old_inode, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001458
1459 if (new_child_locked)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001460 ocfs2_inode_unlock(new_inode, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001461
Mark Fasheh5098c272006-10-05 18:12:57 -07001462 if (orphan_dir) {
1463 /* This was locked for us in ocfs2_prepare_orphan_dir() */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001464 ocfs2_inode_unlock(orphan_dir, 1);
Mark Fasheh5098c272006-10-05 18:12:57 -07001465 mutex_unlock(&orphan_dir->i_mutex);
1466 iput(orphan_dir);
1467 }
1468
Mark Fashehccd979b2005-12-15 14:31:24 -08001469 if (new_inode)
1470 sync_mapping_buffers(old_inode->i_mapping);
1471
1472 if (new_inode)
1473 iput(new_inode);
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001474
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
Mark Fasheha81cb882008-10-07 14:25:16 -07001481 brelse(newfe_bh);
1482 brelse(old_inode_bh);
1483 brelse(old_dir_bh);
1484 brelse(new_dir_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001485
Tao Mac1e8d352011-03-07 16:43:21 +08001486 if (status)
1487 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001488
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,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001497 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001498 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;
Mark Fasheh4f902c32007-03-09 16:26:50 -08001504 u64 p_blkno, p_blocks;
Mark Fashehccd979b2005-12-15 14:31:24 -08001505 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
Tao Maef6b6892011-02-21 11:10:44 +08001512 mlog(0, "i_blocks = %llu, i_size = %llu, blocks = %d\n",
1513 (unsigned long long)inode->i_blocks,
1514 i_size_read(inode), blocks);
Mark Fashehccd979b2005-12-15 14:31:24 -08001515
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
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001531 status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
1532 NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001533 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 }
Joel Becker8cb471e2009-02-10 20:00:41 -08001557 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode),
1558 bhs[virtual]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001559
Joel Becker0cf2f762009-02-12 16:41:25 -08001560 status = ocfs2_journal_access(handle, INODE_CACHE(inode),
1561 bhs[virtual],
Mark Fashehccd979b2005-12-15 14:31:24 -08001562 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
Joel Beckerec20cec2010-03-19 14:13:52 -07001574 ocfs2_journal_dirty(handle, bhs[virtual]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001575
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++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001586 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001587 kfree(bhs);
1588 }
1589
Tao Mac1e8d352011-03-07 16:43:21 +08001590 if (status)
1591 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001592 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;
Mark Fashehccd979b2005-12-15 14:31:24 -08001605 struct buffer_head *parent_fe_bh = NULL;
1606 struct ocfs2_dinode *fe = NULL;
1607 struct ocfs2_dinode *dirfe;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001608 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001609 struct ocfs2_alloc_context *inode_ac = NULL;
1610 struct ocfs2_alloc_context *data_ac = NULL;
Tiger Yang534eadd2008-11-14 11:16:41 +08001611 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 };
Jan Karaa90714c2008-10-09 19:38:40 +02001617 int did_quota = 0, did_quota_inode = 0;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001618 struct ocfs2_dir_lookup_result lookup = { NULL, };
Joel Becker547ba7c2010-05-10 11:56:52 -07001619 sigset_t oldset;
1620 int did_block_signals = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001621
Tao Maef6b6892011-02-21 11:10:44 +08001622 mlog(0, "(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
1623 dentry, symname, dentry->d_name.len, dentry->d_name.name);
Mark Fashehccd979b2005-12-15 14:31:24 -08001624
Christoph Hellwig871a2932010-03-03 09:05:07 -05001625 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05001626
Mark Fashehccd979b2005-12-15 14:31:24 -08001627 sb = dir->i_sb;
1628 osb = OCFS2_SB(sb);
1629
1630 l = strlen(symname) + 1;
1631
1632 credits = ocfs2_calc_symlink_credits(sb);
1633
Mark Fashehccd979b2005-12-15 14:31:24 -08001634 /* lock the parent directory */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001635 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001636 if (status < 0) {
1637 if (status != -ENOENT)
1638 mlog_errno(status);
Mark Fasheh6d8fc402006-10-06 11:54:33 -07001639 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001640 }
1641
1642 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
Mark Fasheh198a1ca2008-11-20 17:54:57 -08001643 if (!ocfs2_read_links_count(dirfe)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001644 /* 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,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001656 dentry->d_name.len, &lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -08001657 if (status < 0) {
1658 mlog_errno(status);
1659 goto bail;
1660 }
1661
Mark Fashehda5cbf22006-10-06 18:34:35 -07001662 status = ocfs2_reserve_new_inode(osb, &inode_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001663 if (status < 0) {
1664 if (status != -ENOSPC)
1665 mlog_errno(status);
1666 goto bail;
1667 }
1668
Tiger Yangf5d36202008-11-14 11:15:44 +08001669 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
Tiger Yang534eadd2008-11-14 11:16:41 +08001676 /* 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);
Mark Fashehccd979b2005-12-15 14:31:24 -08001683 goto bail;
1684 }
1685 }
1686
Tiger Yang534eadd2008-11-14 11:16:41 +08001687 /* 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);
1691 if (status < 0) {
1692 mlog_errno(status);
1693 goto bail;
1694 }
1695 }
1696
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);
Mark Fashehccd979b2005-12-15 14:31:24 -08001709 if (IS_ERR(handle)) {
1710 status = PTR_ERR(handle);
1711 handle = NULL;
1712 mlog_errno(status);
1713 goto bail;
1714 }
1715
Joel Becker547ba7c2010-05-10 11:56:52 -07001716 /* Starting to change things, restart is no longer possible. */
1717 ocfs2_block_signals(&oldset);
1718 did_block_signals = 1;
1719
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001720 status = dquot_alloc_inode(inode);
1721 if (status)
Jan Karaa90714c2008-10-09 19:38:40 +02001722 goto bail;
Jan Karaa90714c2008-10-09 19:38:40 +02001723 did_quota_inode = 1;
1724
Tao Maef6b6892011-02-21 11:10:44 +08001725 mlog(0, "(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry,
1726 inode->i_mode, dentry->d_name.len,
1727 dentry->d_name.name);
Tao Ma19bd3412009-08-18 11:44:10 +08001728
1729 status = ocfs2_mknod_locked(osb, dir, inode,
Tiger Yangf5d36202008-11-14 11:15:44 +08001730 0, &new_fe_bh, parent_fe_bh, handle,
1731 inode_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001732 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)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08001741 u32 offset = 0;
1742
Mark Fashehccd979b2005-12-15 14:31:24 -08001743 inode->i_op = &ocfs2_symlink_inode_operations;
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001744 status = dquot_alloc_space_nodirty(inode,
1745 ocfs2_clusters_to_bytes(osb->sb, 1));
1746 if (status)
Jan Karaa90714c2008-10-09 19:38:40 +02001747 goto bail;
Jan Karaa90714c2008-10-09 19:38:40 +02001748 did_quota = 1;
Tao Ma0eb8d472008-08-18 17:38:45 +08001749 status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0,
1750 new_fe_bh,
1751 handle, data_ac, NULL,
1752 NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001753 if (status < 0) {
1754 if (status != -ENOSPC && status != -EINTR) {
Mark Fashehb0697052006-03-03 10:24:33 -08001755 mlog(ML_ERROR,
1756 "Failed to extend file to %llu\n",
1757 (unsigned long long)newsize);
Mark Fashehccd979b2005-12-15 14:31:24 -08001758 mlog_errno(status);
1759 status = -ENOSPC;
1760 }
1761 goto bail;
1762 }
1763 i_size_write(inode, newsize);
Mark Fasheh8110b072007-03-22 16:53:23 -07001764 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001765 } 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
Tiger Yang534eadd2008-11-14 11:16:41 +08001787 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
Mark Fasheha9743fc2010-04-23 11:42:22 -07001796 /*
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) {
1803 mlog_errno(status);
1804 goto bail;
1805 }
Mark Fasheha9743fc2010-04-23 11:42:22 -07001806
Mark Fashehccd979b2005-12-15 14:31:24 -08001807 status = ocfs2_add_entry(handle, dentry, inode,
1808 le64_to_cpu(fe->i_blkno), parent_fe_bh,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001809 &lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -08001810 if (status < 0) {
1811 mlog_errno(status);
1812 goto bail;
1813 }
1814
1815 insert_inode_hash(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001816 d_instantiate(dentry, inode);
1817bail:
Jan Karaa90714c2008-10-09 19:38:40 +02001818 if (status < 0 && did_quota)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001819 dquot_free_space_nodirty(inode,
Jan Karaa90714c2008-10-09 19:38:40 +02001820 ocfs2_clusters_to_bytes(osb->sb, 1));
1821 if (status < 0 && did_quota_inode)
Christoph Hellwig63936dd2010-03-03 09:05:01 -05001822 dquot_free_inode(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001823 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001824 ocfs2_commit_trans(osb, handle);
Mark Fasheh6d8fc402006-10-06 11:54:33 -07001825
Mark Fashehe63aecb62007-10-18 15:30:42 -07001826 ocfs2_inode_unlock(dir, 1);
Joel Becker547ba7c2010-05-10 11:56:52 -07001827 if (did_block_signals)
1828 ocfs2_unblock_signals(&oldset);
Mark Fasheh6d8fc402006-10-06 11:54:33 -07001829
Mark Fasheha81cb882008-10-07 14:25:16 -07001830 brelse(new_fe_bh);
1831 brelse(parent_fe_bh);
Tiger Yang534eadd2008-11-14 11:16:41 +08001832 kfree(si.name);
1833 kfree(si.value);
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001834 ocfs2_free_dir_lookup_result(&lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -08001835 if (inode_ac)
1836 ocfs2_free_alloc_context(inode_ac);
1837 if (data_ac)
1838 ocfs2_free_alloc_context(data_ac);
Tiger Yang534eadd2008-11-14 11:16:41 +08001839 if (xattr_ac)
1840 ocfs2_free_alloc_context(xattr_ac);
Tiger Yangf5d36202008-11-14 11:15:44 +08001841 if ((status < 0) && inode) {
Li Dongyangab41fdc2010-04-22 16:11:25 +08001842 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SKIP_ORPHAN_DIR;
Tiger Yangf5d36202008-11-14 11:15:44 +08001843 clear_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001844 iput(inode);
Tiger Yangf5d36202008-11-14 11:15:44 +08001845 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001846
Tao Mac1e8d352011-03-07 16:43:21 +08001847 if (status)
1848 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001849
1850 return status;
1851}
1852
Mark Fashehccd979b2005-12-15 14:31:24 -08001853static int ocfs2_blkno_stringify(u64 blkno, char *name)
1854{
1855 int status, namelen;
1856
Mark Fashehb0697052006-03-03 10:24:33 -08001857 namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
1858 (long long)blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001859 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:
Tao Mac1e8d352011-03-07 16:43:21 +08001878 if (status < 0)
1879 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001880 return status;
1881}
1882
Mark Fashehdd43bcd2010-08-13 15:15:18 -07001883static int ocfs2_lookup_lock_orphan_dir(struct ocfs2_super *osb,
1884 struct inode **ret_orphan_dir,
1885 struct buffer_head **ret_orphan_dir_bh)
1886{
1887 struct inode *orphan_dir_inode;
1888 struct buffer_head *orphan_dir_bh = NULL;
1889 int ret = 0;
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) {
1895 ret = -ENOENT;
1896 mlog_errno(ret);
1897 return ret;
1898 }
1899
1900 mutex_lock(&orphan_dir_inode->i_mutex);
1901
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;
1909 }
1910
1911 *ret_orphan_dir = orphan_dir_inode;
1912 *ret_orphan_dir_bh = orphan_dir_bh;
1913
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 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001957static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
Mark Fasheh5098c272006-10-05 18:12:57 -07001958 struct inode **ret_orphan_dir,
Tao Ma19bd3412009-08-18 11:44:10 +08001959 u64 blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001960 char *name,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001961 struct ocfs2_dir_lookup_result *lookup)
Mark Fashehccd979b2005-12-15 14:31:24 -08001962{
Mark Fashehdd43bcd2010-08-13 15:15:18 -07001963 struct inode *orphan_dir_inode = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001964 struct buffer_head *orphan_dir_bh = NULL;
Mark Fashehdd43bcd2010-08-13 15:15:18 -07001965 int ret = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001966
Mark Fashehdd43bcd2010-08-13 15:15:18 -07001967 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;
Mark Fashehccd979b2005-12-15 14:31:24 -08001972 }
1973
Mark Fashehdd43bcd2010-08-13 15:15:18 -07001974 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;
Mark Fashehccd979b2005-12-15 14:31:24 -08001979 }
1980
Mark Fasheh5098c272006-10-05 18:12:57 -07001981 *ret_orphan_dir = orphan_dir_inode;
1982
Mark Fashehdd43bcd2010-08-13 15:15:18 -07001983out:
1984 brelse(orphan_dir_bh);
1985
1986 if (ret) {
1987 ocfs2_inode_unlock(orphan_dir_inode, 1);
Mark Fasheh5098c272006-10-05 18:12:57 -07001988 mutex_unlock(&orphan_dir_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001989 iput(orphan_dir_inode);
Mark Fasheh5098c272006-10-05 18:12:57 -07001990 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001991
Tao Mac1e8d352011-03-07 16:43:21 +08001992 if (ret)
1993 mlog_errno(ret);
Mark Fashehdd43bcd2010-08-13 15:15:18 -07001994 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001995}
1996
1997static int ocfs2_orphan_add(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001998 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001999 struct inode *inode,
Tristan Ye3939fda2010-03-19 09:21:09 +08002000 struct buffer_head *fe_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08002001 char *name,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08002002 struct ocfs2_dir_lookup_result *lookup,
Mark Fasheh5098c272006-10-05 18:12:57 -07002003 struct inode *orphan_dir_inode)
Mark Fashehccd979b2005-12-15 14:31:24 -08002004{
Mark Fashehccd979b2005-12-15 14:31:24 -08002005 struct buffer_head *orphan_dir_bh = NULL;
2006 int status = 0;
2007 struct ocfs2_dinode *orphan_fe;
Tristan Ye3939fda2010-03-19 09:21:09 +08002008 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08002009
Tao Maef6b6892011-02-21 11:10:44 +08002010 mlog(0, "(inode->i_ino = %lu)\n", inode->i_ino);
Mark Fashehccd979b2005-12-15 14:31:24 -08002011
Joel Beckerb657c952008-11-13 14:49:11 -08002012 status = ocfs2_read_inode_block(orphan_dir_inode, &orphan_dir_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002013 if (status < 0) {
2014 mlog_errno(status);
2015 goto leave;
2016 }
2017
Joel Becker0cf2f762009-02-12 16:41:25 -08002018 status = ocfs2_journal_access_di(handle,
2019 INODE_CACHE(orphan_dir_inode),
2020 orphan_dir_bh,
Joel Becker13723d02008-10-17 19:25:01 -07002021 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002022 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))
Mark Fasheh198a1ca2008-11-20 17:54:57 -08002031 ocfs2_add_links_count(orphan_fe, 1);
2032 orphan_dir_inode->i_nlink = ocfs2_read_links_count(orphan_fe);
Joel Beckerec20cec2010-03-19 14:13:52 -07002033 ocfs2_journal_dirty(handle, orphan_dir_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002034
2035 status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
2036 OCFS2_ORPHAN_NAMELEN, inode,
2037 OCFS2_I(inode)->ip_blkno,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08002038 orphan_dir_bh, lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -08002039 if (status < 0) {
2040 mlog_errno(status);
2041 goto leave;
2042 }
2043
Tristan Ye3939fda2010-03-19 09:21:09 +08002044 /*
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
Mark Fashehccd979b2005-12-15 14:31:24 -08002059 le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
Li Dongyangd4cd1872010-04-22 16:11:19 +08002060 OCFS2_I(inode)->ip_flags &= ~OCFS2_INODE_SKIP_ORPHAN_DIR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002061
2062 /* Record which orphan dir our inode now resides
2063 * in. delete_inode will use this to determine which orphan
2064 * dir to lock. */
Tiger Yang50008632007-03-20 16:01:38 -07002065 fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08002066
Tristan Ye3939fda2010-03-19 09:21:09 +08002067 ocfs2_journal_dirty(handle, fe_bh);
2068
Mark Fashehb0697052006-03-03 10:24:33 -08002069 mlog(0, "Inode %llu orphaned in slot %d\n",
2070 (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08002071
2072leave:
Mark Fasheha81cb882008-10-07 14:25:16 -07002073 brelse(orphan_dir_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002074
Tao Mac1e8d352011-03-07 16:43:21 +08002075 if (status)
2076 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002077 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,
Mark Fasheh1fabe142006-10-09 18:11:45 -07002082 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002083 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;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08002090 struct ocfs2_dir_lookup_result lookup = { NULL, };
Mark Fashehccd979b2005-12-15 14:31:24 -08002091
Mark Fashehccd979b2005-12-15 14:31:24 -08002092 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2093 if (status < 0) {
2094 mlog_errno(status);
2095 goto leave;
2096 }
2097
Mark Fashehb0697052006-03-03 10:24:33 -08002098 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);
Mark Fashehccd979b2005-12-15 14:31:24 -08002101
2102 /* find it's spot in the orphan directory */
Mark Fasheh4a12ca32008-11-12 15:43:34 -08002103 status = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN, orphan_dir_inode,
2104 &lookup);
2105 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002106 mlog_errno(status);
2107 goto leave;
2108 }
2109
2110 /* remove it from the orphan directory */
Mark Fasheh4a12ca32008-11-12 15:43:34 -08002111 status = ocfs2_delete_entry(handle, orphan_dir_inode, &lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -08002112 if (status < 0) {
2113 mlog_errno(status);
2114 goto leave;
2115 }
2116
Joel Becker0cf2f762009-02-12 16:41:25 -08002117 status = ocfs2_journal_access_di(handle,
2118 INODE_CACHE(orphan_dir_inode),
2119 orphan_dir_bh,
Joel Becker13723d02008-10-17 19:25:01 -07002120 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002121 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))
Mark Fasheh198a1ca2008-11-20 17:54:57 -08002129 ocfs2_add_links_count(orphan_fe, -1);
2130 orphan_dir_inode->i_nlink = ocfs2_read_links_count(orphan_fe);
Joel Beckerec20cec2010-03-19 14:13:52 -07002131 ocfs2_journal_dirty(handle, orphan_dir_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002132
2133leave:
Mark Fasheh4a12ca32008-11-12 15:43:34 -08002134 ocfs2_free_dir_lookup_result(&lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -08002135
Tao Mac1e8d352011-03-07 16:43:21 +08002136 if (status)
2137 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002138 return status;
2139}
2140
Mark Fasheh97b8f4a2010-08-13 15:15:19 -07002141/**
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
Tao Mabc13d342009-08-18 11:44:14 +08002234int 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, };
Mark Fasheh97b8f4a2010-08-13 15:15:19 -07002249 u64 uninitialized_var(di_blkno), suballoc_loc;
2250 u16 suballoc_bit;
Tao Mabc13d342009-08-18 11:44:14 +08002251
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
Mark Fasheh97b8f4a2010-08-13 15:15:19 -07002259 status = ocfs2_prep_new_orphaned_file(dir, parent_di_bh,
2260 orphan_name, &orphan_dir,
2261 &di_blkno, &orphan_insert, &inode_ac);
Tao Mabc13d342009-08-18 11:44:14 +08002262 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
Christoph Hellwig63936dd2010-03-03 09:05:01 -05002283 status = dquot_alloc_inode(inode);
2284 if (status)
Tao Mabc13d342009-08-18 11:44:14 +08002285 goto leave;
Tao Mabc13d342009-08-18 11:44:14 +08002286 did_quota_inode = 1;
2287
Mark Fasheh97b8f4a2010-08-13 15:15:19 -07002288 status = ocfs2_claim_new_inode_at_loc(handle, dir, inode_ac,
2289 &suballoc_loc,
2290 &suballoc_bit, di_blkno);
Tao Mabc13d342009-08-18 11:44:14 +08002291 if (status < 0) {
2292 mlog_errno(status);
2293 goto leave;
2294 }
2295
Mark Fasheh97b8f4a2010-08-13 15:15:19 -07002296 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);
Tao Mabc13d342009-08-18 11:44:14 +08002302 if (status < 0) {
2303 mlog_errno(status);
2304 goto leave;
2305 }
2306
2307 di = (struct ocfs2_dinode *)new_di_bh->b_data;
Tristan Ye3939fda2010-03-19 09:21:09 +08002308 status = ocfs2_orphan_add(osb, handle, inode, new_di_bh, orphan_name,
Tao Mabc13d342009-08-18 11:44:14 +08002309 &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
Tao Mac7d260a2009-12-18 10:24:54 +08002320 insert_inode_hash(inode);
Tao Mabc13d342009-08-18 11:44:14 +08002321leave:
2322 if (status < 0 && did_quota_inode)
Christoph Hellwig63936dd2010-03-03 09:05:01 -05002323 dquot_free_inode(inode);
Tao Mabc13d342009-08-18 11:44:14 +08002324 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
Tao Maef6b6892011-02-21 11:10:44 +08002371 mlog(0, "(0x%p, 0x%p, %.*s')\n", dir, dentry,
2372 dentry->d_name.len, dentry->d_name.name);
Tao Mabc13d342009-08-18 11:44:14 +08002373
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;
Tao Ma10cf1a02009-12-18 10:24:55 +08002452 inode->i_nlink = 1;
2453 ocfs2_set_links_count(di, inode->i_nlink);
Tao Mabc13d342009-08-18 11:44:14 +08002454 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
Tao Mabc13d342009-08-18 11:44:14 +08002471 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
Tao Mac1e8d352011-03-07 16:43:21 +08002489 if (status)
2490 mlog_errno(status);
Tao Mabc13d342009-08-18 11:44:14 +08002491
2492 return status;
2493}
2494
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002495const struct inode_operations ocfs2_dir_iops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002496 .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,
Tiger Yangd38eb8d2006-11-27 09:59:21 +08002507 .permission = ocfs2_permission,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002508 .setxattr = generic_setxattr,
2509 .getxattr = generic_getxattr,
2510 .listxattr = ocfs2_listxattr,
2511 .removexattr = generic_removexattr,
Tristan Ye55f49462009-12-17 18:42:16 +08002512 .fiemap = ocfs2_fiemap,
Mark Fashehccd979b2005-12-15 14:31:24 -08002513};