blob: 76d1d13143088a62fe497a8a392c22b80eb23a17 [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 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
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
331 if (new_fe_bh)
332 brelse(new_fe_bh);
333
334 if (de_bh)
335 brelse(de_bh);
336
337 if (parent_fe_bh)
338 brelse(parent_fe_bh);
339
340 if ((status < 0) && inode)
341 iput(inode);
342
343 if (inode_ac)
344 ocfs2_free_alloc_context(inode_ac);
345
346 if (data_ac)
347 ocfs2_free_alloc_context(data_ac);
348
349 mlog_exit(status);
350
351 return status;
352}
353
354static int ocfs2_mknod_locked(struct ocfs2_super *osb,
355 struct inode *dir,
356 struct dentry *dentry, int mode,
357 dev_t dev,
358 struct buffer_head **new_fe_bh,
359 struct buffer_head *parent_fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700360 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800361 struct inode **ret_inode,
362 struct ocfs2_alloc_context *inode_ac)
363{
364 int status = 0;
365 struct ocfs2_dinode *fe = NULL;
366 struct ocfs2_extent_list *fel;
367 u64 fe_blkno = 0;
368 u16 suballoc_bit;
369 struct inode *inode = NULL;
370
371 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
372 (unsigned long)dev, dentry->d_name.len,
373 dentry->d_name.name);
374
375 *new_fe_bh = NULL;
376 *ret_inode = NULL;
377
378 status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit,
379 &fe_blkno);
380 if (status < 0) {
381 mlog_errno(status);
382 goto leave;
383 }
384
385 inode = new_inode(dir->i_sb);
386 if (IS_ERR(inode)) {
387 status = PTR_ERR(inode);
388 mlog(ML_ERROR, "new_inode failed!\n");
389 goto leave;
390 }
391
392 /* populate as many fields early on as possible - many of
393 * these are used by the support functions here and in
394 * callers. */
395 inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
396 OCFS2_I(inode)->ip_blkno = fe_blkno;
397 if (S_ISDIR(mode))
398 inode->i_nlink = 2;
399 else
400 inode->i_nlink = 1;
401 inode->i_mode = mode;
402 spin_lock(&osb->osb_lock);
403 inode->i_generation = osb->s_next_generation++;
404 spin_unlock(&osb->osb_lock);
405
406 *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
407 if (!*new_fe_bh) {
408 status = -EIO;
409 mlog_errno(status);
410 goto leave;
411 }
412 ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh);
413
414 status = ocfs2_journal_access(handle, inode, *new_fe_bh,
415 OCFS2_JOURNAL_ACCESS_CREATE);
416 if (status < 0) {
417 mlog_errno(status);
418 goto leave;
419 }
420
421 fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
422 memset(fe, 0, osb->sb->s_blocksize);
423
424 fe->i_generation = cpu_to_le32(inode->i_generation);
425 fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
426 fe->i_blkno = cpu_to_le64(fe_blkno);
427 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800428 fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot);
Mark Fashehccd979b2005-12-15 14:31:24 -0800429 fe->i_uid = cpu_to_le32(current->fsuid);
430 if (dir->i_mode & S_ISGID) {
431 fe->i_gid = cpu_to_le32(dir->i_gid);
432 if (S_ISDIR(mode))
433 mode |= S_ISGID;
434 } else
435 fe->i_gid = cpu_to_le32(current->fsgid);
436 fe->i_mode = cpu_to_le16(mode);
437 if (S_ISCHR(mode) || S_ISBLK(mode))
438 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
439
440 fe->i_links_count = cpu_to_le16(inode->i_nlink);
441
442 fe->i_last_eb_blk = 0;
443 strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
444 le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
445 fe->i_atime = fe->i_ctime = fe->i_mtime =
446 cpu_to_le64(CURRENT_TIME.tv_sec);
447 fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
448 cpu_to_le32(CURRENT_TIME.tv_nsec);
449 fe->i_dtime = 0;
450
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700451 /*
452 * If supported, directories start with inline data.
453 */
454 if (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) {
455 u16 feat = le16_to_cpu(fe->i_dyn_features);
456
457 fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL);
458
459 fe->id2.i_data.id_count = cpu_to_le16(ocfs2_max_inline_data(osb->sb));
460 } else {
461 fel = &fe->id2.i_list;
462 fel->l_tree_depth = 0;
463 fel->l_next_free_rec = 0;
464 fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
465 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800466
467 status = ocfs2_journal_dirty(handle, *new_fe_bh);
468 if (status < 0) {
469 mlog_errno(status);
470 goto leave;
471 }
472
473 if (ocfs2_populate_inode(inode, fe, 1) < 0) {
474 mlog(ML_ERROR, "populate inode failed! bh->b_blocknr=%llu, "
Mark Fashehb0697052006-03-03 10:24:33 -0800475 "i_blkno=%llu, i_ino=%lu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700476 (unsigned long long)(*new_fe_bh)->b_blocknr,
477 (unsigned long long)le64_to_cpu(fe->i_blkno),
478 inode->i_ino);
Mark Fashehccd979b2005-12-15 14:31:24 -0800479 BUG();
480 }
481
482 ocfs2_inode_set_new(osb, inode);
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800483 if (!ocfs2_mount_local(osb)) {
484 status = ocfs2_create_new_inode_locks(inode);
485 if (status < 0)
486 mlog_errno(status);
487 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800488
489 status = 0; /* error in ocfs2_create_new_inode_locks is not
490 * critical */
491
492 *ret_inode = inode;
493leave:
494 if (status < 0) {
495 if (*new_fe_bh) {
496 brelse(*new_fe_bh);
497 *new_fe_bh = NULL;
498 }
499 if (inode)
500 iput(inode);
501 }
502
503 mlog_exit(status);
504 return status;
505}
506
507static int ocfs2_mkdir(struct inode *dir,
508 struct dentry *dentry,
509 int mode)
510{
511 int ret;
512
513 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
514 dentry->d_name.len, dentry->d_name.name);
515 ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
516 mlog_exit(ret);
517
518 return ret;
519}
520
521static int ocfs2_create(struct inode *dir,
522 struct dentry *dentry,
523 int mode,
524 struct nameidata *nd)
525{
526 int ret;
527
528 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
529 dentry->d_name.len, dentry->d_name.name);
530 ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
531 mlog_exit(ret);
532
533 return ret;
534}
535
536static int ocfs2_link(struct dentry *old_dentry,
537 struct inode *dir,
538 struct dentry *dentry)
539{
Mark Fasheh1fabe142006-10-09 18:11:45 -0700540 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800541 struct inode *inode = old_dentry->d_inode;
542 int err;
543 struct buffer_head *fe_bh = NULL;
544 struct buffer_head *parent_fe_bh = NULL;
545 struct buffer_head *de_bh = NULL;
546 struct ocfs2_dinode *fe = NULL;
547 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
548
549 mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
550 old_dentry->d_name.len, old_dentry->d_name.name,
551 dentry->d_name.len, dentry->d_name.name);
552
Mark Fasheh123a9642006-10-05 16:48:23 -0700553 if (S_ISDIR(inode->i_mode))
554 return -EPERM;
Mark Fashehccd979b2005-12-15 14:31:24 -0800555
Mark Fashehe63aecb62007-10-18 15:30:42 -0700556 err = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800557 if (err < 0) {
558 if (err != -ENOENT)
559 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700560 return err;
Mark Fashehccd979b2005-12-15 14:31:24 -0800561 }
562
Tiger Yang0f62de22006-08-31 20:39:47 -0700563 if (!dir->i_nlink) {
564 err = -ENOENT;
Mark Fasheh123a9642006-10-05 16:48:23 -0700565 goto out;
Tiger Yang0f62de22006-08-31 20:39:47 -0700566 }
567
Mark Fashehccd979b2005-12-15 14:31:24 -0800568 err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
569 dentry->d_name.len);
570 if (err)
Mark Fasheh123a9642006-10-05 16:48:23 -0700571 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800572
573 err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
574 dentry->d_name.name,
575 dentry->d_name.len, &de_bh);
576 if (err < 0) {
577 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700578 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800579 }
580
Mark Fashehe63aecb62007-10-18 15:30:42 -0700581 err = ocfs2_inode_lock(inode, &fe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800582 if (err < 0) {
583 if (err != -ENOENT)
584 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700585 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800586 }
587
588 fe = (struct ocfs2_dinode *) fe_bh->b_data;
589 if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) {
590 err = -EMLINK;
Mark Fasheh123a9642006-10-05 16:48:23 -0700591 goto out_unlock_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800592 }
593
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700594 handle = ocfs2_start_trans(osb, OCFS2_LINK_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800595 if (IS_ERR(handle)) {
596 err = PTR_ERR(handle);
597 handle = NULL;
598 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700599 goto out_unlock_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800600 }
601
602 err = ocfs2_journal_access(handle, inode, fe_bh,
603 OCFS2_JOURNAL_ACCESS_WRITE);
604 if (err < 0) {
605 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700606 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800607 }
608
Dave Hansend8c76e62006-09-30 23:29:04 -0700609 inc_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800610 inode->i_ctime = CURRENT_TIME;
611 fe->i_links_count = cpu_to_le16(inode->i_nlink);
612 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
613 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
614
615 err = ocfs2_journal_dirty(handle, fe_bh);
616 if (err < 0) {
617 le16_add_cpu(&fe->i_links_count, -1);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700618 drop_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800619 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700620 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800621 }
622
623 err = ocfs2_add_entry(handle, dentry, inode,
624 OCFS2_I(inode)->ip_blkno,
625 parent_fe_bh, de_bh);
626 if (err) {
627 le16_add_cpu(&fe->i_links_count, -1);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700628 drop_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800629 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700630 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800631 }
632
Mark Fasheh0027dd52006-09-21 16:51:28 -0700633 err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
Mark Fasheh379dfe92006-09-08 14:21:03 -0700634 if (err) {
635 mlog_errno(err);
Mark Fasheh123a9642006-10-05 16:48:23 -0700636 goto out_commit;
Mark Fasheh379dfe92006-09-08 14:21:03 -0700637 }
638
Mark Fashehccd979b2005-12-15 14:31:24 -0800639 atomic_inc(&inode->i_count);
640 dentry->d_op = &ocfs2_dentry_ops;
641 d_instantiate(dentry, inode);
Mark Fasheh123a9642006-10-05 16:48:23 -0700642
643out_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700644 ocfs2_commit_trans(osb, handle);
Mark Fasheh123a9642006-10-05 16:48:23 -0700645out_unlock_inode:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700646 ocfs2_inode_unlock(inode, 1);
Mark Fasheh123a9642006-10-05 16:48:23 -0700647
648out:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700649 ocfs2_inode_unlock(dir, 1);
Mark Fasheh123a9642006-10-05 16:48:23 -0700650
Mark Fashehccd979b2005-12-15 14:31:24 -0800651 if (de_bh)
652 brelse(de_bh);
653 if (fe_bh)
654 brelse(fe_bh);
655 if (parent_fe_bh)
656 brelse(parent_fe_bh);
657
658 mlog_exit(err);
659
660 return err;
661}
662
Mark Fasheh379dfe92006-09-08 14:21:03 -0700663/*
664 * Takes and drops an exclusive lock on the given dentry. This will
665 * force other nodes to drop it.
666 */
667static int ocfs2_remote_dentry_delete(struct dentry *dentry)
668{
669 int ret;
670
671 ret = ocfs2_dentry_lock(dentry, 1);
672 if (ret)
673 mlog_errno(ret);
674 else
675 ocfs2_dentry_unlock(dentry, 1);
676
677 return ret;
678}
679
Mark Fasheh17ff7852006-09-30 23:29:05 -0700680static inline int inode_is_unlinkable(struct inode *inode)
681{
682 if (S_ISDIR(inode->i_mode)) {
683 if (inode->i_nlink == 2)
684 return 1;
685 return 0;
686 }
687
688 if (inode->i_nlink == 1)
689 return 1;
690 return 0;
691}
692
Mark Fashehccd979b2005-12-15 14:31:24 -0800693static int ocfs2_unlink(struct inode *dir,
694 struct dentry *dentry)
695{
696 int status;
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700697 int child_locked = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800698 struct inode *inode = dentry->d_inode;
Mark Fasheh5098c272006-10-05 18:12:57 -0700699 struct inode *orphan_dir = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800700 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
701 u64 blkno;
702 struct ocfs2_dinode *fe = NULL;
703 struct buffer_head *fe_bh = NULL;
704 struct buffer_head *parent_node_bh = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700705 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800706 struct ocfs2_dir_entry *dirent = NULL;
707 struct buffer_head *dirent_bh = NULL;
708 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
709 struct buffer_head *orphan_entry_bh = NULL;
710
711 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
712 dentry->d_name.len, dentry->d_name.name);
713
714 BUG_ON(dentry->d_parent->d_inode != dir);
715
Mark Fashehb0697052006-03-03 10:24:33 -0800716 mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800717
718 if (inode == osb->root_inode) {
719 mlog(0, "Cannot delete the root directory\n");
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700720 return -EPERM;
Mark Fashehccd979b2005-12-15 14:31:24 -0800721 }
722
Mark Fashehe63aecb62007-10-18 15:30:42 -0700723 status = ocfs2_inode_lock(dir, &parent_node_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800724 if (status < 0) {
725 if (status != -ENOENT)
726 mlog_errno(status);
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700727 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -0800728 }
729
730 status = ocfs2_find_files_on_disk(dentry->d_name.name,
731 dentry->d_name.len, &blkno,
732 dir, &dirent_bh, &dirent);
733 if (status < 0) {
734 if (status != -ENOENT)
735 mlog_errno(status);
736 goto leave;
737 }
738
739 if (OCFS2_I(inode)->ip_blkno != blkno) {
740 status = -ENOENT;
741
Mark Fashehb0697052006-03-03 10:24:33 -0800742 mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
743 (unsigned long long)OCFS2_I(inode)->ip_blkno,
744 (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800745 goto leave;
746 }
747
Mark Fashehe63aecb62007-10-18 15:30:42 -0700748 status = ocfs2_inode_lock(inode, &fe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800749 if (status < 0) {
750 if (status != -ENOENT)
751 mlog_errno(status);
752 goto leave;
753 }
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700754 child_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800755
756 if (S_ISDIR(inode->i_mode)) {
757 if (!ocfs2_empty_dir(inode)) {
758 status = -ENOTEMPTY;
759 goto leave;
760 } else if (inode->i_nlink != 2) {
761 status = -ENOTEMPTY;
762 goto leave;
763 }
764 }
765
Mark Fasheh379dfe92006-09-08 14:21:03 -0700766 status = ocfs2_remote_dentry_delete(dentry);
Mark Fashehccd979b2005-12-15 14:31:24 -0800767 if (status < 0) {
Mark Fasheh34d024f2007-09-24 15:56:19 -0700768 /* This remote delete should succeed under all normal
Mark Fashehccd979b2005-12-15 14:31:24 -0800769 * circumstances. */
770 mlog_errno(status);
771 goto leave;
772 }
773
Mark Fasheh17ff7852006-09-30 23:29:05 -0700774 if (inode_is_unlinkable(inode)) {
Mark Fasheh5098c272006-10-05 18:12:57 -0700775 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800776 orphan_name,
777 &orphan_entry_bh);
778 if (status < 0) {
779 mlog_errno(status);
780 goto leave;
781 }
782 }
783
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700784 handle = ocfs2_start_trans(osb, OCFS2_UNLINK_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800785 if (IS_ERR(handle)) {
786 status = PTR_ERR(handle);
787 handle = NULL;
788 mlog_errno(status);
789 goto leave;
790 }
791
792 status = ocfs2_journal_access(handle, inode, fe_bh,
793 OCFS2_JOURNAL_ACCESS_WRITE);
794 if (status < 0) {
795 mlog_errno(status);
796 goto leave;
797 }
798
799 fe = (struct ocfs2_dinode *) fe_bh->b_data;
800
Mark Fasheh17ff7852006-09-30 23:29:05 -0700801 if (inode_is_unlinkable(inode)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800802 status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
Mark Fasheh5098c272006-10-05 18:12:57 -0700803 orphan_entry_bh, orphan_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -0800804 if (status < 0) {
805 mlog_errno(status);
806 goto leave;
807 }
808 }
809
810 /* delete the name from the parent dir */
811 status = ocfs2_delete_entry(handle, dir, dirent, dirent_bh);
812 if (status < 0) {
813 mlog_errno(status);
814 goto leave;
815 }
816
Mark Fasheh17ff7852006-09-30 23:29:05 -0700817 if (S_ISDIR(inode->i_mode))
818 drop_nlink(inode);
819 drop_nlink(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800820 fe->i_links_count = cpu_to_le16(inode->i_nlink);
Mark Fashehccd979b2005-12-15 14:31:24 -0800821
822 status = ocfs2_journal_dirty(handle, fe_bh);
823 if (status < 0) {
824 mlog_errno(status);
825 goto leave;
826 }
827
Mark Fasheh592282c2007-01-02 17:59:40 -0800828 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
829 if (S_ISDIR(inode->i_mode))
Mark Fasheh17ff7852006-09-30 23:29:05 -0700830 drop_nlink(dir);
Mark Fasheh592282c2007-01-02 17:59:40 -0800831
832 status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
833 if (status < 0) {
834 mlog_errno(status);
835 if (S_ISDIR(inode->i_mode))
Dave Hansend8c76e62006-09-30 23:29:04 -0700836 inc_nlink(dir);
Mark Fashehccd979b2005-12-15 14:31:24 -0800837 }
838
839leave:
Mark Fashehccd979b2005-12-15 14:31:24 -0800840 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700841 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800842
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700843 if (child_locked)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700844 ocfs2_inode_unlock(inode, 1);
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700845
Mark Fashehe63aecb62007-10-18 15:30:42 -0700846 ocfs2_inode_unlock(dir, 1);
Mark Fasheh30a4f5e2006-10-06 11:49:45 -0700847
Mark Fasheh5098c272006-10-05 18:12:57 -0700848 if (orphan_dir) {
849 /* This was locked for us in ocfs2_prepare_orphan_dir() */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700850 ocfs2_inode_unlock(orphan_dir, 1);
Mark Fasheh5098c272006-10-05 18:12:57 -0700851 mutex_unlock(&orphan_dir->i_mutex);
852 iput(orphan_dir);
853 }
854
Mark Fashehccd979b2005-12-15 14:31:24 -0800855 if (fe_bh)
856 brelse(fe_bh);
857
858 if (dirent_bh)
859 brelse(dirent_bh);
860
861 if (parent_node_bh)
862 brelse(parent_node_bh);
863
864 if (orphan_entry_bh)
865 brelse(orphan_entry_bh);
866
867 mlog_exit(status);
868
869 return status;
870}
871
872/*
873 * The only place this should be used is rename!
874 * if they have the same id, then the 1st one is the only one locked.
875 */
876static int ocfs2_double_lock(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800877 struct buffer_head **bh1,
878 struct inode *inode1,
879 struct buffer_head **bh2,
880 struct inode *inode2)
881{
882 int status;
883 struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
884 struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
885 struct buffer_head **tmpbh;
886 struct inode *tmpinode;
887
Mark Fashehb0697052006-03-03 10:24:33 -0800888 mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
889 (unsigned long long)oi1->ip_blkno,
890 (unsigned long long)oi2->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800891
Mark Fashehccd979b2005-12-15 14:31:24 -0800892 if (*bh1)
893 *bh1 = NULL;
894 if (*bh2)
895 *bh2 = NULL;
896
897 /* we always want to lock the one with the lower lockid first. */
898 if (oi1->ip_blkno != oi2->ip_blkno) {
899 if (oi1->ip_blkno < oi2->ip_blkno) {
900 /* switch id1 and id2 around */
901 mlog(0, "switching them around...\n");
902 tmpbh = bh2;
903 bh2 = bh1;
904 bh1 = tmpbh;
905
906 tmpinode = inode2;
907 inode2 = inode1;
908 inode1 = tmpinode;
909 }
910 /* lock id2 */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700911 status = ocfs2_inode_lock(inode2, bh2, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800912 if (status < 0) {
913 if (status != -ENOENT)
914 mlog_errno(status);
915 goto bail;
916 }
917 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700918
Mark Fashehccd979b2005-12-15 14:31:24 -0800919 /* lock id1 */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700920 status = ocfs2_inode_lock(inode1, bh1, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800921 if (status < 0) {
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700922 /*
923 * An error return must mean that no cluster locks
924 * were held on function exit.
925 */
926 if (oi1->ip_blkno != oi2->ip_blkno)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700927 ocfs2_inode_unlock(inode2, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700928
Mark Fashehccd979b2005-12-15 14:31:24 -0800929 if (status != -ENOENT)
930 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800931 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700932
Mark Fashehccd979b2005-12-15 14:31:24 -0800933bail:
934 mlog_exit(status);
935 return status;
936}
937
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700938static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
939{
Mark Fashehe63aecb62007-10-18 15:30:42 -0700940 ocfs2_inode_unlock(inode1, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700941
942 if (inode1 != inode2)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700943 ocfs2_inode_unlock(inode2, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700944}
945
Mark Fashehccd979b2005-12-15 14:31:24 -0800946static int ocfs2_rename(struct inode *old_dir,
947 struct dentry *old_dentry,
948 struct inode *new_dir,
949 struct dentry *new_dentry)
950{
Mark Fasheh8d5596c2006-10-06 15:04:51 -0700951 int status = 0, rename_lock = 0, parents_locked = 0;
952 int old_child_locked = 0, new_child_locked = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800953 struct inode *old_inode = old_dentry->d_inode;
954 struct inode *new_inode = new_dentry->d_inode;
Mark Fasheh5098c272006-10-05 18:12:57 -0700955 struct inode *orphan_dir = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800956 struct ocfs2_dinode *newfe = NULL;
957 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
958 struct buffer_head *orphan_entry_bh = NULL;
959 struct buffer_head *newfe_bh = NULL;
Mark Fasheh592282c2007-01-02 17:59:40 -0800960 struct buffer_head *old_inode_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800961 struct buffer_head *insert_entry_bh = NULL;
962 struct ocfs2_super *osb = NULL;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -0700963 u64 newfe_blkno, old_de_ino;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700964 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800965 struct buffer_head *old_dir_bh = NULL;
966 struct buffer_head *new_dir_bh = NULL;
Mark Fasheh38760e22007-09-11 17:21:56 -0700967 struct ocfs2_dir_entry *old_inode_dot_dot_de = NULL, *old_de = NULL,
968 *new_de = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800969 struct buffer_head *new_de_bh = NULL, *old_de_bh = NULL; // bhs for above
970 struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir,
971 // this is the 1st dirent bh
Mark Fasheh592282c2007-01-02 17:59:40 -0800972 nlink_t old_dir_nlink = old_dir->i_nlink;
Sunil Mushran480214d2007-08-06 15:11:56 -0700973 struct ocfs2_dinode *old_di;
Mark Fashehccd979b2005-12-15 14:31:24 -0800974
975 /* At some point it might be nice to break this function up a
976 * bit. */
977
978 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
979 old_dir, old_dentry, new_dir, new_dentry,
980 old_dentry->d_name.len, old_dentry->d_name.name,
981 new_dentry->d_name.len, new_dentry->d_name.name);
982
983 osb = OCFS2_SB(old_dir->i_sb);
984
985 if (new_inode) {
986 if (!igrab(new_inode))
987 BUG();
988 }
989
Uwe Kleine-König1b3c3712007-02-17 19:23:03 +0100990 /* Assume a directory hierarchy thusly:
Mark Fashehccd979b2005-12-15 14:31:24 -0800991 * a/b/c
992 * a/d
993 * a,b,c, and d are all directories.
994 *
995 * from cwd of 'a' on both nodes:
996 * node1: mv b/c d
997 * node2: mv d b/c
998 *
999 * And that's why, just like the VFS, we need a file system
1000 * rename lock. */
Jan Kara5dabd692008-02-21 18:00:00 +01001001 if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001002 status = ocfs2_rename_lock(osb);
1003 if (status < 0) {
1004 mlog_errno(status);
1005 goto bail;
1006 }
1007 rename_lock = 1;
1008 }
1009
Mark Fashehccd979b2005-12-15 14:31:24 -08001010 /* if old and new are the same, this'll just do one lock. */
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001011 status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
1012 &new_dir_bh, new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001013 if (status < 0) {
1014 mlog_errno(status);
1015 goto bail;
1016 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001017 parents_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001018
1019 /* make sure both dirs have bhs
1020 * get an extra ref on old_dir_bh if old==new */
1021 if (!new_dir_bh) {
1022 if (old_dir_bh) {
1023 new_dir_bh = old_dir_bh;
1024 get_bh(new_dir_bh);
1025 } else {
1026 mlog(ML_ERROR, "no old_dir_bh!\n");
1027 status = -EIO;
1028 goto bail;
1029 }
1030 }
1031
Mark Fasheh379dfe92006-09-08 14:21:03 -07001032 /*
Mark Fasheh592282c2007-01-02 17:59:40 -08001033 * Aside from allowing a meta data update, the locking here
Mark Fasheh34d024f2007-09-24 15:56:19 -07001034 * also ensures that the downconvert thread on other nodes
1035 * won't have to concurrently downconvert the inode and the
1036 * dentry locks.
Mark Fasheh379dfe92006-09-08 14:21:03 -07001037 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001038 status = ocfs2_inode_lock(old_inode, &old_inode_bh, 1);
Mark Fasheh379dfe92006-09-08 14:21:03 -07001039 if (status < 0) {
1040 if (status != -ENOENT)
Mark Fashehccd979b2005-12-15 14:31:24 -08001041 mlog_errno(status);
Mark Fasheh379dfe92006-09-08 14:21:03 -07001042 goto bail;
1043 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001044 old_child_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001045
Mark Fasheh379dfe92006-09-08 14:21:03 -07001046 status = ocfs2_remote_dentry_delete(old_dentry);
1047 if (status < 0) {
1048 mlog_errno(status);
1049 goto bail;
1050 }
1051
1052 if (S_ISDIR(old_inode->i_mode)) {
Mark Fasheh38760e22007-09-11 17:21:56 -07001053 u64 old_inode_parent;
Mark Fashehccd979b2005-12-15 14:31:24 -08001054
Mark Fasheh38760e22007-09-11 17:21:56 -07001055 status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent,
1056 old_inode, &old_inode_de_bh,
1057 &old_inode_dot_dot_de);
1058 if (status) {
1059 status = -EIO;
Mark Fashehccd979b2005-12-15 14:31:24 -08001060 goto bail;
Mark Fasheh38760e22007-09-11 17:21:56 -07001061 }
1062
1063 if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) {
1064 status = -EIO;
Mark Fashehccd979b2005-12-15 14:31:24 -08001065 goto bail;
Mark Fasheh38760e22007-09-11 17:21:56 -07001066 }
1067
1068 if (!new_inode && new_dir != old_dir &&
1069 new_dir->i_nlink >= OCFS2_LINK_MAX) {
1070 status = -EMLINK;
1071 goto bail;
1072 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001073 }
1074
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001075 status = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name,
1076 old_dentry->d_name.len,
1077 &old_de_ino);
1078 if (status) {
1079 status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -08001080 goto bail;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001081 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001082
1083 /*
1084 * Check for inode number is _not_ due to possible IO errors.
1085 * We might rmdir the source, keep it as pwd of some process
1086 * and merrily kill the link to whatever was created under the
1087 * same name. Goodbye sticky bit ;-<
1088 */
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001089 if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) {
1090 status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -08001091 goto bail;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001092 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001093
1094 /* check if the target already exists (in which case we need
1095 * to delete it */
1096 status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
1097 new_dentry->d_name.len,
1098 &newfe_blkno, new_dir, &new_de_bh,
1099 &new_de);
1100 /* The only error we allow here is -ENOENT because the new
1101 * file not existing is perfectly valid. */
1102 if ((status < 0) && (status != -ENOENT)) {
1103 /* If we cannot find the file specified we should just */
1104 /* return the error... */
1105 mlog_errno(status);
1106 goto bail;
1107 }
1108
Srinivas Eedae325a882007-10-31 16:49:43 -07001109 if (!new_de && new_inode) {
1110 /*
1111 * Target was unlinked by another node while we were
1112 * waiting to get to ocfs2_rename(). There isn't
1113 * anything we can do here to help the situation, so
1114 * bubble up the appropriate error.
1115 */
1116 status = -ENOENT;
1117 goto bail;
1118 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001119
1120 /* In case we need to overwrite an existing file, we blow it
1121 * away first */
1122 if (new_de) {
1123 /* VFS didn't think there existed an inode here, but
1124 * someone else in the cluster must have raced our
1125 * rename to create one. Today we error cleanly, in
1126 * the future we should consider calling iget to build
1127 * a new struct inode for this entry. */
1128 if (!new_inode) {
1129 status = -EACCES;
1130
1131 mlog(0, "We found an inode for name %.*s but VFS "
1132 "didn't give us one.\n", new_dentry->d_name.len,
1133 new_dentry->d_name.name);
1134 goto bail;
1135 }
1136
1137 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
1138 status = -EACCES;
1139
Mark Fashehb0697052006-03-03 10:24:33 -08001140 mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
1141 (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
1142 (unsigned long long)newfe_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001143 OCFS2_I(new_inode)->ip_flags);
1144 goto bail;
1145 }
1146
Mark Fashehe63aecb62007-10-18 15:30:42 -07001147 status = ocfs2_inode_lock(new_inode, &newfe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001148 if (status < 0) {
1149 if (status != -ENOENT)
1150 mlog_errno(status);
1151 goto bail;
1152 }
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001153 new_child_locked = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001154
Mark Fasheh379dfe92006-09-08 14:21:03 -07001155 status = ocfs2_remote_dentry_delete(new_dentry);
Mark Fashehccd979b2005-12-15 14:31:24 -08001156 if (status < 0) {
1157 mlog_errno(status);
1158 goto bail;
1159 }
1160
1161 newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
1162
Mark Fashehb0697052006-03-03 10:24:33 -08001163 mlog(0, "aha rename over existing... new_de=%p new_blkno=%llu "
1164 "newfebh=%p bhblocknr=%llu\n", new_de,
1165 (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
Mark Fashehccd979b2005-12-15 14:31:24 -08001166 (unsigned long long)newfe_bh->b_blocknr : 0ULL);
1167
1168 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
Mark Fasheh5098c272006-10-05 18:12:57 -07001169 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
Mark Fashehccd979b2005-12-15 14:31:24 -08001170 new_inode,
1171 orphan_name,
1172 &orphan_entry_bh);
1173 if (status < 0) {
1174 mlog_errno(status);
1175 goto bail;
1176 }
1177 }
1178 } else {
1179 BUG_ON(new_dentry->d_parent->d_inode != new_dir);
1180
1181 status = ocfs2_check_dir_for_entry(new_dir,
1182 new_dentry->d_name.name,
1183 new_dentry->d_name.len);
1184 if (status)
1185 goto bail;
1186
1187 status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
1188 new_dentry->d_name.name,
1189 new_dentry->d_name.len,
1190 &insert_entry_bh);
1191 if (status < 0) {
1192 mlog_errno(status);
1193 goto bail;
1194 }
1195 }
1196
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001197 handle = ocfs2_start_trans(osb, OCFS2_RENAME_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001198 if (IS_ERR(handle)) {
1199 status = PTR_ERR(handle);
1200 handle = NULL;
1201 mlog_errno(status);
1202 goto bail;
1203 }
1204
1205 if (new_de) {
1206 if (S_ISDIR(new_inode->i_mode)) {
1207 if (!ocfs2_empty_dir(new_inode) ||
1208 new_inode->i_nlink != 2) {
1209 status = -ENOTEMPTY;
1210 goto bail;
1211 }
1212 }
1213 status = ocfs2_journal_access(handle, new_inode, newfe_bh,
1214 OCFS2_JOURNAL_ACCESS_WRITE);
1215 if (status < 0) {
1216 mlog_errno(status);
1217 goto bail;
1218 }
1219
1220 if (S_ISDIR(new_inode->i_mode) ||
1221 (newfe->i_links_count == cpu_to_le16(1))){
1222 status = ocfs2_orphan_add(osb, handle, new_inode,
1223 newfe, orphan_name,
Mark Fasheh5098c272006-10-05 18:12:57 -07001224 orphan_entry_bh, orphan_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001225 if (status < 0) {
1226 mlog_errno(status);
1227 goto bail;
1228 }
1229 }
1230
1231 /* change the dirent to point to the correct inode */
Mark Fasheh38760e22007-09-11 17:21:56 -07001232 status = ocfs2_update_entry(new_dir, handle, new_de_bh,
1233 new_de, old_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001234 if (status < 0) {
1235 mlog_errno(status);
1236 goto bail;
1237 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001238 new_dir->i_version++;
Mark Fashehccd979b2005-12-15 14:31:24 -08001239
1240 if (S_ISDIR(new_inode->i_mode))
1241 newfe->i_links_count = 0;
1242 else
1243 le16_add_cpu(&newfe->i_links_count, -1);
1244
1245 status = ocfs2_journal_dirty(handle, newfe_bh);
1246 if (status < 0) {
1247 mlog_errno(status);
1248 goto bail;
1249 }
1250 } else {
1251 /* if the name was not found in new_dir, add it now */
1252 status = ocfs2_add_entry(handle, new_dentry, old_inode,
1253 OCFS2_I(old_inode)->ip_blkno,
1254 new_dir_bh, insert_entry_bh);
1255 }
1256
1257 old_inode->i_ctime = CURRENT_TIME;
1258 mark_inode_dirty(old_inode);
Sunil Mushran480214d2007-08-06 15:11:56 -07001259
1260 status = ocfs2_journal_access(handle, old_inode, old_inode_bh,
1261 OCFS2_JOURNAL_ACCESS_WRITE);
1262 if (status >= 0) {
1263 old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
1264
1265 old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
1266 old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
1267
1268 status = ocfs2_journal_dirty(handle, old_inode_bh);
1269 if (status < 0)
1270 mlog_errno(status);
1271 } else
1272 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001273
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001274 /*
1275 * Now that the name has been added to new_dir, remove the old name.
1276 *
1277 * We don't keep any directory entry context around until now
1278 * because the insert might have changed the type of directory
1279 * we're dealing with.
1280 */
1281 old_de_bh = ocfs2_find_entry(old_dentry->d_name.name,
1282 old_dentry->d_name.len,
1283 old_dir, &old_de);
1284 if (!old_de_bh) {
1285 status = -EIO;
1286 goto bail;
1287 }
1288
Mark Fashehccd979b2005-12-15 14:31:24 -08001289 status = ocfs2_delete_entry(handle, old_dir, old_de, old_de_bh);
1290 if (status < 0) {
1291 mlog_errno(status);
1292 goto bail;
1293 }
1294
1295 if (new_inode) {
1296 new_inode->i_nlink--;
1297 new_inode->i_ctime = CURRENT_TIME;
1298 }
1299 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1300 if (old_inode_de_bh) {
Mark Fasheh38760e22007-09-11 17:21:56 -07001301 status = ocfs2_update_entry(old_inode, handle, old_inode_de_bh,
1302 old_inode_dot_dot_de, new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001303 old_dir->i_nlink--;
1304 if (new_inode) {
1305 new_inode->i_nlink--;
1306 } else {
Dave Hansend8c76e62006-09-30 23:29:04 -07001307 inc_nlink(new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001308 mark_inode_dirty(new_dir);
1309 }
1310 }
1311 mark_inode_dirty(old_dir);
Mark Fasheh592282c2007-01-02 17:59:40 -08001312 ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
1313 if (new_inode) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001314 mark_inode_dirty(new_inode);
Mark Fasheh592282c2007-01-02 17:59:40 -08001315 ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
1316 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001317
Mark Fasheh592282c2007-01-02 17:59:40 -08001318 if (old_dir != new_dir) {
1319 /* Keep the same times on both directories.*/
1320 new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
1321
1322 /*
1323 * This will also pick up the i_nlink change from the
1324 * block above.
1325 */
1326 ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
1327 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001328
1329 if (old_dir_nlink != old_dir->i_nlink) {
1330 if (!old_dir_bh) {
1331 mlog(ML_ERROR, "need to change nlink for old dir "
Mark Fashehb0697052006-03-03 10:24:33 -08001332 "%llu from %d to %d but bh is NULL!\n",
1333 (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
1334 (int)old_dir_nlink, old_dir->i_nlink);
Mark Fashehccd979b2005-12-15 14:31:24 -08001335 } else {
1336 struct ocfs2_dinode *fe;
1337 status = ocfs2_journal_access(handle, old_dir,
1338 old_dir_bh,
1339 OCFS2_JOURNAL_ACCESS_WRITE);
1340 fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
1341 fe->i_links_count = cpu_to_le16(old_dir->i_nlink);
1342 status = ocfs2_journal_dirty(handle, old_dir_bh);
1343 }
1344 }
1345
Mark Fasheh379dfe92006-09-08 14:21:03 -07001346 ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08001347 status = 0;
1348bail:
1349 if (rename_lock)
1350 ocfs2_rename_unlock(osb);
1351
1352 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001353 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001354
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001355 if (parents_locked)
1356 ocfs2_double_unlock(old_dir, new_dir);
1357
1358 if (old_child_locked)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001359 ocfs2_inode_unlock(old_inode, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001360
1361 if (new_child_locked)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001362 ocfs2_inode_unlock(new_inode, 1);
Mark Fasheh8d5596c2006-10-06 15:04:51 -07001363
Mark Fasheh5098c272006-10-05 18:12:57 -07001364 if (orphan_dir) {
1365 /* This was locked for us in ocfs2_prepare_orphan_dir() */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001366 ocfs2_inode_unlock(orphan_dir, 1);
Mark Fasheh5098c272006-10-05 18:12:57 -07001367 mutex_unlock(&orphan_dir->i_mutex);
1368 iput(orphan_dir);
1369 }
1370
Mark Fashehccd979b2005-12-15 14:31:24 -08001371 if (new_inode)
1372 sync_mapping_buffers(old_inode->i_mapping);
1373
1374 if (new_inode)
1375 iput(new_inode);
1376 if (newfe_bh)
1377 brelse(newfe_bh);
Mark Fasheh592282c2007-01-02 17:59:40 -08001378 if (old_inode_bh)
1379 brelse(old_inode_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001380 if (old_dir_bh)
1381 brelse(old_dir_bh);
1382 if (new_dir_bh)
1383 brelse(new_dir_bh);
1384 if (new_de_bh)
1385 brelse(new_de_bh);
1386 if (old_de_bh)
1387 brelse(old_de_bh);
1388 if (old_inode_de_bh)
1389 brelse(old_inode_de_bh);
1390 if (orphan_entry_bh)
1391 brelse(orphan_entry_bh);
1392 if (insert_entry_bh)
1393 brelse(insert_entry_bh);
1394
1395 mlog_exit(status);
1396
1397 return status;
1398}
1399
1400/*
1401 * we expect i_size = strlen(symname). Copy symname into the file
1402 * data, including the null terminator.
1403 */
1404static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001405 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001406 struct inode *inode,
1407 const char *symname)
1408{
1409 struct buffer_head **bhs = NULL;
1410 const char *c;
1411 struct super_block *sb = osb->sb;
Mark Fasheh4f902c32007-03-09 16:26:50 -08001412 u64 p_blkno, p_blocks;
Mark Fashehccd979b2005-12-15 14:31:24 -08001413 int virtual, blocks, status, i, bytes_left;
1414
1415 bytes_left = i_size_read(inode) + 1;
1416 /* we can't trust i_blocks because we're actually going to
1417 * write i_size + 1 bytes. */
1418 blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1419
Andrew Morton5515eff2006-03-26 01:37:53 -08001420 mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
1421 (unsigned long long)inode->i_blocks,
1422 i_size_read(inode), blocks);
Mark Fashehccd979b2005-12-15 14:31:24 -08001423
1424 /* Sanity check -- make sure we're going to fit. */
1425 if (bytes_left >
1426 ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1427 status = -EIO;
1428 mlog_errno(status);
1429 goto bail;
1430 }
1431
1432 bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
1433 if (!bhs) {
1434 status = -ENOMEM;
1435 mlog_errno(status);
1436 goto bail;
1437 }
1438
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001439 status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
1440 NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001441 if (status < 0) {
1442 mlog_errno(status);
1443 goto bail;
1444 }
1445
1446 /* links can never be larger than one cluster so we know this
1447 * is all going to be contiguous, but do a sanity check
1448 * anyway. */
1449 if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
1450 status = -EIO;
1451 mlog_errno(status);
1452 goto bail;
1453 }
1454
1455 virtual = 0;
1456 while(bytes_left > 0) {
1457 c = &symname[virtual * sb->s_blocksize];
1458
1459 bhs[virtual] = sb_getblk(sb, p_blkno);
1460 if (!bhs[virtual]) {
1461 status = -ENOMEM;
1462 mlog_errno(status);
1463 goto bail;
1464 }
1465 ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
1466
1467 status = ocfs2_journal_access(handle, inode, bhs[virtual],
1468 OCFS2_JOURNAL_ACCESS_CREATE);
1469 if (status < 0) {
1470 mlog_errno(status);
1471 goto bail;
1472 }
1473
1474 memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
1475
1476 memcpy(bhs[virtual]->b_data, c,
1477 (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
1478 bytes_left);
1479
1480 status = ocfs2_journal_dirty(handle, bhs[virtual]);
1481 if (status < 0) {
1482 mlog_errno(status);
1483 goto bail;
1484 }
1485
1486 virtual++;
1487 p_blkno++;
1488 bytes_left -= sb->s_blocksize;
1489 }
1490
1491 status = 0;
1492bail:
1493
1494 if (bhs) {
1495 for(i = 0; i < blocks; i++)
1496 if (bhs[i])
1497 brelse(bhs[i]);
1498 kfree(bhs);
1499 }
1500
1501 mlog_exit(status);
1502 return status;
1503}
1504
1505static int ocfs2_symlink(struct inode *dir,
1506 struct dentry *dentry,
1507 const char *symname)
1508{
1509 int status, l, credits;
1510 u64 newsize;
1511 struct ocfs2_super *osb = NULL;
1512 struct inode *inode = NULL;
1513 struct super_block *sb;
1514 struct buffer_head *new_fe_bh = NULL;
1515 struct buffer_head *de_bh = NULL;
1516 struct buffer_head *parent_fe_bh = NULL;
1517 struct ocfs2_dinode *fe = NULL;
1518 struct ocfs2_dinode *dirfe;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001519 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001520 struct ocfs2_alloc_context *inode_ac = NULL;
1521 struct ocfs2_alloc_context *data_ac = NULL;
1522
1523 mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
1524 dentry, symname, dentry->d_name.len, dentry->d_name.name);
1525
1526 sb = dir->i_sb;
1527 osb = OCFS2_SB(sb);
1528
1529 l = strlen(symname) + 1;
1530
1531 credits = ocfs2_calc_symlink_credits(sb);
1532
Mark Fashehccd979b2005-12-15 14:31:24 -08001533 /* lock the parent directory */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001534 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001535 if (status < 0) {
1536 if (status != -ENOENT)
1537 mlog_errno(status);
Mark Fasheh6d8fc402006-10-06 11:54:33 -07001538 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001539 }
1540
1541 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1542 if (!dirfe->i_links_count) {
1543 /* can't make a file in a deleted directory. */
1544 status = -ENOENT;
1545 goto bail;
1546 }
1547
1548 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
1549 dentry->d_name.len);
1550 if (status)
1551 goto bail;
1552
1553 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
1554 dentry->d_name.name,
1555 dentry->d_name.len, &de_bh);
1556 if (status < 0) {
1557 mlog_errno(status);
1558 goto bail;
1559 }
1560
Mark Fashehda5cbf22006-10-06 18:34:35 -07001561 status = ocfs2_reserve_new_inode(osb, &inode_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001562 if (status < 0) {
1563 if (status != -ENOSPC)
1564 mlog_errno(status);
1565 goto bail;
1566 }
1567
1568 /* don't reserve bitmap space for fast symlinks. */
1569 if (l > ocfs2_fast_symlink_chars(sb)) {
Mark Fashehda5cbf22006-10-06 18:34:35 -07001570 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001571 if (status < 0) {
1572 if (status != -ENOSPC)
1573 mlog_errno(status);
1574 goto bail;
1575 }
1576 }
1577
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001578 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001579 if (IS_ERR(handle)) {
1580 status = PTR_ERR(handle);
1581 handle = NULL;
1582 mlog_errno(status);
1583 goto bail;
1584 }
1585
1586 status = ocfs2_mknod_locked(osb, dir, dentry,
1587 S_IFLNK | S_IRWXUGO, 0,
1588 &new_fe_bh, parent_fe_bh, handle,
1589 &inode, inode_ac);
1590 if (status < 0) {
1591 mlog_errno(status);
1592 goto bail;
1593 }
1594
1595 fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
1596 inode->i_rdev = 0;
1597 newsize = l - 1;
1598 if (l > ocfs2_fast_symlink_chars(sb)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08001599 u32 offset = 0;
1600
Mark Fashehccd979b2005-12-15 14:31:24 -08001601 inode->i_op = &ocfs2_symlink_inode_operations;
Tao Ma0eb8d472008-08-18 17:38:45 +08001602 status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0,
1603 new_fe_bh,
1604 handle, data_ac, NULL,
1605 NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001606 if (status < 0) {
1607 if (status != -ENOSPC && status != -EINTR) {
Mark Fashehb0697052006-03-03 10:24:33 -08001608 mlog(ML_ERROR,
1609 "Failed to extend file to %llu\n",
1610 (unsigned long long)newsize);
Mark Fashehccd979b2005-12-15 14:31:24 -08001611 mlog_errno(status);
1612 status = -ENOSPC;
1613 }
1614 goto bail;
1615 }
1616 i_size_write(inode, newsize);
Mark Fasheh8110b072007-03-22 16:53:23 -07001617 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001618 } else {
1619 inode->i_op = &ocfs2_fast_symlink_inode_operations;
1620 memcpy((char *) fe->id2.i_symlink, symname, l);
1621 i_size_write(inode, newsize);
1622 inode->i_blocks = 0;
1623 }
1624
1625 status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
1626 if (status < 0) {
1627 mlog_errno(status);
1628 goto bail;
1629 }
1630
1631 if (!ocfs2_inode_is_fast_symlink(inode)) {
1632 status = ocfs2_create_symlink_data(osb, handle, inode,
1633 symname);
1634 if (status < 0) {
1635 mlog_errno(status);
1636 goto bail;
1637 }
1638 }
1639
1640 status = ocfs2_add_entry(handle, dentry, inode,
1641 le64_to_cpu(fe->i_blkno), parent_fe_bh,
1642 de_bh);
1643 if (status < 0) {
1644 mlog_errno(status);
1645 goto bail;
1646 }
1647
Mark Fasheh0027dd52006-09-21 16:51:28 -07001648 status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
Mark Fasheh379dfe92006-09-08 14:21:03 -07001649 if (status) {
1650 mlog_errno(status);
1651 goto bail;
1652 }
1653
Mark Fashehccd979b2005-12-15 14:31:24 -08001654 insert_inode_hash(inode);
1655 dentry->d_op = &ocfs2_dentry_ops;
1656 d_instantiate(dentry, inode);
1657bail:
1658 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001659 ocfs2_commit_trans(osb, handle);
Mark Fasheh6d8fc402006-10-06 11:54:33 -07001660
Mark Fashehe63aecb62007-10-18 15:30:42 -07001661 ocfs2_inode_unlock(dir, 1);
Mark Fasheh6d8fc402006-10-06 11:54:33 -07001662
Mark Fashehccd979b2005-12-15 14:31:24 -08001663 if (new_fe_bh)
1664 brelse(new_fe_bh);
1665 if (parent_fe_bh)
1666 brelse(parent_fe_bh);
1667 if (de_bh)
1668 brelse(de_bh);
1669 if (inode_ac)
1670 ocfs2_free_alloc_context(inode_ac);
1671 if (data_ac)
1672 ocfs2_free_alloc_context(data_ac);
1673 if ((status < 0) && inode)
1674 iput(inode);
1675
1676 mlog_exit(status);
1677
1678 return status;
1679}
1680
Mark Fashehccd979b2005-12-15 14:31:24 -08001681static int ocfs2_blkno_stringify(u64 blkno, char *name)
1682{
1683 int status, namelen;
1684
1685 mlog_entry_void();
1686
Mark Fashehb0697052006-03-03 10:24:33 -08001687 namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
1688 (long long)blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001689 if (namelen <= 0) {
1690 if (namelen)
1691 status = namelen;
1692 else
1693 status = -EINVAL;
1694 mlog_errno(status);
1695 goto bail;
1696 }
1697 if (namelen != OCFS2_ORPHAN_NAMELEN) {
1698 status = -EINVAL;
1699 mlog_errno(status);
1700 goto bail;
1701 }
1702
1703 mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
1704 namelen);
1705
1706 status = 0;
1707bail:
1708 mlog_exit(status);
1709 return status;
1710}
1711
1712static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
Mark Fasheh5098c272006-10-05 18:12:57 -07001713 struct inode **ret_orphan_dir,
Mark Fashehccd979b2005-12-15 14:31:24 -08001714 struct inode *inode,
1715 char *name,
1716 struct buffer_head **de_bh)
1717{
Mark Fasheh5098c272006-10-05 18:12:57 -07001718 struct inode *orphan_dir_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -08001719 struct buffer_head *orphan_dir_bh = NULL;
1720 int status = 0;
1721
1722 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
1723 if (status < 0) {
1724 mlog_errno(status);
Mark Fasheh5098c272006-10-05 18:12:57 -07001725 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001726 }
1727
1728 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1729 ORPHAN_DIR_SYSTEM_INODE,
1730 osb->slot_num);
1731 if (!orphan_dir_inode) {
1732 status = -ENOENT;
1733 mlog_errno(status);
Mark Fasheh5098c272006-10-05 18:12:57 -07001734 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001735 }
1736
Mark Fasheh5098c272006-10-05 18:12:57 -07001737 mutex_lock(&orphan_dir_inode->i_mutex);
1738
Mark Fashehe63aecb62007-10-18 15:30:42 -07001739 status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001740 if (status < 0) {
1741 mlog_errno(status);
1742 goto leave;
1743 }
1744
1745 status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
1746 orphan_dir_bh, name,
1747 OCFS2_ORPHAN_NAMELEN, de_bh);
1748 if (status < 0) {
Mark Fashehe63aecb62007-10-18 15:30:42 -07001749 ocfs2_inode_unlock(orphan_dir_inode, 1);
Mark Fasheh5098c272006-10-05 18:12:57 -07001750
Mark Fashehccd979b2005-12-15 14:31:24 -08001751 mlog_errno(status);
1752 goto leave;
1753 }
1754
Mark Fasheh5098c272006-10-05 18:12:57 -07001755 *ret_orphan_dir = orphan_dir_inode;
1756
Mark Fashehccd979b2005-12-15 14:31:24 -08001757leave:
Mark Fasheh5098c272006-10-05 18:12:57 -07001758 if (status) {
1759 mutex_unlock(&orphan_dir_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001760 iput(orphan_dir_inode);
Mark Fasheh5098c272006-10-05 18:12:57 -07001761 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001762
1763 if (orphan_dir_bh)
1764 brelse(orphan_dir_bh);
1765
1766 mlog_exit(status);
1767 return status;
1768}
1769
1770static int ocfs2_orphan_add(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001771 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001772 struct inode *inode,
1773 struct ocfs2_dinode *fe,
1774 char *name,
Mark Fasheh5098c272006-10-05 18:12:57 -07001775 struct buffer_head *de_bh,
1776 struct inode *orphan_dir_inode)
Mark Fashehccd979b2005-12-15 14:31:24 -08001777{
Mark Fashehccd979b2005-12-15 14:31:24 -08001778 struct buffer_head *orphan_dir_bh = NULL;
1779 int status = 0;
1780 struct ocfs2_dinode *orphan_fe;
1781
1782 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
1783
Mark Fashehccd979b2005-12-15 14:31:24 -08001784 status = ocfs2_read_block(osb,
1785 OCFS2_I(orphan_dir_inode)->ip_blkno,
1786 &orphan_dir_bh, OCFS2_BH_CACHED,
1787 orphan_dir_inode);
1788 if (status < 0) {
1789 mlog_errno(status);
1790 goto leave;
1791 }
1792
1793 status = ocfs2_journal_access(handle, orphan_dir_inode, orphan_dir_bh,
1794 OCFS2_JOURNAL_ACCESS_WRITE);
1795 if (status < 0) {
1796 mlog_errno(status);
1797 goto leave;
1798 }
1799
1800 /* we're a cluster, and nlink can change on disk from
1801 * underneath us... */
1802 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
1803 if (S_ISDIR(inode->i_mode))
1804 le16_add_cpu(&orphan_fe->i_links_count, 1);
1805 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
1806
1807 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
1808 if (status < 0) {
1809 mlog_errno(status);
1810 goto leave;
1811 }
1812
1813 status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
1814 OCFS2_ORPHAN_NAMELEN, inode,
1815 OCFS2_I(inode)->ip_blkno,
1816 orphan_dir_bh, de_bh);
1817 if (status < 0) {
1818 mlog_errno(status);
1819 goto leave;
1820 }
1821
1822 le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
1823
1824 /* Record which orphan dir our inode now resides
1825 * in. delete_inode will use this to determine which orphan
1826 * dir to lock. */
Tiger Yang50008632007-03-20 16:01:38 -07001827 fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001828
Mark Fashehb0697052006-03-03 10:24:33 -08001829 mlog(0, "Inode %llu orphaned in slot %d\n",
1830 (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001831
1832leave:
Mark Fashehccd979b2005-12-15 14:31:24 -08001833 if (orphan_dir_bh)
1834 brelse(orphan_dir_bh);
1835
1836 mlog_exit(status);
1837 return status;
1838}
1839
1840/* unlike orphan_add, we expect the orphan dir to already be locked here. */
1841int ocfs2_orphan_del(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001842 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001843 struct inode *orphan_dir_inode,
1844 struct inode *inode,
1845 struct buffer_head *orphan_dir_bh)
1846{
1847 char name[OCFS2_ORPHAN_NAMELEN + 1];
1848 struct ocfs2_dinode *orphan_fe;
1849 int status = 0;
1850 struct buffer_head *target_de_bh = NULL;
1851 struct ocfs2_dir_entry *target_de = NULL;
1852
1853 mlog_entry_void();
1854
1855 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
1856 if (status < 0) {
1857 mlog_errno(status);
1858 goto leave;
1859 }
1860
Mark Fashehb0697052006-03-03 10:24:33 -08001861 mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
1862 name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
1863 OCFS2_ORPHAN_NAMELEN);
Mark Fashehccd979b2005-12-15 14:31:24 -08001864
1865 /* find it's spot in the orphan directory */
1866 target_de_bh = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN,
1867 orphan_dir_inode, &target_de);
1868 if (!target_de_bh) {
1869 status = -ENOENT;
1870 mlog_errno(status);
1871 goto leave;
1872 }
1873
1874 /* remove it from the orphan directory */
1875 status = ocfs2_delete_entry(handle, orphan_dir_inode, target_de,
1876 target_de_bh);
1877 if (status < 0) {
1878 mlog_errno(status);
1879 goto leave;
1880 }
1881
1882 status = ocfs2_journal_access(handle,orphan_dir_inode, orphan_dir_bh,
1883 OCFS2_JOURNAL_ACCESS_WRITE);
1884 if (status < 0) {
1885 mlog_errno(status);
1886 goto leave;
1887 }
1888
1889 /* do the i_nlink dance! :) */
1890 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
1891 if (S_ISDIR(inode->i_mode))
1892 le16_add_cpu(&orphan_fe->i_links_count, -1);
1893 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
1894
1895 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
1896 if (status < 0) {
1897 mlog_errno(status);
1898 goto leave;
1899 }
1900
1901leave:
1902 if (target_de_bh)
1903 brelse(target_de_bh);
1904
1905 mlog_exit(status);
1906 return status;
1907}
1908
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001909const struct inode_operations ocfs2_dir_iops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08001910 .create = ocfs2_create,
1911 .lookup = ocfs2_lookup,
1912 .link = ocfs2_link,
1913 .unlink = ocfs2_unlink,
1914 .rmdir = ocfs2_unlink,
1915 .symlink = ocfs2_symlink,
1916 .mkdir = ocfs2_mkdir,
1917 .mknod = ocfs2_mknod,
1918 .rename = ocfs2_rename,
1919 .setattr = ocfs2_setattr,
1920 .getattr = ocfs2_getattr,
Tiger Yangd38eb8d2006-11-27 09:59:21 +08001921 .permission = ocfs2_permission,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001922 .setxattr = generic_setxattr,
1923 .getxattr = generic_getxattr,
1924 .listxattr = ocfs2_listxattr,
1925 .removexattr = generic_removexattr,
Mark Fashehccd979b2005-12-15 14:31:24 -08001926};