ocfs2: Add a name indexed b-tree to directory inodes
[deliverable/linux.git] / fs / ocfs2 / namei.c
CommitLineData
ccd979bd
MF
1/* -*- 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>
a90714c1 43#include <linux/quotaops.h>
ccd979bd
MF
44
45#define MLOG_MASK_PREFIX ML_NAMEI
46#include <cluster/masklog.h>
47
48#include "ocfs2.h"
49
50#include "alloc.h"
51#include "dcache.h"
52#include "dir.h"
53#include "dlmglue.h"
54#include "extent_map.h"
55#include "file.h"
56#include "inode.h"
57#include "journal.h"
58#include "namei.h"
59#include "suballoc.h"
aa958874 60#include "super.h"
ccd979bd
MF
61#include "symlink.h"
62#include "sysfile.h"
63#include "uptodate.h"
cf1d6c76 64#include "xattr.h"
89c38bd0 65#include "acl.h"
ccd979bd
MF
66
67#include "buffer_head_io.h"
68
ccd979bd
MF
69static int ocfs2_mknod_locked(struct ocfs2_super *osb,
70 struct inode *dir,
f5d36202
TY
71 struct inode *inode,
72 struct dentry *dentry,
ccd979bd
MF
73 dev_t dev,
74 struct buffer_head **new_fe_bh,
75 struct buffer_head *parent_fe_bh,
1fabe148 76 handle_t *handle,
ccd979bd
MF
77 struct ocfs2_alloc_context *inode_ac);
78
ccd979bd 79static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
5098c27b 80 struct inode **ret_orphan_dir,
ccd979bd
MF
81 struct inode *inode,
82 char *name,
4a12ca3a 83 struct ocfs2_dir_lookup_result *lookup);
ccd979bd
MF
84
85static int ocfs2_orphan_add(struct ocfs2_super *osb,
1fabe148 86 handle_t *handle,
ccd979bd
MF
87 struct inode *inode,
88 struct ocfs2_dinode *fe,
89 char *name,
4a12ca3a 90 struct ocfs2_dir_lookup_result *lookup,
5098c27b 91 struct inode *orphan_dir_inode);
ccd979bd
MF
92
93static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
1fabe148 94 handle_t *handle,
ccd979bd
MF
95 struct inode *inode,
96 const char *symname);
97
ccd979bd
MF
98/* An orphan dir name is an 8 byte value, printed as a hex string */
99#define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
100
101static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
102 struct nameidata *nd)
103{
104 int status;
105 u64 blkno;
ccd979bd
MF
106 struct inode *inode = NULL;
107 struct dentry *ret;
ccd979bd
MF
108 struct ocfs2_inode_info *oi;
109
110 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
111 dentry->d_name.len, dentry->d_name.name);
112
113 if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
114 ret = ERR_PTR(-ENAMETOOLONG);
115 goto bail;
116 }
117
b0697053
MF
118 mlog(0, "find name %.*s in directory %llu\n", dentry->d_name.len,
119 dentry->d_name.name, (unsigned long long)OCFS2_I(dir)->ip_blkno);
ccd979bd 120
e63aecb6 121 status = ocfs2_inode_lock(dir, NULL, 0);
ccd979bd
MF
122 if (status < 0) {
123 if (status != -ENOENT)
124 mlog_errno(status);
125 ret = ERR_PTR(status);
126 goto bail;
127 }
128
be94d117
MF
129 status = ocfs2_lookup_ino_from_name(dir, dentry->d_name.name,
130 dentry->d_name.len, &blkno);
ccd979bd
MF
131 if (status < 0)
132 goto bail_add;
133
5fa0613e 134 inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0);
ccd979bd 135 if (IS_ERR(inode)) {
ccd979bd
MF
136 ret = ERR_PTR(-EACCES);
137 goto bail_unlock;
138 }
139
140 oi = OCFS2_I(inode);
141 /* Clear any orphaned state... If we were able to look up the
142 * inode from a directory, it certainly can't be orphaned. We
143 * might have the bad state from a node which intended to
144 * orphan this inode but crashed before it could commit the
145 * unlink. */
146 spin_lock(&oi->ip_lock);
147 oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
ccd979bd
MF
148 spin_unlock(&oi->ip_lock);
149
150bail_add:
ccd979bd
MF
151 dentry->d_op = &ocfs2_dentry_ops;
152 ret = d_splice_alias(inode, dentry);
153
379dfe9d
MF
154 if (inode) {
155 /*
156 * If d_splice_alias() finds a DCACHE_DISCONNECTED
157 * dentry, it will d_move() it on top of ourse. The
158 * return value will indicate this however, so in
159 * those cases, we switch them around for the locking
160 * code.
161 *
162 * NOTE: This dentry already has ->d_op set from
163 * ocfs2_get_parent() and ocfs2_get_dentry()
164 */
165 if (ret)
166 dentry = ret;
167
168 status = ocfs2_dentry_attach_lock(dentry, inode,
0027dd5b 169 OCFS2_I(dir)->ip_blkno);
379dfe9d
MF
170 if (status) {
171 mlog_errno(status);
172 ret = ERR_PTR(status);
173 goto bail_unlock;
174 }
175 }
176
ccd979bd
MF
177bail_unlock:
178 /* Don't drop the cluster lock until *after* the d_add --
179 * unlink on another node will message us to remove that
180 * dentry under this lock so otherwise we can race this with
34d024f8 181 * the downconvert thread and have a stale dentry. */
e63aecb6 182 ocfs2_inode_unlock(dir, 0);
ccd979bd
MF
183
184bail:
ccd979bd
MF
185
186 mlog_exit_ptr(ret);
187
188 return ret;
189}
190
f5d36202
TY
191static struct inode *ocfs2_get_init_inode(struct inode *dir, int mode)
192{
193 struct inode *inode;
194
195 inode = new_inode(dir->i_sb);
196 if (!inode) {
197 mlog(ML_ERROR, "new_inode failed!\n");
198 return NULL;
199 }
200
201 /* populate as many fields early on as possible - many of
202 * these are used by the support functions here and in
203 * callers. */
204 if (S_ISDIR(mode))
205 inode->i_nlink = 2;
206 else
207 inode->i_nlink = 1;
208 inode->i_uid = current_fsuid();
209 if (dir->i_mode & S_ISGID) {
210 inode->i_gid = dir->i_gid;
211 if (S_ISDIR(mode))
212 mode |= S_ISGID;
213 } else
214 inode->i_gid = current_fsgid();
215 inode->i_mode = mode;
a90714c1 216 vfs_dq_init(inode);
f5d36202
TY
217 return inode;
218}
219
ccd979bd
MF
220static int ocfs2_mknod(struct inode *dir,
221 struct dentry *dentry,
222 int mode,
223 dev_t dev)
224{
225 int status = 0;
226 struct buffer_head *parent_fe_bh = NULL;
1fabe148 227 handle_t *handle = NULL;
ccd979bd
MF
228 struct ocfs2_super *osb;
229 struct ocfs2_dinode *dirfe;
230 struct buffer_head *new_fe_bh = NULL;
ccd979bd
MF
231 struct inode *inode = NULL;
232 struct ocfs2_alloc_context *inode_ac = NULL;
233 struct ocfs2_alloc_context *data_ac = NULL;
9b7895ef 234 struct ocfs2_alloc_context *meta_ac = NULL;
534eaddd 235 int want_clusters = 0;
9b7895ef 236 int want_meta = 0;
534eaddd
TY
237 int xattr_credits = 0;
238 struct ocfs2_security_xattr_info si = {
239 .enable = 1,
240 };
a90714c1 241 int did_quota_inode = 0;
4a12ca3a 242 struct ocfs2_dir_lookup_result lookup = { NULL, };
ccd979bd
MF
243
244 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
245 (unsigned long)dev, dentry->d_name.len,
246 dentry->d_name.name);
247
248 /* get our super block */
249 osb = OCFS2_SB(dir->i_sb);
250
e63aecb6 251 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
e3a82138
MF
252 if (status < 0) {
253 if (status != -ENOENT)
254 mlog_errno(status);
255 return status;
256 }
257
a663e305
MF
258 if (S_ISDIR(mode) && (dir->i_nlink >= OCFS2_LINK_MAX)) {
259 status = -EMLINK;
260 goto leave;
261 }
262
ccd979bd
MF
263 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
264 if (!dirfe->i_links_count) {
265 /* can't make a file in a deleted directory. */
266 status = -ENOENT;
267 goto leave;
268 }
269
270 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
271 dentry->d_name.len);
272 if (status)
273 goto leave;
274
275 /* get a spot inside the dir. */
276 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
277 dentry->d_name.name,
4a12ca3a 278 dentry->d_name.len, &lookup);
ccd979bd
MF
279 if (status < 0) {
280 mlog_errno(status);
281 goto leave;
282 }
283
284 /* reserve an inode spot */
da5cbf2f 285 status = ocfs2_reserve_new_inode(osb, &inode_ac);
ccd979bd
MF
286 if (status < 0) {
287 if (status != -ENOSPC)
288 mlog_errno(status);
289 goto leave;
290 }
291
f5d36202
TY
292 inode = ocfs2_get_init_inode(dir, mode);
293 if (!inode) {
294 status = -ENOMEM;
295 mlog_errno(status);
296 goto leave;
297 }
298
534eaddd
TY
299 /* get security xattr */
300 status = ocfs2_init_security_get(inode, dir, &si);
301 if (status) {
302 if (status == -EOPNOTSUPP)
303 si.enable = 0;
304 else {
305 mlog_errno(status);
306 goto leave;
307 }
308 }
309
89c38bd0
TY
310 /* calculate meta data/clusters for setting security and acl xattr */
311 status = ocfs2_calc_xattr_init(dir, parent_fe_bh, mode,
9b7895ef
MF
312 &si, &want_clusters,
313 &xattr_credits, &want_meta);
89c38bd0
TY
314 if (status < 0) {
315 mlog_errno(status);
316 goto leave;
ccd979bd
MF
317 }
318
534eaddd 319 /* Reserve a cluster if creating an extent based directory. */
9b7895ef 320 if (S_ISDIR(mode) && !ocfs2_supports_inline_data(osb)) {
534eaddd
TY
321 want_clusters += 1;
322
9b7895ef
MF
323 /* Dir indexing requires extra space as well */
324 if (ocfs2_supports_indexed_dirs(osb)) {
325 want_clusters++;
326 want_meta++;
327 }
328 }
329
330 status = ocfs2_reserve_new_metadata_blocks(osb, want_meta, &meta_ac);
331 if (status < 0) {
332 if (status != -ENOSPC)
333 mlog_errno(status);
334 goto leave;
335 }
336
534eaddd
TY
337 status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
338 if (status < 0) {
339 if (status != -ENOSPC)
340 mlog_errno(status);
341 goto leave;
342 }
343
9b7895ef
MF
344 handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb,
345 S_ISDIR(mode),
346 xattr_credits));
ccd979bd
MF
347 if (IS_ERR(handle)) {
348 status = PTR_ERR(handle);
349 handle = NULL;
350 mlog_errno(status);
351 goto leave;
352 }
353
a90714c1
JK
354 /* We don't use standard VFS wrapper because we don't want vfs_dq_init
355 * to be called. */
356 if (sb_any_quota_active(osb->sb) &&
357 osb->sb->dq_op->alloc_inode(inode, 1) == NO_QUOTA) {
358 status = -EDQUOT;
359 goto leave;
360 }
361 did_quota_inode = 1;
362
ccd979bd 363 /* do the real work now. */
f5d36202 364 status = ocfs2_mknod_locked(osb, dir, inode, dentry, dev,
ccd979bd 365 &new_fe_bh, parent_fe_bh, handle,
f5d36202 366 inode_ac);
ccd979bd
MF
367 if (status < 0) {
368 mlog_errno(status);
369 goto leave;
370 }
371
372 if (S_ISDIR(mode)) {
373 status = ocfs2_fill_new_dir(osb, handle, dir, inode,
9b7895ef 374 new_fe_bh, data_ac, meta_ac);
ccd979bd
MF
375 if (status < 0) {
376 mlog_errno(status);
377 goto leave;
378 }
379
13723d00
JB
380 status = ocfs2_journal_access_di(handle, dir, parent_fe_bh,
381 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
382 if (status < 0) {
383 mlog_errno(status);
384 goto leave;
385 }
386 le16_add_cpu(&dirfe->i_links_count, 1);
387 status = ocfs2_journal_dirty(handle, parent_fe_bh);
388 if (status < 0) {
389 mlog_errno(status);
390 goto leave;
391 }
d8c76e6f 392 inc_nlink(dir);
ccd979bd
MF
393 }
394
89c38bd0 395 status = ocfs2_init_acl(handle, inode, dir, new_fe_bh, parent_fe_bh,
9b7895ef 396 meta_ac, data_ac);
89c38bd0
TY
397 if (status < 0) {
398 mlog_errno(status);
399 goto leave;
400 }
401
534eaddd
TY
402 if (si.enable) {
403 status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
9b7895ef 404 meta_ac, data_ac);
534eaddd
TY
405 if (status < 0) {
406 mlog_errno(status);
407 goto leave;
408 }
409 }
410
ccd979bd
MF
411 status = ocfs2_add_entry(handle, dentry, inode,
412 OCFS2_I(inode)->ip_blkno, parent_fe_bh,
4a12ca3a 413 &lookup);
ccd979bd
MF
414 if (status < 0) {
415 mlog_errno(status);
416 goto leave;
417 }
418
379dfe9d 419 status = ocfs2_dentry_attach_lock(dentry, inode,
0027dd5b 420 OCFS2_I(dir)->ip_blkno);
379dfe9d
MF
421 if (status) {
422 mlog_errno(status);
423 goto leave;
424 }
425
ccd979bd
MF
426 insert_inode_hash(inode);
427 dentry->d_op = &ocfs2_dentry_ops;
428 d_instantiate(dentry, inode);
429 status = 0;
430leave:
a90714c1
JK
431 if (status < 0 && did_quota_inode)
432 vfs_dq_free_inode(inode);
ccd979bd 433 if (handle)
02dc1af4 434 ocfs2_commit_trans(osb, handle);
ccd979bd 435
e63aecb6 436 ocfs2_inode_unlock(dir, 1);
e3a82138 437
ccd979bd
MF
438 if (status == -ENOSPC)
439 mlog(0, "Disk is full\n");
440
a81cb88b 441 brelse(new_fe_bh);
a81cb88b 442 brelse(parent_fe_bh);
534eaddd
TY
443 kfree(si.name);
444 kfree(si.value);
ccd979bd 445
4a12ca3a
MF
446 ocfs2_free_dir_lookup_result(&lookup);
447
f5d36202
TY
448 if ((status < 0) && inode) {
449 clear_nlink(inode);
ccd979bd 450 iput(inode);
f5d36202 451 }
ccd979bd
MF
452
453 if (inode_ac)
454 ocfs2_free_alloc_context(inode_ac);
455
456 if (data_ac)
457 ocfs2_free_alloc_context(data_ac);
458
9b7895ef
MF
459 if (meta_ac)
460 ocfs2_free_alloc_context(meta_ac);
534eaddd 461
ccd979bd
MF
462 mlog_exit(status);
463
464 return status;
465}
466
467static int ocfs2_mknod_locked(struct ocfs2_super *osb,
468 struct inode *dir,
f5d36202
TY
469 struct inode *inode,
470 struct dentry *dentry,
ccd979bd
MF
471 dev_t dev,
472 struct buffer_head **new_fe_bh,
473 struct buffer_head *parent_fe_bh,
1fabe148 474 handle_t *handle,
ccd979bd
MF
475 struct ocfs2_alloc_context *inode_ac)
476{
477 int status = 0;
478 struct ocfs2_dinode *fe = NULL;
479 struct ocfs2_extent_list *fel;
480 u64 fe_blkno = 0;
481 u16 suballoc_bit;
9b7895ef 482 u16 feat;
ccd979bd 483
f5d36202
TY
484 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry,
485 inode->i_mode, (unsigned long)dev, dentry->d_name.len,
ccd979bd
MF
486 dentry->d_name.name);
487
488 *new_fe_bh = NULL;
ccd979bd
MF
489
490 status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit,
491 &fe_blkno);
492 if (status < 0) {
493 mlog_errno(status);
494 goto leave;
495 }
496
ccd979bd
MF
497 /* populate as many fields early on as possible - many of
498 * these are used by the support functions here and in
499 * callers. */
500 inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
501 OCFS2_I(inode)->ip_blkno = fe_blkno;
ccd979bd
MF
502 spin_lock(&osb->osb_lock);
503 inode->i_generation = osb->s_next_generation++;
504 spin_unlock(&osb->osb_lock);
505
506 *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
507 if (!*new_fe_bh) {
508 status = -EIO;
509 mlog_errno(status);
510 goto leave;
511 }
512 ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh);
513
13723d00
JB
514 status = ocfs2_journal_access_di(handle, inode, *new_fe_bh,
515 OCFS2_JOURNAL_ACCESS_CREATE);
ccd979bd
MF
516 if (status < 0) {
517 mlog_errno(status);
518 goto leave;
519 }
520
521 fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
522 memset(fe, 0, osb->sb->s_blocksize);
523
524 fe->i_generation = cpu_to_le32(inode->i_generation);
525 fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
526 fe->i_blkno = cpu_to_le64(fe_blkno);
527 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
4d0ddb2c 528 fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot);
f5d36202
TY
529 fe->i_uid = cpu_to_le32(inode->i_uid);
530 fe->i_gid = cpu_to_le32(inode->i_gid);
531 fe->i_mode = cpu_to_le16(inode->i_mode);
532 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
ccd979bd 533 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
ccd979bd
MF
534 fe->i_links_count = cpu_to_le16(inode->i_nlink);
535
536 fe->i_last_eb_blk = 0;
537 strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
538 le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
539 fe->i_atime = fe->i_ctime = fe->i_mtime =
540 cpu_to_le64(CURRENT_TIME.tv_sec);
541 fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
542 cpu_to_le32(CURRENT_TIME.tv_nsec);
543 fe->i_dtime = 0;
544
5b6a3a2b 545 /*
9b7895ef
MF
546 * If supported, directories start with inline data. If inline
547 * isn't supported, but indexing is, we start them as indexed.
5b6a3a2b 548 */
9b7895ef 549 feat = le16_to_cpu(fe->i_dyn_features);
f5d36202 550 if (S_ISDIR(inode->i_mode) && ocfs2_supports_inline_data(osb)) {
5b6a3a2b
MF
551 fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL);
552
d9ae49d6
TY
553 fe->id2.i_data.id_count = cpu_to_le16(
554 ocfs2_max_inline_data_with_xattr(osb->sb, fe));
5b6a3a2b
MF
555 } else {
556 fel = &fe->id2.i_list;
557 fel->l_tree_depth = 0;
558 fel->l_next_free_rec = 0;
559 fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
560 }
ccd979bd
MF
561
562 status = ocfs2_journal_dirty(handle, *new_fe_bh);
563 if (status < 0) {
564 mlog_errno(status);
565 goto leave;
566 }
567
b657c95c 568 ocfs2_populate_inode(inode, fe, 1);
ccd979bd 569 ocfs2_inode_set_new(osb, inode);
c271c5c2
SM
570 if (!ocfs2_mount_local(osb)) {
571 status = ocfs2_create_new_inode_locks(inode);
572 if (status < 0)
573 mlog_errno(status);
574 }
ccd979bd
MF
575
576 status = 0; /* error in ocfs2_create_new_inode_locks is not
577 * critical */
578
ccd979bd
MF
579leave:
580 if (status < 0) {
581 if (*new_fe_bh) {
582 brelse(*new_fe_bh);
583 *new_fe_bh = NULL;
584 }
ccd979bd
MF
585 }
586
587 mlog_exit(status);
588 return status;
589}
590
591static int ocfs2_mkdir(struct inode *dir,
592 struct dentry *dentry,
593 int mode)
594{
595 int ret;
596
597 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
598 dentry->d_name.len, dentry->d_name.name);
599 ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
600 mlog_exit(ret);
601
602 return ret;
603}
604
605static int ocfs2_create(struct inode *dir,
606 struct dentry *dentry,
607 int mode,
608 struct nameidata *nd)
609{
610 int ret;
611
612 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
613 dentry->d_name.len, dentry->d_name.name);
614 ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
615 mlog_exit(ret);
616
617 return ret;
618}
619
620static int ocfs2_link(struct dentry *old_dentry,
621 struct inode *dir,
622 struct dentry *dentry)
623{
1fabe148 624 handle_t *handle;
ccd979bd
MF
625 struct inode *inode = old_dentry->d_inode;
626 int err;
627 struct buffer_head *fe_bh = NULL;
628 struct buffer_head *parent_fe_bh = NULL;
ccd979bd
MF
629 struct ocfs2_dinode *fe = NULL;
630 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4a12ca3a 631 struct ocfs2_dir_lookup_result lookup = { NULL, };
ccd979bd
MF
632
633 mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
634 old_dentry->d_name.len, old_dentry->d_name.name,
635 dentry->d_name.len, dentry->d_name.name);
636
123a9643
MF
637 if (S_ISDIR(inode->i_mode))
638 return -EPERM;
ccd979bd 639
e63aecb6 640 err = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
ccd979bd
MF
641 if (err < 0) {
642 if (err != -ENOENT)
643 mlog_errno(err);
123a9643 644 return err;
ccd979bd
MF
645 }
646
0f62de2c
TY
647 if (!dir->i_nlink) {
648 err = -ENOENT;
123a9643 649 goto out;
0f62de2c
TY
650 }
651
ccd979bd
MF
652 err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
653 dentry->d_name.len);
654 if (err)
123a9643 655 goto out;
ccd979bd
MF
656
657 err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
658 dentry->d_name.name,
4a12ca3a 659 dentry->d_name.len, &lookup);
ccd979bd
MF
660 if (err < 0) {
661 mlog_errno(err);
123a9643 662 goto out;
ccd979bd
MF
663 }
664
e63aecb6 665 err = ocfs2_inode_lock(inode, &fe_bh, 1);
ccd979bd
MF
666 if (err < 0) {
667 if (err != -ENOENT)
668 mlog_errno(err);
123a9643 669 goto out;
ccd979bd
MF
670 }
671
672 fe = (struct ocfs2_dinode *) fe_bh->b_data;
673 if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) {
674 err = -EMLINK;
123a9643 675 goto out_unlock_inode;
ccd979bd
MF
676 }
677
a90714c1 678 handle = ocfs2_start_trans(osb, ocfs2_link_credits(osb->sb));
ccd979bd
MF
679 if (IS_ERR(handle)) {
680 err = PTR_ERR(handle);
681 handle = NULL;
682 mlog_errno(err);
123a9643 683 goto out_unlock_inode;
ccd979bd
MF
684 }
685
13723d00
JB
686 err = ocfs2_journal_access_di(handle, inode, fe_bh,
687 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
688 if (err < 0) {
689 mlog_errno(err);
123a9643 690 goto out_commit;
ccd979bd
MF
691 }
692
d8c76e6f 693 inc_nlink(inode);
ccd979bd
MF
694 inode->i_ctime = CURRENT_TIME;
695 fe->i_links_count = cpu_to_le16(inode->i_nlink);
696 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
697 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
698
699 err = ocfs2_journal_dirty(handle, fe_bh);
700 if (err < 0) {
701 le16_add_cpu(&fe->i_links_count, -1);
9a53c3a7 702 drop_nlink(inode);
ccd979bd 703 mlog_errno(err);
123a9643 704 goto out_commit;
ccd979bd
MF
705 }
706
707 err = ocfs2_add_entry(handle, dentry, inode,
708 OCFS2_I(inode)->ip_blkno,
4a12ca3a 709 parent_fe_bh, &lookup);
ccd979bd
MF
710 if (err) {
711 le16_add_cpu(&fe->i_links_count, -1);
9a53c3a7 712 drop_nlink(inode);
ccd979bd 713 mlog_errno(err);
123a9643 714 goto out_commit;
ccd979bd
MF
715 }
716
0027dd5b 717 err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
379dfe9d
MF
718 if (err) {
719 mlog_errno(err);
123a9643 720 goto out_commit;
379dfe9d
MF
721 }
722
ccd979bd
MF
723 atomic_inc(&inode->i_count);
724 dentry->d_op = &ocfs2_dentry_ops;
725 d_instantiate(dentry, inode);
123a9643
MF
726
727out_commit:
02dc1af4 728 ocfs2_commit_trans(osb, handle);
123a9643 729out_unlock_inode:
e63aecb6 730 ocfs2_inode_unlock(inode, 1);
123a9643
MF
731
732out:
e63aecb6 733 ocfs2_inode_unlock(dir, 1);
123a9643 734
a81cb88b
MF
735 brelse(fe_bh);
736 brelse(parent_fe_bh);
ccd979bd 737
4a12ca3a
MF
738 ocfs2_free_dir_lookup_result(&lookup);
739
ccd979bd
MF
740 mlog_exit(err);
741
742 return err;
743}
744
379dfe9d
MF
745/*
746 * Takes and drops an exclusive lock on the given dentry. This will
747 * force other nodes to drop it.
748 */
749static int ocfs2_remote_dentry_delete(struct dentry *dentry)
750{
751 int ret;
752
753 ret = ocfs2_dentry_lock(dentry, 1);
754 if (ret)
755 mlog_errno(ret);
756 else
757 ocfs2_dentry_unlock(dentry, 1);
758
759 return ret;
760}
761
17ff7856
MF
762static inline int inode_is_unlinkable(struct inode *inode)
763{
764 if (S_ISDIR(inode->i_mode)) {
765 if (inode->i_nlink == 2)
766 return 1;
767 return 0;
768 }
769
770 if (inode->i_nlink == 1)
771 return 1;
772 return 0;
773}
774
ccd979bd
MF
775static int ocfs2_unlink(struct inode *dir,
776 struct dentry *dentry)
777{
778 int status;
30a4f5e8 779 int child_locked = 0;
ccd979bd 780 struct inode *inode = dentry->d_inode;
5098c27b 781 struct inode *orphan_dir = NULL;
ccd979bd
MF
782 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
783 u64 blkno;
784 struct ocfs2_dinode *fe = NULL;
785 struct buffer_head *fe_bh = NULL;
786 struct buffer_head *parent_node_bh = NULL;
1fabe148 787 handle_t *handle = NULL;
ccd979bd 788 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
4a12ca3a
MF
789 struct ocfs2_dir_lookup_result lookup = { NULL, };
790 struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
ccd979bd
MF
791
792 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
793 dentry->d_name.len, dentry->d_name.name);
794
795 BUG_ON(dentry->d_parent->d_inode != dir);
796
b0697053 797 mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
ccd979bd
MF
798
799 if (inode == osb->root_inode) {
800 mlog(0, "Cannot delete the root directory\n");
30a4f5e8 801 return -EPERM;
ccd979bd
MF
802 }
803
e63aecb6 804 status = ocfs2_inode_lock(dir, &parent_node_bh, 1);
ccd979bd
MF
805 if (status < 0) {
806 if (status != -ENOENT)
807 mlog_errno(status);
30a4f5e8 808 return status;
ccd979bd
MF
809 }
810
811 status = ocfs2_find_files_on_disk(dentry->d_name.name,
4a12ca3a
MF
812 dentry->d_name.len, &blkno, dir,
813 &lookup);
ccd979bd
MF
814 if (status < 0) {
815 if (status != -ENOENT)
816 mlog_errno(status);
817 goto leave;
818 }
819
820 if (OCFS2_I(inode)->ip_blkno != blkno) {
821 status = -ENOENT;
822
b0697053
MF
823 mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
824 (unsigned long long)OCFS2_I(inode)->ip_blkno,
825 (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
ccd979bd
MF
826 goto leave;
827 }
828
e63aecb6 829 status = ocfs2_inode_lock(inode, &fe_bh, 1);
ccd979bd
MF
830 if (status < 0) {
831 if (status != -ENOENT)
832 mlog_errno(status);
833 goto leave;
834 }
30a4f5e8 835 child_locked = 1;
ccd979bd
MF
836
837 if (S_ISDIR(inode->i_mode)) {
838 if (!ocfs2_empty_dir(inode)) {
839 status = -ENOTEMPTY;
840 goto leave;
841 } else if (inode->i_nlink != 2) {
842 status = -ENOTEMPTY;
843 goto leave;
844 }
845 }
846
379dfe9d 847 status = ocfs2_remote_dentry_delete(dentry);
ccd979bd 848 if (status < 0) {
34d024f8 849 /* This remote delete should succeed under all normal
ccd979bd
MF
850 * circumstances. */
851 mlog_errno(status);
852 goto leave;
853 }
854
17ff7856 855 if (inode_is_unlinkable(inode)) {
5098c27b 856 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode,
4a12ca3a 857 orphan_name, &orphan_insert);
ccd979bd
MF
858 if (status < 0) {
859 mlog_errno(status);
860 goto leave;
861 }
862 }
863
a90714c1 864 handle = ocfs2_start_trans(osb, ocfs2_unlink_credits(osb->sb));
ccd979bd
MF
865 if (IS_ERR(handle)) {
866 status = PTR_ERR(handle);
867 handle = NULL;
868 mlog_errno(status);
869 goto leave;
870 }
871
13723d00
JB
872 status = ocfs2_journal_access_di(handle, inode, fe_bh,
873 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
874 if (status < 0) {
875 mlog_errno(status);
876 goto leave;
877 }
878
879 fe = (struct ocfs2_dinode *) fe_bh->b_data;
880
17ff7856 881 if (inode_is_unlinkable(inode)) {
ccd979bd 882 status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
4a12ca3a 883 &orphan_insert, orphan_dir);
ccd979bd
MF
884 if (status < 0) {
885 mlog_errno(status);
886 goto leave;
887 }
888 }
889
890 /* delete the name from the parent dir */
4a12ca3a 891 status = ocfs2_delete_entry(handle, dir, &lookup);
ccd979bd
MF
892 if (status < 0) {
893 mlog_errno(status);
894 goto leave;
895 }
896
17ff7856
MF
897 if (S_ISDIR(inode->i_mode))
898 drop_nlink(inode);
899 drop_nlink(inode);
ccd979bd 900 fe->i_links_count = cpu_to_le16(inode->i_nlink);
ccd979bd
MF
901
902 status = ocfs2_journal_dirty(handle, fe_bh);
903 if (status < 0) {
904 mlog_errno(status);
905 goto leave;
906 }
907
592282cf
MF
908 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
909 if (S_ISDIR(inode->i_mode))
17ff7856 910 drop_nlink(dir);
592282cf
MF
911
912 status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
913 if (status < 0) {
914 mlog_errno(status);
915 if (S_ISDIR(inode->i_mode))
d8c76e6f 916 inc_nlink(dir);
ccd979bd
MF
917 }
918
919leave:
ccd979bd 920 if (handle)
02dc1af4 921 ocfs2_commit_trans(osb, handle);
ccd979bd 922
30a4f5e8 923 if (child_locked)
e63aecb6 924 ocfs2_inode_unlock(inode, 1);
30a4f5e8 925
e63aecb6 926 ocfs2_inode_unlock(dir, 1);
30a4f5e8 927
5098c27b
MF
928 if (orphan_dir) {
929 /* This was locked for us in ocfs2_prepare_orphan_dir() */
e63aecb6 930 ocfs2_inode_unlock(orphan_dir, 1);
5098c27b
MF
931 mutex_unlock(&orphan_dir->i_mutex);
932 iput(orphan_dir);
933 }
934
a81cb88b 935 brelse(fe_bh);
a81cb88b 936 brelse(parent_node_bh);
4a12ca3a
MF
937
938 ocfs2_free_dir_lookup_result(&orphan_insert);
939 ocfs2_free_dir_lookup_result(&lookup);
ccd979bd
MF
940
941 mlog_exit(status);
942
943 return status;
944}
945
946/*
947 * The only place this should be used is rename!
948 * if they have the same id, then the 1st one is the only one locked.
949 */
950static int ocfs2_double_lock(struct ocfs2_super *osb,
ccd979bd
MF
951 struct buffer_head **bh1,
952 struct inode *inode1,
953 struct buffer_head **bh2,
954 struct inode *inode2)
955{
956 int status;
957 struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
958 struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
959 struct buffer_head **tmpbh;
960 struct inode *tmpinode;
961
b0697053
MF
962 mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
963 (unsigned long long)oi1->ip_blkno,
964 (unsigned long long)oi2->ip_blkno);
ccd979bd 965
ccd979bd
MF
966 if (*bh1)
967 *bh1 = NULL;
968 if (*bh2)
969 *bh2 = NULL;
970
971 /* we always want to lock the one with the lower lockid first. */
972 if (oi1->ip_blkno != oi2->ip_blkno) {
973 if (oi1->ip_blkno < oi2->ip_blkno) {
974 /* switch id1 and id2 around */
975 mlog(0, "switching them around...\n");
976 tmpbh = bh2;
977 bh2 = bh1;
978 bh1 = tmpbh;
979
980 tmpinode = inode2;
981 inode2 = inode1;
982 inode1 = tmpinode;
983 }
984 /* lock id2 */
e63aecb6 985 status = ocfs2_inode_lock(inode2, bh2, 1);
ccd979bd
MF
986 if (status < 0) {
987 if (status != -ENOENT)
988 mlog_errno(status);
989 goto bail;
990 }
991 }
8d5596c6 992
ccd979bd 993 /* lock id1 */
e63aecb6 994 status = ocfs2_inode_lock(inode1, bh1, 1);
ccd979bd 995 if (status < 0) {
8d5596c6
MF
996 /*
997 * An error return must mean that no cluster locks
998 * were held on function exit.
999 */
1000 if (oi1->ip_blkno != oi2->ip_blkno)
e63aecb6 1001 ocfs2_inode_unlock(inode2, 1);
8d5596c6 1002
ccd979bd
MF
1003 if (status != -ENOENT)
1004 mlog_errno(status);
ccd979bd 1005 }
8d5596c6 1006
ccd979bd
MF
1007bail:
1008 mlog_exit(status);
1009 return status;
1010}
1011
8d5596c6
MF
1012static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
1013{
e63aecb6 1014 ocfs2_inode_unlock(inode1, 1);
8d5596c6
MF
1015
1016 if (inode1 != inode2)
e63aecb6 1017 ocfs2_inode_unlock(inode2, 1);
8d5596c6
MF
1018}
1019
ccd979bd
MF
1020static int ocfs2_rename(struct inode *old_dir,
1021 struct dentry *old_dentry,
1022 struct inode *new_dir,
1023 struct dentry *new_dentry)
1024{
4a12ca3a
MF
1025 int status = 0, rename_lock = 0, parents_locked = 0, target_exists = 0;
1026 int old_child_locked = 0, new_child_locked = 0, update_dot_dot = 0;
ccd979bd
MF
1027 struct inode *old_inode = old_dentry->d_inode;
1028 struct inode *new_inode = new_dentry->d_inode;
5098c27b 1029 struct inode *orphan_dir = NULL;
ccd979bd
MF
1030 struct ocfs2_dinode *newfe = NULL;
1031 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
1032 struct buffer_head *orphan_entry_bh = NULL;
1033 struct buffer_head *newfe_bh = NULL;
592282cf 1034 struct buffer_head *old_inode_bh = NULL;
ccd979bd
MF
1035 struct buffer_head *insert_entry_bh = NULL;
1036 struct ocfs2_super *osb = NULL;
5b6a3a2b 1037 u64 newfe_blkno, old_de_ino;
1fabe148 1038 handle_t *handle = NULL;
ccd979bd
MF
1039 struct buffer_head *old_dir_bh = NULL;
1040 struct buffer_head *new_dir_bh = NULL;
592282cf 1041 nlink_t old_dir_nlink = old_dir->i_nlink;
480214d7 1042 struct ocfs2_dinode *old_di;
4a12ca3a
MF
1043 struct ocfs2_dir_lookup_result old_inode_dot_dot_res = { NULL, };
1044 struct ocfs2_dir_lookup_result target_lookup_res = { NULL, };
1045 struct ocfs2_dir_lookup_result old_entry_lookup = { NULL, };
1046 struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
1047 struct ocfs2_dir_lookup_result target_insert = { NULL, };
ccd979bd
MF
1048
1049 /* At some point it might be nice to break this function up a
1050 * bit. */
1051
1052 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
1053 old_dir, old_dentry, new_dir, new_dentry,
1054 old_dentry->d_name.len, old_dentry->d_name.name,
1055 new_dentry->d_name.len, new_dentry->d_name.name);
1056
1057 osb = OCFS2_SB(old_dir->i_sb);
1058
1059 if (new_inode) {
1060 if (!igrab(new_inode))
1061 BUG();
1062 }
1063
1b3c3714 1064 /* Assume a directory hierarchy thusly:
ccd979bd
MF
1065 * a/b/c
1066 * a/d
1067 * a,b,c, and d are all directories.
1068 *
1069 * from cwd of 'a' on both nodes:
1070 * node1: mv b/c d
1071 * node2: mv d b/c
1072 *
1073 * And that's why, just like the VFS, we need a file system
1074 * rename lock. */
5dabd695 1075 if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) {
ccd979bd
MF
1076 status = ocfs2_rename_lock(osb);
1077 if (status < 0) {
1078 mlog_errno(status);
1079 goto bail;
1080 }
1081 rename_lock = 1;
1082 }
1083
ccd979bd 1084 /* if old and new are the same, this'll just do one lock. */
8d5596c6
MF
1085 status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
1086 &new_dir_bh, new_dir);
ccd979bd
MF
1087 if (status < 0) {
1088 mlog_errno(status);
1089 goto bail;
1090 }
8d5596c6 1091 parents_locked = 1;
ccd979bd
MF
1092
1093 /* make sure both dirs have bhs
1094 * get an extra ref on old_dir_bh if old==new */
1095 if (!new_dir_bh) {
1096 if (old_dir_bh) {
1097 new_dir_bh = old_dir_bh;
1098 get_bh(new_dir_bh);
1099 } else {
1100 mlog(ML_ERROR, "no old_dir_bh!\n");
1101 status = -EIO;
1102 goto bail;
1103 }
1104 }
1105
379dfe9d 1106 /*
592282cf 1107 * Aside from allowing a meta data update, the locking here
34d024f8
MF
1108 * also ensures that the downconvert thread on other nodes
1109 * won't have to concurrently downconvert the inode and the
1110 * dentry locks.
379dfe9d 1111 */
e63aecb6 1112 status = ocfs2_inode_lock(old_inode, &old_inode_bh, 1);
379dfe9d
MF
1113 if (status < 0) {
1114 if (status != -ENOENT)
ccd979bd 1115 mlog_errno(status);
379dfe9d
MF
1116 goto bail;
1117 }
8d5596c6 1118 old_child_locked = 1;
ccd979bd 1119
379dfe9d
MF
1120 status = ocfs2_remote_dentry_delete(old_dentry);
1121 if (status < 0) {
1122 mlog_errno(status);
1123 goto bail;
1124 }
1125
1126 if (S_ISDIR(old_inode->i_mode)) {
38760e24
MF
1127 u64 old_inode_parent;
1128
4a12ca3a 1129 update_dot_dot = 1;
38760e24 1130 status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent,
4a12ca3a
MF
1131 old_inode,
1132 &old_inode_dot_dot_res);
38760e24
MF
1133 if (status) {
1134 status = -EIO;
ccd979bd 1135 goto bail;
38760e24 1136 }
ccd979bd 1137
38760e24
MF
1138 if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) {
1139 status = -EIO;
ccd979bd 1140 goto bail;
38760e24
MF
1141 }
1142
1143 if (!new_inode && new_dir != old_dir &&
1144 new_dir->i_nlink >= OCFS2_LINK_MAX) {
1145 status = -EMLINK;
ccd979bd 1146 goto bail;
38760e24 1147 }
ccd979bd
MF
1148 }
1149
5b6a3a2b
MF
1150 status = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name,
1151 old_dentry->d_name.len,
1152 &old_de_ino);
1153 if (status) {
1154 status = -ENOENT;
ccd979bd 1155 goto bail;
5b6a3a2b 1156 }
ccd979bd
MF
1157
1158 /*
1159 * Check for inode number is _not_ due to possible IO errors.
1160 * We might rmdir the source, keep it as pwd of some process
1161 * and merrily kill the link to whatever was created under the
1162 * same name. Goodbye sticky bit ;-<
1163 */
5b6a3a2b
MF
1164 if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) {
1165 status = -ENOENT;
ccd979bd 1166 goto bail;
5b6a3a2b 1167 }
ccd979bd
MF
1168
1169 /* check if the target already exists (in which case we need
1170 * to delete it */
1171 status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
1172 new_dentry->d_name.len,
4a12ca3a
MF
1173 &newfe_blkno, new_dir,
1174 &target_lookup_res);
ccd979bd
MF
1175 /* The only error we allow here is -ENOENT because the new
1176 * file not existing is perfectly valid. */
1177 if ((status < 0) && (status != -ENOENT)) {
1178 /* If we cannot find the file specified we should just */
1179 /* return the error... */
1180 mlog_errno(status);
1181 goto bail;
1182 }
4a12ca3a
MF
1183 if (status == 0)
1184 target_exists = 1;
ccd979bd 1185
4a12ca3a 1186 if (!target_exists && new_inode) {
e325a88f
SE
1187 /*
1188 * Target was unlinked by another node while we were
1189 * waiting to get to ocfs2_rename(). There isn't
1190 * anything we can do here to help the situation, so
1191 * bubble up the appropriate error.
1192 */
1193 status = -ENOENT;
1194 goto bail;
1195 }
ccd979bd
MF
1196
1197 /* In case we need to overwrite an existing file, we blow it
1198 * away first */
4a12ca3a 1199 if (target_exists) {
ccd979bd
MF
1200 /* VFS didn't think there existed an inode here, but
1201 * someone else in the cluster must have raced our
1202 * rename to create one. Today we error cleanly, in
1203 * the future we should consider calling iget to build
1204 * a new struct inode for this entry. */
1205 if (!new_inode) {
1206 status = -EACCES;
1207
1208 mlog(0, "We found an inode for name %.*s but VFS "
1209 "didn't give us one.\n", new_dentry->d_name.len,
1210 new_dentry->d_name.name);
1211 goto bail;
1212 }
1213
1214 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
1215 status = -EACCES;
1216
b0697053
MF
1217 mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
1218 (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
1219 (unsigned long long)newfe_blkno,
ccd979bd
MF
1220 OCFS2_I(new_inode)->ip_flags);
1221 goto bail;
1222 }
1223
e63aecb6 1224 status = ocfs2_inode_lock(new_inode, &newfe_bh, 1);
ccd979bd
MF
1225 if (status < 0) {
1226 if (status != -ENOENT)
1227 mlog_errno(status);
1228 goto bail;
1229 }
8d5596c6 1230 new_child_locked = 1;
ccd979bd 1231
379dfe9d 1232 status = ocfs2_remote_dentry_delete(new_dentry);
ccd979bd
MF
1233 if (status < 0) {
1234 mlog_errno(status);
1235 goto bail;
1236 }
1237
1238 newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
1239
4a12ca3a
MF
1240 mlog(0, "aha rename over existing... new_blkno=%llu "
1241 "newfebh=%p bhblocknr=%llu\n",
b0697053 1242 (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
ccd979bd
MF
1243 (unsigned long long)newfe_bh->b_blocknr : 0ULL);
1244
1245 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
5098c27b 1246 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
ccd979bd
MF
1247 new_inode,
1248 orphan_name,
4a12ca3a 1249 &orphan_insert);
ccd979bd
MF
1250 if (status < 0) {
1251 mlog_errno(status);
1252 goto bail;
1253 }
1254 }
1255 } else {
1256 BUG_ON(new_dentry->d_parent->d_inode != new_dir);
1257
1258 status = ocfs2_check_dir_for_entry(new_dir,
1259 new_dentry->d_name.name,
1260 new_dentry->d_name.len);
1261 if (status)
1262 goto bail;
1263
1264 status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
1265 new_dentry->d_name.name,
1266 new_dentry->d_name.len,
4a12ca3a 1267 &target_insert);
ccd979bd
MF
1268 if (status < 0) {
1269 mlog_errno(status);
1270 goto bail;
1271 }
1272 }
1273
a90714c1 1274 handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb));
ccd979bd
MF
1275 if (IS_ERR(handle)) {
1276 status = PTR_ERR(handle);
1277 handle = NULL;
1278 mlog_errno(status);
1279 goto bail;
1280 }
1281
4a12ca3a 1282 if (target_exists) {
ccd979bd
MF
1283 if (S_ISDIR(new_inode->i_mode)) {
1284 if (!ocfs2_empty_dir(new_inode) ||
1285 new_inode->i_nlink != 2) {
1286 status = -ENOTEMPTY;
1287 goto bail;
1288 }
1289 }
13723d00
JB
1290 status = ocfs2_journal_access_di(handle, new_inode, newfe_bh,
1291 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
1292 if (status < 0) {
1293 mlog_errno(status);
1294 goto bail;
1295 }
1296
1297 if (S_ISDIR(new_inode->i_mode) ||
1298 (newfe->i_links_count == cpu_to_le16(1))){
1299 status = ocfs2_orphan_add(osb, handle, new_inode,
1300 newfe, orphan_name,
4a12ca3a 1301 &orphan_insert, orphan_dir);
ccd979bd
MF
1302 if (status < 0) {
1303 mlog_errno(status);
1304 goto bail;
1305 }
1306 }
1307
1308 /* change the dirent to point to the correct inode */
4a12ca3a
MF
1309 status = ocfs2_update_entry(new_dir, handle, &target_lookup_res,
1310 old_inode);
ccd979bd
MF
1311 if (status < 0) {
1312 mlog_errno(status);
1313 goto bail;
1314 }
ccd979bd 1315 new_dir->i_version++;
ccd979bd
MF
1316
1317 if (S_ISDIR(new_inode->i_mode))
1318 newfe->i_links_count = 0;
1319 else
1320 le16_add_cpu(&newfe->i_links_count, -1);
1321
1322 status = ocfs2_journal_dirty(handle, newfe_bh);
1323 if (status < 0) {
1324 mlog_errno(status);
1325 goto bail;
1326 }
1327 } else {
1328 /* if the name was not found in new_dir, add it now */
1329 status = ocfs2_add_entry(handle, new_dentry, old_inode,
1330 OCFS2_I(old_inode)->ip_blkno,
4a12ca3a 1331 new_dir_bh, &target_insert);
ccd979bd
MF
1332 }
1333
1334 old_inode->i_ctime = CURRENT_TIME;
1335 mark_inode_dirty(old_inode);
480214d7 1336
13723d00
JB
1337 status = ocfs2_journal_access_di(handle, old_inode, old_inode_bh,
1338 OCFS2_JOURNAL_ACCESS_WRITE);
480214d7
SM
1339 if (status >= 0) {
1340 old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
1341
1342 old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
1343 old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
1344
1345 status = ocfs2_journal_dirty(handle, old_inode_bh);
1346 if (status < 0)
1347 mlog_errno(status);
1348 } else
1349 mlog_errno(status);
ccd979bd 1350
5b6a3a2b
MF
1351 /*
1352 * Now that the name has been added to new_dir, remove the old name.
1353 *
1354 * We don't keep any directory entry context around until now
1355 * because the insert might have changed the type of directory
1356 * we're dealing with.
1357 */
4a12ca3a
MF
1358 status = ocfs2_find_entry(old_dentry->d_name.name,
1359 old_dentry->d_name.len, old_dir,
1360 &old_entry_lookup);
1361 if (status)
5b6a3a2b 1362 goto bail;
5b6a3a2b 1363
4a12ca3a 1364 status = ocfs2_delete_entry(handle, old_dir, &old_entry_lookup);
ccd979bd
MF
1365 if (status < 0) {
1366 mlog_errno(status);
1367 goto bail;
1368 }
1369
1370 if (new_inode) {
1371 new_inode->i_nlink--;
1372 new_inode->i_ctime = CURRENT_TIME;
1373 }
1374 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
4a12ca3a
MF
1375
1376 if (update_dot_dot) {
1377 status = ocfs2_update_entry(old_inode, handle,
1378 &old_inode_dot_dot_res, new_dir);
ccd979bd
MF
1379 old_dir->i_nlink--;
1380 if (new_inode) {
1381 new_inode->i_nlink--;
1382 } else {
d8c76e6f 1383 inc_nlink(new_dir);
ccd979bd
MF
1384 mark_inode_dirty(new_dir);
1385 }
1386 }
1387 mark_inode_dirty(old_dir);
592282cf
MF
1388 ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
1389 if (new_inode) {
ccd979bd 1390 mark_inode_dirty(new_inode);
592282cf
MF
1391 ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
1392 }
ccd979bd 1393
592282cf
MF
1394 if (old_dir != new_dir) {
1395 /* Keep the same times on both directories.*/
1396 new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
1397
1398 /*
1399 * This will also pick up the i_nlink change from the
1400 * block above.
1401 */
1402 ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
1403 }
ccd979bd
MF
1404
1405 if (old_dir_nlink != old_dir->i_nlink) {
1406 if (!old_dir_bh) {
1407 mlog(ML_ERROR, "need to change nlink for old dir "
b0697053
MF
1408 "%llu from %d to %d but bh is NULL!\n",
1409 (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
1410 (int)old_dir_nlink, old_dir->i_nlink);
ccd979bd
MF
1411 } else {
1412 struct ocfs2_dinode *fe;
13723d00
JB
1413 status = ocfs2_journal_access_di(handle, old_dir,
1414 old_dir_bh,
1415 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
1416 fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
1417 fe->i_links_count = cpu_to_le16(old_dir->i_nlink);
1418 status = ocfs2_journal_dirty(handle, old_dir_bh);
1419 }
1420 }
1421
379dfe9d 1422 ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
ccd979bd
MF
1423 status = 0;
1424bail:
1425 if (rename_lock)
1426 ocfs2_rename_unlock(osb);
1427
1428 if (handle)
02dc1af4 1429 ocfs2_commit_trans(osb, handle);
ccd979bd 1430
8d5596c6
MF
1431 if (parents_locked)
1432 ocfs2_double_unlock(old_dir, new_dir);
1433
1434 if (old_child_locked)
e63aecb6 1435 ocfs2_inode_unlock(old_inode, 1);
8d5596c6
MF
1436
1437 if (new_child_locked)
e63aecb6 1438 ocfs2_inode_unlock(new_inode, 1);
8d5596c6 1439
5098c27b
MF
1440 if (orphan_dir) {
1441 /* This was locked for us in ocfs2_prepare_orphan_dir() */
e63aecb6 1442 ocfs2_inode_unlock(orphan_dir, 1);
5098c27b
MF
1443 mutex_unlock(&orphan_dir->i_mutex);
1444 iput(orphan_dir);
1445 }
1446
ccd979bd
MF
1447 if (new_inode)
1448 sync_mapping_buffers(old_inode->i_mapping);
1449
1450 if (new_inode)
1451 iput(new_inode);
4a12ca3a
MF
1452
1453 ocfs2_free_dir_lookup_result(&target_lookup_res);
1454 ocfs2_free_dir_lookup_result(&old_entry_lookup);
1455 ocfs2_free_dir_lookup_result(&old_inode_dot_dot_res);
1456 ocfs2_free_dir_lookup_result(&orphan_insert);
1457 ocfs2_free_dir_lookup_result(&target_insert);
1458
a81cb88b
MF
1459 brelse(newfe_bh);
1460 brelse(old_inode_bh);
1461 brelse(old_dir_bh);
1462 brelse(new_dir_bh);
a81cb88b
MF
1463 brelse(orphan_entry_bh);
1464 brelse(insert_entry_bh);
ccd979bd
MF
1465
1466 mlog_exit(status);
1467
1468 return status;
1469}
1470
1471/*
1472 * we expect i_size = strlen(symname). Copy symname into the file
1473 * data, including the null terminator.
1474 */
1475static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
1fabe148 1476 handle_t *handle,
ccd979bd
MF
1477 struct inode *inode,
1478 const char *symname)
1479{
1480 struct buffer_head **bhs = NULL;
1481 const char *c;
1482 struct super_block *sb = osb->sb;
4f902c37 1483 u64 p_blkno, p_blocks;
ccd979bd
MF
1484 int virtual, blocks, status, i, bytes_left;
1485
1486 bytes_left = i_size_read(inode) + 1;
1487 /* we can't trust i_blocks because we're actually going to
1488 * write i_size + 1 bytes. */
1489 blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1490
5515eff8
AM
1491 mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
1492 (unsigned long long)inode->i_blocks,
1493 i_size_read(inode), blocks);
ccd979bd
MF
1494
1495 /* Sanity check -- make sure we're going to fit. */
1496 if (bytes_left >
1497 ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1498 status = -EIO;
1499 mlog_errno(status);
1500 goto bail;
1501 }
1502
1503 bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
1504 if (!bhs) {
1505 status = -ENOMEM;
1506 mlog_errno(status);
1507 goto bail;
1508 }
1509
49cb8d2d
MF
1510 status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
1511 NULL);
ccd979bd
MF
1512 if (status < 0) {
1513 mlog_errno(status);
1514 goto bail;
1515 }
1516
1517 /* links can never be larger than one cluster so we know this
1518 * is all going to be contiguous, but do a sanity check
1519 * anyway. */
1520 if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
1521 status = -EIO;
1522 mlog_errno(status);
1523 goto bail;
1524 }
1525
1526 virtual = 0;
1527 while(bytes_left > 0) {
1528 c = &symname[virtual * sb->s_blocksize];
1529
1530 bhs[virtual] = sb_getblk(sb, p_blkno);
1531 if (!bhs[virtual]) {
1532 status = -ENOMEM;
1533 mlog_errno(status);
1534 goto bail;
1535 }
1536 ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
1537
1538 status = ocfs2_journal_access(handle, inode, bhs[virtual],
1539 OCFS2_JOURNAL_ACCESS_CREATE);
1540 if (status < 0) {
1541 mlog_errno(status);
1542 goto bail;
1543 }
1544
1545 memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
1546
1547 memcpy(bhs[virtual]->b_data, c,
1548 (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
1549 bytes_left);
1550
1551 status = ocfs2_journal_dirty(handle, bhs[virtual]);
1552 if (status < 0) {
1553 mlog_errno(status);
1554 goto bail;
1555 }
1556
1557 virtual++;
1558 p_blkno++;
1559 bytes_left -= sb->s_blocksize;
1560 }
1561
1562 status = 0;
1563bail:
1564
1565 if (bhs) {
1566 for(i = 0; i < blocks; i++)
a81cb88b 1567 brelse(bhs[i]);
ccd979bd
MF
1568 kfree(bhs);
1569 }
1570
1571 mlog_exit(status);
1572 return status;
1573}
1574
1575static int ocfs2_symlink(struct inode *dir,
1576 struct dentry *dentry,
1577 const char *symname)
1578{
1579 int status, l, credits;
1580 u64 newsize;
1581 struct ocfs2_super *osb = NULL;
1582 struct inode *inode = NULL;
1583 struct super_block *sb;
1584 struct buffer_head *new_fe_bh = NULL;
ccd979bd
MF
1585 struct buffer_head *parent_fe_bh = NULL;
1586 struct ocfs2_dinode *fe = NULL;
1587 struct ocfs2_dinode *dirfe;
1fabe148 1588 handle_t *handle = NULL;
ccd979bd
MF
1589 struct ocfs2_alloc_context *inode_ac = NULL;
1590 struct ocfs2_alloc_context *data_ac = NULL;
534eaddd
TY
1591 struct ocfs2_alloc_context *xattr_ac = NULL;
1592 int want_clusters = 0;
1593 int xattr_credits = 0;
1594 struct ocfs2_security_xattr_info si = {
1595 .enable = 1,
1596 };
a90714c1 1597 int did_quota = 0, did_quota_inode = 0;
4a12ca3a 1598 struct ocfs2_dir_lookup_result lookup = { NULL, };
ccd979bd
MF
1599
1600 mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
1601 dentry, symname, dentry->d_name.len, dentry->d_name.name);
1602
1603 sb = dir->i_sb;
1604 osb = OCFS2_SB(sb);
1605
1606 l = strlen(symname) + 1;
1607
1608 credits = ocfs2_calc_symlink_credits(sb);
1609
ccd979bd 1610 /* lock the parent directory */
e63aecb6 1611 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
ccd979bd
MF
1612 if (status < 0) {
1613 if (status != -ENOENT)
1614 mlog_errno(status);
6d8fc40e 1615 return status;
ccd979bd
MF
1616 }
1617
1618 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1619 if (!dirfe->i_links_count) {
1620 /* can't make a file in a deleted directory. */
1621 status = -ENOENT;
1622 goto bail;
1623 }
1624
1625 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
1626 dentry->d_name.len);
1627 if (status)
1628 goto bail;
1629
1630 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
1631 dentry->d_name.name,
4a12ca3a 1632 dentry->d_name.len, &lookup);
ccd979bd
MF
1633 if (status < 0) {
1634 mlog_errno(status);
1635 goto bail;
1636 }
1637
da5cbf2f 1638 status = ocfs2_reserve_new_inode(osb, &inode_ac);
ccd979bd
MF
1639 if (status < 0) {
1640 if (status != -ENOSPC)
1641 mlog_errno(status);
1642 goto bail;
1643 }
1644
f5d36202
TY
1645 inode = ocfs2_get_init_inode(dir, S_IFLNK | S_IRWXUGO);
1646 if (!inode) {
1647 status = -ENOMEM;
1648 mlog_errno(status);
1649 goto bail;
1650 }
1651
534eaddd
TY
1652 /* get security xattr */
1653 status = ocfs2_init_security_get(inode, dir, &si);
1654 if (status) {
1655 if (status == -EOPNOTSUPP)
1656 si.enable = 0;
1657 else {
1658 mlog_errno(status);
1659 goto bail;
1660 }
1661 }
1662
1663 /* calculate meta data/clusters for setting security xattr */
1664 if (si.enable) {
1665 status = ocfs2_calc_security_init(dir, &si, &want_clusters,
1666 &xattr_credits, &xattr_ac);
ccd979bd 1667 if (status < 0) {
534eaddd 1668 mlog_errno(status);
ccd979bd
MF
1669 goto bail;
1670 }
1671 }
1672
534eaddd
TY
1673 /* don't reserve bitmap space for fast symlinks. */
1674 if (l > ocfs2_fast_symlink_chars(sb))
1675 want_clusters += 1;
1676
1677 status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
1678 if (status < 0) {
1679 if (status != -ENOSPC)
1680 mlog_errno(status);
1681 goto bail;
1682 }
1683
1684 handle = ocfs2_start_trans(osb, credits + xattr_credits);
ccd979bd
MF
1685 if (IS_ERR(handle)) {
1686 status = PTR_ERR(handle);
1687 handle = NULL;
1688 mlog_errno(status);
1689 goto bail;
1690 }
1691
a90714c1
JK
1692 /* We don't use standard VFS wrapper because we don't want vfs_dq_init
1693 * to be called. */
1694 if (sb_any_quota_active(osb->sb) &&
1695 osb->sb->dq_op->alloc_inode(inode, 1) == NO_QUOTA) {
1696 status = -EDQUOT;
1697 goto bail;
1698 }
1699 did_quota_inode = 1;
1700
f5d36202
TY
1701 status = ocfs2_mknod_locked(osb, dir, inode, dentry,
1702 0, &new_fe_bh, parent_fe_bh, handle,
1703 inode_ac);
ccd979bd
MF
1704 if (status < 0) {
1705 mlog_errno(status);
1706 goto bail;
1707 }
1708
1709 fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
1710 inode->i_rdev = 0;
1711 newsize = l - 1;
1712 if (l > ocfs2_fast_symlink_chars(sb)) {
dcd0538f
MF
1713 u32 offset = 0;
1714
ccd979bd 1715 inode->i_op = &ocfs2_symlink_inode_operations;
a90714c1
JK
1716 if (vfs_dq_alloc_space_nodirty(inode,
1717 ocfs2_clusters_to_bytes(osb->sb, 1))) {
1718 status = -EDQUOT;
1719 goto bail;
1720 }
1721 did_quota = 1;
0eb8d47e
TM
1722 status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0,
1723 new_fe_bh,
1724 handle, data_ac, NULL,
1725 NULL);
ccd979bd
MF
1726 if (status < 0) {
1727 if (status != -ENOSPC && status != -EINTR) {
b0697053
MF
1728 mlog(ML_ERROR,
1729 "Failed to extend file to %llu\n",
1730 (unsigned long long)newsize);
ccd979bd
MF
1731 mlog_errno(status);
1732 status = -ENOSPC;
1733 }
1734 goto bail;
1735 }
1736 i_size_write(inode, newsize);
8110b073 1737 inode->i_blocks = ocfs2_inode_sector_count(inode);
ccd979bd
MF
1738 } else {
1739 inode->i_op = &ocfs2_fast_symlink_inode_operations;
1740 memcpy((char *) fe->id2.i_symlink, symname, l);
1741 i_size_write(inode, newsize);
1742 inode->i_blocks = 0;
1743 }
1744
1745 status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
1746 if (status < 0) {
1747 mlog_errno(status);
1748 goto bail;
1749 }
1750
1751 if (!ocfs2_inode_is_fast_symlink(inode)) {
1752 status = ocfs2_create_symlink_data(osb, handle, inode,
1753 symname);
1754 if (status < 0) {
1755 mlog_errno(status);
1756 goto bail;
1757 }
1758 }
1759
534eaddd
TY
1760 if (si.enable) {
1761 status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
1762 xattr_ac, data_ac);
1763 if (status < 0) {
1764 mlog_errno(status);
1765 goto bail;
1766 }
1767 }
1768
ccd979bd
MF
1769 status = ocfs2_add_entry(handle, dentry, inode,
1770 le64_to_cpu(fe->i_blkno), parent_fe_bh,
4a12ca3a 1771 &lookup);
ccd979bd
MF
1772 if (status < 0) {
1773 mlog_errno(status);
1774 goto bail;
1775 }
1776
0027dd5b 1777 status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
379dfe9d
MF
1778 if (status) {
1779 mlog_errno(status);
1780 goto bail;
1781 }
1782
ccd979bd
MF
1783 insert_inode_hash(inode);
1784 dentry->d_op = &ocfs2_dentry_ops;
1785 d_instantiate(dentry, inode);
1786bail:
a90714c1
JK
1787 if (status < 0 && did_quota)
1788 vfs_dq_free_space_nodirty(inode,
1789 ocfs2_clusters_to_bytes(osb->sb, 1));
1790 if (status < 0 && did_quota_inode)
1791 vfs_dq_free_inode(inode);
ccd979bd 1792 if (handle)
02dc1af4 1793 ocfs2_commit_trans(osb, handle);
6d8fc40e 1794
e63aecb6 1795 ocfs2_inode_unlock(dir, 1);
6d8fc40e 1796
a81cb88b
MF
1797 brelse(new_fe_bh);
1798 brelse(parent_fe_bh);
534eaddd
TY
1799 kfree(si.name);
1800 kfree(si.value);
4a12ca3a 1801 ocfs2_free_dir_lookup_result(&lookup);
ccd979bd
MF
1802 if (inode_ac)
1803 ocfs2_free_alloc_context(inode_ac);
1804 if (data_ac)
1805 ocfs2_free_alloc_context(data_ac);
534eaddd
TY
1806 if (xattr_ac)
1807 ocfs2_free_alloc_context(xattr_ac);
f5d36202
TY
1808 if ((status < 0) && inode) {
1809 clear_nlink(inode);
ccd979bd 1810 iput(inode);
f5d36202 1811 }
ccd979bd
MF
1812
1813 mlog_exit(status);
1814
1815 return status;
1816}
1817
ccd979bd
MF
1818static int ocfs2_blkno_stringify(u64 blkno, char *name)
1819{
1820 int status, namelen;
1821
1822 mlog_entry_void();
1823
b0697053
MF
1824 namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
1825 (long long)blkno);
ccd979bd
MF
1826 if (namelen <= 0) {
1827 if (namelen)
1828 status = namelen;
1829 else
1830 status = -EINVAL;
1831 mlog_errno(status);
1832 goto bail;
1833 }
1834 if (namelen != OCFS2_ORPHAN_NAMELEN) {
1835 status = -EINVAL;
1836 mlog_errno(status);
1837 goto bail;
1838 }
1839
1840 mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
1841 namelen);
1842
1843 status = 0;
1844bail:
1845 mlog_exit(status);
1846 return status;
1847}
1848
1849static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
5098c27b 1850 struct inode **ret_orphan_dir,
ccd979bd
MF
1851 struct inode *inode,
1852 char *name,
4a12ca3a 1853 struct ocfs2_dir_lookup_result *lookup)
ccd979bd 1854{
5098c27b 1855 struct inode *orphan_dir_inode;
ccd979bd
MF
1856 struct buffer_head *orphan_dir_bh = NULL;
1857 int status = 0;
1858
1859 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
1860 if (status < 0) {
1861 mlog_errno(status);
5098c27b 1862 return status;
ccd979bd
MF
1863 }
1864
1865 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1866 ORPHAN_DIR_SYSTEM_INODE,
1867 osb->slot_num);
1868 if (!orphan_dir_inode) {
1869 status = -ENOENT;
1870 mlog_errno(status);
5098c27b 1871 return status;
ccd979bd
MF
1872 }
1873
5098c27b
MF
1874 mutex_lock(&orphan_dir_inode->i_mutex);
1875
e63aecb6 1876 status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
ccd979bd
MF
1877 if (status < 0) {
1878 mlog_errno(status);
1879 goto leave;
1880 }
1881
1882 status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
1883 orphan_dir_bh, name,
4a12ca3a 1884 OCFS2_ORPHAN_NAMELEN, lookup);
ccd979bd 1885 if (status < 0) {
e63aecb6 1886 ocfs2_inode_unlock(orphan_dir_inode, 1);
5098c27b 1887
ccd979bd
MF
1888 mlog_errno(status);
1889 goto leave;
1890 }
1891
5098c27b
MF
1892 *ret_orphan_dir = orphan_dir_inode;
1893
ccd979bd 1894leave:
5098c27b
MF
1895 if (status) {
1896 mutex_unlock(&orphan_dir_inode->i_mutex);
ccd979bd 1897 iput(orphan_dir_inode);
5098c27b 1898 }
ccd979bd 1899
a81cb88b 1900 brelse(orphan_dir_bh);
ccd979bd
MF
1901
1902 mlog_exit(status);
1903 return status;
1904}
1905
1906static int ocfs2_orphan_add(struct ocfs2_super *osb,
1fabe148 1907 handle_t *handle,
ccd979bd
MF
1908 struct inode *inode,
1909 struct ocfs2_dinode *fe,
1910 char *name,
4a12ca3a 1911 struct ocfs2_dir_lookup_result *lookup,
5098c27b 1912 struct inode *orphan_dir_inode)
ccd979bd 1913{
ccd979bd
MF
1914 struct buffer_head *orphan_dir_bh = NULL;
1915 int status = 0;
1916 struct ocfs2_dinode *orphan_fe;
1917
1918 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
1919
b657c95c 1920 status = ocfs2_read_inode_block(orphan_dir_inode, &orphan_dir_bh);
ccd979bd
MF
1921 if (status < 0) {
1922 mlog_errno(status);
1923 goto leave;
1924 }
1925
13723d00
JB
1926 status = ocfs2_journal_access_di(handle, orphan_dir_inode, orphan_dir_bh,
1927 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
1928 if (status < 0) {
1929 mlog_errno(status);
1930 goto leave;
1931 }
1932
1933 /* we're a cluster, and nlink can change on disk from
1934 * underneath us... */
1935 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
1936 if (S_ISDIR(inode->i_mode))
1937 le16_add_cpu(&orphan_fe->i_links_count, 1);
1938 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
1939
1940 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
1941 if (status < 0) {
1942 mlog_errno(status);
1943 goto leave;
1944 }
1945
1946 status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
1947 OCFS2_ORPHAN_NAMELEN, inode,
1948 OCFS2_I(inode)->ip_blkno,
4a12ca3a 1949 orphan_dir_bh, lookup);
ccd979bd
MF
1950 if (status < 0) {
1951 mlog_errno(status);
1952 goto leave;
1953 }
1954
1955 le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
1956
1957 /* Record which orphan dir our inode now resides
1958 * in. delete_inode will use this to determine which orphan
1959 * dir to lock. */
50008630 1960 fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
ccd979bd 1961
b0697053
MF
1962 mlog(0, "Inode %llu orphaned in slot %d\n",
1963 (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
ccd979bd
MF
1964
1965leave:
a81cb88b 1966 brelse(orphan_dir_bh);
ccd979bd
MF
1967
1968 mlog_exit(status);
1969 return status;
1970}
1971
1972/* unlike orphan_add, we expect the orphan dir to already be locked here. */
1973int ocfs2_orphan_del(struct ocfs2_super *osb,
1fabe148 1974 handle_t *handle,
ccd979bd
MF
1975 struct inode *orphan_dir_inode,
1976 struct inode *inode,
1977 struct buffer_head *orphan_dir_bh)
1978{
1979 char name[OCFS2_ORPHAN_NAMELEN + 1];
1980 struct ocfs2_dinode *orphan_fe;
1981 int status = 0;
4a12ca3a 1982 struct ocfs2_dir_lookup_result lookup = { NULL, };
ccd979bd
MF
1983
1984 mlog_entry_void();
1985
1986 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
1987 if (status < 0) {
1988 mlog_errno(status);
1989 goto leave;
1990 }
1991
b0697053
MF
1992 mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
1993 name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
1994 OCFS2_ORPHAN_NAMELEN);
ccd979bd
MF
1995
1996 /* find it's spot in the orphan directory */
4a12ca3a
MF
1997 status = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN, orphan_dir_inode,
1998 &lookup);
1999 if (status) {
ccd979bd
MF
2000 mlog_errno(status);
2001 goto leave;
2002 }
2003
2004 /* remove it from the orphan directory */
4a12ca3a 2005 status = ocfs2_delete_entry(handle, orphan_dir_inode, &lookup);
ccd979bd
MF
2006 if (status < 0) {
2007 mlog_errno(status);
2008 goto leave;
2009 }
2010
13723d00
JB
2011 status = ocfs2_journal_access_di(handle,orphan_dir_inode, orphan_dir_bh,
2012 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
2013 if (status < 0) {
2014 mlog_errno(status);
2015 goto leave;
2016 }
2017
2018 /* do the i_nlink dance! :) */
2019 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2020 if (S_ISDIR(inode->i_mode))
2021 le16_add_cpu(&orphan_fe->i_links_count, -1);
2022 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
2023
2024 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
2025 if (status < 0) {
2026 mlog_errno(status);
2027 goto leave;
2028 }
2029
2030leave:
4a12ca3a 2031 ocfs2_free_dir_lookup_result(&lookup);
ccd979bd
MF
2032
2033 mlog_exit(status);
2034 return status;
2035}
2036
92e1d5be 2037const struct inode_operations ocfs2_dir_iops = {
ccd979bd
MF
2038 .create = ocfs2_create,
2039 .lookup = ocfs2_lookup,
2040 .link = ocfs2_link,
2041 .unlink = ocfs2_unlink,
2042 .rmdir = ocfs2_unlink,
2043 .symlink = ocfs2_symlink,
2044 .mkdir = ocfs2_mkdir,
2045 .mknod = ocfs2_mknod,
2046 .rename = ocfs2_rename,
2047 .setattr = ocfs2_setattr,
2048 .getattr = ocfs2_getattr,
d38eb8db 2049 .permission = ocfs2_permission,
cf1d6c76
TY
2050 .setxattr = generic_setxattr,
2051 .getxattr = generic_getxattr,
2052 .listxattr = ocfs2_listxattr,
2053 .removexattr = generic_removexattr,
ccd979bd 2054};
This page took 0.517728 seconds and 5 git commands to generate.