blob: f4967e634ffd01283e299bb6a7ee0d886048b813 [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,
69 struct dentry *dentry, int mode,
70 dev_t dev,
71 struct buffer_head **new_fe_bh,
72 struct buffer_head *parent_fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -070073 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080074 struct inode **ret_inode,
75 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 Fashehb06970532006-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
Mark Fashehccd979b2005-12-15 14:31:24 -0800189static int ocfs2_mknod(struct inode *dir,
190 struct dentry *dentry,
191 int mode,
192 dev_t dev)
193{
194 int status = 0;
195 struct buffer_head *parent_fe_bh = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700196 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800197 struct ocfs2_super *osb;
198 struct ocfs2_dinode *dirfe;
199 struct buffer_head *new_fe_bh = NULL;
200 struct buffer_head *de_bh = NULL;
201 struct inode *inode = NULL;
202 struct ocfs2_alloc_context *inode_ac = NULL;
203 struct ocfs2_alloc_context *data_ac = NULL;
204
205 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
206 (unsigned long)dev, dentry->d_name.len,
207 dentry->d_name.name);
208
209 /* get our super block */
210 osb = OCFS2_SB(dir->i_sb);
211
Mark Fashehe63aecb62007-10-18 15:30:42 -0700212 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
Mark Fashehe3a82132006-10-05 16:04:17 -0700213 if (status < 0) {
214 if (status != -ENOENT)
215 mlog_errno(status);
216 return status;
217 }
218
Mark Fasheha663e302006-08-09 11:45:07 -0700219 if (S_ISDIR(mode) && (dir->i_nlink >= OCFS2_LINK_MAX)) {
220 status = -EMLINK;
221 goto leave;
222 }
223
Mark Fashehccd979b2005-12-15 14:31:24 -0800224 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
225 if (!dirfe->i_links_count) {
226 /* can't make a file in a deleted directory. */
227 status = -ENOENT;
228 goto leave;
229 }
230
231 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
232 dentry->d_name.len);
233 if (status)
234 goto leave;
235
236 /* get a spot inside the dir. */
237 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
238 dentry->d_name.name,
239 dentry->d_name.len, &de_bh);
240 if (status < 0) {
241 mlog_errno(status);
242 goto leave;
243 }
244
245 /* reserve an inode spot */
Mark Fashehda5cbf22006-10-06 18:34:35 -0700246 status = ocfs2_reserve_new_inode(osb, &inode_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800247 if (status < 0) {
248 if (status != -ENOSPC)
249 mlog_errno(status);
250 goto leave;
251 }
252
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700253 /* Reserve a cluster if creating an extent based directory. */
254 if (S_ISDIR(mode) && !ocfs2_supports_inline_data(osb)) {
Mark Fashehda5cbf22006-10-06 18:34:35 -0700255 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800256 if (status < 0) {
257 if (status != -ENOSPC)
258 mlog_errno(status);
259 goto leave;
260 }
261 }
262
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700263 handle = ocfs2_start_trans(osb, OCFS2_MKNOD_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800264 if (IS_ERR(handle)) {
265 status = PTR_ERR(handle);
266 handle = NULL;
267 mlog_errno(status);
268 goto leave;
269 }
270
271 /* do the real work now. */
272 status = ocfs2_mknod_locked(osb, dir, dentry, mode, dev,
273 &new_fe_bh, parent_fe_bh, handle,
274 &inode, inode_ac);
275 if (status < 0) {
276 mlog_errno(status);
277 goto leave;
278 }
279
280 if (S_ISDIR(mode)) {
281 status = ocfs2_fill_new_dir(osb, handle, dir, inode,
282 new_fe_bh, data_ac);
283 if (status < 0) {
284 mlog_errno(status);
285 goto leave;
286 }
287
288 status = ocfs2_journal_access(handle, dir, parent_fe_bh,
289 OCFS2_JOURNAL_ACCESS_WRITE);
290 if (status < 0) {
291 mlog_errno(status);
292 goto leave;
293 }
294 le16_add_cpu(&dirfe->i_links_count, 1);
295 status = ocfs2_journal_dirty(handle, parent_fe_bh);
296 if (status < 0) {
297 mlog_errno(status);
298 goto leave;
299 }
Dave Hansend8c76e62006-09-30 23:29:04 -0700300 inc_nlink(dir);
Mark Fashehccd979b2005-12-15 14:31:24 -0800301 }
302
303 status = ocfs2_add_entry(handle, dentry, inode,
304 OCFS2_I(inode)->ip_blkno, parent_fe_bh,
305 de_bh);
306 if (status < 0) {
307 mlog_errno(status);
308 goto leave;
309 }
310
Mark Fasheh379dfe92006-09-08 14:21:03 -0700311 status = ocfs2_dentry_attach_lock(dentry, inode,
Mark Fasheh0027dd52006-09-21 16:51:28 -0700312 OCFS2_I(dir)->ip_blkno);
Mark Fasheh379dfe92006-09-08 14:21:03 -0700313 if (status) {
314 mlog_errno(status);
315 goto leave;
316 }
317
Mark Fashehccd979b2005-12-15 14:31:24 -0800318 insert_inode_hash(inode);
319 dentry->d_op = &ocfs2_dentry_ops;
320 d_instantiate(dentry, inode);
321 status = 0;
322leave:
323 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700324 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800325
Mark Fashehe63aecb62007-10-18 15:30:42 -0700326 ocfs2_inode_unlock(dir, 1);
Mark Fashehe3a82132006-10-05 16:04:17 -0700327
Mark Fashehccd979b2005-12-15 14:31:24 -0800328 if (status == -ENOSPC)
329 mlog(0, "Disk is full\n");
330
Mark Fasheha81cb882008-10-07 14:25:16 -0700331 brelse(new_fe_bh);
332 brelse(de_bh);
333 brelse(parent_fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800334
335 if ((status < 0) && inode)
336 iput(inode);
337
338 if (inode_ac)
339 ocfs2_free_alloc_context(inode_ac);
340
341 if (data_ac)
342 ocfs2_free_alloc_context(data_ac);
343
344 mlog_exit(status);
345
346 return status;
347}
348
349static int ocfs2_mknod_locked(struct ocfs2_super *osb,
350 struct inode *dir,
351 struct dentry *dentry, int mode,
352 dev_t dev,
353 struct buffer_head **new_fe_bh,
354 struct buffer_head *parent_fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700355 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800356 struct inode **ret_inode,
357 struct ocfs2_alloc_context *inode_ac)
358{
359 int status = 0;
360 struct ocfs2_dinode *fe = NULL;
361 struct ocfs2_extent_list *fel;
362 u64 fe_blkno = 0;
363 u16 suballoc_bit;
364 struct inode *inode = NULL;
365
366 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
367 (unsigned long)dev, dentry->d_name.len,
368 dentry->d_name.name);
369
370 *new_fe_bh = NULL;
371 *ret_inode = NULL;
372
373 status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit,
374 &fe_blkno);
375 if (status < 0) {
376 mlog_errno(status);
377 goto leave;
378 }
379
380 inode = new_inode(dir->i_sb);
Jan Kara87cfa002008-10-20 19:23:53 +0200381 if (!inode) {
382 status = -ENOMEM;
Mark Fashehccd979b2005-12-15 14:31:24 -0800383 mlog(ML_ERROR, "new_inode failed!\n");
384 goto leave;
385 }
386
387 /* populate as many fields early on as possible - many of
388 * these are used by the support functions here and in
389 * callers. */
390 inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
391 OCFS2_I(inode)->ip_blkno = fe_blkno;
392 if (S_ISDIR(mode))
393 inode->i_nlink = 2;
394 else
395 inode->i_nlink = 1;
396 inode->i_mode = mode;
397 spin_lock(&osb->osb_lock);
398 inode->i_generation = osb->s_next_generation++;
399 spin_unlock(&osb->osb_lock);
400
401 *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
402 if (!*new_fe_bh) {
403 status = -EIO;
404 mlog_errno(status);
405 goto leave;
406 }
407 ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh);
408
409 status = ocfs2_journal_access(handle, inode, *new_fe_bh,
410 OCFS2_JOURNAL_ACCESS_CREATE);
411 if (status < 0) {
412 mlog_errno(status);
413 goto leave;
414 }
415
416 fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
417 memset(fe, 0, osb->sb->s_blocksize);
418
419 fe->i_generation = cpu_to_le32(inode->i_generation);
420 fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
421 fe->i_blkno = cpu_to_le64(fe_blkno);
422 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800423 fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot);
Mark Fashehccd979b2005-12-15 14:31:24 -0800424 fe->i_uid = cpu_to_le32(current->fsuid);
425 if (dir->i_mode & S_ISGID) {
426 fe->i_gid = cpu_to_le32(dir->i_gid);
427 if (S_ISDIR(mode))
428 mode |= S_ISGID;
429 } else
430 fe->i_gid = cpu_to_le32(current->fsgid);
431 fe->i_mode = cpu_to_le16(mode);
432 if (S_ISCHR(mode) || S_ISBLK(mode))
433 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
434
435 fe->i_links_count = cpu_to_le16(inode->i_nlink);
436
437 fe->i_last_eb_blk = 0;
438 strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
439 le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
440 fe->i_atime = fe->i_ctime = fe->i_mtime =
441 cpu_to_le64(CURRENT_TIME.tv_sec);
442 fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
443 cpu_to_le32(CURRENT_TIME.tv_nsec);
444 fe->i_dtime = 0;
445
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700446 /*
447 * If supported, directories start with inline data.
448 */
449 if (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) {
450 u16 feat = le16_to_cpu(fe->i_dyn_features);
451
452 fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL);
453
454 fe->id2.i_data.id_count = cpu_to_le16(ocfs2_max_inline_data(osb->sb));
455 } else {
456 fel = &fe->id2.i_list;
457 fel->l_tree_depth = 0;
458 fel->l_next_free_rec = 0;
459 fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
460 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800461
462 status = ocfs2_journal_dirty(handle, *new_fe_bh);
463 if (status < 0) {
464 mlog_errno(status);
465 goto leave;
466 }
467
468 if (ocfs2_populate_inode(inode, fe, 1) < 0) {
469 mlog(ML_ERROR, "populate inode failed! bh->b_blocknr=%llu, "
Mark Fashehb06970532006-03-03 10:24:33 -0800470 "i_blkno=%llu, i_ino=%lu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700471 (unsigned long long)(*new_fe_bh)->b_blocknr,
472 (unsigned long long)le64_to_cpu(fe->i_blkno),
473 inode->i_ino);
Mark Fashehccd979b2005-12-15 14:31:24 -0800474 BUG();
475 }
476
477 ocfs2_inode_set_new(osb, inode);
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800478 if (!ocfs2_mount_local(osb)) {
479 status = ocfs2_create_new_inode_locks(inode);
480 if (status < 0)
481 mlog_errno(status);
482 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800483
484 status = 0; /* error in ocfs2_create_new_inode_locks is not
485 * critical */
486
487 *ret_inode = inode;
488leave:
489 if (status < 0) {
490 if (*new_fe_bh) {
491 brelse(*new_fe_bh);
492 *new_fe_bh = NULL;
493 }
Jan Karab99835c2008-10-20 19:23:54 +0200494 if (inode) {
495 clear_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800496 iput(inode);
Jan Karab99835c2008-10-20 19:23:54 +0200497 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800498 }
499
500 mlog_exit(status);
501 return status;
502}
503
504static int ocfs2_mkdir(struct inode *dir,
505 struct dentry *dentry,
506 int mode)
507{
508 int ret;
509
510 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
511 dentry->d_name.len, dentry->d_name.name);
512 ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
513 mlog_exit(ret);
514
515 return ret;
516}
517
518static int ocfs2_create(struct inode *dir,
519 struct dentry *dentry,
520 int mode,
521 struct nameidata *nd)
522{
523 int ret;
524
525 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
526 dentry->d_name.len, dentry->d_name.name);
527 ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
528 mlog_exit(ret);
529
530 return ret;
531}
532
533static int ocfs2_link(struct dentry *old_dentry,
534 struct inode *dir,
535 struct dentry *dentry)
536{
Mark Fasheh1fabe142006-10-09 18:11:45 -0700537 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800538 struct inode *inode = old_dentry->d_inode;
539 int err;
540 struct buffer_head *fe_bh = NULL;
541 struct buffer_head *parent_fe_bh = NULL;
542 struct buffer_head *de_bh = NULL;
543 struct ocfs2_dinode *fe = NULL;
544 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
545
546 mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
547 old_dentry->d_name.len, old_dentry->d_name.name,
548 dentry->d_name.len, dentry->d_name.name);
549
Mark Fasheh123a9642006-10-05 16:48:23 -0700550 if (S_ISDIR(inode->i_mode))
551 return -EPERM;
Mark Fashehccd979b2005-12-15 14:31:24 -0800552
Mark Fashehe63aecb62007-10-18 15:30:42 -0700553 err = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800554 if (err < 0) {
555 if (err != -ENOENT)
556 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700557 return err;
Mark Fashehccd979b2005-12-15 14:31:24 -0800558 }
559
Tiger Yang0f62de22006-08-31 20:39:47 -0700560 if (!dir->i_nlink) {
561 err = -ENOENT;
Mark Fasheh123a9642006-10-05 16:48:23 -0700562 goto out;
Tiger Yang0f62de22006-08-31 20:39:47 -0700563 }
564
Mark Fashehccd979b2005-12-15 14:31:24 -0800565 err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
566 dentry->d_name.len);
567 if (err)
Mark Fasheh123a9642006-10-05 16:48:23 -0700568 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800569
570 err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
571 dentry->d_name.name,
572 dentry->d_name.len, &de_bh);
573 if (err < 0) {
574 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700575 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800576 }
577
Mark Fashehe63aecb62007-10-18 15:30:42 -0700578 err = ocfs2_inode_lock(inode, &fe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800579 if (err < 0) {
580 if (err != -ENOENT)
581 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700582 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800583 }
584
585 fe = (struct ocfs2_dinode *) fe_bh->b_data;
586 if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) {
587 err = -EMLINK;
Mark Fasheh123a9642006-10-05 16:48:23 -0700588 goto out_unlock_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800589 }
590
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700591 handle = ocfs2_start_trans(osb, OCFS2_LINK_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800592 if (IS_ERR(handle)) {
593 err = PTR_ERR(handle);
594 handle = NULL;
595 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700596 goto out_unlock_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800597 }
598
599 err = ocfs2_journal_access(handle, inode, fe_bh,
600 OCFS2_JOURNAL_ACCESS_WRITE);
601 if (err < 0) {
602 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700603 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800604 }
605
Dave Hansend8c76e62006-09-30 23:29:04 -0700606 inc_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800607 inode->i_ctime = CURRENT_TIME;
608 fe->i_links_count = cpu_to_le16(inode->i_nlink);
609 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
610 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
611
612 err = ocfs2_journal_dirty(handle, fe_bh);
613 if (err < 0) {
614 le16_add_cpu(&fe->i_links_count, -1);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700615 drop_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800616 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700617 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800618 }
619
620 err = ocfs2_add_entry(handle, dentry, inode,
621 OCFS2_I(inode)->ip_blkno,
622 parent_fe_bh, de_bh);
623 if (err) {
624 le16_add_cpu(&fe->i_links_count, -1);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700625 drop_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800626 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700627 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800628 }
629
Mark Fasheh0027dd52006-09-21 16:51:28 -0700630 err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
Mark Fasheh379dfe92006-09-08 14:21:03 -0700631 if (err) {
632 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700633 goto out_commit;
Mark Fasheh379dfe92006-09-08 14:21:03 -0700634 }
635
Mark Fashehccd979b2005-12-15 14:31:24 -0800636 atomic_inc(&inode->i_count);
637 dentry->d_op = &ocfs2_dentry_ops;
638 d_instantiate(dentry, inode);
Mark Fasheh123a9642006-10-05 16:48:23 -0700639
640out_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700641 ocfs2_commit_trans(osb, handle);
Mark Fasheh123a9642006-10-05 16:48:23 -0700642out_unlock_inode:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700643 ocfs2_inode_unlock(inode, 1);
Mark Fasheh123a9642006-10-05 16:48:23 -0700644
645out:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700646 ocfs2_inode_unlock(dir, 1);
Mark Fasheh123a9642006-10-05 16:48:23 -0700647
Mark Fasheha81cb882008-10-07 14:25:16 -0700648 brelse(de_bh);
649 brelse(fe_bh);
650 brelse(parent_fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800651
652 mlog_exit(err);
653
654 return err;
655}
656
Mark Fasheh379dfe92006-09-08 14:21:03 -0700657/*
658 * Takes and drops an exclusive lock on the given dentry. This will
659 * force other nodes to drop it.
660 */
661static int ocfs2_remote_dentry_delete(struct dentry *dentry)
662{
663 int ret;
664
665 ret = ocfs2_dentry_lock(dentry, 1);
666 if (ret)
667 mlog_errno(ret);
668 else
669 ocfs2_dentry_unlock(dentry, 1);
670
671 return ret;
672}
673
Mark Fasheh17ff7852006-09-30 23:29:05 -0700674static inline int inode_is_unlinkable(struct inode *inode)
675{
676 if (S_ISDIR(inode->i_mode)) {
677 if (inode->i_nlink == 2)
678 return 1;
679 return 0;
680 }
681
682 if (inode->i_nlink == 1)
683 return 1;
684 return 0;
685}
686
Mark Fashehccd979b2005-12-15 14:31:24 -0800687static int ocfs2_unlink(struct inode *dir,
688 struct dentry *dentry)
689{
690 int status;
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700691 int child_locked = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800692 struct inode *inode = dentry->d_inode;
Mark Fasheh5098c272006-10-05 18:12:57 -0700693 struct inode *orphan_dir = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800694 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
695 u64 blkno;
696 struct ocfs2_dinode *fe = NULL;
697 struct buffer_head *fe_bh = NULL;
698 struct buffer_head *parent_node_bh = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700699 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800700 struct ocfs2_dir_entry *dirent = NULL;
701 struct buffer_head *dirent_bh = NULL;
702 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
703 struct buffer_head *orphan_entry_bh = NULL;
704
705 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
706 dentry->d_name.len, dentry->d_name.name);
707
708 BUG_ON(dentry->d_parent->d_inode != dir);
709
Mark Fashehb06970532006-03-03 10:24:33 -0800710 mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800711
712 if (inode == osb->root_inode) {
713 mlog(0, "Cannot delete the root directory\n");
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700714 return -EPERM;
Mark Fashehccd979b2005-12-15 14:31:24 -0800715 }
716
Mark Fashehe63aecb62007-10-18 15:30:42 -0700717 status = ocfs2_inode_lock(dir, &parent_node_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800718 if (status < 0) {
719 if (status != -ENOENT)
720 mlog_errno(status);
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700721 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -0800722 }
723
724 status = ocfs2_find_files_on_disk(dentry->d_name.name,
725 dentry->d_name.len, &blkno,
726 dir, &dirent_bh, &dirent);
727 if (status < 0) {
728 if (status != -ENOENT)
729 mlog_errno(status);
730 goto leave;
731 }
732
733 if (OCFS2_I(inode)->ip_blkno != blkno) {
734 status = -ENOENT;
735
Mark Fashehb06970532006-03-03 10:24:33 -0800736 mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
737 (unsigned long long)OCFS2_I(inode)->ip_blkno,
738 (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800739 goto leave;
740 }
741
Mark Fashehe63aecb62007-10-18 15:30:42 -0700742 status = ocfs2_inode_lock(inode, &fe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800743 if (status < 0) {
744 if (status != -ENOENT)
745 mlog_errno(status);
746 goto leave;
747 }
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700748 child_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800749
750 if (S_ISDIR(inode->i_mode)) {
751 if (!ocfs2_empty_dir(inode)) {
752 status = -ENOTEMPTY;
753 goto leave;
754 } else if (inode->i_nlink != 2) {
755 status = -ENOTEMPTY;
756 goto leave;
757 }
758 }
759
Mark Fasheh379dfe92006-09-08 14:21:03 -0700760 status = ocfs2_remote_dentry_delete(dentry);
Mark Fashehccd979b2005-12-15 14:31:24 -0800761 if (status < 0) {
Mark Fasheh34d024f2007-09-24 15:56:19 -0700762 /* This remote delete should succeed under all normal
Mark Fashehccd979b2005-12-15 14:31:24 -0800763 * circumstances. */
764 mlog_errno(status);
765 goto leave;
766 }
767
Mark Fasheh17ff7852006-09-30 23:29:05 -0700768 if (inode_is_unlinkable(inode)) {
Mark Fasheh5098c272006-10-05 18:12:57 -0700769 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800770 orphan_name,
771 &orphan_entry_bh);
772 if (status < 0) {
773 mlog_errno(status);
774 goto leave;
775 }
776 }
777
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700778 handle = ocfs2_start_trans(osb, OCFS2_UNLINK_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800779 if (IS_ERR(handle)) {
780 status = PTR_ERR(handle);
781 handle = NULL;
782 mlog_errno(status);
783 goto leave;
784 }
785
786 status = ocfs2_journal_access(handle, inode, fe_bh,
787 OCFS2_JOURNAL_ACCESS_WRITE);
788 if (status < 0) {
789 mlog_errno(status);
790 goto leave;
791 }
792
793 fe = (struct ocfs2_dinode *) fe_bh->b_data;
794
Mark Fasheh17ff7852006-09-30 23:29:05 -0700795 if (inode_is_unlinkable(inode)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800796 status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
Mark Fasheh5098c272006-10-05 18:12:57 -0700797 orphan_entry_bh, orphan_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -0800798 if (status < 0) {
799 mlog_errno(status);
800 goto leave;
801 }
802 }
803
804 /* delete the name from the parent dir */
805 status = ocfs2_delete_entry(handle, dir, dirent, dirent_bh);
806 if (status < 0) {
807 mlog_errno(status);
808 goto leave;
809 }
810
Mark Fasheh17ff7852006-09-30 23:29:05 -0700811 if (S_ISDIR(inode->i_mode))
812 drop_nlink(inode);
813 drop_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800814 fe->i_links_count = cpu_to_le16(inode->i_nlink);
Mark Fashehccd979b2005-12-15 14:31:24 -0800815
816 status = ocfs2_journal_dirty(handle, fe_bh);
817 if (status < 0) {
818 mlog_errno(status);
819 goto leave;
820 }
821
Mark Fasheh592282c2007-01-02 17:59:40 -0800822 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
823 if (S_ISDIR(inode->i_mode))
Mark Fasheh17ff7852006-09-30 23:29:05 -0700824 drop_nlink(dir);
Mark Fasheh592282c2007-01-02 17:59:40 -0800825
826 status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
827 if (status < 0) {
828 mlog_errno(status);
829 if (S_ISDIR(inode->i_mode))
Dave Hansend8c76e62006-09-30 23:29:04 -0700830 inc_nlink(dir);
Mark Fashehccd979b2005-12-15 14:31:24 -0800831 }
832
833leave:
Mark Fashehccd979b2005-12-15 14:31:24 -0800834 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700835 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800836
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700837 if (child_locked)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700838 ocfs2_inode_unlock(inode, 1);
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700839
Mark Fashehe63aecb62007-10-18 15:30:42 -0700840 ocfs2_inode_unlock(dir, 1);
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700841
Mark Fasheh5098c272006-10-05 18:12:57 -0700842 if (orphan_dir) {
843 /* This was locked for us in ocfs2_prepare_orphan_dir() */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700844 ocfs2_inode_unlock(orphan_dir, 1);
Mark Fasheh5098c272006-10-05 18:12:57 -0700845 mutex_unlock(&orphan_dir->i_mutex);
846 iput(orphan_dir);
847 }
848
Mark Fasheha81cb882008-10-07 14:25:16 -0700849 brelse(fe_bh);
850 brelse(dirent_bh);
851 brelse(parent_node_bh);
852 brelse(orphan_entry_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800853
854 mlog_exit(status);
855
856 return status;
857}
858
859/*
860 * The only place this should be used is rename!
861 * if they have the same id, then the 1st one is the only one locked.
862 */
863static int ocfs2_double_lock(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800864 struct buffer_head **bh1,
865 struct inode *inode1,
866 struct buffer_head **bh2,
867 struct inode *inode2)
868{
869 int status;
870 struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
871 struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
872 struct buffer_head **tmpbh;
873 struct inode *tmpinode;
874
Mark Fashehb06970532006-03-03 10:24:33 -0800875 mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
876 (unsigned long long)oi1->ip_blkno,
877 (unsigned long long)oi2->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800878
Mark Fashehccd979b2005-12-15 14:31:24 -0800879 if (*bh1)
880 *bh1 = NULL;
881 if (*bh2)
882 *bh2 = NULL;
883
884 /* we always want to lock the one with the lower lockid first. */
885 if (oi1->ip_blkno != oi2->ip_blkno) {
886 if (oi1->ip_blkno < oi2->ip_blkno) {
887 /* switch id1 and id2 around */
888 mlog(0, "switching them around...\n");
889 tmpbh = bh2;
890 bh2 = bh1;
891 bh1 = tmpbh;
892
893 tmpinode = inode2;
894 inode2 = inode1;
895 inode1 = tmpinode;
896 }
897 /* lock id2 */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700898 status = ocfs2_inode_lock(inode2, bh2, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800899 if (status < 0) {
900 if (status != -ENOENT)
901 mlog_errno(status);
902 goto bail;
903 }
904 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700905
Mark Fashehccd979b2005-12-15 14:31:24 -0800906 /* lock id1 */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700907 status = ocfs2_inode_lock(inode1, bh1, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800908 if (status < 0) {
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700909 /*
910 * An error return must mean that no cluster locks
911 * were held on function exit.
912 */
913 if (oi1->ip_blkno != oi2->ip_blkno)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700914 ocfs2_inode_unlock(inode2, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700915
Mark Fashehccd979b2005-12-15 14:31:24 -0800916 if (status != -ENOENT)
917 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800918 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700919
Mark Fashehccd979b2005-12-15 14:31:24 -0800920bail:
921 mlog_exit(status);
922 return status;
923}
924
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700925static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
926{
Mark Fashehe63aecb62007-10-18 15:30:42 -0700927 ocfs2_inode_unlock(inode1, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700928
929 if (inode1 != inode2)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700930 ocfs2_inode_unlock(inode2, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700931}
932
Mark Fashehccd979b2005-12-15 14:31:24 -0800933static int ocfs2_rename(struct inode *old_dir,
934 struct dentry *old_dentry,
935 struct inode *new_dir,
936 struct dentry *new_dentry)
937{
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700938 int status = 0, rename_lock = 0, parents_locked = 0;
939 int old_child_locked = 0, new_child_locked = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800940 struct inode *old_inode = old_dentry->d_inode;
941 struct inode *new_inode = new_dentry->d_inode;
Mark Fasheh5098c272006-10-05 18:12:57 -0700942 struct inode *orphan_dir = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800943 struct ocfs2_dinode *newfe = NULL;
944 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
945 struct buffer_head *orphan_entry_bh = NULL;
946 struct buffer_head *newfe_bh = NULL;
Mark Fasheh592282c2007-01-02 17:59:40 -0800947 struct buffer_head *old_inode_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800948 struct buffer_head *insert_entry_bh = NULL;
949 struct ocfs2_super *osb = NULL;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700950 u64 newfe_blkno, old_de_ino;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700951 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800952 struct buffer_head *old_dir_bh = NULL;
953 struct buffer_head *new_dir_bh = NULL;
Mark Fasheh38760e22007-09-11 17:21:56 -0700954 struct ocfs2_dir_entry *old_inode_dot_dot_de = NULL, *old_de = NULL,
955 *new_de = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800956 struct buffer_head *new_de_bh = NULL, *old_de_bh = NULL; // bhs for above
957 struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir,
958 // this is the 1st dirent bh
Mark Fasheh592282c2007-01-02 17:59:40 -0800959 nlink_t old_dir_nlink = old_dir->i_nlink;
Sunil Mushran480214d2007-08-06 15:11:56 -0700960 struct ocfs2_dinode *old_di;
Mark Fashehccd979b2005-12-15 14:31:24 -0800961
962 /* At some point it might be nice to break this function up a
963 * bit. */
964
965 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
966 old_dir, old_dentry, new_dir, new_dentry,
967 old_dentry->d_name.len, old_dentry->d_name.name,
968 new_dentry->d_name.len, new_dentry->d_name.name);
969
970 osb = OCFS2_SB(old_dir->i_sb);
971
972 if (new_inode) {
973 if (!igrab(new_inode))
974 BUG();
975 }
976
Uwe Kleine-König1b3c3712007-02-17 19:23:03 +0100977 /* Assume a directory hierarchy thusly:
Mark Fashehccd979b2005-12-15 14:31:24 -0800978 * a/b/c
979 * a/d
980 * a,b,c, and d are all directories.
981 *
982 * from cwd of 'a' on both nodes:
983 * node1: mv b/c d
984 * node2: mv d b/c
985 *
986 * And that's why, just like the VFS, we need a file system
987 * rename lock. */
Jan Kara5dabd692008-02-21 18:00:00 +0100988 if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800989 status = ocfs2_rename_lock(osb);
990 if (status < 0) {
991 mlog_errno(status);
992 goto bail;
993 }
994 rename_lock = 1;
995 }
996
Mark Fashehccd979b2005-12-15 14:31:24 -0800997 /* if old and new are the same, this'll just do one lock. */
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700998 status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
999 &new_dir_bh, new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001000 if (status < 0) {
1001 mlog_errno(status);
1002 goto bail;
1003 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001004 parents_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001005
1006 /* make sure both dirs have bhs
1007 * get an extra ref on old_dir_bh if old==new */
1008 if (!new_dir_bh) {
1009 if (old_dir_bh) {
1010 new_dir_bh = old_dir_bh;
1011 get_bh(new_dir_bh);
1012 } else {
1013 mlog(ML_ERROR, "no old_dir_bh!\n");
1014 status = -EIO;
1015 goto bail;
1016 }
1017 }
1018
Mark Fasheh379dfe92006-09-08 14:21:03 -07001019 /*
Mark Fasheh592282c2007-01-02 17:59:40 -08001020 * Aside from allowing a meta data update, the locking here
Mark Fasheh34d024f2007-09-24 15:56:19 -07001021 * also ensures that the downconvert thread on other nodes
1022 * won't have to concurrently downconvert the inode and the
1023 * dentry locks.
Mark Fasheh379dfe92006-09-08 14:21:03 -07001024 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001025 status = ocfs2_inode_lock(old_inode, &old_inode_bh, 1);
Mark Fasheh379dfe92006-09-08 14:21:03 -07001026 if (status < 0) {
1027 if (status != -ENOENT)
Mark Fashehccd979b2005-12-15 14:31:24 -08001028 mlog_errno(status);
Mark Fasheh379dfe92006-09-08 14:21:03 -07001029 goto bail;
1030 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001031 old_child_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001032
Mark Fasheh379dfe92006-09-08 14:21:03 -07001033 status = ocfs2_remote_dentry_delete(old_dentry);
1034 if (status < 0) {
1035 mlog_errno(status);
1036 goto bail;
1037 }
1038
1039 if (S_ISDIR(old_inode->i_mode)) {
Mark Fasheh38760e22007-09-11 17:21:56 -07001040 u64 old_inode_parent;
Mark Fashehccd979b2005-12-15 14:31:24 -08001041
Mark Fasheh38760e22007-09-11 17:21:56 -07001042 status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent,
1043 old_inode, &old_inode_de_bh,
1044 &old_inode_dot_dot_de);
1045 if (status) {
1046 status = -EIO;
Mark Fashehccd979b2005-12-15 14:31:24 -08001047 goto bail;
Mark Fasheh38760e22007-09-11 17:21:56 -07001048 }
1049
1050 if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) {
1051 status = -EIO;
Mark Fashehccd979b2005-12-15 14:31:24 -08001052 goto bail;
Mark Fasheh38760e22007-09-11 17:21:56 -07001053 }
1054
1055 if (!new_inode && new_dir != old_dir &&
1056 new_dir->i_nlink >= OCFS2_LINK_MAX) {
1057 status = -EMLINK;
1058 goto bail;
1059 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001060 }
1061
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001062 status = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name,
1063 old_dentry->d_name.len,
1064 &old_de_ino);
1065 if (status) {
1066 status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -08001067 goto bail;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001068 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001069
1070 /*
1071 * Check for inode number is _not_ due to possible IO errors.
1072 * We might rmdir the source, keep it as pwd of some process
1073 * and merrily kill the link to whatever was created under the
1074 * same name. Goodbye sticky bit ;-<
1075 */
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001076 if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) {
1077 status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -08001078 goto bail;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001079 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001080
1081 /* check if the target already exists (in which case we need
1082 * to delete it */
1083 status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
1084 new_dentry->d_name.len,
1085 &newfe_blkno, new_dir, &new_de_bh,
1086 &new_de);
1087 /* The only error we allow here is -ENOENT because the new
1088 * file not existing is perfectly valid. */
1089 if ((status < 0) && (status != -ENOENT)) {
1090 /* If we cannot find the file specified we should just */
1091 /* return the error... */
1092 mlog_errno(status);
1093 goto bail;
1094 }
1095
Srinivas Eedae325a882007-10-31 16:49:43 -07001096 if (!new_de && new_inode) {
1097 /*
1098 * Target was unlinked by another node while we were
1099 * waiting to get to ocfs2_rename(). There isn't
1100 * anything we can do here to help the situation, so
1101 * bubble up the appropriate error.
1102 */
1103 status = -ENOENT;
1104 goto bail;
1105 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001106
1107 /* In case we need to overwrite an existing file, we blow it
1108 * away first */
1109 if (new_de) {
1110 /* VFS didn't think there existed an inode here, but
1111 * someone else in the cluster must have raced our
1112 * rename to create one. Today we error cleanly, in
1113 * the future we should consider calling iget to build
1114 * a new struct inode for this entry. */
1115 if (!new_inode) {
1116 status = -EACCES;
1117
1118 mlog(0, "We found an inode for name %.*s but VFS "
1119 "didn't give us one.\n", new_dentry->d_name.len,
1120 new_dentry->d_name.name);
1121 goto bail;
1122 }
1123
1124 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
1125 status = -EACCES;
1126
Mark Fashehb06970532006-03-03 10:24:33 -08001127 mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
1128 (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
1129 (unsigned long long)newfe_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001130 OCFS2_I(new_inode)->ip_flags);
1131 goto bail;
1132 }
1133
Mark Fashehe63aecb62007-10-18 15:30:42 -07001134 status = ocfs2_inode_lock(new_inode, &newfe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001135 if (status < 0) {
1136 if (status != -ENOENT)
1137 mlog_errno(status);
1138 goto bail;
1139 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001140 new_child_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001141
Mark Fasheh379dfe92006-09-08 14:21:03 -07001142 status = ocfs2_remote_dentry_delete(new_dentry);
Mark Fashehccd979b2005-12-15 14:31:24 -08001143 if (status < 0) {
1144 mlog_errno(status);
1145 goto bail;
1146 }
1147
1148 newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
1149
Mark Fashehb06970532006-03-03 10:24:33 -08001150 mlog(0, "aha rename over existing... new_de=%p new_blkno=%llu "
1151 "newfebh=%p bhblocknr=%llu\n", new_de,
1152 (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
Mark Fashehccd979b2005-12-15 14:31:24 -08001153 (unsigned long long)newfe_bh->b_blocknr : 0ULL);
1154
1155 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
Mark Fasheh5098c272006-10-05 18:12:57 -07001156 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
Mark Fashehccd979b2005-12-15 14:31:24 -08001157 new_inode,
1158 orphan_name,
1159 &orphan_entry_bh);
1160 if (status < 0) {
1161 mlog_errno(status);
1162 goto bail;
1163 }
1164 }
1165 } else {
1166 BUG_ON(new_dentry->d_parent->d_inode != new_dir);
1167
1168 status = ocfs2_check_dir_for_entry(new_dir,
1169 new_dentry->d_name.name,
1170 new_dentry->d_name.len);
1171 if (status)
1172 goto bail;
1173
1174 status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
1175 new_dentry->d_name.name,
1176 new_dentry->d_name.len,
1177 &insert_entry_bh);
1178 if (status < 0) {
1179 mlog_errno(status);
1180 goto bail;
1181 }
1182 }
1183
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001184 handle = ocfs2_start_trans(osb, OCFS2_RENAME_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001185 if (IS_ERR(handle)) {
1186 status = PTR_ERR(handle);
1187 handle = NULL;
1188 mlog_errno(status);
1189 goto bail;
1190 }
1191
1192 if (new_de) {
1193 if (S_ISDIR(new_inode->i_mode)) {
1194 if (!ocfs2_empty_dir(new_inode) ||
1195 new_inode->i_nlink != 2) {
1196 status = -ENOTEMPTY;
1197 goto bail;
1198 }
1199 }
1200 status = ocfs2_journal_access(handle, new_inode, newfe_bh,
1201 OCFS2_JOURNAL_ACCESS_WRITE);
1202 if (status < 0) {
1203 mlog_errno(status);
1204 goto bail;
1205 }
1206
1207 if (S_ISDIR(new_inode->i_mode) ||
1208 (newfe->i_links_count == cpu_to_le16(1))){
1209 status = ocfs2_orphan_add(osb, handle, new_inode,
1210 newfe, orphan_name,
Mark Fasheh5098c272006-10-05 18:12:57 -07001211 orphan_entry_bh, orphan_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001212 if (status < 0) {
1213 mlog_errno(status);
1214 goto bail;
1215 }
1216 }
1217
1218 /* change the dirent to point to the correct inode */
Mark Fasheh38760e22007-09-11 17:21:56 -07001219 status = ocfs2_update_entry(new_dir, handle, new_de_bh,
1220 new_de, old_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001221 if (status < 0) {
1222 mlog_errno(status);
1223 goto bail;
1224 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001225 new_dir->i_version++;
Mark Fashehccd979b2005-12-15 14:31:24 -08001226
1227 if (S_ISDIR(new_inode->i_mode))
1228 newfe->i_links_count = 0;
1229 else
1230 le16_add_cpu(&newfe->i_links_count, -1);
1231
1232 status = ocfs2_journal_dirty(handle, newfe_bh);
1233 if (status < 0) {
1234 mlog_errno(status);
1235 goto bail;
1236 }
1237 } else {
1238 /* if the name was not found in new_dir, add it now */
1239 status = ocfs2_add_entry(handle, new_dentry, old_inode,
1240 OCFS2_I(old_inode)->ip_blkno,
1241 new_dir_bh, insert_entry_bh);
1242 }
1243
1244 old_inode->i_ctime = CURRENT_TIME;
1245 mark_inode_dirty(old_inode);
Sunil Mushran480214d2007-08-06 15:11:56 -07001246
1247 status = ocfs2_journal_access(handle, old_inode, old_inode_bh,
1248 OCFS2_JOURNAL_ACCESS_WRITE);
1249 if (status >= 0) {
1250 old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
1251
1252 old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
1253 old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
1254
1255 status = ocfs2_journal_dirty(handle, old_inode_bh);
1256 if (status < 0)
1257 mlog_errno(status);
1258 } else
1259 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001260
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001261 /*
1262 * Now that the name has been added to new_dir, remove the old name.
1263 *
1264 * We don't keep any directory entry context around until now
1265 * because the insert might have changed the type of directory
1266 * we're dealing with.
1267 */
1268 old_de_bh = ocfs2_find_entry(old_dentry->d_name.name,
1269 old_dentry->d_name.len,
1270 old_dir, &old_de);
1271 if (!old_de_bh) {
1272 status = -EIO;
1273 goto bail;
1274 }
1275
Mark Fashehccd979b2005-12-15 14:31:24 -08001276 status = ocfs2_delete_entry(handle, old_dir, old_de, old_de_bh);
1277 if (status < 0) {
1278 mlog_errno(status);
1279 goto bail;
1280 }
1281
1282 if (new_inode) {
1283 new_inode->i_nlink--;
1284 new_inode->i_ctime = CURRENT_TIME;
1285 }
1286 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1287 if (old_inode_de_bh) {
Mark Fasheh38760e22007-09-11 17:21:56 -07001288 status = ocfs2_update_entry(old_inode, handle, old_inode_de_bh,
1289 old_inode_dot_dot_de, new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001290 old_dir->i_nlink--;
1291 if (new_inode) {
1292 new_inode->i_nlink--;
1293 } else {
Dave Hansend8c76e62006-09-30 23:29:04 -07001294 inc_nlink(new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001295 mark_inode_dirty(new_dir);
1296 }
1297 }
1298 mark_inode_dirty(old_dir);
Mark Fasheh592282c2007-01-02 17:59:40 -08001299 ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
1300 if (new_inode) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001301 mark_inode_dirty(new_inode);
Mark Fasheh592282c2007-01-02 17:59:40 -08001302 ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
1303 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001304
Mark Fasheh592282c2007-01-02 17:59:40 -08001305 if (old_dir != new_dir) {
1306 /* Keep the same times on both directories.*/
1307 new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
1308
1309 /*
1310 * This will also pick up the i_nlink change from the
1311 * block above.
1312 */
1313 ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
1314 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001315
1316 if (old_dir_nlink != old_dir->i_nlink) {
1317 if (!old_dir_bh) {
1318 mlog(ML_ERROR, "need to change nlink for old dir "
Mark Fashehb06970532006-03-03 10:24:33 -08001319 "%llu from %d to %d but bh is NULL!\n",
1320 (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
1321 (int)old_dir_nlink, old_dir->i_nlink);
Mark Fashehccd979b2005-12-15 14:31:24 -08001322 } else {
1323 struct ocfs2_dinode *fe;
1324 status = ocfs2_journal_access(handle, old_dir,
1325 old_dir_bh,
1326 OCFS2_JOURNAL_ACCESS_WRITE);
1327 fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
1328 fe->i_links_count = cpu_to_le16(old_dir->i_nlink);
1329 status = ocfs2_journal_dirty(handle, old_dir_bh);
1330 }
1331 }
1332
Mark Fasheh379dfe92006-09-08 14:21:03 -07001333 ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001334 status = 0;
1335bail:
1336 if (rename_lock)
1337 ocfs2_rename_unlock(osb);
1338
1339 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001340 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001341
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001342 if (parents_locked)
1343 ocfs2_double_unlock(old_dir, new_dir);
1344
1345 if (old_child_locked)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001346 ocfs2_inode_unlock(old_inode, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001347
1348 if (new_child_locked)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001349 ocfs2_inode_unlock(new_inode, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001350
Mark Fasheh5098c272006-10-05 18:12:57 -07001351 if (orphan_dir) {
1352 /* This was locked for us in ocfs2_prepare_orphan_dir() */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001353 ocfs2_inode_unlock(orphan_dir, 1);
Mark Fasheh5098c272006-10-05 18:12:57 -07001354 mutex_unlock(&orphan_dir->i_mutex);
1355 iput(orphan_dir);
1356 }
1357
Mark Fashehccd979b2005-12-15 14:31:24 -08001358 if (new_inode)
1359 sync_mapping_buffers(old_inode->i_mapping);
1360
1361 if (new_inode)
1362 iput(new_inode);
Mark Fasheha81cb882008-10-07 14:25:16 -07001363 brelse(newfe_bh);
1364 brelse(old_inode_bh);
1365 brelse(old_dir_bh);
1366 brelse(new_dir_bh);
1367 brelse(new_de_bh);
1368 brelse(old_de_bh);
1369 brelse(old_inode_de_bh);
1370 brelse(orphan_entry_bh);
1371 brelse(insert_entry_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001372
1373 mlog_exit(status);
1374
1375 return status;
1376}
1377
1378/*
1379 * we expect i_size = strlen(symname). Copy symname into the file
1380 * data, including the null terminator.
1381 */
1382static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001383 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001384 struct inode *inode,
1385 const char *symname)
1386{
1387 struct buffer_head **bhs = NULL;
1388 const char *c;
1389 struct super_block *sb = osb->sb;
Mark Fasheh4f902c32007-03-09 16:26:50 -08001390 u64 p_blkno, p_blocks;
Mark Fashehccd979b2005-12-15 14:31:24 -08001391 int virtual, blocks, status, i, bytes_left;
1392
1393 bytes_left = i_size_read(inode) + 1;
1394 /* we can't trust i_blocks because we're actually going to
1395 * write i_size + 1 bytes. */
1396 blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1397
Andrew Morton5515eff2006-03-26 01:37:53 -08001398 mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
1399 (unsigned long long)inode->i_blocks,
1400 i_size_read(inode), blocks);
Mark Fashehccd979b2005-12-15 14:31:24 -08001401
1402 /* Sanity check -- make sure we're going to fit. */
1403 if (bytes_left >
1404 ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1405 status = -EIO;
1406 mlog_errno(status);
1407 goto bail;
1408 }
1409
1410 bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
1411 if (!bhs) {
1412 status = -ENOMEM;
1413 mlog_errno(status);
1414 goto bail;
1415 }
1416
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001417 status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
1418 NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001419 if (status < 0) {
1420 mlog_errno(status);
1421 goto bail;
1422 }
1423
1424 /* links can never be larger than one cluster so we know this
1425 * is all going to be contiguous, but do a sanity check
1426 * anyway. */
1427 if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
1428 status = -EIO;
1429 mlog_errno(status);
1430 goto bail;
1431 }
1432
1433 virtual = 0;
1434 while(bytes_left > 0) {
1435 c = &symname[virtual * sb->s_blocksize];
1436
1437 bhs[virtual] = sb_getblk(sb, p_blkno);
1438 if (!bhs[virtual]) {
1439 status = -ENOMEM;
1440 mlog_errno(status);
1441 goto bail;
1442 }
1443 ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
1444
1445 status = ocfs2_journal_access(handle, inode, bhs[virtual],
1446 OCFS2_JOURNAL_ACCESS_CREATE);
1447 if (status < 0) {
1448 mlog_errno(status);
1449 goto bail;
1450 }
1451
1452 memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
1453
1454 memcpy(bhs[virtual]->b_data, c,
1455 (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
1456 bytes_left);
1457
1458 status = ocfs2_journal_dirty(handle, bhs[virtual]);
1459 if (status < 0) {
1460 mlog_errno(status);
1461 goto bail;
1462 }
1463
1464 virtual++;
1465 p_blkno++;
1466 bytes_left -= sb->s_blocksize;
1467 }
1468
1469 status = 0;
1470bail:
1471
1472 if (bhs) {
1473 for(i = 0; i < blocks; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001474 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001475 kfree(bhs);
1476 }
1477
1478 mlog_exit(status);
1479 return status;
1480}
1481
1482static int ocfs2_symlink(struct inode *dir,
1483 struct dentry *dentry,
1484 const char *symname)
1485{
1486 int status, l, credits;
1487 u64 newsize;
1488 struct ocfs2_super *osb = NULL;
1489 struct inode *inode = NULL;
1490 struct super_block *sb;
1491 struct buffer_head *new_fe_bh = NULL;
1492 struct buffer_head *de_bh = NULL;
1493 struct buffer_head *parent_fe_bh = NULL;
1494 struct ocfs2_dinode *fe = NULL;
1495 struct ocfs2_dinode *dirfe;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001496 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001497 struct ocfs2_alloc_context *inode_ac = NULL;
1498 struct ocfs2_alloc_context *data_ac = NULL;
1499
1500 mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
1501 dentry, symname, dentry->d_name.len, dentry->d_name.name);
1502
1503 sb = dir->i_sb;
1504 osb = OCFS2_SB(sb);
1505
1506 l = strlen(symname) + 1;
1507
1508 credits = ocfs2_calc_symlink_credits(sb);
1509
Mark Fashehccd979b2005-12-15 14:31:24 -08001510 /* lock the parent directory */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001511 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001512 if (status < 0) {
1513 if (status != -ENOENT)
1514 mlog_errno(status);
Mark Fasheh6d8fc402006-10-06 11:54:33 -07001515 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001516 }
1517
1518 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1519 if (!dirfe->i_links_count) {
1520 /* can't make a file in a deleted directory. */
1521 status = -ENOENT;
1522 goto bail;
1523 }
1524
1525 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
1526 dentry->d_name.len);
1527 if (status)
1528 goto bail;
1529
1530 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
1531 dentry->d_name.name,
1532 dentry->d_name.len, &de_bh);
1533 if (status < 0) {
1534 mlog_errno(status);
1535 goto bail;
1536 }
1537
Mark Fashehda5cbf22006-10-06 18:34:35 -07001538 status = ocfs2_reserve_new_inode(osb, &inode_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001539 if (status < 0) {
1540 if (status != -ENOSPC)
1541 mlog_errno(status);
1542 goto bail;
1543 }
1544
1545 /* don't reserve bitmap space for fast symlinks. */
1546 if (l > ocfs2_fast_symlink_chars(sb)) {
Mark Fashehda5cbf22006-10-06 18:34:35 -07001547 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001548 if (status < 0) {
1549 if (status != -ENOSPC)
1550 mlog_errno(status);
1551 goto bail;
1552 }
1553 }
1554
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001555 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001556 if (IS_ERR(handle)) {
1557 status = PTR_ERR(handle);
1558 handle = NULL;
1559 mlog_errno(status);
1560 goto bail;
1561 }
1562
1563 status = ocfs2_mknod_locked(osb, dir, dentry,
1564 S_IFLNK | S_IRWXUGO, 0,
1565 &new_fe_bh, parent_fe_bh, handle,
1566 &inode, inode_ac);
1567 if (status < 0) {
1568 mlog_errno(status);
1569 goto bail;
1570 }
1571
1572 fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
1573 inode->i_rdev = 0;
1574 newsize = l - 1;
1575 if (l > ocfs2_fast_symlink_chars(sb)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08001576 u32 offset = 0;
1577
Mark Fashehccd979b2005-12-15 14:31:24 -08001578 inode->i_op = &ocfs2_symlink_inode_operations;
Tao Ma0eb8d472008-08-18 17:38:45 +08001579 status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0,
1580 new_fe_bh,
1581 handle, data_ac, NULL,
1582 NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001583 if (status < 0) {
1584 if (status != -ENOSPC && status != -EINTR) {
Mark Fashehb06970532006-03-03 10:24:33 -08001585 mlog(ML_ERROR,
1586 "Failed to extend file to %llu\n",
1587 (unsigned long long)newsize);
Mark Fashehccd979b2005-12-15 14:31:24 -08001588 mlog_errno(status);
1589 status = -ENOSPC;
1590 }
1591 goto bail;
1592 }
1593 i_size_write(inode, newsize);
Mark Fasheh8110b072007-03-22 16:53:23 -07001594 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001595 } else {
1596 inode->i_op = &ocfs2_fast_symlink_inode_operations;
1597 memcpy((char *) fe->id2.i_symlink, symname, l);
1598 i_size_write(inode, newsize);
1599 inode->i_blocks = 0;
1600 }
1601
1602 status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
1603 if (status < 0) {
1604 mlog_errno(status);
1605 goto bail;
1606 }
1607
1608 if (!ocfs2_inode_is_fast_symlink(inode)) {
1609 status = ocfs2_create_symlink_data(osb, handle, inode,
1610 symname);
1611 if (status < 0) {
1612 mlog_errno(status);
1613 goto bail;
1614 }
1615 }
1616
1617 status = ocfs2_add_entry(handle, dentry, inode,
1618 le64_to_cpu(fe->i_blkno), parent_fe_bh,
1619 de_bh);
1620 if (status < 0) {
1621 mlog_errno(status);
1622 goto bail;
1623 }
1624
Mark Fasheh0027dd52006-09-21 16:51:28 -07001625 status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
Mark Fasheh379dfe92006-09-08 14:21:03 -07001626 if (status) {
1627 mlog_errno(status);
1628 goto bail;
1629 }
1630
Mark Fashehccd979b2005-12-15 14:31:24 -08001631 insert_inode_hash(inode);
1632 dentry->d_op = &ocfs2_dentry_ops;
1633 d_instantiate(dentry, inode);
1634bail:
1635 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001636 ocfs2_commit_trans(osb, handle);
Mark Fasheh6d8fc402006-10-06 11:54:33 -07001637
Mark Fashehe63aecb62007-10-18 15:30:42 -07001638 ocfs2_inode_unlock(dir, 1);
Mark Fasheh6d8fc402006-10-06 11:54:33 -07001639
Mark Fasheha81cb882008-10-07 14:25:16 -07001640 brelse(new_fe_bh);
1641 brelse(parent_fe_bh);
1642 brelse(de_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001643 if (inode_ac)
1644 ocfs2_free_alloc_context(inode_ac);
1645 if (data_ac)
1646 ocfs2_free_alloc_context(data_ac);
1647 if ((status < 0) && inode)
1648 iput(inode);
1649
1650 mlog_exit(status);
1651
1652 return status;
1653}
1654
Mark Fashehccd979b2005-12-15 14:31:24 -08001655static int ocfs2_blkno_stringify(u64 blkno, char *name)
1656{
1657 int status, namelen;
1658
1659 mlog_entry_void();
1660
Mark Fashehb06970532006-03-03 10:24:33 -08001661 namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
1662 (long long)blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001663 if (namelen <= 0) {
1664 if (namelen)
1665 status = namelen;
1666 else
1667 status = -EINVAL;
1668 mlog_errno(status);
1669 goto bail;
1670 }
1671 if (namelen != OCFS2_ORPHAN_NAMELEN) {
1672 status = -EINVAL;
1673 mlog_errno(status);
1674 goto bail;
1675 }
1676
1677 mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
1678 namelen);
1679
1680 status = 0;
1681bail:
1682 mlog_exit(status);
1683 return status;
1684}
1685
1686static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
Mark Fasheh5098c272006-10-05 18:12:57 -07001687 struct inode **ret_orphan_dir,
Mark Fashehccd979b2005-12-15 14:31:24 -08001688 struct inode *inode,
1689 char *name,
1690 struct buffer_head **de_bh)
1691{
Mark Fasheh5098c272006-10-05 18:12:57 -07001692 struct inode *orphan_dir_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -08001693 struct buffer_head *orphan_dir_bh = NULL;
1694 int status = 0;
1695
1696 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
1697 if (status < 0) {
1698 mlog_errno(status);
Mark Fasheh5098c272006-10-05 18:12:57 -07001699 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001700 }
1701
1702 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1703 ORPHAN_DIR_SYSTEM_INODE,
1704 osb->slot_num);
1705 if (!orphan_dir_inode) {
1706 status = -ENOENT;
1707 mlog_errno(status);
Mark Fasheh5098c272006-10-05 18:12:57 -07001708 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001709 }
1710
Mark Fasheh5098c272006-10-05 18:12:57 -07001711 mutex_lock(&orphan_dir_inode->i_mutex);
1712
Mark Fashehe63aecb62007-10-18 15:30:42 -07001713 status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001714 if (status < 0) {
1715 mlog_errno(status);
1716 goto leave;
1717 }
1718
1719 status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
1720 orphan_dir_bh, name,
1721 OCFS2_ORPHAN_NAMELEN, de_bh);
1722 if (status < 0) {
Mark Fashehe63aecb62007-10-18 15:30:42 -07001723 ocfs2_inode_unlock(orphan_dir_inode, 1);
Mark Fasheh5098c272006-10-05 18:12:57 -07001724
Mark Fashehccd979b2005-12-15 14:31:24 -08001725 mlog_errno(status);
1726 goto leave;
1727 }
1728
Mark Fasheh5098c272006-10-05 18:12:57 -07001729 *ret_orphan_dir = orphan_dir_inode;
1730
Mark Fashehccd979b2005-12-15 14:31:24 -08001731leave:
Mark Fasheh5098c272006-10-05 18:12:57 -07001732 if (status) {
1733 mutex_unlock(&orphan_dir_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001734 iput(orphan_dir_inode);
Mark Fasheh5098c272006-10-05 18:12:57 -07001735 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001736
Mark Fasheha81cb882008-10-07 14:25:16 -07001737 brelse(orphan_dir_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001738
1739 mlog_exit(status);
1740 return status;
1741}
1742
1743static int ocfs2_orphan_add(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001744 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001745 struct inode *inode,
1746 struct ocfs2_dinode *fe,
1747 char *name,
Mark Fasheh5098c272006-10-05 18:12:57 -07001748 struct buffer_head *de_bh,
1749 struct inode *orphan_dir_inode)
Mark Fashehccd979b2005-12-15 14:31:24 -08001750{
Mark Fashehccd979b2005-12-15 14:31:24 -08001751 struct buffer_head *orphan_dir_bh = NULL;
1752 int status = 0;
1753 struct ocfs2_dinode *orphan_fe;
1754
1755 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
1756
Joel Becker31d33072008-10-09 17:20:30 -07001757 status = ocfs2_read_block(orphan_dir_inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08001758 OCFS2_I(orphan_dir_inode)->ip_blkno,
Joel Becker0fcaa56a2008-10-09 17:20:31 -07001759 &orphan_dir_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001760 if (status < 0) {
1761 mlog_errno(status);
1762 goto leave;
1763 }
1764
1765 status = ocfs2_journal_access(handle, orphan_dir_inode, orphan_dir_bh,
1766 OCFS2_JOURNAL_ACCESS_WRITE);
1767 if (status < 0) {
1768 mlog_errno(status);
1769 goto leave;
1770 }
1771
1772 /* we're a cluster, and nlink can change on disk from
1773 * underneath us... */
1774 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
1775 if (S_ISDIR(inode->i_mode))
1776 le16_add_cpu(&orphan_fe->i_links_count, 1);
1777 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
1778
1779 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
1780 if (status < 0) {
1781 mlog_errno(status);
1782 goto leave;
1783 }
1784
1785 status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
1786 OCFS2_ORPHAN_NAMELEN, inode,
1787 OCFS2_I(inode)->ip_blkno,
1788 orphan_dir_bh, de_bh);
1789 if (status < 0) {
1790 mlog_errno(status);
1791 goto leave;
1792 }
1793
1794 le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
1795
1796 /* Record which orphan dir our inode now resides
1797 * in. delete_inode will use this to determine which orphan
1798 * dir to lock. */
Tiger Yang50008632007-03-20 16:01:38 -07001799 fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001800
Mark Fashehb06970532006-03-03 10:24:33 -08001801 mlog(0, "Inode %llu orphaned in slot %d\n",
1802 (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001803
1804leave:
Mark Fasheha81cb882008-10-07 14:25:16 -07001805 brelse(orphan_dir_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001806
1807 mlog_exit(status);
1808 return status;
1809}
1810
1811/* unlike orphan_add, we expect the orphan dir to already be locked here. */
1812int ocfs2_orphan_del(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001813 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001814 struct inode *orphan_dir_inode,
1815 struct inode *inode,
1816 struct buffer_head *orphan_dir_bh)
1817{
1818 char name[OCFS2_ORPHAN_NAMELEN + 1];
1819 struct ocfs2_dinode *orphan_fe;
1820 int status = 0;
1821 struct buffer_head *target_de_bh = NULL;
1822 struct ocfs2_dir_entry *target_de = NULL;
1823
1824 mlog_entry_void();
1825
1826 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
1827 if (status < 0) {
1828 mlog_errno(status);
1829 goto leave;
1830 }
1831
Mark Fashehb06970532006-03-03 10:24:33 -08001832 mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
1833 name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
1834 OCFS2_ORPHAN_NAMELEN);
Mark Fashehccd979b2005-12-15 14:31:24 -08001835
1836 /* find it's spot in the orphan directory */
1837 target_de_bh = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN,
1838 orphan_dir_inode, &target_de);
1839 if (!target_de_bh) {
1840 status = -ENOENT;
1841 mlog_errno(status);
1842 goto leave;
1843 }
1844
1845 /* remove it from the orphan directory */
1846 status = ocfs2_delete_entry(handle, orphan_dir_inode, target_de,
1847 target_de_bh);
1848 if (status < 0) {
1849 mlog_errno(status);
1850 goto leave;
1851 }
1852
1853 status = ocfs2_journal_access(handle,orphan_dir_inode, orphan_dir_bh,
1854 OCFS2_JOURNAL_ACCESS_WRITE);
1855 if (status < 0) {
1856 mlog_errno(status);
1857 goto leave;
1858 }
1859
1860 /* do the i_nlink dance! :) */
1861 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
1862 if (S_ISDIR(inode->i_mode))
1863 le16_add_cpu(&orphan_fe->i_links_count, -1);
1864 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
1865
1866 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
1867 if (status < 0) {
1868 mlog_errno(status);
1869 goto leave;
1870 }
1871
1872leave:
Mark Fasheha81cb882008-10-07 14:25:16 -07001873 brelse(target_de_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001874
1875 mlog_exit(status);
1876 return status;
1877}
1878
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001879const struct inode_operations ocfs2_dir_iops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08001880 .create = ocfs2_create,
1881 .lookup = ocfs2_lookup,
1882 .link = ocfs2_link,
1883 .unlink = ocfs2_unlink,
1884 .rmdir = ocfs2_unlink,
1885 .symlink = ocfs2_symlink,
1886 .mkdir = ocfs2_mkdir,
1887 .mknod = ocfs2_mknod,
1888 .rename = ocfs2_rename,
1889 .setattr = ocfs2_setattr,
1890 .getattr = ocfs2_getattr,
Tiger Yangd38eb8d2006-11-27 09:59:21 +08001891 .permission = ocfs2_permission,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001892 .setxattr = generic_setxattr,
1893 .getxattr = generic_getxattr,
1894 .listxattr = ocfs2_listxattr,
1895 .removexattr = generic_removexattr,
Mark Fashehccd979b2005-12-15 14:31:24 -08001896};