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