blob: 40da46b907fb5ac8b89bb9cd68aa01b0db09ae96 [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>
43
44#define MLOG_MASK_PREFIX ML_NAMEI
45#include <cluster/masklog.h>
46
47#include "ocfs2.h"
48
49#include "alloc.h"
50#include "dcache.h"
51#include "dir.h"
52#include "dlmglue.h"
53#include "extent_map.h"
54#include "file.h"
55#include "inode.h"
56#include "journal.h"
57#include "namei.h"
58#include "suballoc.h"
Mark Fashehaa958872006-04-21 13:49:02 -070059#include "super.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080060#include "symlink.h"
61#include "sysfile.h"
62#include "uptodate.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080063#include "xattr.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080064
65#include "buffer_head_io.h"
66
Mark Fashehccd979b2005-12-15 14:31:24 -080067static int ocfs2_mknod_locked(struct ocfs2_super *osb,
68 struct inode *dir,
Tiger Yangf5d36202008-11-14 11:15:44 +080069 struct inode *inode,
70 struct dentry *dentry,
Mark Fashehccd979b2005-12-15 14:31:24 -080071 dev_t dev,
72 struct buffer_head **new_fe_bh,
73 struct buffer_head *parent_fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -070074 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080075 struct ocfs2_alloc_context *inode_ac);
76
Mark Fashehccd979b2005-12-15 14:31:24 -080077static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
Mark Fasheh5098c272006-10-05 18:12:57 -070078 struct inode **ret_orphan_dir,
Mark Fashehccd979b2005-12-15 14:31:24 -080079 struct inode *inode,
80 char *name,
81 struct buffer_head **de_bh);
82
83static int ocfs2_orphan_add(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -070084 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080085 struct inode *inode,
86 struct ocfs2_dinode *fe,
87 char *name,
Mark Fasheh5098c272006-10-05 18:12:57 -070088 struct buffer_head *de_bh,
89 struct inode *orphan_dir_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -080090
91static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -070092 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080093 struct inode *inode,
94 const char *symname);
95
Mark Fashehccd979b2005-12-15 14:31:24 -080096/* An orphan dir name is an 8 byte value, printed as a hex string */
97#define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
98
99static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
100 struct nameidata *nd)
101{
102 int status;
103 u64 blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -0800104 struct inode *inode = NULL;
105 struct dentry *ret;
Mark Fashehccd979b2005-12-15 14:31:24 -0800106 struct ocfs2_inode_info *oi;
107
108 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
109 dentry->d_name.len, dentry->d_name.name);
110
111 if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
112 ret = ERR_PTR(-ENAMETOOLONG);
113 goto bail;
114 }
115
Mark Fashehb0697052006-03-03 10:24:33 -0800116 mlog(0, "find name %.*s in directory %llu\n", dentry->d_name.len,
117 dentry->d_name.name, (unsigned long long)OCFS2_I(dir)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800118
Mark Fashehe63aecb62007-10-18 15:30:42 -0700119 status = ocfs2_inode_lock(dir, NULL, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800120 if (status < 0) {
121 if (status != -ENOENT)
122 mlog_errno(status);
123 ret = ERR_PTR(status);
124 goto bail;
125 }
126
Mark Fashehbe94d112007-09-11 15:22:06 -0700127 status = ocfs2_lookup_ino_from_name(dir, dentry->d_name.name,
128 dentry->d_name.len, &blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800129 if (status < 0)
130 goto bail_add;
131
Jan Kara5fa06132008-01-11 00:11:45 +0100132 inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800133 if (IS_ERR(inode)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800134 ret = ERR_PTR(-EACCES);
135 goto bail_unlock;
136 }
137
138 oi = OCFS2_I(inode);
139 /* Clear any orphaned state... If we were able to look up the
140 * inode from a directory, it certainly can't be orphaned. We
141 * might have the bad state from a node which intended to
142 * orphan this inode but crashed before it could commit the
143 * unlink. */
144 spin_lock(&oi->ip_lock);
145 oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
Mark Fashehccd979b2005-12-15 14:31:24 -0800146 spin_unlock(&oi->ip_lock);
147
148bail_add:
Mark Fashehccd979b2005-12-15 14:31:24 -0800149 dentry->d_op = &ocfs2_dentry_ops;
150 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 }
173 }
174
Mark Fashehccd979b2005-12-15 14:31:24 -0800175bail_unlock:
176 /* Don't drop the cluster lock until *after* the d_add --
177 * unlink on another node will message us to remove that
178 * dentry under this lock so otherwise we can race this with
Mark Fasheh34d024f2007-09-24 15:56:19 -0700179 * the downconvert thread and have a stale dentry. */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700180 ocfs2_inode_unlock(dir, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800181
182bail:
Mark Fashehccd979b2005-12-15 14:31:24 -0800183
184 mlog_exit_ptr(ret);
185
186 return ret;
187}
188
Tiger Yangf5d36202008-11-14 11:15:44 +0800189static struct inode *ocfs2_get_init_inode(struct inode *dir, int mode)
190{
191 struct inode *inode;
192
193 inode = new_inode(dir->i_sb);
194 if (!inode) {
195 mlog(ML_ERROR, "new_inode failed!\n");
196 return NULL;
197 }
198
199 /* populate as many fields early on as possible - many of
200 * these are used by the support functions here and in
201 * callers. */
202 if (S_ISDIR(mode))
203 inode->i_nlink = 2;
204 else
205 inode->i_nlink = 1;
206 inode->i_uid = current_fsuid();
207 if (dir->i_mode & S_ISGID) {
208 inode->i_gid = dir->i_gid;
209 if (S_ISDIR(mode))
210 mode |= S_ISGID;
211 } else
212 inode->i_gid = current_fsgid();
213 inode->i_mode = mode;
214 return inode;
215}
216
Mark Fashehccd979b2005-12-15 14:31:24 -0800217static int ocfs2_mknod(struct inode *dir,
218 struct dentry *dentry,
219 int mode,
220 dev_t dev)
221{
222 int status = 0;
223 struct buffer_head *parent_fe_bh = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700224 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800225 struct ocfs2_super *osb;
226 struct ocfs2_dinode *dirfe;
227 struct buffer_head *new_fe_bh = NULL;
228 struct buffer_head *de_bh = NULL;
229 struct inode *inode = NULL;
230 struct ocfs2_alloc_context *inode_ac = NULL;
231 struct ocfs2_alloc_context *data_ac = NULL;
Tiger Yang534eadd2008-11-14 11:16:41 +0800232 struct ocfs2_alloc_context *xattr_ac = NULL;
233 int want_clusters = 0;
234 int xattr_credits = 0;
235 struct ocfs2_security_xattr_info si = {
236 .enable = 1,
237 };
Mark Fashehccd979b2005-12-15 14:31:24 -0800238
239 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
240 (unsigned long)dev, dentry->d_name.len,
241 dentry->d_name.name);
242
243 /* get our super block */
244 osb = OCFS2_SB(dir->i_sb);
245
Mark Fashehe63aecb62007-10-18 15:30:42 -0700246 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
Mark Fashehe3a82132006-10-05 16:04:17 -0700247 if (status < 0) {
248 if (status != -ENOENT)
249 mlog_errno(status);
250 return status;
251 }
252
Mark Fasheha663e302006-08-09 11:45:07 -0700253 if (S_ISDIR(mode) && (dir->i_nlink >= OCFS2_LINK_MAX)) {
254 status = -EMLINK;
255 goto leave;
256 }
257
Mark Fashehccd979b2005-12-15 14:31:24 -0800258 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
259 if (!dirfe->i_links_count) {
260 /* can't make a file in a deleted directory. */
261 status = -ENOENT;
262 goto leave;
263 }
264
265 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
266 dentry->d_name.len);
267 if (status)
268 goto leave;
269
270 /* get a spot inside the dir. */
271 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
272 dentry->d_name.name,
273 dentry->d_name.len, &de_bh);
274 if (status < 0) {
275 mlog_errno(status);
276 goto leave;
277 }
278
279 /* reserve an inode spot */
Mark Fashehda5cbf22006-10-06 18:34:35 -0700280 status = ocfs2_reserve_new_inode(osb, &inode_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800281 if (status < 0) {
282 if (status != -ENOSPC)
283 mlog_errno(status);
284 goto leave;
285 }
286
Tiger Yangf5d36202008-11-14 11:15:44 +0800287 inode = ocfs2_get_init_inode(dir, mode);
288 if (!inode) {
289 status = -ENOMEM;
290 mlog_errno(status);
291 goto leave;
292 }
293
Tiger Yang534eadd2008-11-14 11:16:41 +0800294 /* get security xattr */
295 status = ocfs2_init_security_get(inode, dir, &si);
296 if (status) {
297 if (status == -EOPNOTSUPP)
298 si.enable = 0;
299 else {
300 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800301 goto leave;
302 }
303 }
304
Tiger Yang534eadd2008-11-14 11:16:41 +0800305 /* calculate meta data/clusters for setting security xattr */
306 if (si.enable) {
307 status = ocfs2_calc_security_init(dir, &si, &want_clusters,
308 &xattr_credits, &xattr_ac);
309 if (status < 0) {
310 mlog_errno(status);
311 goto leave;
312 }
313 }
314
315 /* Reserve a cluster if creating an extent based directory. */
316 if (S_ISDIR(mode) && !ocfs2_supports_inline_data(osb))
317 want_clusters += 1;
318
319 status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
320 if (status < 0) {
321 if (status != -ENOSPC)
322 mlog_errno(status);
323 goto leave;
324 }
325
326 handle = ocfs2_start_trans(osb, OCFS2_MKNOD_CREDITS + xattr_credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800327 if (IS_ERR(handle)) {
328 status = PTR_ERR(handle);
329 handle = NULL;
330 mlog_errno(status);
331 goto leave;
332 }
333
334 /* do the real work now. */
Tiger Yangf5d36202008-11-14 11:15:44 +0800335 status = ocfs2_mknod_locked(osb, dir, inode, dentry, dev,
Mark Fashehccd979b2005-12-15 14:31:24 -0800336 &new_fe_bh, parent_fe_bh, handle,
Tiger Yangf5d36202008-11-14 11:15:44 +0800337 inode_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800338 if (status < 0) {
339 mlog_errno(status);
340 goto leave;
341 }
342
343 if (S_ISDIR(mode)) {
344 status = ocfs2_fill_new_dir(osb, handle, dir, inode,
345 new_fe_bh, data_ac);
346 if (status < 0) {
347 mlog_errno(status);
348 goto leave;
349 }
350
351 status = ocfs2_journal_access(handle, dir, parent_fe_bh,
352 OCFS2_JOURNAL_ACCESS_WRITE);
353 if (status < 0) {
354 mlog_errno(status);
355 goto leave;
356 }
357 le16_add_cpu(&dirfe->i_links_count, 1);
358 status = ocfs2_journal_dirty(handle, parent_fe_bh);
359 if (status < 0) {
360 mlog_errno(status);
361 goto leave;
362 }
Dave Hansend8c76e62006-09-30 23:29:04 -0700363 inc_nlink(dir);
Mark Fashehccd979b2005-12-15 14:31:24 -0800364 }
365
Tiger Yang534eadd2008-11-14 11:16:41 +0800366 if (si.enable) {
367 status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
368 xattr_ac, data_ac);
369 if (status < 0) {
370 mlog_errno(status);
371 goto leave;
372 }
373 }
374
Mark Fashehccd979b2005-12-15 14:31:24 -0800375 status = ocfs2_add_entry(handle, dentry, inode,
376 OCFS2_I(inode)->ip_blkno, parent_fe_bh,
377 de_bh);
378 if (status < 0) {
379 mlog_errno(status);
380 goto leave;
381 }
382
Mark Fasheh379dfe92006-09-08 14:21:03 -0700383 status = ocfs2_dentry_attach_lock(dentry, inode,
Mark Fasheh0027dd52006-09-21 16:51:28 -0700384 OCFS2_I(dir)->ip_blkno);
Mark Fasheh379dfe92006-09-08 14:21:03 -0700385 if (status) {
386 mlog_errno(status);
387 goto leave;
388 }
389
Mark Fashehccd979b2005-12-15 14:31:24 -0800390 insert_inode_hash(inode);
391 dentry->d_op = &ocfs2_dentry_ops;
392 d_instantiate(dentry, inode);
393 status = 0;
394leave:
395 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700396 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800397
Mark Fashehe63aecb62007-10-18 15:30:42 -0700398 ocfs2_inode_unlock(dir, 1);
Mark Fashehe3a82132006-10-05 16:04:17 -0700399
Mark Fashehccd979b2005-12-15 14:31:24 -0800400 if (status == -ENOSPC)
401 mlog(0, "Disk is full\n");
402
Mark Fasheha81cb882008-10-07 14:25:16 -0700403 brelse(new_fe_bh);
404 brelse(de_bh);
405 brelse(parent_fe_bh);
Tiger Yang534eadd2008-11-14 11:16:41 +0800406 kfree(si.name);
407 kfree(si.value);
Mark Fashehccd979b2005-12-15 14:31:24 -0800408
Tiger Yangf5d36202008-11-14 11:15:44 +0800409 if ((status < 0) && inode) {
410 clear_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800411 iput(inode);
Tiger Yangf5d36202008-11-14 11:15:44 +0800412 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800413
414 if (inode_ac)
415 ocfs2_free_alloc_context(inode_ac);
416
417 if (data_ac)
418 ocfs2_free_alloc_context(data_ac);
419
Tiger Yang534eadd2008-11-14 11:16:41 +0800420 if (xattr_ac)
421 ocfs2_free_alloc_context(xattr_ac);
422
Mark Fashehccd979b2005-12-15 14:31:24 -0800423 mlog_exit(status);
424
425 return status;
426}
427
428static int ocfs2_mknod_locked(struct ocfs2_super *osb,
429 struct inode *dir,
Tiger Yangf5d36202008-11-14 11:15:44 +0800430 struct inode *inode,
431 struct dentry *dentry,
Mark Fashehccd979b2005-12-15 14:31:24 -0800432 dev_t dev,
433 struct buffer_head **new_fe_bh,
434 struct buffer_head *parent_fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700435 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800436 struct ocfs2_alloc_context *inode_ac)
437{
438 int status = 0;
439 struct ocfs2_dinode *fe = NULL;
440 struct ocfs2_extent_list *fel;
441 u64 fe_blkno = 0;
442 u16 suballoc_bit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800443
Tiger Yangf5d36202008-11-14 11:15:44 +0800444 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry,
445 inode->i_mode, (unsigned long)dev, dentry->d_name.len,
Mark Fashehccd979b2005-12-15 14:31:24 -0800446 dentry->d_name.name);
447
448 *new_fe_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800449
450 status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit,
451 &fe_blkno);
452 if (status < 0) {
453 mlog_errno(status);
454 goto leave;
455 }
456
Mark Fashehccd979b2005-12-15 14:31:24 -0800457 /* populate as many fields early on as possible - many of
458 * these are used by the support functions here and in
459 * callers. */
460 inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
461 OCFS2_I(inode)->ip_blkno = fe_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -0800462 spin_lock(&osb->osb_lock);
463 inode->i_generation = osb->s_next_generation++;
464 spin_unlock(&osb->osb_lock);
465
466 *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
467 if (!*new_fe_bh) {
468 status = -EIO;
469 mlog_errno(status);
470 goto leave;
471 }
472 ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh);
473
474 status = ocfs2_journal_access(handle, inode, *new_fe_bh,
475 OCFS2_JOURNAL_ACCESS_CREATE);
476 if (status < 0) {
477 mlog_errno(status);
478 goto leave;
479 }
480
481 fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
482 memset(fe, 0, osb->sb->s_blocksize);
483
484 fe->i_generation = cpu_to_le32(inode->i_generation);
485 fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
486 fe->i_blkno = cpu_to_le64(fe_blkno);
487 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800488 fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot);
Tiger Yangf5d36202008-11-14 11:15:44 +0800489 fe->i_uid = cpu_to_le32(inode->i_uid);
490 fe->i_gid = cpu_to_le32(inode->i_gid);
491 fe->i_mode = cpu_to_le16(inode->i_mode);
492 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
Mark Fashehccd979b2005-12-15 14:31:24 -0800493 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
Mark Fashehccd979b2005-12-15 14:31:24 -0800494 fe->i_links_count = cpu_to_le16(inode->i_nlink);
495
496 fe->i_last_eb_blk = 0;
497 strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
498 le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
499 fe->i_atime = fe->i_ctime = fe->i_mtime =
500 cpu_to_le64(CURRENT_TIME.tv_sec);
501 fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
502 cpu_to_le32(CURRENT_TIME.tv_nsec);
503 fe->i_dtime = 0;
504
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700505 /*
506 * If supported, directories start with inline data.
507 */
Tiger Yangf5d36202008-11-14 11:15:44 +0800508 if (S_ISDIR(inode->i_mode) && ocfs2_supports_inline_data(osb)) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700509 u16 feat = le16_to_cpu(fe->i_dyn_features);
510
511 fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL);
512
513 fe->id2.i_data.id_count = cpu_to_le16(ocfs2_max_inline_data(osb->sb));
514 } else {
515 fel = &fe->id2.i_list;
516 fel->l_tree_depth = 0;
517 fel->l_next_free_rec = 0;
518 fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
519 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800520
521 status = ocfs2_journal_dirty(handle, *new_fe_bh);
522 if (status < 0) {
523 mlog_errno(status);
524 goto leave;
525 }
526
527 if (ocfs2_populate_inode(inode, fe, 1) < 0) {
528 mlog(ML_ERROR, "populate inode failed! bh->b_blocknr=%llu, "
Mark Fashehb0697052006-03-03 10:24:33 -0800529 "i_blkno=%llu, i_ino=%lu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700530 (unsigned long long)(*new_fe_bh)->b_blocknr,
531 (unsigned long long)le64_to_cpu(fe->i_blkno),
532 inode->i_ino);
Mark Fashehccd979b2005-12-15 14:31:24 -0800533 BUG();
534 }
535
536 ocfs2_inode_set_new(osb, inode);
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800537 if (!ocfs2_mount_local(osb)) {
538 status = ocfs2_create_new_inode_locks(inode);
539 if (status < 0)
540 mlog_errno(status);
541 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800542
543 status = 0; /* error in ocfs2_create_new_inode_locks is not
544 * critical */
545
Mark Fashehccd979b2005-12-15 14:31:24 -0800546leave:
547 if (status < 0) {
548 if (*new_fe_bh) {
549 brelse(*new_fe_bh);
550 *new_fe_bh = NULL;
551 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800552 }
553
554 mlog_exit(status);
555 return status;
556}
557
558static int ocfs2_mkdir(struct inode *dir,
559 struct dentry *dentry,
560 int mode)
561{
562 int ret;
563
564 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
565 dentry->d_name.len, dentry->d_name.name);
566 ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
567 mlog_exit(ret);
568
569 return ret;
570}
571
572static int ocfs2_create(struct inode *dir,
573 struct dentry *dentry,
574 int mode,
575 struct nameidata *nd)
576{
577 int ret;
578
579 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
580 dentry->d_name.len, dentry->d_name.name);
581 ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
582 mlog_exit(ret);
583
584 return ret;
585}
586
587static int ocfs2_link(struct dentry *old_dentry,
588 struct inode *dir,
589 struct dentry *dentry)
590{
Mark Fasheh1fabe142006-10-09 18:11:45 -0700591 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800592 struct inode *inode = old_dentry->d_inode;
593 int err;
594 struct buffer_head *fe_bh = NULL;
595 struct buffer_head *parent_fe_bh = NULL;
596 struct buffer_head *de_bh = NULL;
597 struct ocfs2_dinode *fe = NULL;
598 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
599
600 mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
601 old_dentry->d_name.len, old_dentry->d_name.name,
602 dentry->d_name.len, dentry->d_name.name);
603
Mark Fasheh123a9642006-10-05 16:48:23 -0700604 if (S_ISDIR(inode->i_mode))
605 return -EPERM;
Mark Fashehccd979b2005-12-15 14:31:24 -0800606
Mark Fashehe63aecb62007-10-18 15:30:42 -0700607 err = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800608 if (err < 0) {
609 if (err != -ENOENT)
610 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700611 return err;
Mark Fashehccd979b2005-12-15 14:31:24 -0800612 }
613
Tiger Yang0f62de22006-08-31 20:39:47 -0700614 if (!dir->i_nlink) {
615 err = -ENOENT;
Mark Fasheh123a9642006-10-05 16:48:23 -0700616 goto out;
Tiger Yang0f62de22006-08-31 20:39:47 -0700617 }
618
Mark Fashehccd979b2005-12-15 14:31:24 -0800619 err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
620 dentry->d_name.len);
621 if (err)
Mark Fasheh123a9642006-10-05 16:48:23 -0700622 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800623
624 err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
625 dentry->d_name.name,
626 dentry->d_name.len, &de_bh);
627 if (err < 0) {
628 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700629 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800630 }
631
Mark Fashehe63aecb62007-10-18 15:30:42 -0700632 err = ocfs2_inode_lock(inode, &fe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800633 if (err < 0) {
634 if (err != -ENOENT)
635 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700636 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800637 }
638
639 fe = (struct ocfs2_dinode *) fe_bh->b_data;
640 if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) {
641 err = -EMLINK;
Mark Fasheh123a9642006-10-05 16:48:23 -0700642 goto out_unlock_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800643 }
644
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700645 handle = ocfs2_start_trans(osb, OCFS2_LINK_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800646 if (IS_ERR(handle)) {
647 err = PTR_ERR(handle);
648 handle = NULL;
649 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700650 goto out_unlock_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800651 }
652
653 err = ocfs2_journal_access(handle, inode, fe_bh,
654 OCFS2_JOURNAL_ACCESS_WRITE);
655 if (err < 0) {
656 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700657 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800658 }
659
Dave Hansend8c76e62006-09-30 23:29:04 -0700660 inc_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800661 inode->i_ctime = CURRENT_TIME;
662 fe->i_links_count = cpu_to_le16(inode->i_nlink);
663 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
664 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
665
666 err = ocfs2_journal_dirty(handle, fe_bh);
667 if (err < 0) {
668 le16_add_cpu(&fe->i_links_count, -1);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700669 drop_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800670 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700671 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800672 }
673
674 err = ocfs2_add_entry(handle, dentry, inode,
675 OCFS2_I(inode)->ip_blkno,
676 parent_fe_bh, de_bh);
677 if (err) {
678 le16_add_cpu(&fe->i_links_count, -1);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700679 drop_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800680 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700681 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800682 }
683
Mark Fasheh0027dd52006-09-21 16:51:28 -0700684 err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
Mark Fasheh379dfe92006-09-08 14:21:03 -0700685 if (err) {
686 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700687 goto out_commit;
Mark Fasheh379dfe92006-09-08 14:21:03 -0700688 }
689
Mark Fashehccd979b2005-12-15 14:31:24 -0800690 atomic_inc(&inode->i_count);
691 dentry->d_op = &ocfs2_dentry_ops;
692 d_instantiate(dentry, inode);
Mark Fasheh123a9642006-10-05 16:48:23 -0700693
694out_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700695 ocfs2_commit_trans(osb, handle);
Mark Fasheh123a9642006-10-05 16:48:23 -0700696out_unlock_inode:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700697 ocfs2_inode_unlock(inode, 1);
Mark Fasheh123a9642006-10-05 16:48:23 -0700698
699out:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700700 ocfs2_inode_unlock(dir, 1);
Mark Fasheh123a9642006-10-05 16:48:23 -0700701
Mark Fasheha81cb882008-10-07 14:25:16 -0700702 brelse(de_bh);
703 brelse(fe_bh);
704 brelse(parent_fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800705
706 mlog_exit(err);
707
708 return err;
709}
710
Mark Fasheh379dfe92006-09-08 14:21:03 -0700711/*
712 * Takes and drops an exclusive lock on the given dentry. This will
713 * force other nodes to drop it.
714 */
715static int ocfs2_remote_dentry_delete(struct dentry *dentry)
716{
717 int ret;
718
719 ret = ocfs2_dentry_lock(dentry, 1);
720 if (ret)
721 mlog_errno(ret);
722 else
723 ocfs2_dentry_unlock(dentry, 1);
724
725 return ret;
726}
727
Mark Fasheh17ff7852006-09-30 23:29:05 -0700728static inline int inode_is_unlinkable(struct inode *inode)
729{
730 if (S_ISDIR(inode->i_mode)) {
731 if (inode->i_nlink == 2)
732 return 1;
733 return 0;
734 }
735
736 if (inode->i_nlink == 1)
737 return 1;
738 return 0;
739}
740
Mark Fashehccd979b2005-12-15 14:31:24 -0800741static int ocfs2_unlink(struct inode *dir,
742 struct dentry *dentry)
743{
744 int status;
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700745 int child_locked = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800746 struct inode *inode = dentry->d_inode;
Mark Fasheh5098c272006-10-05 18:12:57 -0700747 struct inode *orphan_dir = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800748 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
749 u64 blkno;
750 struct ocfs2_dinode *fe = NULL;
751 struct buffer_head *fe_bh = NULL;
752 struct buffer_head *parent_node_bh = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700753 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800754 struct ocfs2_dir_entry *dirent = NULL;
755 struct buffer_head *dirent_bh = NULL;
756 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
757 struct buffer_head *orphan_entry_bh = NULL;
758
759 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
760 dentry->d_name.len, dentry->d_name.name);
761
762 BUG_ON(dentry->d_parent->d_inode != dir);
763
Mark Fashehb0697052006-03-03 10:24:33 -0800764 mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800765
766 if (inode == osb->root_inode) {
767 mlog(0, "Cannot delete the root directory\n");
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700768 return -EPERM;
Mark Fashehccd979b2005-12-15 14:31:24 -0800769 }
770
Mark Fashehe63aecb62007-10-18 15:30:42 -0700771 status = ocfs2_inode_lock(dir, &parent_node_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800772 if (status < 0) {
773 if (status != -ENOENT)
774 mlog_errno(status);
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700775 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -0800776 }
777
778 status = ocfs2_find_files_on_disk(dentry->d_name.name,
779 dentry->d_name.len, &blkno,
780 dir, &dirent_bh, &dirent);
781 if (status < 0) {
782 if (status != -ENOENT)
783 mlog_errno(status);
784 goto leave;
785 }
786
787 if (OCFS2_I(inode)->ip_blkno != blkno) {
788 status = -ENOENT;
789
Mark Fashehb0697052006-03-03 10:24:33 -0800790 mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
791 (unsigned long long)OCFS2_I(inode)->ip_blkno,
792 (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800793 goto leave;
794 }
795
Mark Fashehe63aecb62007-10-18 15:30:42 -0700796 status = ocfs2_inode_lock(inode, &fe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800797 if (status < 0) {
798 if (status != -ENOENT)
799 mlog_errno(status);
800 goto leave;
801 }
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700802 child_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800803
804 if (S_ISDIR(inode->i_mode)) {
805 if (!ocfs2_empty_dir(inode)) {
806 status = -ENOTEMPTY;
807 goto leave;
808 } else if (inode->i_nlink != 2) {
809 status = -ENOTEMPTY;
810 goto leave;
811 }
812 }
813
Mark Fasheh379dfe92006-09-08 14:21:03 -0700814 status = ocfs2_remote_dentry_delete(dentry);
Mark Fashehccd979b2005-12-15 14:31:24 -0800815 if (status < 0) {
Mark Fasheh34d024f2007-09-24 15:56:19 -0700816 /* This remote delete should succeed under all normal
Mark Fashehccd979b2005-12-15 14:31:24 -0800817 * circumstances. */
818 mlog_errno(status);
819 goto leave;
820 }
821
Mark Fasheh17ff7852006-09-30 23:29:05 -0700822 if (inode_is_unlinkable(inode)) {
Mark Fasheh5098c272006-10-05 18:12:57 -0700823 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800824 orphan_name,
825 &orphan_entry_bh);
826 if (status < 0) {
827 mlog_errno(status);
828 goto leave;
829 }
830 }
831
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700832 handle = ocfs2_start_trans(osb, OCFS2_UNLINK_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800833 if (IS_ERR(handle)) {
834 status = PTR_ERR(handle);
835 handle = NULL;
836 mlog_errno(status);
837 goto leave;
838 }
839
840 status = ocfs2_journal_access(handle, inode, fe_bh,
841 OCFS2_JOURNAL_ACCESS_WRITE);
842 if (status < 0) {
843 mlog_errno(status);
844 goto leave;
845 }
846
847 fe = (struct ocfs2_dinode *) fe_bh->b_data;
848
Mark Fasheh17ff7852006-09-30 23:29:05 -0700849 if (inode_is_unlinkable(inode)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800850 status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
Mark Fasheh5098c272006-10-05 18:12:57 -0700851 orphan_entry_bh, orphan_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -0800852 if (status < 0) {
853 mlog_errno(status);
854 goto leave;
855 }
856 }
857
858 /* delete the name from the parent dir */
859 status = ocfs2_delete_entry(handle, dir, dirent, dirent_bh);
860 if (status < 0) {
861 mlog_errno(status);
862 goto leave;
863 }
864
Mark Fasheh17ff7852006-09-30 23:29:05 -0700865 if (S_ISDIR(inode->i_mode))
866 drop_nlink(inode);
867 drop_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800868 fe->i_links_count = cpu_to_le16(inode->i_nlink);
Mark Fashehccd979b2005-12-15 14:31:24 -0800869
870 status = ocfs2_journal_dirty(handle, fe_bh);
871 if (status < 0) {
872 mlog_errno(status);
873 goto leave;
874 }
875
Mark Fasheh592282c2007-01-02 17:59:40 -0800876 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
877 if (S_ISDIR(inode->i_mode))
Mark Fasheh17ff7852006-09-30 23:29:05 -0700878 drop_nlink(dir);
Mark Fasheh592282c2007-01-02 17:59:40 -0800879
880 status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
881 if (status < 0) {
882 mlog_errno(status);
883 if (S_ISDIR(inode->i_mode))
Dave Hansend8c76e62006-09-30 23:29:04 -0700884 inc_nlink(dir);
Mark Fashehccd979b2005-12-15 14:31:24 -0800885 }
886
887leave:
Mark Fashehccd979b2005-12-15 14:31:24 -0800888 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700889 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800890
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700891 if (child_locked)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700892 ocfs2_inode_unlock(inode, 1);
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700893
Mark Fashehe63aecb62007-10-18 15:30:42 -0700894 ocfs2_inode_unlock(dir, 1);
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700895
Mark Fasheh5098c272006-10-05 18:12:57 -0700896 if (orphan_dir) {
897 /* This was locked for us in ocfs2_prepare_orphan_dir() */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700898 ocfs2_inode_unlock(orphan_dir, 1);
Mark Fasheh5098c272006-10-05 18:12:57 -0700899 mutex_unlock(&orphan_dir->i_mutex);
900 iput(orphan_dir);
901 }
902
Mark Fasheha81cb882008-10-07 14:25:16 -0700903 brelse(fe_bh);
904 brelse(dirent_bh);
905 brelse(parent_node_bh);
906 brelse(orphan_entry_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800907
908 mlog_exit(status);
909
910 return status;
911}
912
913/*
914 * The only place this should be used is rename!
915 * if they have the same id, then the 1st one is the only one locked.
916 */
917static int ocfs2_double_lock(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800918 struct buffer_head **bh1,
919 struct inode *inode1,
920 struct buffer_head **bh2,
921 struct inode *inode2)
922{
923 int status;
924 struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
925 struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
926 struct buffer_head **tmpbh;
927 struct inode *tmpinode;
928
Mark Fashehb0697052006-03-03 10:24:33 -0800929 mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
930 (unsigned long long)oi1->ip_blkno,
931 (unsigned long long)oi2->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800932
Mark Fashehccd979b2005-12-15 14:31:24 -0800933 if (*bh1)
934 *bh1 = NULL;
935 if (*bh2)
936 *bh2 = NULL;
937
938 /* we always want to lock the one with the lower lockid first. */
939 if (oi1->ip_blkno != oi2->ip_blkno) {
940 if (oi1->ip_blkno < oi2->ip_blkno) {
941 /* switch id1 and id2 around */
942 mlog(0, "switching them around...\n");
943 tmpbh = bh2;
944 bh2 = bh1;
945 bh1 = tmpbh;
946
947 tmpinode = inode2;
948 inode2 = inode1;
949 inode1 = tmpinode;
950 }
951 /* lock id2 */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700952 status = ocfs2_inode_lock(inode2, bh2, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800953 if (status < 0) {
954 if (status != -ENOENT)
955 mlog_errno(status);
956 goto bail;
957 }
958 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700959
Mark Fashehccd979b2005-12-15 14:31:24 -0800960 /* lock id1 */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700961 status = ocfs2_inode_lock(inode1, bh1, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800962 if (status < 0) {
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700963 /*
964 * An error return must mean that no cluster locks
965 * were held on function exit.
966 */
967 if (oi1->ip_blkno != oi2->ip_blkno)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700968 ocfs2_inode_unlock(inode2, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700969
Mark Fashehccd979b2005-12-15 14:31:24 -0800970 if (status != -ENOENT)
971 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800972 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700973
Mark Fashehccd979b2005-12-15 14:31:24 -0800974bail:
975 mlog_exit(status);
976 return status;
977}
978
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700979static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
980{
Mark Fashehe63aecb62007-10-18 15:30:42 -0700981 ocfs2_inode_unlock(inode1, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700982
983 if (inode1 != inode2)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700984 ocfs2_inode_unlock(inode2, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700985}
986
Mark Fashehccd979b2005-12-15 14:31:24 -0800987static int ocfs2_rename(struct inode *old_dir,
988 struct dentry *old_dentry,
989 struct inode *new_dir,
990 struct dentry *new_dentry)
991{
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700992 int status = 0, rename_lock = 0, parents_locked = 0;
993 int old_child_locked = 0, new_child_locked = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800994 struct inode *old_inode = old_dentry->d_inode;
995 struct inode *new_inode = new_dentry->d_inode;
Mark Fasheh5098c272006-10-05 18:12:57 -0700996 struct inode *orphan_dir = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800997 struct ocfs2_dinode *newfe = NULL;
998 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
999 struct buffer_head *orphan_entry_bh = NULL;
1000 struct buffer_head *newfe_bh = NULL;
Mark Fasheh592282c2007-01-02 17:59:40 -08001001 struct buffer_head *old_inode_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001002 struct buffer_head *insert_entry_bh = NULL;
1003 struct ocfs2_super *osb = NULL;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001004 u64 newfe_blkno, old_de_ino;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001005 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001006 struct buffer_head *old_dir_bh = NULL;
1007 struct buffer_head *new_dir_bh = NULL;
Mark Fasheh38760e22007-09-11 17:21:56 -07001008 struct ocfs2_dir_entry *old_inode_dot_dot_de = NULL, *old_de = NULL,
1009 *new_de = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001010 struct buffer_head *new_de_bh = NULL, *old_de_bh = NULL; // bhs for above
1011 struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir,
1012 // this is the 1st dirent bh
Mark Fasheh592282c2007-01-02 17:59:40 -08001013 nlink_t old_dir_nlink = old_dir->i_nlink;
Sunil Mushran480214d2007-08-06 15:11:56 -07001014 struct ocfs2_dinode *old_di;
Mark Fashehccd979b2005-12-15 14:31:24 -08001015
1016 /* At some point it might be nice to break this function up a
1017 * bit. */
1018
1019 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
1020 old_dir, old_dentry, new_dir, new_dentry,
1021 old_dentry->d_name.len, old_dentry->d_name.name,
1022 new_dentry->d_name.len, new_dentry->d_name.name);
1023
1024 osb = OCFS2_SB(old_dir->i_sb);
1025
1026 if (new_inode) {
1027 if (!igrab(new_inode))
1028 BUG();
1029 }
1030
Uwe Kleine-König1b3c3712007-02-17 19:23:03 +01001031 /* Assume a directory hierarchy thusly:
Mark Fashehccd979b2005-12-15 14:31:24 -08001032 * a/b/c
1033 * a/d
1034 * a,b,c, and d are all directories.
1035 *
1036 * from cwd of 'a' on both nodes:
1037 * node1: mv b/c d
1038 * node2: mv d b/c
1039 *
1040 * And that's why, just like the VFS, we need a file system
1041 * rename lock. */
Jan Kara5dabd692008-02-21 18:00:00 +01001042 if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001043 status = ocfs2_rename_lock(osb);
1044 if (status < 0) {
1045 mlog_errno(status);
1046 goto bail;
1047 }
1048 rename_lock = 1;
1049 }
1050
Mark Fashehccd979b2005-12-15 14:31:24 -08001051 /* if old and new are the same, this'll just do one lock. */
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001052 status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
1053 &new_dir_bh, new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001054 if (status < 0) {
1055 mlog_errno(status);
1056 goto bail;
1057 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001058 parents_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001059
1060 /* make sure both dirs have bhs
1061 * get an extra ref on old_dir_bh if old==new */
1062 if (!new_dir_bh) {
1063 if (old_dir_bh) {
1064 new_dir_bh = old_dir_bh;
1065 get_bh(new_dir_bh);
1066 } else {
1067 mlog(ML_ERROR, "no old_dir_bh!\n");
1068 status = -EIO;
1069 goto bail;
1070 }
1071 }
1072
Mark Fasheh379dfe92006-09-08 14:21:03 -07001073 /*
Mark Fasheh592282c2007-01-02 17:59:40 -08001074 * Aside from allowing a meta data update, the locking here
Mark Fasheh34d024f2007-09-24 15:56:19 -07001075 * also ensures that the downconvert thread on other nodes
1076 * won't have to concurrently downconvert the inode and the
1077 * dentry locks.
Mark Fasheh379dfe92006-09-08 14:21:03 -07001078 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001079 status = ocfs2_inode_lock(old_inode, &old_inode_bh, 1);
Mark Fasheh379dfe92006-09-08 14:21:03 -07001080 if (status < 0) {
1081 if (status != -ENOENT)
Mark Fashehccd979b2005-12-15 14:31:24 -08001082 mlog_errno(status);
Mark Fasheh379dfe92006-09-08 14:21:03 -07001083 goto bail;
1084 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001085 old_child_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001086
Mark Fasheh379dfe92006-09-08 14:21:03 -07001087 status = ocfs2_remote_dentry_delete(old_dentry);
1088 if (status < 0) {
1089 mlog_errno(status);
1090 goto bail;
1091 }
1092
1093 if (S_ISDIR(old_inode->i_mode)) {
Mark Fasheh38760e22007-09-11 17:21:56 -07001094 u64 old_inode_parent;
Mark Fashehccd979b2005-12-15 14:31:24 -08001095
Mark Fasheh38760e22007-09-11 17:21:56 -07001096 status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent,
1097 old_inode, &old_inode_de_bh,
1098 &old_inode_dot_dot_de);
1099 if (status) {
1100 status = -EIO;
Mark Fashehccd979b2005-12-15 14:31:24 -08001101 goto bail;
Mark Fasheh38760e22007-09-11 17:21:56 -07001102 }
1103
1104 if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) {
1105 status = -EIO;
Mark Fashehccd979b2005-12-15 14:31:24 -08001106 goto bail;
Mark Fasheh38760e22007-09-11 17:21:56 -07001107 }
1108
1109 if (!new_inode && new_dir != old_dir &&
1110 new_dir->i_nlink >= OCFS2_LINK_MAX) {
1111 status = -EMLINK;
1112 goto bail;
1113 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001114 }
1115
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001116 status = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name,
1117 old_dentry->d_name.len,
1118 &old_de_ino);
1119 if (status) {
1120 status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -08001121 goto bail;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001122 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001123
1124 /*
1125 * Check for inode number is _not_ due to possible IO errors.
1126 * We might rmdir the source, keep it as pwd of some process
1127 * and merrily kill the link to whatever was created under the
1128 * same name. Goodbye sticky bit ;-<
1129 */
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001130 if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) {
1131 status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -08001132 goto bail;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001133 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001134
1135 /* check if the target already exists (in which case we need
1136 * to delete it */
1137 status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
1138 new_dentry->d_name.len,
1139 &newfe_blkno, new_dir, &new_de_bh,
1140 &new_de);
1141 /* The only error we allow here is -ENOENT because the new
1142 * file not existing is perfectly valid. */
1143 if ((status < 0) && (status != -ENOENT)) {
1144 /* If we cannot find the file specified we should just */
1145 /* return the error... */
1146 mlog_errno(status);
1147 goto bail;
1148 }
1149
Srinivas Eedae325a882007-10-31 16:49:43 -07001150 if (!new_de && new_inode) {
1151 /*
1152 * Target was unlinked by another node while we were
1153 * waiting to get to ocfs2_rename(). There isn't
1154 * anything we can do here to help the situation, so
1155 * bubble up the appropriate error.
1156 */
1157 status = -ENOENT;
1158 goto bail;
1159 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001160
1161 /* In case we need to overwrite an existing file, we blow it
1162 * away first */
1163 if (new_de) {
1164 /* VFS didn't think there existed an inode here, but
1165 * someone else in the cluster must have raced our
1166 * rename to create one. Today we error cleanly, in
1167 * the future we should consider calling iget to build
1168 * a new struct inode for this entry. */
1169 if (!new_inode) {
1170 status = -EACCES;
1171
1172 mlog(0, "We found an inode for name %.*s but VFS "
1173 "didn't give us one.\n", new_dentry->d_name.len,
1174 new_dentry->d_name.name);
1175 goto bail;
1176 }
1177
1178 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
1179 status = -EACCES;
1180
Mark Fashehb0697052006-03-03 10:24:33 -08001181 mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
1182 (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
1183 (unsigned long long)newfe_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001184 OCFS2_I(new_inode)->ip_flags);
1185 goto bail;
1186 }
1187
Mark Fashehe63aecb62007-10-18 15:30:42 -07001188 status = ocfs2_inode_lock(new_inode, &newfe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001189 if (status < 0) {
1190 if (status != -ENOENT)
1191 mlog_errno(status);
1192 goto bail;
1193 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001194 new_child_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001195
Mark Fasheh379dfe92006-09-08 14:21:03 -07001196 status = ocfs2_remote_dentry_delete(new_dentry);
Mark Fashehccd979b2005-12-15 14:31:24 -08001197 if (status < 0) {
1198 mlog_errno(status);
1199 goto bail;
1200 }
1201
1202 newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
1203
Mark Fashehb0697052006-03-03 10:24:33 -08001204 mlog(0, "aha rename over existing... new_de=%p new_blkno=%llu "
1205 "newfebh=%p bhblocknr=%llu\n", new_de,
1206 (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
Mark Fashehccd979b2005-12-15 14:31:24 -08001207 (unsigned long long)newfe_bh->b_blocknr : 0ULL);
1208
1209 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
Mark Fasheh5098c272006-10-05 18:12:57 -07001210 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
Mark Fashehccd979b2005-12-15 14:31:24 -08001211 new_inode,
1212 orphan_name,
1213 &orphan_entry_bh);
1214 if (status < 0) {
1215 mlog_errno(status);
1216 goto bail;
1217 }
1218 }
1219 } else {
1220 BUG_ON(new_dentry->d_parent->d_inode != new_dir);
1221
1222 status = ocfs2_check_dir_for_entry(new_dir,
1223 new_dentry->d_name.name,
1224 new_dentry->d_name.len);
1225 if (status)
1226 goto bail;
1227
1228 status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
1229 new_dentry->d_name.name,
1230 new_dentry->d_name.len,
1231 &insert_entry_bh);
1232 if (status < 0) {
1233 mlog_errno(status);
1234 goto bail;
1235 }
1236 }
1237
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001238 handle = ocfs2_start_trans(osb, OCFS2_RENAME_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001239 if (IS_ERR(handle)) {
1240 status = PTR_ERR(handle);
1241 handle = NULL;
1242 mlog_errno(status);
1243 goto bail;
1244 }
1245
1246 if (new_de) {
1247 if (S_ISDIR(new_inode->i_mode)) {
1248 if (!ocfs2_empty_dir(new_inode) ||
1249 new_inode->i_nlink != 2) {
1250 status = -ENOTEMPTY;
1251 goto bail;
1252 }
1253 }
1254 status = ocfs2_journal_access(handle, new_inode, newfe_bh,
1255 OCFS2_JOURNAL_ACCESS_WRITE);
1256 if (status < 0) {
1257 mlog_errno(status);
1258 goto bail;
1259 }
1260
1261 if (S_ISDIR(new_inode->i_mode) ||
1262 (newfe->i_links_count == cpu_to_le16(1))){
1263 status = ocfs2_orphan_add(osb, handle, new_inode,
1264 newfe, orphan_name,
Mark Fasheh5098c272006-10-05 18:12:57 -07001265 orphan_entry_bh, orphan_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001266 if (status < 0) {
1267 mlog_errno(status);
1268 goto bail;
1269 }
1270 }
1271
1272 /* change the dirent to point to the correct inode */
Mark Fasheh38760e22007-09-11 17:21:56 -07001273 status = ocfs2_update_entry(new_dir, handle, new_de_bh,
1274 new_de, old_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001275 if (status < 0) {
1276 mlog_errno(status);
1277 goto bail;
1278 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001279 new_dir->i_version++;
Mark Fashehccd979b2005-12-15 14:31:24 -08001280
1281 if (S_ISDIR(new_inode->i_mode))
1282 newfe->i_links_count = 0;
1283 else
1284 le16_add_cpu(&newfe->i_links_count, -1);
1285
1286 status = ocfs2_journal_dirty(handle, newfe_bh);
1287 if (status < 0) {
1288 mlog_errno(status);
1289 goto bail;
1290 }
1291 } else {
1292 /* if the name was not found in new_dir, add it now */
1293 status = ocfs2_add_entry(handle, new_dentry, old_inode,
1294 OCFS2_I(old_inode)->ip_blkno,
1295 new_dir_bh, insert_entry_bh);
1296 }
1297
1298 old_inode->i_ctime = CURRENT_TIME;
1299 mark_inode_dirty(old_inode);
Sunil Mushran480214d2007-08-06 15:11:56 -07001300
1301 status = ocfs2_journal_access(handle, old_inode, old_inode_bh,
1302 OCFS2_JOURNAL_ACCESS_WRITE);
1303 if (status >= 0) {
1304 old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
1305
1306 old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
1307 old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
1308
1309 status = ocfs2_journal_dirty(handle, old_inode_bh);
1310 if (status < 0)
1311 mlog_errno(status);
1312 } else
1313 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001314
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001315 /*
1316 * Now that the name has been added to new_dir, remove the old name.
1317 *
1318 * We don't keep any directory entry context around until now
1319 * because the insert might have changed the type of directory
1320 * we're dealing with.
1321 */
1322 old_de_bh = ocfs2_find_entry(old_dentry->d_name.name,
1323 old_dentry->d_name.len,
1324 old_dir, &old_de);
1325 if (!old_de_bh) {
1326 status = -EIO;
1327 goto bail;
1328 }
1329
Mark Fashehccd979b2005-12-15 14:31:24 -08001330 status = ocfs2_delete_entry(handle, old_dir, old_de, old_de_bh);
1331 if (status < 0) {
1332 mlog_errno(status);
1333 goto bail;
1334 }
1335
1336 if (new_inode) {
1337 new_inode->i_nlink--;
1338 new_inode->i_ctime = CURRENT_TIME;
1339 }
1340 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1341 if (old_inode_de_bh) {
Mark Fasheh38760e22007-09-11 17:21:56 -07001342 status = ocfs2_update_entry(old_inode, handle, old_inode_de_bh,
1343 old_inode_dot_dot_de, new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001344 old_dir->i_nlink--;
1345 if (new_inode) {
1346 new_inode->i_nlink--;
1347 } else {
Dave Hansend8c76e62006-09-30 23:29:04 -07001348 inc_nlink(new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001349 mark_inode_dirty(new_dir);
1350 }
1351 }
1352 mark_inode_dirty(old_dir);
Mark Fasheh592282c2007-01-02 17:59:40 -08001353 ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
1354 if (new_inode) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001355 mark_inode_dirty(new_inode);
Mark Fasheh592282c2007-01-02 17:59:40 -08001356 ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
1357 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001358
Mark Fasheh592282c2007-01-02 17:59:40 -08001359 if (old_dir != new_dir) {
1360 /* Keep the same times on both directories.*/
1361 new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
1362
1363 /*
1364 * This will also pick up the i_nlink change from the
1365 * block above.
1366 */
1367 ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
1368 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001369
1370 if (old_dir_nlink != old_dir->i_nlink) {
1371 if (!old_dir_bh) {
1372 mlog(ML_ERROR, "need to change nlink for old dir "
Mark Fashehb0697052006-03-03 10:24:33 -08001373 "%llu from %d to %d but bh is NULL!\n",
1374 (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
1375 (int)old_dir_nlink, old_dir->i_nlink);
Mark Fashehccd979b2005-12-15 14:31:24 -08001376 } else {
1377 struct ocfs2_dinode *fe;
1378 status = ocfs2_journal_access(handle, old_dir,
1379 old_dir_bh,
1380 OCFS2_JOURNAL_ACCESS_WRITE);
1381 fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
1382 fe->i_links_count = cpu_to_le16(old_dir->i_nlink);
1383 status = ocfs2_journal_dirty(handle, old_dir_bh);
1384 }
1385 }
1386
Mark Fasheh379dfe92006-09-08 14:21:03 -07001387 ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001388 status = 0;
1389bail:
1390 if (rename_lock)
1391 ocfs2_rename_unlock(osb);
1392
1393 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001394 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001395
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001396 if (parents_locked)
1397 ocfs2_double_unlock(old_dir, new_dir);
1398
1399 if (old_child_locked)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001400 ocfs2_inode_unlock(old_inode, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001401
1402 if (new_child_locked)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001403 ocfs2_inode_unlock(new_inode, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001404
Mark Fasheh5098c272006-10-05 18:12:57 -07001405 if (orphan_dir) {
1406 /* This was locked for us in ocfs2_prepare_orphan_dir() */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001407 ocfs2_inode_unlock(orphan_dir, 1);
Mark Fasheh5098c272006-10-05 18:12:57 -07001408 mutex_unlock(&orphan_dir->i_mutex);
1409 iput(orphan_dir);
1410 }
1411
Mark Fashehccd979b2005-12-15 14:31:24 -08001412 if (new_inode)
1413 sync_mapping_buffers(old_inode->i_mapping);
1414
1415 if (new_inode)
1416 iput(new_inode);
Mark Fasheha81cb882008-10-07 14:25:16 -07001417 brelse(newfe_bh);
1418 brelse(old_inode_bh);
1419 brelse(old_dir_bh);
1420 brelse(new_dir_bh);
1421 brelse(new_de_bh);
1422 brelse(old_de_bh);
1423 brelse(old_inode_de_bh);
1424 brelse(orphan_entry_bh);
1425 brelse(insert_entry_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001426
1427 mlog_exit(status);
1428
1429 return status;
1430}
1431
1432/*
1433 * we expect i_size = strlen(symname). Copy symname into the file
1434 * data, including the null terminator.
1435 */
1436static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001437 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001438 struct inode *inode,
1439 const char *symname)
1440{
1441 struct buffer_head **bhs = NULL;
1442 const char *c;
1443 struct super_block *sb = osb->sb;
Mark Fasheh4f902c32007-03-09 16:26:50 -08001444 u64 p_blkno, p_blocks;
Mark Fashehccd979b2005-12-15 14:31:24 -08001445 int virtual, blocks, status, i, bytes_left;
1446
1447 bytes_left = i_size_read(inode) + 1;
1448 /* we can't trust i_blocks because we're actually going to
1449 * write i_size + 1 bytes. */
1450 blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1451
Andrew Morton5515eff2006-03-26 01:37:53 -08001452 mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
1453 (unsigned long long)inode->i_blocks,
1454 i_size_read(inode), blocks);
Mark Fashehccd979b2005-12-15 14:31:24 -08001455
1456 /* Sanity check -- make sure we're going to fit. */
1457 if (bytes_left >
1458 ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1459 status = -EIO;
1460 mlog_errno(status);
1461 goto bail;
1462 }
1463
1464 bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
1465 if (!bhs) {
1466 status = -ENOMEM;
1467 mlog_errno(status);
1468 goto bail;
1469 }
1470
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001471 status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
1472 NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001473 if (status < 0) {
1474 mlog_errno(status);
1475 goto bail;
1476 }
1477
1478 /* links can never be larger than one cluster so we know this
1479 * is all going to be contiguous, but do a sanity check
1480 * anyway. */
1481 if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
1482 status = -EIO;
1483 mlog_errno(status);
1484 goto bail;
1485 }
1486
1487 virtual = 0;
1488 while(bytes_left > 0) {
1489 c = &symname[virtual * sb->s_blocksize];
1490
1491 bhs[virtual] = sb_getblk(sb, p_blkno);
1492 if (!bhs[virtual]) {
1493 status = -ENOMEM;
1494 mlog_errno(status);
1495 goto bail;
1496 }
1497 ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
1498
1499 status = ocfs2_journal_access(handle, inode, bhs[virtual],
1500 OCFS2_JOURNAL_ACCESS_CREATE);
1501 if (status < 0) {
1502 mlog_errno(status);
1503 goto bail;
1504 }
1505
1506 memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
1507
1508 memcpy(bhs[virtual]->b_data, c,
1509 (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
1510 bytes_left);
1511
1512 status = ocfs2_journal_dirty(handle, bhs[virtual]);
1513 if (status < 0) {
1514 mlog_errno(status);
1515 goto bail;
1516 }
1517
1518 virtual++;
1519 p_blkno++;
1520 bytes_left -= sb->s_blocksize;
1521 }
1522
1523 status = 0;
1524bail:
1525
1526 if (bhs) {
1527 for(i = 0; i < blocks; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001528 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001529 kfree(bhs);
1530 }
1531
1532 mlog_exit(status);
1533 return status;
1534}
1535
1536static int ocfs2_symlink(struct inode *dir,
1537 struct dentry *dentry,
1538 const char *symname)
1539{
1540 int status, l, credits;
1541 u64 newsize;
1542 struct ocfs2_super *osb = NULL;
1543 struct inode *inode = NULL;
1544 struct super_block *sb;
1545 struct buffer_head *new_fe_bh = NULL;
1546 struct buffer_head *de_bh = NULL;
1547 struct buffer_head *parent_fe_bh = NULL;
1548 struct ocfs2_dinode *fe = NULL;
1549 struct ocfs2_dinode *dirfe;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001550 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001551 struct ocfs2_alloc_context *inode_ac = NULL;
1552 struct ocfs2_alloc_context *data_ac = NULL;
Tiger Yang534eadd2008-11-14 11:16:41 +08001553 struct ocfs2_alloc_context *xattr_ac = NULL;
1554 int want_clusters = 0;
1555 int xattr_credits = 0;
1556 struct ocfs2_security_xattr_info si = {
1557 .enable = 1,
1558 };
Mark Fashehccd979b2005-12-15 14:31:24 -08001559
1560 mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
1561 dentry, symname, dentry->d_name.len, dentry->d_name.name);
1562
1563 sb = dir->i_sb;
1564 osb = OCFS2_SB(sb);
1565
1566 l = strlen(symname) + 1;
1567
1568 credits = ocfs2_calc_symlink_credits(sb);
1569
Mark Fashehccd979b2005-12-15 14:31:24 -08001570 /* lock the parent directory */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001571 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001572 if (status < 0) {
1573 if (status != -ENOENT)
1574 mlog_errno(status);
Mark Fasheh6d8fc402006-10-06 11:54:33 -07001575 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001576 }
1577
1578 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1579 if (!dirfe->i_links_count) {
1580 /* can't make a file in a deleted directory. */
1581 status = -ENOENT;
1582 goto bail;
1583 }
1584
1585 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
1586 dentry->d_name.len);
1587 if (status)
1588 goto bail;
1589
1590 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
1591 dentry->d_name.name,
1592 dentry->d_name.len, &de_bh);
1593 if (status < 0) {
1594 mlog_errno(status);
1595 goto bail;
1596 }
1597
Mark Fashehda5cbf22006-10-06 18:34:35 -07001598 status = ocfs2_reserve_new_inode(osb, &inode_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001599 if (status < 0) {
1600 if (status != -ENOSPC)
1601 mlog_errno(status);
1602 goto bail;
1603 }
1604
Tiger Yangf5d36202008-11-14 11:15:44 +08001605 inode = ocfs2_get_init_inode(dir, S_IFLNK | S_IRWXUGO);
1606 if (!inode) {
1607 status = -ENOMEM;
1608 mlog_errno(status);
1609 goto bail;
1610 }
1611
Tiger Yang534eadd2008-11-14 11:16:41 +08001612 /* get security xattr */
1613 status = ocfs2_init_security_get(inode, dir, &si);
1614 if (status) {
1615 if (status == -EOPNOTSUPP)
1616 si.enable = 0;
1617 else {
1618 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001619 goto bail;
1620 }
1621 }
1622
Tiger Yang534eadd2008-11-14 11:16:41 +08001623 /* calculate meta data/clusters for setting security xattr */
1624 if (si.enable) {
1625 status = ocfs2_calc_security_init(dir, &si, &want_clusters,
1626 &xattr_credits, &xattr_ac);
1627 if (status < 0) {
1628 mlog_errno(status);
1629 goto bail;
1630 }
1631 }
1632
1633 /* don't reserve bitmap space for fast symlinks. */
1634 if (l > ocfs2_fast_symlink_chars(sb))
1635 want_clusters += 1;
1636
1637 status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
1638 if (status < 0) {
1639 if (status != -ENOSPC)
1640 mlog_errno(status);
1641 goto bail;
1642 }
1643
1644 handle = ocfs2_start_trans(osb, credits + xattr_credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001645 if (IS_ERR(handle)) {
1646 status = PTR_ERR(handle);
1647 handle = NULL;
1648 mlog_errno(status);
1649 goto bail;
1650 }
1651
Tiger Yangf5d36202008-11-14 11:15:44 +08001652 status = ocfs2_mknod_locked(osb, dir, inode, dentry,
1653 0, &new_fe_bh, parent_fe_bh, handle,
1654 inode_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001655 if (status < 0) {
1656 mlog_errno(status);
1657 goto bail;
1658 }
1659
1660 fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
1661 inode->i_rdev = 0;
1662 newsize = l - 1;
1663 if (l > ocfs2_fast_symlink_chars(sb)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08001664 u32 offset = 0;
1665
Mark Fashehccd979b2005-12-15 14:31:24 -08001666 inode->i_op = &ocfs2_symlink_inode_operations;
Tao Ma0eb8d472008-08-18 17:38:45 +08001667 status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0,
1668 new_fe_bh,
1669 handle, data_ac, NULL,
1670 NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001671 if (status < 0) {
1672 if (status != -ENOSPC && status != -EINTR) {
Mark Fashehb0697052006-03-03 10:24:33 -08001673 mlog(ML_ERROR,
1674 "Failed to extend file to %llu\n",
1675 (unsigned long long)newsize);
Mark Fashehccd979b2005-12-15 14:31:24 -08001676 mlog_errno(status);
1677 status = -ENOSPC;
1678 }
1679 goto bail;
1680 }
1681 i_size_write(inode, newsize);
Mark Fasheh8110b072007-03-22 16:53:23 -07001682 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001683 } else {
1684 inode->i_op = &ocfs2_fast_symlink_inode_operations;
1685 memcpy((char *) fe->id2.i_symlink, symname, l);
1686 i_size_write(inode, newsize);
1687 inode->i_blocks = 0;
1688 }
1689
1690 status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
1691 if (status < 0) {
1692 mlog_errno(status);
1693 goto bail;
1694 }
1695
1696 if (!ocfs2_inode_is_fast_symlink(inode)) {
1697 status = ocfs2_create_symlink_data(osb, handle, inode,
1698 symname);
1699 if (status < 0) {
1700 mlog_errno(status);
1701 goto bail;
1702 }
1703 }
1704
Tiger Yang534eadd2008-11-14 11:16:41 +08001705 if (si.enable) {
1706 status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
1707 xattr_ac, data_ac);
1708 if (status < 0) {
1709 mlog_errno(status);
1710 goto bail;
1711 }
1712 }
1713
Mark Fashehccd979b2005-12-15 14:31:24 -08001714 status = ocfs2_add_entry(handle, dentry, inode,
1715 le64_to_cpu(fe->i_blkno), parent_fe_bh,
1716 de_bh);
1717 if (status < 0) {
1718 mlog_errno(status);
1719 goto bail;
1720 }
1721
Mark Fasheh0027dd52006-09-21 16:51:28 -07001722 status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
Mark Fasheh379dfe92006-09-08 14:21:03 -07001723 if (status) {
1724 mlog_errno(status);
1725 goto bail;
1726 }
1727
Mark Fashehccd979b2005-12-15 14:31:24 -08001728 insert_inode_hash(inode);
1729 dentry->d_op = &ocfs2_dentry_ops;
1730 d_instantiate(dentry, inode);
1731bail:
1732 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001733 ocfs2_commit_trans(osb, handle);
Mark Fasheh6d8fc402006-10-06 11:54:33 -07001734
Mark Fashehe63aecb62007-10-18 15:30:42 -07001735 ocfs2_inode_unlock(dir, 1);
Mark Fasheh6d8fc402006-10-06 11:54:33 -07001736
Mark Fasheha81cb882008-10-07 14:25:16 -07001737 brelse(new_fe_bh);
1738 brelse(parent_fe_bh);
1739 brelse(de_bh);
Tiger Yang534eadd2008-11-14 11:16:41 +08001740 kfree(si.name);
1741 kfree(si.value);
Mark Fashehccd979b2005-12-15 14:31:24 -08001742 if (inode_ac)
1743 ocfs2_free_alloc_context(inode_ac);
1744 if (data_ac)
1745 ocfs2_free_alloc_context(data_ac);
Tiger Yang534eadd2008-11-14 11:16:41 +08001746 if (xattr_ac)
1747 ocfs2_free_alloc_context(xattr_ac);
Tiger Yangf5d36202008-11-14 11:15:44 +08001748 if ((status < 0) && inode) {
1749 clear_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001750 iput(inode);
Tiger Yangf5d36202008-11-14 11:15:44 +08001751 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001752
1753 mlog_exit(status);
1754
1755 return status;
1756}
1757
Mark Fashehccd979b2005-12-15 14:31:24 -08001758static int ocfs2_blkno_stringify(u64 blkno, char *name)
1759{
1760 int status, namelen;
1761
1762 mlog_entry_void();
1763
Mark Fashehb0697052006-03-03 10:24:33 -08001764 namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
1765 (long long)blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001766 if (namelen <= 0) {
1767 if (namelen)
1768 status = namelen;
1769 else
1770 status = -EINVAL;
1771 mlog_errno(status);
1772 goto bail;
1773 }
1774 if (namelen != OCFS2_ORPHAN_NAMELEN) {
1775 status = -EINVAL;
1776 mlog_errno(status);
1777 goto bail;
1778 }
1779
1780 mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
1781 namelen);
1782
1783 status = 0;
1784bail:
1785 mlog_exit(status);
1786 return status;
1787}
1788
1789static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
Mark Fasheh5098c272006-10-05 18:12:57 -07001790 struct inode **ret_orphan_dir,
Mark Fashehccd979b2005-12-15 14:31:24 -08001791 struct inode *inode,
1792 char *name,
1793 struct buffer_head **de_bh)
1794{
Mark Fasheh5098c272006-10-05 18:12:57 -07001795 struct inode *orphan_dir_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -08001796 struct buffer_head *orphan_dir_bh = NULL;
1797 int status = 0;
1798
1799 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
1800 if (status < 0) {
1801 mlog_errno(status);
Mark Fasheh5098c272006-10-05 18:12:57 -07001802 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001803 }
1804
1805 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1806 ORPHAN_DIR_SYSTEM_INODE,
1807 osb->slot_num);
1808 if (!orphan_dir_inode) {
1809 status = -ENOENT;
1810 mlog_errno(status);
Mark Fasheh5098c272006-10-05 18:12:57 -07001811 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001812 }
1813
Mark Fasheh5098c272006-10-05 18:12:57 -07001814 mutex_lock(&orphan_dir_inode->i_mutex);
1815
Mark Fashehe63aecb62007-10-18 15:30:42 -07001816 status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001817 if (status < 0) {
1818 mlog_errno(status);
1819 goto leave;
1820 }
1821
1822 status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
1823 orphan_dir_bh, name,
1824 OCFS2_ORPHAN_NAMELEN, de_bh);
1825 if (status < 0) {
Mark Fashehe63aecb62007-10-18 15:30:42 -07001826 ocfs2_inode_unlock(orphan_dir_inode, 1);
Mark Fasheh5098c272006-10-05 18:12:57 -07001827
Mark Fashehccd979b2005-12-15 14:31:24 -08001828 mlog_errno(status);
1829 goto leave;
1830 }
1831
Mark Fasheh5098c272006-10-05 18:12:57 -07001832 *ret_orphan_dir = orphan_dir_inode;
1833
Mark Fashehccd979b2005-12-15 14:31:24 -08001834leave:
Mark Fasheh5098c272006-10-05 18:12:57 -07001835 if (status) {
1836 mutex_unlock(&orphan_dir_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001837 iput(orphan_dir_inode);
Mark Fasheh5098c272006-10-05 18:12:57 -07001838 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001839
Mark Fasheha81cb882008-10-07 14:25:16 -07001840 brelse(orphan_dir_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001841
1842 mlog_exit(status);
1843 return status;
1844}
1845
1846static int ocfs2_orphan_add(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001847 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001848 struct inode *inode,
1849 struct ocfs2_dinode *fe,
1850 char *name,
Mark Fasheh5098c272006-10-05 18:12:57 -07001851 struct buffer_head *de_bh,
1852 struct inode *orphan_dir_inode)
Mark Fashehccd979b2005-12-15 14:31:24 -08001853{
Mark Fashehccd979b2005-12-15 14:31:24 -08001854 struct buffer_head *orphan_dir_bh = NULL;
1855 int status = 0;
1856 struct ocfs2_dinode *orphan_fe;
1857
1858 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
1859
Joel Becker31d33072008-10-09 17:20:30 -07001860 status = ocfs2_read_block(orphan_dir_inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08001861 OCFS2_I(orphan_dir_inode)->ip_blkno,
Joel Becker0fcaa562008-10-09 17:20:31 -07001862 &orphan_dir_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001863 if (status < 0) {
1864 mlog_errno(status);
1865 goto leave;
1866 }
1867
1868 status = ocfs2_journal_access(handle, orphan_dir_inode, orphan_dir_bh,
1869 OCFS2_JOURNAL_ACCESS_WRITE);
1870 if (status < 0) {
1871 mlog_errno(status);
1872 goto leave;
1873 }
1874
1875 /* we're a cluster, and nlink can change on disk from
1876 * underneath us... */
1877 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
1878 if (S_ISDIR(inode->i_mode))
1879 le16_add_cpu(&orphan_fe->i_links_count, 1);
1880 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
1881
1882 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
1883 if (status < 0) {
1884 mlog_errno(status);
1885 goto leave;
1886 }
1887
1888 status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
1889 OCFS2_ORPHAN_NAMELEN, inode,
1890 OCFS2_I(inode)->ip_blkno,
1891 orphan_dir_bh, de_bh);
1892 if (status < 0) {
1893 mlog_errno(status);
1894 goto leave;
1895 }
1896
1897 le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
1898
1899 /* Record which orphan dir our inode now resides
1900 * in. delete_inode will use this to determine which orphan
1901 * dir to lock. */
Tiger Yang50008632007-03-20 16:01:38 -07001902 fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001903
Mark Fashehb0697052006-03-03 10:24:33 -08001904 mlog(0, "Inode %llu orphaned in slot %d\n",
1905 (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001906
1907leave:
Mark Fasheha81cb882008-10-07 14:25:16 -07001908 brelse(orphan_dir_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001909
1910 mlog_exit(status);
1911 return status;
1912}
1913
1914/* unlike orphan_add, we expect the orphan dir to already be locked here. */
1915int ocfs2_orphan_del(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001916 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001917 struct inode *orphan_dir_inode,
1918 struct inode *inode,
1919 struct buffer_head *orphan_dir_bh)
1920{
1921 char name[OCFS2_ORPHAN_NAMELEN + 1];
1922 struct ocfs2_dinode *orphan_fe;
1923 int status = 0;
1924 struct buffer_head *target_de_bh = NULL;
1925 struct ocfs2_dir_entry *target_de = NULL;
1926
1927 mlog_entry_void();
1928
1929 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
1930 if (status < 0) {
1931 mlog_errno(status);
1932 goto leave;
1933 }
1934
Mark Fashehb0697052006-03-03 10:24:33 -08001935 mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
1936 name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
1937 OCFS2_ORPHAN_NAMELEN);
Mark Fashehccd979b2005-12-15 14:31:24 -08001938
1939 /* find it's spot in the orphan directory */
1940 target_de_bh = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN,
1941 orphan_dir_inode, &target_de);
1942 if (!target_de_bh) {
1943 status = -ENOENT;
1944 mlog_errno(status);
1945 goto leave;
1946 }
1947
1948 /* remove it from the orphan directory */
1949 status = ocfs2_delete_entry(handle, orphan_dir_inode, target_de,
1950 target_de_bh);
1951 if (status < 0) {
1952 mlog_errno(status);
1953 goto leave;
1954 }
1955
1956 status = ocfs2_journal_access(handle,orphan_dir_inode, orphan_dir_bh,
1957 OCFS2_JOURNAL_ACCESS_WRITE);
1958 if (status < 0) {
1959 mlog_errno(status);
1960 goto leave;
1961 }
1962
1963 /* do the i_nlink dance! :) */
1964 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
1965 if (S_ISDIR(inode->i_mode))
1966 le16_add_cpu(&orphan_fe->i_links_count, -1);
1967 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
1968
1969 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
1970 if (status < 0) {
1971 mlog_errno(status);
1972 goto leave;
1973 }
1974
1975leave:
Mark Fasheha81cb882008-10-07 14:25:16 -07001976 brelse(target_de_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001977
1978 mlog_exit(status);
1979 return status;
1980}
1981
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001982const struct inode_operations ocfs2_dir_iops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08001983 .create = ocfs2_create,
1984 .lookup = ocfs2_lookup,
1985 .link = ocfs2_link,
1986 .unlink = ocfs2_unlink,
1987 .rmdir = ocfs2_unlink,
1988 .symlink = ocfs2_symlink,
1989 .mkdir = ocfs2_mkdir,
1990 .mknod = ocfs2_mknod,
1991 .rename = ocfs2_rename,
1992 .setattr = ocfs2_setattr,
1993 .getattr = ocfs2_getattr,
Tiger Yangd38eb8d2006-11-27 09:59:21 +08001994 .permission = ocfs2_permission,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001995 .setxattr = generic_setxattr,
1996 .getxattr = generic_getxattr,
1997 .listxattr = ocfs2_listxattr,
1998 .removexattr = generic_removexattr,
Mark Fashehccd979b2005-12-15 14:31:24 -08001999};