blob: e8ff0bae179d6d0bacd97218d7c00129a4424b2d [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;
232
233 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
234 (unsigned long)dev, dentry->d_name.len,
235 dentry->d_name.name);
236
237 /* get our super block */
238 osb = OCFS2_SB(dir->i_sb);
239
Mark Fashehe63aecb62007-10-18 15:30:42 -0700240 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
Mark Fashehe3a82132006-10-05 16:04:17 -0700241 if (status < 0) {
242 if (status != -ENOENT)
243 mlog_errno(status);
244 return status;
245 }
246
Mark Fasheha663e302006-08-09 11:45:07 -0700247 if (S_ISDIR(mode) && (dir->i_nlink >= OCFS2_LINK_MAX)) {
248 status = -EMLINK;
249 goto leave;
250 }
251
Mark Fashehccd979b2005-12-15 14:31:24 -0800252 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
253 if (!dirfe->i_links_count) {
254 /* can't make a file in a deleted directory. */
255 status = -ENOENT;
256 goto leave;
257 }
258
259 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
260 dentry->d_name.len);
261 if (status)
262 goto leave;
263
264 /* get a spot inside the dir. */
265 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
266 dentry->d_name.name,
267 dentry->d_name.len, &de_bh);
268 if (status < 0) {
269 mlog_errno(status);
270 goto leave;
271 }
272
273 /* reserve an inode spot */
Mark Fashehda5cbf22006-10-06 18:34:35 -0700274 status = ocfs2_reserve_new_inode(osb, &inode_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800275 if (status < 0) {
276 if (status != -ENOSPC)
277 mlog_errno(status);
278 goto leave;
279 }
280
Tiger Yangf5d36202008-11-14 11:15:44 +0800281 inode = ocfs2_get_init_inode(dir, mode);
282 if (!inode) {
283 status = -ENOMEM;
284 mlog_errno(status);
285 goto leave;
286 }
287
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700288 /* Reserve a cluster if creating an extent based directory. */
289 if (S_ISDIR(mode) && !ocfs2_supports_inline_data(osb)) {
Mark Fashehda5cbf22006-10-06 18:34:35 -0700290 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800291 if (status < 0) {
292 if (status != -ENOSPC)
293 mlog_errno(status);
294 goto leave;
295 }
296 }
297
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700298 handle = ocfs2_start_trans(osb, OCFS2_MKNOD_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800299 if (IS_ERR(handle)) {
300 status = PTR_ERR(handle);
301 handle = NULL;
302 mlog_errno(status);
303 goto leave;
304 }
305
306 /* do the real work now. */
Tiger Yangf5d36202008-11-14 11:15:44 +0800307 status = ocfs2_mknod_locked(osb, dir, inode, dentry, dev,
Mark Fashehccd979b2005-12-15 14:31:24 -0800308 &new_fe_bh, parent_fe_bh, handle,
Tiger Yangf5d36202008-11-14 11:15:44 +0800309 inode_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800310 if (status < 0) {
311 mlog_errno(status);
312 goto leave;
313 }
314
315 if (S_ISDIR(mode)) {
316 status = ocfs2_fill_new_dir(osb, handle, dir, inode,
317 new_fe_bh, data_ac);
318 if (status < 0) {
319 mlog_errno(status);
320 goto leave;
321 }
322
323 status = ocfs2_journal_access(handle, dir, parent_fe_bh,
324 OCFS2_JOURNAL_ACCESS_WRITE);
325 if (status < 0) {
326 mlog_errno(status);
327 goto leave;
328 }
329 le16_add_cpu(&dirfe->i_links_count, 1);
330 status = ocfs2_journal_dirty(handle, parent_fe_bh);
331 if (status < 0) {
332 mlog_errno(status);
333 goto leave;
334 }
Dave Hansend8c76e62006-09-30 23:29:04 -0700335 inc_nlink(dir);
Mark Fashehccd979b2005-12-15 14:31:24 -0800336 }
337
338 status = ocfs2_add_entry(handle, dentry, inode,
339 OCFS2_I(inode)->ip_blkno, parent_fe_bh,
340 de_bh);
341 if (status < 0) {
342 mlog_errno(status);
343 goto leave;
344 }
345
Mark Fasheh379dfe92006-09-08 14:21:03 -0700346 status = ocfs2_dentry_attach_lock(dentry, inode,
Mark Fasheh0027dd52006-09-21 16:51:28 -0700347 OCFS2_I(dir)->ip_blkno);
Mark Fasheh379dfe92006-09-08 14:21:03 -0700348 if (status) {
349 mlog_errno(status);
350 goto leave;
351 }
352
Mark Fashehccd979b2005-12-15 14:31:24 -0800353 insert_inode_hash(inode);
354 dentry->d_op = &ocfs2_dentry_ops;
355 d_instantiate(dentry, inode);
356 status = 0;
357leave:
358 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700359 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800360
Mark Fashehe63aecb62007-10-18 15:30:42 -0700361 ocfs2_inode_unlock(dir, 1);
Mark Fashehe3a82132006-10-05 16:04:17 -0700362
Mark Fashehccd979b2005-12-15 14:31:24 -0800363 if (status == -ENOSPC)
364 mlog(0, "Disk is full\n");
365
Mark Fasheha81cb882008-10-07 14:25:16 -0700366 brelse(new_fe_bh);
367 brelse(de_bh);
368 brelse(parent_fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800369
Tiger Yangf5d36202008-11-14 11:15:44 +0800370 if ((status < 0) && inode) {
371 clear_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800372 iput(inode);
Tiger Yangf5d36202008-11-14 11:15:44 +0800373 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800374
375 if (inode_ac)
376 ocfs2_free_alloc_context(inode_ac);
377
378 if (data_ac)
379 ocfs2_free_alloc_context(data_ac);
380
381 mlog_exit(status);
382
383 return status;
384}
385
386static int ocfs2_mknod_locked(struct ocfs2_super *osb,
387 struct inode *dir,
Tiger Yangf5d36202008-11-14 11:15:44 +0800388 struct inode *inode,
389 struct dentry *dentry,
Mark Fashehccd979b2005-12-15 14:31:24 -0800390 dev_t dev,
391 struct buffer_head **new_fe_bh,
392 struct buffer_head *parent_fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700393 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800394 struct ocfs2_alloc_context *inode_ac)
395{
396 int status = 0;
397 struct ocfs2_dinode *fe = NULL;
398 struct ocfs2_extent_list *fel;
399 u64 fe_blkno = 0;
400 u16 suballoc_bit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800401
Tiger Yangf5d36202008-11-14 11:15:44 +0800402 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry,
403 inode->i_mode, (unsigned long)dev, dentry->d_name.len,
Mark Fashehccd979b2005-12-15 14:31:24 -0800404 dentry->d_name.name);
405
406 *new_fe_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800407
408 status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit,
409 &fe_blkno);
410 if (status < 0) {
411 mlog_errno(status);
412 goto leave;
413 }
414
Mark Fashehccd979b2005-12-15 14:31:24 -0800415 /* populate as many fields early on as possible - many of
416 * these are used by the support functions here and in
417 * callers. */
418 inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
419 OCFS2_I(inode)->ip_blkno = fe_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -0800420 spin_lock(&osb->osb_lock);
421 inode->i_generation = osb->s_next_generation++;
422 spin_unlock(&osb->osb_lock);
423
424 *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
425 if (!*new_fe_bh) {
426 status = -EIO;
427 mlog_errno(status);
428 goto leave;
429 }
430 ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh);
431
432 status = ocfs2_journal_access(handle, inode, *new_fe_bh,
433 OCFS2_JOURNAL_ACCESS_CREATE);
434 if (status < 0) {
435 mlog_errno(status);
436 goto leave;
437 }
438
439 fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
440 memset(fe, 0, osb->sb->s_blocksize);
441
442 fe->i_generation = cpu_to_le32(inode->i_generation);
443 fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
444 fe->i_blkno = cpu_to_le64(fe_blkno);
445 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800446 fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot);
Tiger Yangf5d36202008-11-14 11:15:44 +0800447 fe->i_uid = cpu_to_le32(inode->i_uid);
448 fe->i_gid = cpu_to_le32(inode->i_gid);
449 fe->i_mode = cpu_to_le16(inode->i_mode);
450 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
Mark Fashehccd979b2005-12-15 14:31:24 -0800451 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
Mark Fashehccd979b2005-12-15 14:31:24 -0800452 fe->i_links_count = cpu_to_le16(inode->i_nlink);
453
454 fe->i_last_eb_blk = 0;
455 strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
456 le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
457 fe->i_atime = fe->i_ctime = fe->i_mtime =
458 cpu_to_le64(CURRENT_TIME.tv_sec);
459 fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
460 cpu_to_le32(CURRENT_TIME.tv_nsec);
461 fe->i_dtime = 0;
462
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700463 /*
464 * If supported, directories start with inline data.
465 */
Tiger Yangf5d36202008-11-14 11:15:44 +0800466 if (S_ISDIR(inode->i_mode) && ocfs2_supports_inline_data(osb)) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700467 u16 feat = le16_to_cpu(fe->i_dyn_features);
468
469 fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL);
470
471 fe->id2.i_data.id_count = cpu_to_le16(ocfs2_max_inline_data(osb->sb));
472 } else {
473 fel = &fe->id2.i_list;
474 fel->l_tree_depth = 0;
475 fel->l_next_free_rec = 0;
476 fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
477 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800478
479 status = ocfs2_journal_dirty(handle, *new_fe_bh);
480 if (status < 0) {
481 mlog_errno(status);
482 goto leave;
483 }
484
485 if (ocfs2_populate_inode(inode, fe, 1) < 0) {
486 mlog(ML_ERROR, "populate inode failed! bh->b_blocknr=%llu, "
Mark Fashehb0697052006-03-03 10:24:33 -0800487 "i_blkno=%llu, i_ino=%lu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700488 (unsigned long long)(*new_fe_bh)->b_blocknr,
489 (unsigned long long)le64_to_cpu(fe->i_blkno),
490 inode->i_ino);
Mark Fashehccd979b2005-12-15 14:31:24 -0800491 BUG();
492 }
493
494 ocfs2_inode_set_new(osb, inode);
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800495 if (!ocfs2_mount_local(osb)) {
496 status = ocfs2_create_new_inode_locks(inode);
497 if (status < 0)
498 mlog_errno(status);
499 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800500
501 status = 0; /* error in ocfs2_create_new_inode_locks is not
502 * critical */
503
Mark Fashehccd979b2005-12-15 14:31:24 -0800504leave:
505 if (status < 0) {
506 if (*new_fe_bh) {
507 brelse(*new_fe_bh);
508 *new_fe_bh = NULL;
509 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800510 }
511
512 mlog_exit(status);
513 return status;
514}
515
516static int ocfs2_mkdir(struct inode *dir,
517 struct dentry *dentry,
518 int mode)
519{
520 int ret;
521
522 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
523 dentry->d_name.len, dentry->d_name.name);
524 ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
525 mlog_exit(ret);
526
527 return ret;
528}
529
530static int ocfs2_create(struct inode *dir,
531 struct dentry *dentry,
532 int mode,
533 struct nameidata *nd)
534{
535 int ret;
536
537 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
538 dentry->d_name.len, dentry->d_name.name);
539 ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
540 mlog_exit(ret);
541
542 return ret;
543}
544
545static int ocfs2_link(struct dentry *old_dentry,
546 struct inode *dir,
547 struct dentry *dentry)
548{
Mark Fasheh1fabe142006-10-09 18:11:45 -0700549 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800550 struct inode *inode = old_dentry->d_inode;
551 int err;
552 struct buffer_head *fe_bh = NULL;
553 struct buffer_head *parent_fe_bh = NULL;
554 struct buffer_head *de_bh = NULL;
555 struct ocfs2_dinode *fe = NULL;
556 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
557
558 mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
559 old_dentry->d_name.len, old_dentry->d_name.name,
560 dentry->d_name.len, dentry->d_name.name);
561
Mark Fasheh123a9642006-10-05 16:48:23 -0700562 if (S_ISDIR(inode->i_mode))
563 return -EPERM;
Mark Fashehccd979b2005-12-15 14:31:24 -0800564
Mark Fashehe63aecb62007-10-18 15:30:42 -0700565 err = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800566 if (err < 0) {
567 if (err != -ENOENT)
568 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700569 return err;
Mark Fashehccd979b2005-12-15 14:31:24 -0800570 }
571
Tiger Yang0f62de22006-08-31 20:39:47 -0700572 if (!dir->i_nlink) {
573 err = -ENOENT;
Mark Fasheh123a9642006-10-05 16:48:23 -0700574 goto out;
Tiger Yang0f62de22006-08-31 20:39:47 -0700575 }
576
Mark Fashehccd979b2005-12-15 14:31:24 -0800577 err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
578 dentry->d_name.len);
579 if (err)
Mark Fasheh123a9642006-10-05 16:48:23 -0700580 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800581
582 err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
583 dentry->d_name.name,
584 dentry->d_name.len, &de_bh);
585 if (err < 0) {
586 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700587 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800588 }
589
Mark Fashehe63aecb62007-10-18 15:30:42 -0700590 err = ocfs2_inode_lock(inode, &fe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800591 if (err < 0) {
592 if (err != -ENOENT)
593 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700594 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800595 }
596
597 fe = (struct ocfs2_dinode *) fe_bh->b_data;
598 if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) {
599 err = -EMLINK;
Mark Fasheh123a9642006-10-05 16:48:23 -0700600 goto out_unlock_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800601 }
602
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700603 handle = ocfs2_start_trans(osb, OCFS2_LINK_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800604 if (IS_ERR(handle)) {
605 err = PTR_ERR(handle);
606 handle = NULL;
607 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700608 goto out_unlock_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800609 }
610
611 err = ocfs2_journal_access(handle, inode, fe_bh,
612 OCFS2_JOURNAL_ACCESS_WRITE);
613 if (err < 0) {
614 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700615 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800616 }
617
Dave Hansend8c76e62006-09-30 23:29:04 -0700618 inc_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800619 inode->i_ctime = CURRENT_TIME;
620 fe->i_links_count = cpu_to_le16(inode->i_nlink);
621 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
622 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
623
624 err = ocfs2_journal_dirty(handle, fe_bh);
625 if (err < 0) {
626 le16_add_cpu(&fe->i_links_count, -1);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700627 drop_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800628 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700629 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800630 }
631
632 err = ocfs2_add_entry(handle, dentry, inode,
633 OCFS2_I(inode)->ip_blkno,
634 parent_fe_bh, de_bh);
635 if (err) {
636 le16_add_cpu(&fe->i_links_count, -1);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700637 drop_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800638 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700639 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800640 }
641
Mark Fasheh0027dd52006-09-21 16:51:28 -0700642 err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
Mark Fasheh379dfe92006-09-08 14:21:03 -0700643 if (err) {
644 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700645 goto out_commit;
Mark Fasheh379dfe92006-09-08 14:21:03 -0700646 }
647
Mark Fashehccd979b2005-12-15 14:31:24 -0800648 atomic_inc(&inode->i_count);
649 dentry->d_op = &ocfs2_dentry_ops;
650 d_instantiate(dentry, inode);
Mark Fasheh123a9642006-10-05 16:48:23 -0700651
652out_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700653 ocfs2_commit_trans(osb, handle);
Mark Fasheh123a9642006-10-05 16:48:23 -0700654out_unlock_inode:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700655 ocfs2_inode_unlock(inode, 1);
Mark Fasheh123a9642006-10-05 16:48:23 -0700656
657out:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700658 ocfs2_inode_unlock(dir, 1);
Mark Fasheh123a9642006-10-05 16:48:23 -0700659
Mark Fasheha81cb882008-10-07 14:25:16 -0700660 brelse(de_bh);
661 brelse(fe_bh);
662 brelse(parent_fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800663
664 mlog_exit(err);
665
666 return err;
667}
668
Mark Fasheh379dfe92006-09-08 14:21:03 -0700669/*
670 * Takes and drops an exclusive lock on the given dentry. This will
671 * force other nodes to drop it.
672 */
673static int ocfs2_remote_dentry_delete(struct dentry *dentry)
674{
675 int ret;
676
677 ret = ocfs2_dentry_lock(dentry, 1);
678 if (ret)
679 mlog_errno(ret);
680 else
681 ocfs2_dentry_unlock(dentry, 1);
682
683 return ret;
684}
685
Mark Fasheh17ff7852006-09-30 23:29:05 -0700686static inline int inode_is_unlinkable(struct inode *inode)
687{
688 if (S_ISDIR(inode->i_mode)) {
689 if (inode->i_nlink == 2)
690 return 1;
691 return 0;
692 }
693
694 if (inode->i_nlink == 1)
695 return 1;
696 return 0;
697}
698
Mark Fashehccd979b2005-12-15 14:31:24 -0800699static int ocfs2_unlink(struct inode *dir,
700 struct dentry *dentry)
701{
702 int status;
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700703 int child_locked = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800704 struct inode *inode = dentry->d_inode;
Mark Fasheh5098c272006-10-05 18:12:57 -0700705 struct inode *orphan_dir = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800706 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
707 u64 blkno;
708 struct ocfs2_dinode *fe = NULL;
709 struct buffer_head *fe_bh = NULL;
710 struct buffer_head *parent_node_bh = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700711 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800712 struct ocfs2_dir_entry *dirent = NULL;
713 struct buffer_head *dirent_bh = NULL;
714 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
715 struct buffer_head *orphan_entry_bh = NULL;
716
717 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
718 dentry->d_name.len, dentry->d_name.name);
719
720 BUG_ON(dentry->d_parent->d_inode != dir);
721
Mark Fashehb0697052006-03-03 10:24:33 -0800722 mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800723
724 if (inode == osb->root_inode) {
725 mlog(0, "Cannot delete the root directory\n");
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700726 return -EPERM;
Mark Fashehccd979b2005-12-15 14:31:24 -0800727 }
728
Mark Fashehe63aecb62007-10-18 15:30:42 -0700729 status = ocfs2_inode_lock(dir, &parent_node_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800730 if (status < 0) {
731 if (status != -ENOENT)
732 mlog_errno(status);
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700733 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -0800734 }
735
736 status = ocfs2_find_files_on_disk(dentry->d_name.name,
737 dentry->d_name.len, &blkno,
738 dir, &dirent_bh, &dirent);
739 if (status < 0) {
740 if (status != -ENOENT)
741 mlog_errno(status);
742 goto leave;
743 }
744
745 if (OCFS2_I(inode)->ip_blkno != blkno) {
746 status = -ENOENT;
747
Mark Fashehb0697052006-03-03 10:24:33 -0800748 mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
749 (unsigned long long)OCFS2_I(inode)->ip_blkno,
750 (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800751 goto leave;
752 }
753
Mark Fashehe63aecb62007-10-18 15:30:42 -0700754 status = ocfs2_inode_lock(inode, &fe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800755 if (status < 0) {
756 if (status != -ENOENT)
757 mlog_errno(status);
758 goto leave;
759 }
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700760 child_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800761
762 if (S_ISDIR(inode->i_mode)) {
763 if (!ocfs2_empty_dir(inode)) {
764 status = -ENOTEMPTY;
765 goto leave;
766 } else if (inode->i_nlink != 2) {
767 status = -ENOTEMPTY;
768 goto leave;
769 }
770 }
771
Mark Fasheh379dfe92006-09-08 14:21:03 -0700772 status = ocfs2_remote_dentry_delete(dentry);
Mark Fashehccd979b2005-12-15 14:31:24 -0800773 if (status < 0) {
Mark Fasheh34d024f2007-09-24 15:56:19 -0700774 /* This remote delete should succeed under all normal
Mark Fashehccd979b2005-12-15 14:31:24 -0800775 * circumstances. */
776 mlog_errno(status);
777 goto leave;
778 }
779
Mark Fasheh17ff7852006-09-30 23:29:05 -0700780 if (inode_is_unlinkable(inode)) {
Mark Fasheh5098c272006-10-05 18:12:57 -0700781 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800782 orphan_name,
783 &orphan_entry_bh);
784 if (status < 0) {
785 mlog_errno(status);
786 goto leave;
787 }
788 }
789
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700790 handle = ocfs2_start_trans(osb, OCFS2_UNLINK_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800791 if (IS_ERR(handle)) {
792 status = PTR_ERR(handle);
793 handle = NULL;
794 mlog_errno(status);
795 goto leave;
796 }
797
798 status = ocfs2_journal_access(handle, inode, fe_bh,
799 OCFS2_JOURNAL_ACCESS_WRITE);
800 if (status < 0) {
801 mlog_errno(status);
802 goto leave;
803 }
804
805 fe = (struct ocfs2_dinode *) fe_bh->b_data;
806
Mark Fasheh17ff7852006-09-30 23:29:05 -0700807 if (inode_is_unlinkable(inode)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800808 status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
Mark Fasheh5098c272006-10-05 18:12:57 -0700809 orphan_entry_bh, orphan_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -0800810 if (status < 0) {
811 mlog_errno(status);
812 goto leave;
813 }
814 }
815
816 /* delete the name from the parent dir */
817 status = ocfs2_delete_entry(handle, dir, dirent, dirent_bh);
818 if (status < 0) {
819 mlog_errno(status);
820 goto leave;
821 }
822
Mark Fasheh17ff7852006-09-30 23:29:05 -0700823 if (S_ISDIR(inode->i_mode))
824 drop_nlink(inode);
825 drop_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800826 fe->i_links_count = cpu_to_le16(inode->i_nlink);
Mark Fashehccd979b2005-12-15 14:31:24 -0800827
828 status = ocfs2_journal_dirty(handle, fe_bh);
829 if (status < 0) {
830 mlog_errno(status);
831 goto leave;
832 }
833
Mark Fasheh592282c2007-01-02 17:59:40 -0800834 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
835 if (S_ISDIR(inode->i_mode))
Mark Fasheh17ff7852006-09-30 23:29:05 -0700836 drop_nlink(dir);
Mark Fasheh592282c2007-01-02 17:59:40 -0800837
838 status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
839 if (status < 0) {
840 mlog_errno(status);
841 if (S_ISDIR(inode->i_mode))
Dave Hansend8c76e62006-09-30 23:29:04 -0700842 inc_nlink(dir);
Mark Fashehccd979b2005-12-15 14:31:24 -0800843 }
844
845leave:
Mark Fashehccd979b2005-12-15 14:31:24 -0800846 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700847 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800848
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700849 if (child_locked)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700850 ocfs2_inode_unlock(inode, 1);
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700851
Mark Fashehe63aecb62007-10-18 15:30:42 -0700852 ocfs2_inode_unlock(dir, 1);
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700853
Mark Fasheh5098c272006-10-05 18:12:57 -0700854 if (orphan_dir) {
855 /* This was locked for us in ocfs2_prepare_orphan_dir() */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700856 ocfs2_inode_unlock(orphan_dir, 1);
Mark Fasheh5098c272006-10-05 18:12:57 -0700857 mutex_unlock(&orphan_dir->i_mutex);
858 iput(orphan_dir);
859 }
860
Mark Fasheha81cb882008-10-07 14:25:16 -0700861 brelse(fe_bh);
862 brelse(dirent_bh);
863 brelse(parent_node_bh);
864 brelse(orphan_entry_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800865
866 mlog_exit(status);
867
868 return status;
869}
870
871/*
872 * The only place this should be used is rename!
873 * if they have the same id, then the 1st one is the only one locked.
874 */
875static int ocfs2_double_lock(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800876 struct buffer_head **bh1,
877 struct inode *inode1,
878 struct buffer_head **bh2,
879 struct inode *inode2)
880{
881 int status;
882 struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
883 struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
884 struct buffer_head **tmpbh;
885 struct inode *tmpinode;
886
Mark Fashehb0697052006-03-03 10:24:33 -0800887 mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
888 (unsigned long long)oi1->ip_blkno,
889 (unsigned long long)oi2->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800890
Mark Fashehccd979b2005-12-15 14:31:24 -0800891 if (*bh1)
892 *bh1 = NULL;
893 if (*bh2)
894 *bh2 = NULL;
895
896 /* we always want to lock the one with the lower lockid first. */
897 if (oi1->ip_blkno != oi2->ip_blkno) {
898 if (oi1->ip_blkno < oi2->ip_blkno) {
899 /* switch id1 and id2 around */
900 mlog(0, "switching them around...\n");
901 tmpbh = bh2;
902 bh2 = bh1;
903 bh1 = tmpbh;
904
905 tmpinode = inode2;
906 inode2 = inode1;
907 inode1 = tmpinode;
908 }
909 /* lock id2 */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700910 status = ocfs2_inode_lock(inode2, bh2, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800911 if (status < 0) {
912 if (status != -ENOENT)
913 mlog_errno(status);
914 goto bail;
915 }
916 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700917
Mark Fashehccd979b2005-12-15 14:31:24 -0800918 /* lock id1 */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700919 status = ocfs2_inode_lock(inode1, bh1, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800920 if (status < 0) {
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700921 /*
922 * An error return must mean that no cluster locks
923 * were held on function exit.
924 */
925 if (oi1->ip_blkno != oi2->ip_blkno)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700926 ocfs2_inode_unlock(inode2, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700927
Mark Fashehccd979b2005-12-15 14:31:24 -0800928 if (status != -ENOENT)
929 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800930 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700931
Mark Fashehccd979b2005-12-15 14:31:24 -0800932bail:
933 mlog_exit(status);
934 return status;
935}
936
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700937static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
938{
Mark Fashehe63aecb62007-10-18 15:30:42 -0700939 ocfs2_inode_unlock(inode1, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700940
941 if (inode1 != inode2)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700942 ocfs2_inode_unlock(inode2, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700943}
944
Mark Fashehccd979b2005-12-15 14:31:24 -0800945static int ocfs2_rename(struct inode *old_dir,
946 struct dentry *old_dentry,
947 struct inode *new_dir,
948 struct dentry *new_dentry)
949{
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700950 int status = 0, rename_lock = 0, parents_locked = 0;
951 int old_child_locked = 0, new_child_locked = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800952 struct inode *old_inode = old_dentry->d_inode;
953 struct inode *new_inode = new_dentry->d_inode;
Mark Fasheh5098c272006-10-05 18:12:57 -0700954 struct inode *orphan_dir = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800955 struct ocfs2_dinode *newfe = NULL;
956 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
957 struct buffer_head *orphan_entry_bh = NULL;
958 struct buffer_head *newfe_bh = NULL;
Mark Fasheh592282c2007-01-02 17:59:40 -0800959 struct buffer_head *old_inode_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800960 struct buffer_head *insert_entry_bh = NULL;
961 struct ocfs2_super *osb = NULL;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700962 u64 newfe_blkno, old_de_ino;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700963 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800964 struct buffer_head *old_dir_bh = NULL;
965 struct buffer_head *new_dir_bh = NULL;
Mark Fasheh38760e22007-09-11 17:21:56 -0700966 struct ocfs2_dir_entry *old_inode_dot_dot_de = NULL, *old_de = NULL,
967 *new_de = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800968 struct buffer_head *new_de_bh = NULL, *old_de_bh = NULL; // bhs for above
969 struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir,
970 // this is the 1st dirent bh
Mark Fasheh592282c2007-01-02 17:59:40 -0800971 nlink_t old_dir_nlink = old_dir->i_nlink;
Sunil Mushran480214d2007-08-06 15:11:56 -0700972 struct ocfs2_dinode *old_di;
Mark Fashehccd979b2005-12-15 14:31:24 -0800973
974 /* At some point it might be nice to break this function up a
975 * bit. */
976
977 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
978 old_dir, old_dentry, new_dir, new_dentry,
979 old_dentry->d_name.len, old_dentry->d_name.name,
980 new_dentry->d_name.len, new_dentry->d_name.name);
981
982 osb = OCFS2_SB(old_dir->i_sb);
983
984 if (new_inode) {
985 if (!igrab(new_inode))
986 BUG();
987 }
988
Uwe Kleine-König1b3c3712007-02-17 19:23:03 +0100989 /* Assume a directory hierarchy thusly:
Mark Fashehccd979b2005-12-15 14:31:24 -0800990 * a/b/c
991 * a/d
992 * a,b,c, and d are all directories.
993 *
994 * from cwd of 'a' on both nodes:
995 * node1: mv b/c d
996 * node2: mv d b/c
997 *
998 * And that's why, just like the VFS, we need a file system
999 * rename lock. */
Jan Kara5dabd692008-02-21 18:00:00 +01001000 if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001001 status = ocfs2_rename_lock(osb);
1002 if (status < 0) {
1003 mlog_errno(status);
1004 goto bail;
1005 }
1006 rename_lock = 1;
1007 }
1008
Mark Fashehccd979b2005-12-15 14:31:24 -08001009 /* if old and new are the same, this'll just do one lock. */
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001010 status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
1011 &new_dir_bh, new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001012 if (status < 0) {
1013 mlog_errno(status);
1014 goto bail;
1015 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001016 parents_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001017
1018 /* make sure both dirs have bhs
1019 * get an extra ref on old_dir_bh if old==new */
1020 if (!new_dir_bh) {
1021 if (old_dir_bh) {
1022 new_dir_bh = old_dir_bh;
1023 get_bh(new_dir_bh);
1024 } else {
1025 mlog(ML_ERROR, "no old_dir_bh!\n");
1026 status = -EIO;
1027 goto bail;
1028 }
1029 }
1030
Mark Fasheh379dfe92006-09-08 14:21:03 -07001031 /*
Mark Fasheh592282c2007-01-02 17:59:40 -08001032 * Aside from allowing a meta data update, the locking here
Mark Fasheh34d024f2007-09-24 15:56:19 -07001033 * also ensures that the downconvert thread on other nodes
1034 * won't have to concurrently downconvert the inode and the
1035 * dentry locks.
Mark Fasheh379dfe92006-09-08 14:21:03 -07001036 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001037 status = ocfs2_inode_lock(old_inode, &old_inode_bh, 1);
Mark Fasheh379dfe92006-09-08 14:21:03 -07001038 if (status < 0) {
1039 if (status != -ENOENT)
Mark Fashehccd979b2005-12-15 14:31:24 -08001040 mlog_errno(status);
Mark Fasheh379dfe92006-09-08 14:21:03 -07001041 goto bail;
1042 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001043 old_child_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001044
Mark Fasheh379dfe92006-09-08 14:21:03 -07001045 status = ocfs2_remote_dentry_delete(old_dentry);
1046 if (status < 0) {
1047 mlog_errno(status);
1048 goto bail;
1049 }
1050
1051 if (S_ISDIR(old_inode->i_mode)) {
Mark Fasheh38760e22007-09-11 17:21:56 -07001052 u64 old_inode_parent;
Mark Fashehccd979b2005-12-15 14:31:24 -08001053
Mark Fasheh38760e22007-09-11 17:21:56 -07001054 status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent,
1055 old_inode, &old_inode_de_bh,
1056 &old_inode_dot_dot_de);
1057 if (status) {
1058 status = -EIO;
Mark Fashehccd979b2005-12-15 14:31:24 -08001059 goto bail;
Mark Fasheh38760e22007-09-11 17:21:56 -07001060 }
1061
1062 if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) {
1063 status = -EIO;
Mark Fashehccd979b2005-12-15 14:31:24 -08001064 goto bail;
Mark Fasheh38760e22007-09-11 17:21:56 -07001065 }
1066
1067 if (!new_inode && new_dir != old_dir &&
1068 new_dir->i_nlink >= OCFS2_LINK_MAX) {
1069 status = -EMLINK;
1070 goto bail;
1071 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001072 }
1073
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001074 status = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name,
1075 old_dentry->d_name.len,
1076 &old_de_ino);
1077 if (status) {
1078 status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -08001079 goto bail;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001080 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001081
1082 /*
1083 * Check for inode number is _not_ due to possible IO errors.
1084 * We might rmdir the source, keep it as pwd of some process
1085 * and merrily kill the link to whatever was created under the
1086 * same name. Goodbye sticky bit ;-<
1087 */
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001088 if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) {
1089 status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -08001090 goto bail;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001091 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001092
1093 /* check if the target already exists (in which case we need
1094 * to delete it */
1095 status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
1096 new_dentry->d_name.len,
1097 &newfe_blkno, new_dir, &new_de_bh,
1098 &new_de);
1099 /* The only error we allow here is -ENOENT because the new
1100 * file not existing is perfectly valid. */
1101 if ((status < 0) && (status != -ENOENT)) {
1102 /* If we cannot find the file specified we should just */
1103 /* return the error... */
1104 mlog_errno(status);
1105 goto bail;
1106 }
1107
Srinivas Eedae325a882007-10-31 16:49:43 -07001108 if (!new_de && new_inode) {
1109 /*
1110 * Target was unlinked by another node while we were
1111 * waiting to get to ocfs2_rename(). There isn't
1112 * anything we can do here to help the situation, so
1113 * bubble up the appropriate error.
1114 */
1115 status = -ENOENT;
1116 goto bail;
1117 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001118
1119 /* In case we need to overwrite an existing file, we blow it
1120 * away first */
1121 if (new_de) {
1122 /* VFS didn't think there existed an inode here, but
1123 * someone else in the cluster must have raced our
1124 * rename to create one. Today we error cleanly, in
1125 * the future we should consider calling iget to build
1126 * a new struct inode for this entry. */
1127 if (!new_inode) {
1128 status = -EACCES;
1129
1130 mlog(0, "We found an inode for name %.*s but VFS "
1131 "didn't give us one.\n", new_dentry->d_name.len,
1132 new_dentry->d_name.name);
1133 goto bail;
1134 }
1135
1136 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
1137 status = -EACCES;
1138
Mark Fashehb0697052006-03-03 10:24:33 -08001139 mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
1140 (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
1141 (unsigned long long)newfe_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001142 OCFS2_I(new_inode)->ip_flags);
1143 goto bail;
1144 }
1145
Mark Fashehe63aecb62007-10-18 15:30:42 -07001146 status = ocfs2_inode_lock(new_inode, &newfe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001147 if (status < 0) {
1148 if (status != -ENOENT)
1149 mlog_errno(status);
1150 goto bail;
1151 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001152 new_child_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001153
Mark Fasheh379dfe92006-09-08 14:21:03 -07001154 status = ocfs2_remote_dentry_delete(new_dentry);
Mark Fashehccd979b2005-12-15 14:31:24 -08001155 if (status < 0) {
1156 mlog_errno(status);
1157 goto bail;
1158 }
1159
1160 newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
1161
Mark Fashehb0697052006-03-03 10:24:33 -08001162 mlog(0, "aha rename over existing... new_de=%p new_blkno=%llu "
1163 "newfebh=%p bhblocknr=%llu\n", new_de,
1164 (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
Mark Fashehccd979b2005-12-15 14:31:24 -08001165 (unsigned long long)newfe_bh->b_blocknr : 0ULL);
1166
1167 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
Mark Fasheh5098c272006-10-05 18:12:57 -07001168 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
Mark Fashehccd979b2005-12-15 14:31:24 -08001169 new_inode,
1170 orphan_name,
1171 &orphan_entry_bh);
1172 if (status < 0) {
1173 mlog_errno(status);
1174 goto bail;
1175 }
1176 }
1177 } else {
1178 BUG_ON(new_dentry->d_parent->d_inode != new_dir);
1179
1180 status = ocfs2_check_dir_for_entry(new_dir,
1181 new_dentry->d_name.name,
1182 new_dentry->d_name.len);
1183 if (status)
1184 goto bail;
1185
1186 status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
1187 new_dentry->d_name.name,
1188 new_dentry->d_name.len,
1189 &insert_entry_bh);
1190 if (status < 0) {
1191 mlog_errno(status);
1192 goto bail;
1193 }
1194 }
1195
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001196 handle = ocfs2_start_trans(osb, OCFS2_RENAME_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001197 if (IS_ERR(handle)) {
1198 status = PTR_ERR(handle);
1199 handle = NULL;
1200 mlog_errno(status);
1201 goto bail;
1202 }
1203
1204 if (new_de) {
1205 if (S_ISDIR(new_inode->i_mode)) {
1206 if (!ocfs2_empty_dir(new_inode) ||
1207 new_inode->i_nlink != 2) {
1208 status = -ENOTEMPTY;
1209 goto bail;
1210 }
1211 }
1212 status = ocfs2_journal_access(handle, new_inode, newfe_bh,
1213 OCFS2_JOURNAL_ACCESS_WRITE);
1214 if (status < 0) {
1215 mlog_errno(status);
1216 goto bail;
1217 }
1218
1219 if (S_ISDIR(new_inode->i_mode) ||
1220 (newfe->i_links_count == cpu_to_le16(1))){
1221 status = ocfs2_orphan_add(osb, handle, new_inode,
1222 newfe, orphan_name,
Mark Fasheh5098c272006-10-05 18:12:57 -07001223 orphan_entry_bh, orphan_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001224 if (status < 0) {
1225 mlog_errno(status);
1226 goto bail;
1227 }
1228 }
1229
1230 /* change the dirent to point to the correct inode */
Mark Fasheh38760e22007-09-11 17:21:56 -07001231 status = ocfs2_update_entry(new_dir, handle, new_de_bh,
1232 new_de, old_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001233 if (status < 0) {
1234 mlog_errno(status);
1235 goto bail;
1236 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001237 new_dir->i_version++;
Mark Fashehccd979b2005-12-15 14:31:24 -08001238
1239 if (S_ISDIR(new_inode->i_mode))
1240 newfe->i_links_count = 0;
1241 else
1242 le16_add_cpu(&newfe->i_links_count, -1);
1243
1244 status = ocfs2_journal_dirty(handle, newfe_bh);
1245 if (status < 0) {
1246 mlog_errno(status);
1247 goto bail;
1248 }
1249 } else {
1250 /* if the name was not found in new_dir, add it now */
1251 status = ocfs2_add_entry(handle, new_dentry, old_inode,
1252 OCFS2_I(old_inode)->ip_blkno,
1253 new_dir_bh, insert_entry_bh);
1254 }
1255
1256 old_inode->i_ctime = CURRENT_TIME;
1257 mark_inode_dirty(old_inode);
Sunil Mushran480214d2007-08-06 15:11:56 -07001258
1259 status = ocfs2_journal_access(handle, old_inode, old_inode_bh,
1260 OCFS2_JOURNAL_ACCESS_WRITE);
1261 if (status >= 0) {
1262 old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
1263
1264 old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
1265 old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
1266
1267 status = ocfs2_journal_dirty(handle, old_inode_bh);
1268 if (status < 0)
1269 mlog_errno(status);
1270 } else
1271 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001272
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001273 /*
1274 * Now that the name has been added to new_dir, remove the old name.
1275 *
1276 * We don't keep any directory entry context around until now
1277 * because the insert might have changed the type of directory
1278 * we're dealing with.
1279 */
1280 old_de_bh = ocfs2_find_entry(old_dentry->d_name.name,
1281 old_dentry->d_name.len,
1282 old_dir, &old_de);
1283 if (!old_de_bh) {
1284 status = -EIO;
1285 goto bail;
1286 }
1287
Mark Fashehccd979b2005-12-15 14:31:24 -08001288 status = ocfs2_delete_entry(handle, old_dir, old_de, old_de_bh);
1289 if (status < 0) {
1290 mlog_errno(status);
1291 goto bail;
1292 }
1293
1294 if (new_inode) {
1295 new_inode->i_nlink--;
1296 new_inode->i_ctime = CURRENT_TIME;
1297 }
1298 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1299 if (old_inode_de_bh) {
Mark Fasheh38760e22007-09-11 17:21:56 -07001300 status = ocfs2_update_entry(old_inode, handle, old_inode_de_bh,
1301 old_inode_dot_dot_de, new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001302 old_dir->i_nlink--;
1303 if (new_inode) {
1304 new_inode->i_nlink--;
1305 } else {
Dave Hansend8c76e62006-09-30 23:29:04 -07001306 inc_nlink(new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001307 mark_inode_dirty(new_dir);
1308 }
1309 }
1310 mark_inode_dirty(old_dir);
Mark Fasheh592282c2007-01-02 17:59:40 -08001311 ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
1312 if (new_inode) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001313 mark_inode_dirty(new_inode);
Mark Fasheh592282c2007-01-02 17:59:40 -08001314 ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
1315 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001316
Mark Fasheh592282c2007-01-02 17:59:40 -08001317 if (old_dir != new_dir) {
1318 /* Keep the same times on both directories.*/
1319 new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
1320
1321 /*
1322 * This will also pick up the i_nlink change from the
1323 * block above.
1324 */
1325 ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
1326 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001327
1328 if (old_dir_nlink != old_dir->i_nlink) {
1329 if (!old_dir_bh) {
1330 mlog(ML_ERROR, "need to change nlink for old dir "
Mark Fashehb0697052006-03-03 10:24:33 -08001331 "%llu from %d to %d but bh is NULL!\n",
1332 (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
1333 (int)old_dir_nlink, old_dir->i_nlink);
Mark Fashehccd979b2005-12-15 14:31:24 -08001334 } else {
1335 struct ocfs2_dinode *fe;
1336 status = ocfs2_journal_access(handle, old_dir,
1337 old_dir_bh,
1338 OCFS2_JOURNAL_ACCESS_WRITE);
1339 fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
1340 fe->i_links_count = cpu_to_le16(old_dir->i_nlink);
1341 status = ocfs2_journal_dirty(handle, old_dir_bh);
1342 }
1343 }
1344
Mark Fasheh379dfe92006-09-08 14:21:03 -07001345 ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001346 status = 0;
1347bail:
1348 if (rename_lock)
1349 ocfs2_rename_unlock(osb);
1350
1351 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001352 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001353
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001354 if (parents_locked)
1355 ocfs2_double_unlock(old_dir, new_dir);
1356
1357 if (old_child_locked)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001358 ocfs2_inode_unlock(old_inode, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001359
1360 if (new_child_locked)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001361 ocfs2_inode_unlock(new_inode, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001362
Mark Fasheh5098c272006-10-05 18:12:57 -07001363 if (orphan_dir) {
1364 /* This was locked for us in ocfs2_prepare_orphan_dir() */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001365 ocfs2_inode_unlock(orphan_dir, 1);
Mark Fasheh5098c272006-10-05 18:12:57 -07001366 mutex_unlock(&orphan_dir->i_mutex);
1367 iput(orphan_dir);
1368 }
1369
Mark Fashehccd979b2005-12-15 14:31:24 -08001370 if (new_inode)
1371 sync_mapping_buffers(old_inode->i_mapping);
1372
1373 if (new_inode)
1374 iput(new_inode);
Mark Fasheha81cb882008-10-07 14:25:16 -07001375 brelse(newfe_bh);
1376 brelse(old_inode_bh);
1377 brelse(old_dir_bh);
1378 brelse(new_dir_bh);
1379 brelse(new_de_bh);
1380 brelse(old_de_bh);
1381 brelse(old_inode_de_bh);
1382 brelse(orphan_entry_bh);
1383 brelse(insert_entry_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001384
1385 mlog_exit(status);
1386
1387 return status;
1388}
1389
1390/*
1391 * we expect i_size = strlen(symname). Copy symname into the file
1392 * data, including the null terminator.
1393 */
1394static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001395 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001396 struct inode *inode,
1397 const char *symname)
1398{
1399 struct buffer_head **bhs = NULL;
1400 const char *c;
1401 struct super_block *sb = osb->sb;
Mark Fasheh4f902c32007-03-09 16:26:50 -08001402 u64 p_blkno, p_blocks;
Mark Fashehccd979b2005-12-15 14:31:24 -08001403 int virtual, blocks, status, i, bytes_left;
1404
1405 bytes_left = i_size_read(inode) + 1;
1406 /* we can't trust i_blocks because we're actually going to
1407 * write i_size + 1 bytes. */
1408 blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1409
Andrew Morton5515eff2006-03-26 01:37:53 -08001410 mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
1411 (unsigned long long)inode->i_blocks,
1412 i_size_read(inode), blocks);
Mark Fashehccd979b2005-12-15 14:31:24 -08001413
1414 /* Sanity check -- make sure we're going to fit. */
1415 if (bytes_left >
1416 ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1417 status = -EIO;
1418 mlog_errno(status);
1419 goto bail;
1420 }
1421
1422 bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
1423 if (!bhs) {
1424 status = -ENOMEM;
1425 mlog_errno(status);
1426 goto bail;
1427 }
1428
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001429 status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
1430 NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001431 if (status < 0) {
1432 mlog_errno(status);
1433 goto bail;
1434 }
1435
1436 /* links can never be larger than one cluster so we know this
1437 * is all going to be contiguous, but do a sanity check
1438 * anyway. */
1439 if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
1440 status = -EIO;
1441 mlog_errno(status);
1442 goto bail;
1443 }
1444
1445 virtual = 0;
1446 while(bytes_left > 0) {
1447 c = &symname[virtual * sb->s_blocksize];
1448
1449 bhs[virtual] = sb_getblk(sb, p_blkno);
1450 if (!bhs[virtual]) {
1451 status = -ENOMEM;
1452 mlog_errno(status);
1453 goto bail;
1454 }
1455 ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
1456
1457 status = ocfs2_journal_access(handle, inode, bhs[virtual],
1458 OCFS2_JOURNAL_ACCESS_CREATE);
1459 if (status < 0) {
1460 mlog_errno(status);
1461 goto bail;
1462 }
1463
1464 memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
1465
1466 memcpy(bhs[virtual]->b_data, c,
1467 (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
1468 bytes_left);
1469
1470 status = ocfs2_journal_dirty(handle, bhs[virtual]);
1471 if (status < 0) {
1472 mlog_errno(status);
1473 goto bail;
1474 }
1475
1476 virtual++;
1477 p_blkno++;
1478 bytes_left -= sb->s_blocksize;
1479 }
1480
1481 status = 0;
1482bail:
1483
1484 if (bhs) {
1485 for(i = 0; i < blocks; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001486 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001487 kfree(bhs);
1488 }
1489
1490 mlog_exit(status);
1491 return status;
1492}
1493
1494static int ocfs2_symlink(struct inode *dir,
1495 struct dentry *dentry,
1496 const char *symname)
1497{
1498 int status, l, credits;
1499 u64 newsize;
1500 struct ocfs2_super *osb = NULL;
1501 struct inode *inode = NULL;
1502 struct super_block *sb;
1503 struct buffer_head *new_fe_bh = NULL;
1504 struct buffer_head *de_bh = NULL;
1505 struct buffer_head *parent_fe_bh = NULL;
1506 struct ocfs2_dinode *fe = NULL;
1507 struct ocfs2_dinode *dirfe;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001508 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001509 struct ocfs2_alloc_context *inode_ac = NULL;
1510 struct ocfs2_alloc_context *data_ac = NULL;
1511
1512 mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
1513 dentry, symname, dentry->d_name.len, dentry->d_name.name);
1514
1515 sb = dir->i_sb;
1516 osb = OCFS2_SB(sb);
1517
1518 l = strlen(symname) + 1;
1519
1520 credits = ocfs2_calc_symlink_credits(sb);
1521
Mark Fashehccd979b2005-12-15 14:31:24 -08001522 /* lock the parent directory */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001523 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001524 if (status < 0) {
1525 if (status != -ENOENT)
1526 mlog_errno(status);
Mark Fasheh6d8fc402006-10-06 11:54:33 -07001527 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001528 }
1529
1530 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1531 if (!dirfe->i_links_count) {
1532 /* can't make a file in a deleted directory. */
1533 status = -ENOENT;
1534 goto bail;
1535 }
1536
1537 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
1538 dentry->d_name.len);
1539 if (status)
1540 goto bail;
1541
1542 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
1543 dentry->d_name.name,
1544 dentry->d_name.len, &de_bh);
1545 if (status < 0) {
1546 mlog_errno(status);
1547 goto bail;
1548 }
1549
Mark Fashehda5cbf22006-10-06 18:34:35 -07001550 status = ocfs2_reserve_new_inode(osb, &inode_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001551 if (status < 0) {
1552 if (status != -ENOSPC)
1553 mlog_errno(status);
1554 goto bail;
1555 }
1556
Tiger Yangf5d36202008-11-14 11:15:44 +08001557 inode = ocfs2_get_init_inode(dir, S_IFLNK | S_IRWXUGO);
1558 if (!inode) {
1559 status = -ENOMEM;
1560 mlog_errno(status);
1561 goto bail;
1562 }
1563
Mark Fashehccd979b2005-12-15 14:31:24 -08001564 /* don't reserve bitmap space for fast symlinks. */
1565 if (l > ocfs2_fast_symlink_chars(sb)) {
Mark Fashehda5cbf22006-10-06 18:34:35 -07001566 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001567 if (status < 0) {
1568 if (status != -ENOSPC)
1569 mlog_errno(status);
1570 goto bail;
1571 }
1572 }
1573
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001574 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001575 if (IS_ERR(handle)) {
1576 status = PTR_ERR(handle);
1577 handle = NULL;
1578 mlog_errno(status);
1579 goto bail;
1580 }
1581
Tiger Yangf5d36202008-11-14 11:15:44 +08001582 status = ocfs2_mknod_locked(osb, dir, inode, dentry,
1583 0, &new_fe_bh, parent_fe_bh, handle,
1584 inode_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001585 if (status < 0) {
1586 mlog_errno(status);
1587 goto bail;
1588 }
1589
1590 fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
1591 inode->i_rdev = 0;
1592 newsize = l - 1;
1593 if (l > ocfs2_fast_symlink_chars(sb)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08001594 u32 offset = 0;
1595
Mark Fashehccd979b2005-12-15 14:31:24 -08001596 inode->i_op = &ocfs2_symlink_inode_operations;
Tao Ma0eb8d472008-08-18 17:38:45 +08001597 status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0,
1598 new_fe_bh,
1599 handle, data_ac, NULL,
1600 NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001601 if (status < 0) {
1602 if (status != -ENOSPC && status != -EINTR) {
Mark Fashehb0697052006-03-03 10:24:33 -08001603 mlog(ML_ERROR,
1604 "Failed to extend file to %llu\n",
1605 (unsigned long long)newsize);
Mark Fashehccd979b2005-12-15 14:31:24 -08001606 mlog_errno(status);
1607 status = -ENOSPC;
1608 }
1609 goto bail;
1610 }
1611 i_size_write(inode, newsize);
Mark Fasheh8110b072007-03-22 16:53:23 -07001612 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001613 } else {
1614 inode->i_op = &ocfs2_fast_symlink_inode_operations;
1615 memcpy((char *) fe->id2.i_symlink, symname, l);
1616 i_size_write(inode, newsize);
1617 inode->i_blocks = 0;
1618 }
1619
1620 status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
1621 if (status < 0) {
1622 mlog_errno(status);
1623 goto bail;
1624 }
1625
1626 if (!ocfs2_inode_is_fast_symlink(inode)) {
1627 status = ocfs2_create_symlink_data(osb, handle, inode,
1628 symname);
1629 if (status < 0) {
1630 mlog_errno(status);
1631 goto bail;
1632 }
1633 }
1634
1635 status = ocfs2_add_entry(handle, dentry, inode,
1636 le64_to_cpu(fe->i_blkno), parent_fe_bh,
1637 de_bh);
1638 if (status < 0) {
1639 mlog_errno(status);
1640 goto bail;
1641 }
1642
Mark Fasheh0027dd52006-09-21 16:51:28 -07001643 status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
Mark Fasheh379dfe92006-09-08 14:21:03 -07001644 if (status) {
1645 mlog_errno(status);
1646 goto bail;
1647 }
1648
Mark Fashehccd979b2005-12-15 14:31:24 -08001649 insert_inode_hash(inode);
1650 dentry->d_op = &ocfs2_dentry_ops;
1651 d_instantiate(dentry, inode);
1652bail:
1653 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001654 ocfs2_commit_trans(osb, handle);
Mark Fasheh6d8fc402006-10-06 11:54:33 -07001655
Mark Fashehe63aecb62007-10-18 15:30:42 -07001656 ocfs2_inode_unlock(dir, 1);
Mark Fasheh6d8fc402006-10-06 11:54:33 -07001657
Mark Fasheha81cb882008-10-07 14:25:16 -07001658 brelse(new_fe_bh);
1659 brelse(parent_fe_bh);
1660 brelse(de_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001661 if (inode_ac)
1662 ocfs2_free_alloc_context(inode_ac);
1663 if (data_ac)
1664 ocfs2_free_alloc_context(data_ac);
Tiger Yangf5d36202008-11-14 11:15:44 +08001665 if ((status < 0) && inode) {
1666 clear_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001667 iput(inode);
Tiger Yangf5d36202008-11-14 11:15:44 +08001668 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001669
1670 mlog_exit(status);
1671
1672 return status;
1673}
1674
Mark Fashehccd979b2005-12-15 14:31:24 -08001675static int ocfs2_blkno_stringify(u64 blkno, char *name)
1676{
1677 int status, namelen;
1678
1679 mlog_entry_void();
1680
Mark Fashehb0697052006-03-03 10:24:33 -08001681 namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
1682 (long long)blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001683 if (namelen <= 0) {
1684 if (namelen)
1685 status = namelen;
1686 else
1687 status = -EINVAL;
1688 mlog_errno(status);
1689 goto bail;
1690 }
1691 if (namelen != OCFS2_ORPHAN_NAMELEN) {
1692 status = -EINVAL;
1693 mlog_errno(status);
1694 goto bail;
1695 }
1696
1697 mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
1698 namelen);
1699
1700 status = 0;
1701bail:
1702 mlog_exit(status);
1703 return status;
1704}
1705
1706static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
Mark Fasheh5098c272006-10-05 18:12:57 -07001707 struct inode **ret_orphan_dir,
Mark Fashehccd979b2005-12-15 14:31:24 -08001708 struct inode *inode,
1709 char *name,
1710 struct buffer_head **de_bh)
1711{
Mark Fasheh5098c272006-10-05 18:12:57 -07001712 struct inode *orphan_dir_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -08001713 struct buffer_head *orphan_dir_bh = NULL;
1714 int status = 0;
1715
1716 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
1717 if (status < 0) {
1718 mlog_errno(status);
Mark Fasheh5098c272006-10-05 18:12:57 -07001719 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001720 }
1721
1722 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1723 ORPHAN_DIR_SYSTEM_INODE,
1724 osb->slot_num);
1725 if (!orphan_dir_inode) {
1726 status = -ENOENT;
1727 mlog_errno(status);
Mark Fasheh5098c272006-10-05 18:12:57 -07001728 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001729 }
1730
Mark Fasheh5098c272006-10-05 18:12:57 -07001731 mutex_lock(&orphan_dir_inode->i_mutex);
1732
Mark Fashehe63aecb62007-10-18 15:30:42 -07001733 status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001734 if (status < 0) {
1735 mlog_errno(status);
1736 goto leave;
1737 }
1738
1739 status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
1740 orphan_dir_bh, name,
1741 OCFS2_ORPHAN_NAMELEN, de_bh);
1742 if (status < 0) {
Mark Fashehe63aecb62007-10-18 15:30:42 -07001743 ocfs2_inode_unlock(orphan_dir_inode, 1);
Mark Fasheh5098c272006-10-05 18:12:57 -07001744
Mark Fashehccd979b2005-12-15 14:31:24 -08001745 mlog_errno(status);
1746 goto leave;
1747 }
1748
Mark Fasheh5098c272006-10-05 18:12:57 -07001749 *ret_orphan_dir = orphan_dir_inode;
1750
Mark Fashehccd979b2005-12-15 14:31:24 -08001751leave:
Mark Fasheh5098c272006-10-05 18:12:57 -07001752 if (status) {
1753 mutex_unlock(&orphan_dir_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001754 iput(orphan_dir_inode);
Mark Fasheh5098c272006-10-05 18:12:57 -07001755 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001756
Mark Fasheha81cb882008-10-07 14:25:16 -07001757 brelse(orphan_dir_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001758
1759 mlog_exit(status);
1760 return status;
1761}
1762
1763static int ocfs2_orphan_add(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001764 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001765 struct inode *inode,
1766 struct ocfs2_dinode *fe,
1767 char *name,
Mark Fasheh5098c272006-10-05 18:12:57 -07001768 struct buffer_head *de_bh,
1769 struct inode *orphan_dir_inode)
Mark Fashehccd979b2005-12-15 14:31:24 -08001770{
Mark Fashehccd979b2005-12-15 14:31:24 -08001771 struct buffer_head *orphan_dir_bh = NULL;
1772 int status = 0;
1773 struct ocfs2_dinode *orphan_fe;
1774
1775 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
1776
Joel Becker31d33072008-10-09 17:20:30 -07001777 status = ocfs2_read_block(orphan_dir_inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08001778 OCFS2_I(orphan_dir_inode)->ip_blkno,
Joel Becker0fcaa562008-10-09 17:20:31 -07001779 &orphan_dir_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001780 if (status < 0) {
1781 mlog_errno(status);
1782 goto leave;
1783 }
1784
1785 status = ocfs2_journal_access(handle, orphan_dir_inode, orphan_dir_bh,
1786 OCFS2_JOURNAL_ACCESS_WRITE);
1787 if (status < 0) {
1788 mlog_errno(status);
1789 goto leave;
1790 }
1791
1792 /* we're a cluster, and nlink can change on disk from
1793 * underneath us... */
1794 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
1795 if (S_ISDIR(inode->i_mode))
1796 le16_add_cpu(&orphan_fe->i_links_count, 1);
1797 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
1798
1799 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
1800 if (status < 0) {
1801 mlog_errno(status);
1802 goto leave;
1803 }
1804
1805 status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
1806 OCFS2_ORPHAN_NAMELEN, inode,
1807 OCFS2_I(inode)->ip_blkno,
1808 orphan_dir_bh, de_bh);
1809 if (status < 0) {
1810 mlog_errno(status);
1811 goto leave;
1812 }
1813
1814 le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
1815
1816 /* Record which orphan dir our inode now resides
1817 * in. delete_inode will use this to determine which orphan
1818 * dir to lock. */
Tiger Yang50008632007-03-20 16:01:38 -07001819 fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001820
Mark Fashehb0697052006-03-03 10:24:33 -08001821 mlog(0, "Inode %llu orphaned in slot %d\n",
1822 (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001823
1824leave:
Mark Fasheha81cb882008-10-07 14:25:16 -07001825 brelse(orphan_dir_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001826
1827 mlog_exit(status);
1828 return status;
1829}
1830
1831/* unlike orphan_add, we expect the orphan dir to already be locked here. */
1832int ocfs2_orphan_del(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001833 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001834 struct inode *orphan_dir_inode,
1835 struct inode *inode,
1836 struct buffer_head *orphan_dir_bh)
1837{
1838 char name[OCFS2_ORPHAN_NAMELEN + 1];
1839 struct ocfs2_dinode *orphan_fe;
1840 int status = 0;
1841 struct buffer_head *target_de_bh = NULL;
1842 struct ocfs2_dir_entry *target_de = NULL;
1843
1844 mlog_entry_void();
1845
1846 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
1847 if (status < 0) {
1848 mlog_errno(status);
1849 goto leave;
1850 }
1851
Mark Fashehb0697052006-03-03 10:24:33 -08001852 mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
1853 name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
1854 OCFS2_ORPHAN_NAMELEN);
Mark Fashehccd979b2005-12-15 14:31:24 -08001855
1856 /* find it's spot in the orphan directory */
1857 target_de_bh = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN,
1858 orphan_dir_inode, &target_de);
1859 if (!target_de_bh) {
1860 status = -ENOENT;
1861 mlog_errno(status);
1862 goto leave;
1863 }
1864
1865 /* remove it from the orphan directory */
1866 status = ocfs2_delete_entry(handle, orphan_dir_inode, target_de,
1867 target_de_bh);
1868 if (status < 0) {
1869 mlog_errno(status);
1870 goto leave;
1871 }
1872
1873 status = ocfs2_journal_access(handle,orphan_dir_inode, orphan_dir_bh,
1874 OCFS2_JOURNAL_ACCESS_WRITE);
1875 if (status < 0) {
1876 mlog_errno(status);
1877 goto leave;
1878 }
1879
1880 /* do the i_nlink dance! :) */
1881 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
1882 if (S_ISDIR(inode->i_mode))
1883 le16_add_cpu(&orphan_fe->i_links_count, -1);
1884 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
1885
1886 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
1887 if (status < 0) {
1888 mlog_errno(status);
1889 goto leave;
1890 }
1891
1892leave:
Mark Fasheha81cb882008-10-07 14:25:16 -07001893 brelse(target_de_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001894
1895 mlog_exit(status);
1896 return status;
1897}
1898
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001899const struct inode_operations ocfs2_dir_iops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08001900 .create = ocfs2_create,
1901 .lookup = ocfs2_lookup,
1902 .link = ocfs2_link,
1903 .unlink = ocfs2_unlink,
1904 .rmdir = ocfs2_unlink,
1905 .symlink = ocfs2_symlink,
1906 .mkdir = ocfs2_mkdir,
1907 .mknod = ocfs2_mknod,
1908 .rename = ocfs2_rename,
1909 .setattr = ocfs2_setattr,
1910 .getattr = ocfs2_getattr,
Tiger Yangd38eb8d2006-11-27 09:59:21 +08001911 .permission = ocfs2_permission,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001912 .setxattr = generic_setxattr,
1913 .getxattr = generic_getxattr,
1914 .listxattr = ocfs2_listxattr,
1915 .removexattr = generic_removexattr,
Mark Fashehccd979b2005-12-15 14:31:24 -08001916};