ocfs2: don't pass handle to ocfs2_meta_lock() in ocfs2_mknod()
[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>
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
67#define NAMEI_RA_CHUNKS 2
68#define NAMEI_RA_BLOCKS 4
69#define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
70#define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
71
72static int inline ocfs2_search_dirblock(struct buffer_head *bh,
73 struct inode *dir,
74 const char *name, int namelen,
75 unsigned long offset,
76 struct ocfs2_dir_entry **res_dir);
77
78static int ocfs2_delete_entry(struct ocfs2_journal_handle *handle,
79 struct inode *dir,
80 struct ocfs2_dir_entry *de_del,
81 struct buffer_head *bh);
82
83static int __ocfs2_add_entry(struct ocfs2_journal_handle *handle,
84 struct inode *dir,
85 const char *name, int namelen,
86 struct inode *inode, u64 blkno,
87 struct buffer_head *parent_fe_bh,
88 struct buffer_head *insert_bh);
89
90static int ocfs2_mknod_locked(struct ocfs2_super *osb,
91 struct inode *dir,
92 struct dentry *dentry, int mode,
93 dev_t dev,
94 struct buffer_head **new_fe_bh,
95 struct buffer_head *parent_fe_bh,
96 struct ocfs2_journal_handle *handle,
97 struct inode **ret_inode,
98 struct ocfs2_alloc_context *inode_ac);
99
100static int ocfs2_fill_new_dir(struct ocfs2_super *osb,
101 struct ocfs2_journal_handle *handle,
102 struct inode *parent,
103 struct inode *inode,
104 struct buffer_head *fe_bh,
105 struct ocfs2_alloc_context *data_ac);
106
107static int ocfs2_double_lock(struct ocfs2_super *osb,
108 struct ocfs2_journal_handle *handle,
109 struct buffer_head **bh1,
110 struct inode *inode1,
111 struct buffer_head **bh2,
112 struct inode *inode2);
113
114static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
115 struct ocfs2_journal_handle *handle,
116 struct inode *inode,
117 char *name,
118 struct buffer_head **de_bh);
119
120static int ocfs2_orphan_add(struct ocfs2_super *osb,
121 struct ocfs2_journal_handle *handle,
122 struct inode *inode,
123 struct ocfs2_dinode *fe,
124 char *name,
125 struct buffer_head *de_bh);
126
127static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
128 struct ocfs2_journal_handle *handle,
129 struct inode *inode,
130 const char *symname);
131
132static inline int ocfs2_add_entry(struct ocfs2_journal_handle *handle,
133 struct dentry *dentry,
134 struct inode *inode, u64 blkno,
135 struct buffer_head *parent_fe_bh,
136 struct buffer_head *insert_bh)
137{
138 return __ocfs2_add_entry(handle, dentry->d_parent->d_inode,
139 dentry->d_name.name, dentry->d_name.len,
140 inode, blkno, parent_fe_bh, insert_bh);
141}
142
143/* An orphan dir name is an 8 byte value, printed as a hex string */
144#define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
145
146static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
147 struct nameidata *nd)
148{
149 int status;
150 u64 blkno;
151 struct buffer_head *dirent_bh = NULL;
152 struct inode *inode = NULL;
153 struct dentry *ret;
154 struct ocfs2_dir_entry *dirent;
155 struct ocfs2_inode_info *oi;
156
157 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
158 dentry->d_name.len, dentry->d_name.name);
159
160 if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
161 ret = ERR_PTR(-ENAMETOOLONG);
162 goto bail;
163 }
164
b0697053
MF
165 mlog(0, "find name %.*s in directory %llu\n", dentry->d_name.len,
166 dentry->d_name.name, (unsigned long long)OCFS2_I(dir)->ip_blkno);
ccd979bd
MF
167
168 status = ocfs2_meta_lock(dir, NULL, NULL, 0);
169 if (status < 0) {
170 if (status != -ENOENT)
171 mlog_errno(status);
172 ret = ERR_PTR(status);
173 goto bail;
174 }
175
176 status = ocfs2_find_files_on_disk(dentry->d_name.name,
177 dentry->d_name.len, &blkno,
178 dir, &dirent_bh, &dirent);
179 if (status < 0)
180 goto bail_add;
181
24c19ef4 182 inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0);
ccd979bd 183 if (IS_ERR(inode)) {
b0697053
MF
184 mlog(ML_ERROR, "Unable to create inode %llu\n",
185 (unsigned long long)blkno);
ccd979bd
MF
186 ret = ERR_PTR(-EACCES);
187 goto bail_unlock;
188 }
189
190 oi = OCFS2_I(inode);
191 /* Clear any orphaned state... If we were able to look up the
192 * inode from a directory, it certainly can't be orphaned. We
193 * might have the bad state from a node which intended to
194 * orphan this inode but crashed before it could commit the
195 * unlink. */
196 spin_lock(&oi->ip_lock);
197 oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
198 oi->ip_orphaned_slot = OCFS2_INVALID_SLOT;
199 spin_unlock(&oi->ip_lock);
200
201bail_add:
ccd979bd
MF
202 dentry->d_op = &ocfs2_dentry_ops;
203 ret = d_splice_alias(inode, dentry);
204
379dfe9d
MF
205 if (inode) {
206 /*
207 * If d_splice_alias() finds a DCACHE_DISCONNECTED
208 * dentry, it will d_move() it on top of ourse. The
209 * return value will indicate this however, so in
210 * those cases, we switch them around for the locking
211 * code.
212 *
213 * NOTE: This dentry already has ->d_op set from
214 * ocfs2_get_parent() and ocfs2_get_dentry()
215 */
216 if (ret)
217 dentry = ret;
218
219 status = ocfs2_dentry_attach_lock(dentry, inode,
0027dd5b 220 OCFS2_I(dir)->ip_blkno);
379dfe9d
MF
221 if (status) {
222 mlog_errno(status);
223 ret = ERR_PTR(status);
224 goto bail_unlock;
225 }
226 }
227
ccd979bd
MF
228bail_unlock:
229 /* Don't drop the cluster lock until *after* the d_add --
230 * unlink on another node will message us to remove that
231 * dentry under this lock so otherwise we can race this with
232 * the vote thread and have a stale dentry. */
233 ocfs2_meta_unlock(dir, 0);
234
235bail:
236 if (dirent_bh)
237 brelse(dirent_bh);
238
239 mlog_exit_ptr(ret);
240
241 return ret;
242}
243
244static int ocfs2_fill_new_dir(struct ocfs2_super *osb,
245 struct ocfs2_journal_handle *handle,
246 struct inode *parent,
247 struct inode *inode,
248 struct buffer_head *fe_bh,
249 struct ocfs2_alloc_context *data_ac)
250{
251 int status;
252 struct buffer_head *new_bh = NULL;
253 struct ocfs2_dir_entry *de = NULL;
254
255 mlog_entry_void();
256
257 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
258 data_ac, NULL, &new_bh);
259 if (status < 0) {
260 mlog_errno(status);
261 goto bail;
262 }
263
264 ocfs2_set_new_buffer_uptodate(inode, new_bh);
265
266 status = ocfs2_journal_access(handle, inode, new_bh,
267 OCFS2_JOURNAL_ACCESS_CREATE);
268 if (status < 0) {
269 mlog_errno(status);
270 goto bail;
271 }
272 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
273
274 de = (struct ocfs2_dir_entry *) new_bh->b_data;
275 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
276 de->name_len = 1;
277 de->rec_len =
278 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
279 strcpy(de->name, ".");
280 ocfs2_set_de_type(de, S_IFDIR);
281 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
282 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
283 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize -
284 OCFS2_DIR_REC_LEN(1));
285 de->name_len = 2;
286 strcpy(de->name, "..");
287 ocfs2_set_de_type(de, S_IFDIR);
288
289 status = ocfs2_journal_dirty(handle, new_bh);
290 if (status < 0) {
291 mlog_errno(status);
292 goto bail;
293 }
294
295 i_size_write(inode, inode->i_sb->s_blocksize);
296 inode->i_nlink = 2;
297 inode->i_blocks = ocfs2_align_bytes_to_sectors(inode->i_sb->s_blocksize);
298 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
299 if (status < 0) {
300 mlog_errno(status);
301 goto bail;
302 }
303
304 status = 0;
305bail:
306 if (new_bh)
307 brelse(new_bh);
308
309 mlog_exit(status);
310 return status;
311}
312
313static int ocfs2_mknod(struct inode *dir,
314 struct dentry *dentry,
315 int mode,
316 dev_t dev)
317{
318 int status = 0;
319 struct buffer_head *parent_fe_bh = NULL;
320 struct ocfs2_journal_handle *handle = NULL;
321 struct ocfs2_super *osb;
322 struct ocfs2_dinode *dirfe;
323 struct buffer_head *new_fe_bh = NULL;
324 struct buffer_head *de_bh = NULL;
325 struct inode *inode = NULL;
326 struct ocfs2_alloc_context *inode_ac = NULL;
327 struct ocfs2_alloc_context *data_ac = NULL;
328
329 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
330 (unsigned long)dev, dentry->d_name.len,
331 dentry->d_name.name);
332
333 /* get our super block */
334 osb = OCFS2_SB(dir->i_sb);
335
e3a82138
MF
336 status = ocfs2_meta_lock(dir, NULL, &parent_fe_bh, 1);
337 if (status < 0) {
338 if (status != -ENOENT)
339 mlog_errno(status);
340 return status;
341 }
342
ccd979bd
MF
343 handle = ocfs2_alloc_handle(osb);
344 if (handle == NULL) {
345 status = -ENOMEM;
346 mlog_errno(status);
347 goto leave;
348 }
349
a663e305
MF
350 if (S_ISDIR(mode) && (dir->i_nlink >= OCFS2_LINK_MAX)) {
351 status = -EMLINK;
352 goto leave;
353 }
354
ccd979bd
MF
355 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
356 if (!dirfe->i_links_count) {
357 /* can't make a file in a deleted directory. */
358 status = -ENOENT;
359 goto leave;
360 }
361
362 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
363 dentry->d_name.len);
364 if (status)
365 goto leave;
366
367 /* get a spot inside the dir. */
368 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
369 dentry->d_name.name,
370 dentry->d_name.len, &de_bh);
371 if (status < 0) {
372 mlog_errno(status);
373 goto leave;
374 }
375
376 /* reserve an inode spot */
377 status = ocfs2_reserve_new_inode(osb, handle, &inode_ac);
378 if (status < 0) {
379 if (status != -ENOSPC)
380 mlog_errno(status);
381 goto leave;
382 }
383
384 /* are we making a directory? If so, reserve a cluster for his
385 * 1st extent. */
386 if (S_ISDIR(mode)) {
387 status = ocfs2_reserve_clusters(osb, handle, 1, &data_ac);
388 if (status < 0) {
389 if (status != -ENOSPC)
390 mlog_errno(status);
391 goto leave;
392 }
393 }
394
395 handle = ocfs2_start_trans(osb, handle, OCFS2_MKNOD_CREDITS);
396 if (IS_ERR(handle)) {
397 status = PTR_ERR(handle);
398 handle = NULL;
399 mlog_errno(status);
400 goto leave;
401 }
402
403 /* do the real work now. */
404 status = ocfs2_mknod_locked(osb, dir, dentry, mode, dev,
405 &new_fe_bh, parent_fe_bh, handle,
406 &inode, inode_ac);
407 if (status < 0) {
408 mlog_errno(status);
409 goto leave;
410 }
411
412 if (S_ISDIR(mode)) {
413 status = ocfs2_fill_new_dir(osb, handle, dir, inode,
414 new_fe_bh, data_ac);
415 if (status < 0) {
416 mlog_errno(status);
417 goto leave;
418 }
419
420 status = ocfs2_journal_access(handle, dir, parent_fe_bh,
421 OCFS2_JOURNAL_ACCESS_WRITE);
422 if (status < 0) {
423 mlog_errno(status);
424 goto leave;
425 }
426 le16_add_cpu(&dirfe->i_links_count, 1);
427 status = ocfs2_journal_dirty(handle, parent_fe_bh);
428 if (status < 0) {
429 mlog_errno(status);
430 goto leave;
431 }
d8c76e6f 432 inc_nlink(dir);
ccd979bd
MF
433 }
434
435 status = ocfs2_add_entry(handle, dentry, inode,
436 OCFS2_I(inode)->ip_blkno, parent_fe_bh,
437 de_bh);
438 if (status < 0) {
439 mlog_errno(status);
440 goto leave;
441 }
442
379dfe9d 443 status = ocfs2_dentry_attach_lock(dentry, inode,
0027dd5b 444 OCFS2_I(dir)->ip_blkno);
379dfe9d
MF
445 if (status) {
446 mlog_errno(status);
447 goto leave;
448 }
449
ccd979bd
MF
450 insert_inode_hash(inode);
451 dentry->d_op = &ocfs2_dentry_ops;
452 d_instantiate(dentry, inode);
453 status = 0;
454leave:
455 if (handle)
456 ocfs2_commit_trans(handle);
457
e3a82138
MF
458 ocfs2_meta_unlock(dir, 1);
459
ccd979bd
MF
460 if (status == -ENOSPC)
461 mlog(0, "Disk is full\n");
462
463 if (new_fe_bh)
464 brelse(new_fe_bh);
465
466 if (de_bh)
467 brelse(de_bh);
468
469 if (parent_fe_bh)
470 brelse(parent_fe_bh);
471
472 if ((status < 0) && inode)
473 iput(inode);
474
475 if (inode_ac)
476 ocfs2_free_alloc_context(inode_ac);
477
478 if (data_ac)
479 ocfs2_free_alloc_context(data_ac);
480
481 mlog_exit(status);
482
483 return status;
484}
485
486static int ocfs2_mknod_locked(struct ocfs2_super *osb,
487 struct inode *dir,
488 struct dentry *dentry, int mode,
489 dev_t dev,
490 struct buffer_head **new_fe_bh,
491 struct buffer_head *parent_fe_bh,
492 struct ocfs2_journal_handle *handle,
493 struct inode **ret_inode,
494 struct ocfs2_alloc_context *inode_ac)
495{
496 int status = 0;
497 struct ocfs2_dinode *fe = NULL;
498 struct ocfs2_extent_list *fel;
499 u64 fe_blkno = 0;
500 u16 suballoc_bit;
501 struct inode *inode = NULL;
502
503 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
504 (unsigned long)dev, dentry->d_name.len,
505 dentry->d_name.name);
506
507 *new_fe_bh = NULL;
508 *ret_inode = NULL;
509
510 status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit,
511 &fe_blkno);
512 if (status < 0) {
513 mlog_errno(status);
514 goto leave;
515 }
516
517 inode = new_inode(dir->i_sb);
518 if (IS_ERR(inode)) {
519 status = PTR_ERR(inode);
520 mlog(ML_ERROR, "new_inode failed!\n");
521 goto leave;
522 }
523
524 /* populate as many fields early on as possible - many of
525 * these are used by the support functions here and in
526 * callers. */
527 inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
528 OCFS2_I(inode)->ip_blkno = fe_blkno;
529 if (S_ISDIR(mode))
530 inode->i_nlink = 2;
531 else
532 inode->i_nlink = 1;
533 inode->i_mode = mode;
534 spin_lock(&osb->osb_lock);
535 inode->i_generation = osb->s_next_generation++;
536 spin_unlock(&osb->osb_lock);
537
538 *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
539 if (!*new_fe_bh) {
540 status = -EIO;
541 mlog_errno(status);
542 goto leave;
543 }
544 ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh);
545
546 status = ocfs2_journal_access(handle, inode, *new_fe_bh,
547 OCFS2_JOURNAL_ACCESS_CREATE);
548 if (status < 0) {
549 mlog_errno(status);
550 goto leave;
551 }
552
553 fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
554 memset(fe, 0, osb->sb->s_blocksize);
555
556 fe->i_generation = cpu_to_le32(inode->i_generation);
557 fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
558 fe->i_blkno = cpu_to_le64(fe_blkno);
559 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
560 fe->i_suballoc_slot = cpu_to_le16(osb->slot_num);
561 fe->i_uid = cpu_to_le32(current->fsuid);
562 if (dir->i_mode & S_ISGID) {
563 fe->i_gid = cpu_to_le32(dir->i_gid);
564 if (S_ISDIR(mode))
565 mode |= S_ISGID;
566 } else
567 fe->i_gid = cpu_to_le32(current->fsgid);
568 fe->i_mode = cpu_to_le16(mode);
569 if (S_ISCHR(mode) || S_ISBLK(mode))
570 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
571
572 fe->i_links_count = cpu_to_le16(inode->i_nlink);
573
574 fe->i_last_eb_blk = 0;
575 strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
576 le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
577 fe->i_atime = fe->i_ctime = fe->i_mtime =
578 cpu_to_le64(CURRENT_TIME.tv_sec);
579 fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
580 cpu_to_le32(CURRENT_TIME.tv_nsec);
581 fe->i_dtime = 0;
582
583 fel = &fe->id2.i_list;
584 fel->l_tree_depth = 0;
585 fel->l_next_free_rec = 0;
586 fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
587
588 status = ocfs2_journal_dirty(handle, *new_fe_bh);
589 if (status < 0) {
590 mlog_errno(status);
591 goto leave;
592 }
593
594 if (ocfs2_populate_inode(inode, fe, 1) < 0) {
595 mlog(ML_ERROR, "populate inode failed! bh->b_blocknr=%llu, "
b0697053 596 "i_blkno=%llu, i_ino=%lu\n",
ccd979bd 597 (unsigned long long) (*new_fe_bh)->b_blocknr,
b0697053 598 (unsigned long long)fe->i_blkno, inode->i_ino);
ccd979bd
MF
599 BUG();
600 }
601
602 ocfs2_inode_set_new(osb, inode);
603 status = ocfs2_create_new_inode_locks(inode);
604 if (status < 0)
605 mlog_errno(status);
606
607 status = 0; /* error in ocfs2_create_new_inode_locks is not
608 * critical */
609
610 *ret_inode = inode;
611leave:
612 if (status < 0) {
613 if (*new_fe_bh) {
614 brelse(*new_fe_bh);
615 *new_fe_bh = NULL;
616 }
617 if (inode)
618 iput(inode);
619 }
620
621 mlog_exit(status);
622 return status;
623}
624
625static int ocfs2_mkdir(struct inode *dir,
626 struct dentry *dentry,
627 int mode)
628{
629 int ret;
630
631 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
632 dentry->d_name.len, dentry->d_name.name);
633 ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
634 mlog_exit(ret);
635
636 return ret;
637}
638
639static int ocfs2_create(struct inode *dir,
640 struct dentry *dentry,
641 int mode,
642 struct nameidata *nd)
643{
644 int ret;
645
646 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
647 dentry->d_name.len, dentry->d_name.name);
648 ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
649 mlog_exit(ret);
650
651 return ret;
652}
653
654static int ocfs2_link(struct dentry *old_dentry,
655 struct inode *dir,
656 struct dentry *dentry)
657{
658 struct ocfs2_journal_handle *handle = NULL;
659 struct inode *inode = old_dentry->d_inode;
660 int err;
661 struct buffer_head *fe_bh = NULL;
662 struct buffer_head *parent_fe_bh = NULL;
663 struct buffer_head *de_bh = NULL;
664 struct ocfs2_dinode *fe = NULL;
665 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
666
667 mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
668 old_dentry->d_name.len, old_dentry->d_name.name,
669 dentry->d_name.len, dentry->d_name.name);
670
671 if (S_ISDIR(inode->i_mode)) {
672 err = -EPERM;
673 goto bail;
674 }
675
ccd979bd
MF
676 handle = ocfs2_alloc_handle(osb);
677 if (handle == NULL) {
678 err = -ENOMEM;
679 goto bail;
680 }
681
682 err = ocfs2_meta_lock(dir, handle, &parent_fe_bh, 1);
683 if (err < 0) {
684 if (err != -ENOENT)
685 mlog_errno(err);
686 goto bail;
687 }
688
0f62de2c
TY
689 if (!dir->i_nlink) {
690 err = -ENOENT;
691 goto bail;
692 }
693
ccd979bd
MF
694 err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
695 dentry->d_name.len);
696 if (err)
697 goto bail;
698
699 err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
700 dentry->d_name.name,
701 dentry->d_name.len, &de_bh);
702 if (err < 0) {
703 mlog_errno(err);
704 goto bail;
705 }
706
707 err = ocfs2_meta_lock(inode, handle, &fe_bh, 1);
708 if (err < 0) {
709 if (err != -ENOENT)
710 mlog_errno(err);
711 goto bail;
712 }
713
714 fe = (struct ocfs2_dinode *) fe_bh->b_data;
715 if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) {
716 err = -EMLINK;
717 goto bail;
718 }
719
720 handle = ocfs2_start_trans(osb, handle, OCFS2_LINK_CREDITS);
721 if (IS_ERR(handle)) {
722 err = PTR_ERR(handle);
723 handle = NULL;
724 mlog_errno(err);
725 goto bail;
726 }
727
728 err = ocfs2_journal_access(handle, inode, fe_bh,
729 OCFS2_JOURNAL_ACCESS_WRITE);
730 if (err < 0) {
731 mlog_errno(err);
732 goto bail;
733 }
734
d8c76e6f 735 inc_nlink(inode);
ccd979bd
MF
736 inode->i_ctime = CURRENT_TIME;
737 fe->i_links_count = cpu_to_le16(inode->i_nlink);
738 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
739 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
740
741 err = ocfs2_journal_dirty(handle, fe_bh);
742 if (err < 0) {
743 le16_add_cpu(&fe->i_links_count, -1);
9a53c3a7 744 drop_nlink(inode);
ccd979bd
MF
745 mlog_errno(err);
746 goto bail;
747 }
748
749 err = ocfs2_add_entry(handle, dentry, inode,
750 OCFS2_I(inode)->ip_blkno,
751 parent_fe_bh, de_bh);
752 if (err) {
753 le16_add_cpu(&fe->i_links_count, -1);
9a53c3a7 754 drop_nlink(inode);
ccd979bd
MF
755 mlog_errno(err);
756 goto bail;
757 }
758
0027dd5b 759 err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
379dfe9d
MF
760 if (err) {
761 mlog_errno(err);
762 goto bail;
763 }
764
ccd979bd
MF
765 atomic_inc(&inode->i_count);
766 dentry->d_op = &ocfs2_dentry_ops;
767 d_instantiate(dentry, inode);
768bail:
769 if (handle)
770 ocfs2_commit_trans(handle);
771 if (de_bh)
772 brelse(de_bh);
773 if (fe_bh)
774 brelse(fe_bh);
775 if (parent_fe_bh)
776 brelse(parent_fe_bh);
777
778 mlog_exit(err);
779
780 return err;
781}
782
379dfe9d
MF
783/*
784 * Takes and drops an exclusive lock on the given dentry. This will
785 * force other nodes to drop it.
786 */
787static int ocfs2_remote_dentry_delete(struct dentry *dentry)
788{
789 int ret;
790
791 ret = ocfs2_dentry_lock(dentry, 1);
792 if (ret)
793 mlog_errno(ret);
794 else
795 ocfs2_dentry_unlock(dentry, 1);
796
797 return ret;
798}
799
17ff7856
MF
800static inline int inode_is_unlinkable(struct inode *inode)
801{
802 if (S_ISDIR(inode->i_mode)) {
803 if (inode->i_nlink == 2)
804 return 1;
805 return 0;
806 }
807
808 if (inode->i_nlink == 1)
809 return 1;
810 return 0;
811}
812
ccd979bd
MF
813static int ocfs2_unlink(struct inode *dir,
814 struct dentry *dentry)
815{
816 int status;
ccd979bd
MF
817 struct inode *inode = dentry->d_inode;
818 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
819 u64 blkno;
820 struct ocfs2_dinode *fe = NULL;
821 struct buffer_head *fe_bh = NULL;
822 struct buffer_head *parent_node_bh = NULL;
823 struct ocfs2_journal_handle *handle = NULL;
824 struct ocfs2_dir_entry *dirent = NULL;
825 struct buffer_head *dirent_bh = NULL;
826 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
827 struct buffer_head *orphan_entry_bh = NULL;
828
829 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
830 dentry->d_name.len, dentry->d_name.name);
831
832 BUG_ON(dentry->d_parent->d_inode != dir);
833
b0697053 834 mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
ccd979bd
MF
835
836 if (inode == osb->root_inode) {
837 mlog(0, "Cannot delete the root directory\n");
838 status = -EPERM;
839 goto leave;
840 }
841
842 handle = ocfs2_alloc_handle(osb);
843 if (handle == NULL) {
844 status = -ENOMEM;
845 mlog_errno(status);
846 goto leave;
847 }
848
849 status = ocfs2_meta_lock(dir, handle, &parent_node_bh, 1);
850 if (status < 0) {
851 if (status != -ENOENT)
852 mlog_errno(status);
853 goto leave;
854 }
855
856 status = ocfs2_find_files_on_disk(dentry->d_name.name,
857 dentry->d_name.len, &blkno,
858 dir, &dirent_bh, &dirent);
859 if (status < 0) {
860 if (status != -ENOENT)
861 mlog_errno(status);
862 goto leave;
863 }
864
865 if (OCFS2_I(inode)->ip_blkno != blkno) {
866 status = -ENOENT;
867
b0697053
MF
868 mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
869 (unsigned long long)OCFS2_I(inode)->ip_blkno,
870 (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
ccd979bd
MF
871 goto leave;
872 }
873
874 status = ocfs2_meta_lock(inode, handle, &fe_bh, 1);
875 if (status < 0) {
876 if (status != -ENOENT)
877 mlog_errno(status);
878 goto leave;
879 }
880
881 if (S_ISDIR(inode->i_mode)) {
882 if (!ocfs2_empty_dir(inode)) {
883 status = -ENOTEMPTY;
884 goto leave;
885 } else if (inode->i_nlink != 2) {
886 status = -ENOTEMPTY;
887 goto leave;
888 }
889 }
890
379dfe9d 891 status = ocfs2_remote_dentry_delete(dentry);
ccd979bd
MF
892 if (status < 0) {
893 /* This vote should succeed under all normal
894 * circumstances. */
895 mlog_errno(status);
896 goto leave;
897 }
898
17ff7856 899 if (inode_is_unlinkable(inode)) {
ccd979bd
MF
900 status = ocfs2_prepare_orphan_dir(osb, handle, inode,
901 orphan_name,
902 &orphan_entry_bh);
903 if (status < 0) {
904 mlog_errno(status);
905 goto leave;
906 }
907 }
908
909 handle = ocfs2_start_trans(osb, handle, OCFS2_UNLINK_CREDITS);
910 if (IS_ERR(handle)) {
911 status = PTR_ERR(handle);
912 handle = NULL;
913 mlog_errno(status);
914 goto leave;
915 }
916
917 status = ocfs2_journal_access(handle, inode, fe_bh,
918 OCFS2_JOURNAL_ACCESS_WRITE);
919 if (status < 0) {
920 mlog_errno(status);
921 goto leave;
922 }
923
924 fe = (struct ocfs2_dinode *) fe_bh->b_data;
925
17ff7856 926 if (inode_is_unlinkable(inode)) {
ccd979bd
MF
927 status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
928 orphan_entry_bh);
929 if (status < 0) {
930 mlog_errno(status);
931 goto leave;
932 }
933 }
934
935 /* delete the name from the parent dir */
936 status = ocfs2_delete_entry(handle, dir, dirent, dirent_bh);
937 if (status < 0) {
938 mlog_errno(status);
939 goto leave;
940 }
941
17ff7856
MF
942 if (S_ISDIR(inode->i_mode))
943 drop_nlink(inode);
944 drop_nlink(inode);
ccd979bd 945 fe->i_links_count = cpu_to_le16(inode->i_nlink);
ccd979bd
MF
946
947 status = ocfs2_journal_dirty(handle, fe_bh);
948 if (status < 0) {
949 mlog_errno(status);
950 goto leave;
951 }
952
953 if (S_ISDIR(inode->i_mode)) {
17ff7856 954 drop_nlink(dir);
ccd979bd
MF
955 status = ocfs2_mark_inode_dirty(handle, dir,
956 parent_node_bh);
957 if (status < 0) {
958 mlog_errno(status);
d8c76e6f 959 inc_nlink(dir);
ccd979bd
MF
960 }
961 }
962
963leave:
ccd979bd
MF
964 if (handle)
965 ocfs2_commit_trans(handle);
966
967 if (fe_bh)
968 brelse(fe_bh);
969
970 if (dirent_bh)
971 brelse(dirent_bh);
972
973 if (parent_node_bh)
974 brelse(parent_node_bh);
975
976 if (orphan_entry_bh)
977 brelse(orphan_entry_bh);
978
979 mlog_exit(status);
980
981 return status;
982}
983
984/*
985 * The only place this should be used is rename!
986 * if they have the same id, then the 1st one is the only one locked.
987 */
988static int ocfs2_double_lock(struct ocfs2_super *osb,
989 struct ocfs2_journal_handle *handle,
990 struct buffer_head **bh1,
991 struct inode *inode1,
992 struct buffer_head **bh2,
993 struct inode *inode2)
994{
995 int status;
996 struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
997 struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
998 struct buffer_head **tmpbh;
999 struct inode *tmpinode;
1000
b0697053
MF
1001 mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
1002 (unsigned long long)oi1->ip_blkno,
1003 (unsigned long long)oi2->ip_blkno);
ccd979bd
MF
1004
1005 BUG_ON(!handle);
1006
1007 if (*bh1)
1008 *bh1 = NULL;
1009 if (*bh2)
1010 *bh2 = NULL;
1011
1012 /* we always want to lock the one with the lower lockid first. */
1013 if (oi1->ip_blkno != oi2->ip_blkno) {
1014 if (oi1->ip_blkno < oi2->ip_blkno) {
1015 /* switch id1 and id2 around */
1016 mlog(0, "switching them around...\n");
1017 tmpbh = bh2;
1018 bh2 = bh1;
1019 bh1 = tmpbh;
1020
1021 tmpinode = inode2;
1022 inode2 = inode1;
1023 inode1 = tmpinode;
1024 }
1025 /* lock id2 */
1026 status = ocfs2_meta_lock(inode2, handle, bh2, 1);
1027 if (status < 0) {
1028 if (status != -ENOENT)
1029 mlog_errno(status);
1030 goto bail;
1031 }
1032 }
1033 /* lock id1 */
1034 status = ocfs2_meta_lock(inode1, handle, bh1, 1);
1035 if (status < 0) {
1036 if (status != -ENOENT)
1037 mlog_errno(status);
1038 goto bail;
1039 }
1040bail:
1041 mlog_exit(status);
1042 return status;
1043}
1044
1045#define PARENT_INO(buffer) \
1046 ((struct ocfs2_dir_entry *) \
1047 ((char *)buffer + \
1048 le16_to_cpu(((struct ocfs2_dir_entry *)buffer)->rec_len)))->inode
1049
1050static int ocfs2_rename(struct inode *old_dir,
1051 struct dentry *old_dentry,
1052 struct inode *new_dir,
1053 struct dentry *new_dentry)
1054{
1055 int status = 0, rename_lock = 0;
1056 struct inode *old_inode = old_dentry->d_inode;
1057 struct inode *new_inode = new_dentry->d_inode;
1058 struct ocfs2_dinode *newfe = NULL;
1059 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
1060 struct buffer_head *orphan_entry_bh = NULL;
1061 struct buffer_head *newfe_bh = NULL;
1062 struct buffer_head *insert_entry_bh = NULL;
1063 struct ocfs2_super *osb = NULL;
1064 u64 newfe_blkno;
1065 struct ocfs2_journal_handle *handle = NULL;
1066 struct buffer_head *old_dir_bh = NULL;
1067 struct buffer_head *new_dir_bh = NULL;
1068 struct ocfs2_dir_entry *old_de = NULL, *new_de = NULL; // dirent for old_dentry
1069 // and new_dentry
1070 struct buffer_head *new_de_bh = NULL, *old_de_bh = NULL; // bhs for above
1071 struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir,
1072 // this is the 1st dirent bh
1073 nlink_t old_dir_nlink = old_dir->i_nlink, new_dir_nlink = new_dir->i_nlink;
ccd979bd
MF
1074
1075 /* At some point it might be nice to break this function up a
1076 * bit. */
1077
1078 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
1079 old_dir, old_dentry, new_dir, new_dentry,
1080 old_dentry->d_name.len, old_dentry->d_name.name,
1081 new_dentry->d_name.len, new_dentry->d_name.name);
1082
1083 osb = OCFS2_SB(old_dir->i_sb);
1084
1085 if (new_inode) {
1086 if (!igrab(new_inode))
1087 BUG();
1088 }
1089
ccd979bd
MF
1090 /* Assume a directory heirarchy thusly:
1091 * a/b/c
1092 * a/d
1093 * a,b,c, and d are all directories.
1094 *
1095 * from cwd of 'a' on both nodes:
1096 * node1: mv b/c d
1097 * node2: mv d b/c
1098 *
1099 * And that's why, just like the VFS, we need a file system
1100 * rename lock. */
1101 if (old_dentry != new_dentry) {
1102 status = ocfs2_rename_lock(osb);
1103 if (status < 0) {
1104 mlog_errno(status);
1105 goto bail;
1106 }
1107 rename_lock = 1;
1108 }
1109
1110 handle = ocfs2_alloc_handle(osb);
1111 if (handle == NULL) {
1112 status = -ENOMEM;
1113 mlog_errno(status);
1114 goto bail;
1115 }
1116
1117 /* if old and new are the same, this'll just do one lock. */
1118 status = ocfs2_double_lock(osb, handle,
1119 &old_dir_bh, old_dir,
1120 &new_dir_bh, new_dir);
1121 if (status < 0) {
1122 mlog_errno(status);
1123 goto bail;
1124 }
1125
1126 /* make sure both dirs have bhs
1127 * get an extra ref on old_dir_bh if old==new */
1128 if (!new_dir_bh) {
1129 if (old_dir_bh) {
1130 new_dir_bh = old_dir_bh;
1131 get_bh(new_dir_bh);
1132 } else {
1133 mlog(ML_ERROR, "no old_dir_bh!\n");
1134 status = -EIO;
1135 goto bail;
1136 }
1137 }
1138
379dfe9d
MF
1139 /*
1140 * Though we don't require an inode meta data update if
1141 * old_inode is not a directory, we lock anyway here to ensure
1142 * the vote thread on other nodes won't have to concurrently
1143 * downconvert the inode and the dentry locks.
1144 */
1145 status = ocfs2_meta_lock(old_inode, handle, NULL, 1);
1146 if (status < 0) {
1147 if (status != -ENOENT)
ccd979bd 1148 mlog_errno(status);
379dfe9d
MF
1149 goto bail;
1150 }
ccd979bd 1151
379dfe9d
MF
1152 status = ocfs2_remote_dentry_delete(old_dentry);
1153 if (status < 0) {
1154 mlog_errno(status);
1155 goto bail;
1156 }
1157
1158 if (S_ISDIR(old_inode->i_mode)) {
ccd979bd
MF
1159 status = -EIO;
1160 old_inode_de_bh = ocfs2_bread(old_inode, 0, &status, 0);
1161 if (!old_inode_de_bh)
1162 goto bail;
1163
1164 status = -EIO;
1165 if (le64_to_cpu(PARENT_INO(old_inode_de_bh->b_data)) !=
1166 OCFS2_I(old_dir)->ip_blkno)
1167 goto bail;
1168 status = -EMLINK;
1169 if (!new_inode && new_dir!=old_dir &&
1170 new_dir->i_nlink >= OCFS2_LINK_MAX)
1171 goto bail;
ccd979bd
MF
1172 }
1173
1174 status = -ENOENT;
1175 old_de_bh = ocfs2_find_entry(old_dentry->d_name.name,
1176 old_dentry->d_name.len,
1177 old_dir, &old_de);
1178 if (!old_de_bh)
1179 goto bail;
1180
1181 /*
1182 * Check for inode number is _not_ due to possible IO errors.
1183 * We might rmdir the source, keep it as pwd of some process
1184 * and merrily kill the link to whatever was created under the
1185 * same name. Goodbye sticky bit ;-<
1186 */
1187 if (le64_to_cpu(old_de->inode) != OCFS2_I(old_inode)->ip_blkno)
1188 goto bail;
1189
1190 /* check if the target already exists (in which case we need
1191 * to delete it */
1192 status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
1193 new_dentry->d_name.len,
1194 &newfe_blkno, new_dir, &new_de_bh,
1195 &new_de);
1196 /* The only error we allow here is -ENOENT because the new
1197 * file not existing is perfectly valid. */
1198 if ((status < 0) && (status != -ENOENT)) {
1199 /* If we cannot find the file specified we should just */
1200 /* return the error... */
1201 mlog_errno(status);
1202 goto bail;
1203 }
1204
1205 if (!new_de && new_inode)
1206 mlog(ML_ERROR, "inode %lu does not exist in it's parent "
1207 "directory!", new_inode->i_ino);
1208
1209 /* In case we need to overwrite an existing file, we blow it
1210 * away first */
1211 if (new_de) {
1212 /* VFS didn't think there existed an inode here, but
1213 * someone else in the cluster must have raced our
1214 * rename to create one. Today we error cleanly, in
1215 * the future we should consider calling iget to build
1216 * a new struct inode for this entry. */
1217 if (!new_inode) {
1218 status = -EACCES;
1219
1220 mlog(0, "We found an inode for name %.*s but VFS "
1221 "didn't give us one.\n", new_dentry->d_name.len,
1222 new_dentry->d_name.name);
1223 goto bail;
1224 }
1225
1226 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
1227 status = -EACCES;
1228
b0697053
MF
1229 mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
1230 (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
1231 (unsigned long long)newfe_blkno,
ccd979bd
MF
1232 OCFS2_I(new_inode)->ip_flags);
1233 goto bail;
1234 }
1235
1236 status = ocfs2_meta_lock(new_inode, handle, &newfe_bh, 1);
1237 if (status < 0) {
1238 if (status != -ENOENT)
1239 mlog_errno(status);
1240 goto bail;
1241 }
1242
379dfe9d 1243 status = ocfs2_remote_dentry_delete(new_dentry);
ccd979bd
MF
1244 if (status < 0) {
1245 mlog_errno(status);
1246 goto bail;
1247 }
1248
1249 newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
1250
b0697053
MF
1251 mlog(0, "aha rename over existing... new_de=%p new_blkno=%llu "
1252 "newfebh=%p bhblocknr=%llu\n", new_de,
1253 (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
ccd979bd
MF
1254 (unsigned long long)newfe_bh->b_blocknr : 0ULL);
1255
1256 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
1257 status = ocfs2_prepare_orphan_dir(osb, handle,
1258 new_inode,
1259 orphan_name,
1260 &orphan_entry_bh);
1261 if (status < 0) {
1262 mlog_errno(status);
1263 goto bail;
1264 }
1265 }
1266 } else {
1267 BUG_ON(new_dentry->d_parent->d_inode != new_dir);
1268
1269 status = ocfs2_check_dir_for_entry(new_dir,
1270 new_dentry->d_name.name,
1271 new_dentry->d_name.len);
1272 if (status)
1273 goto bail;
1274
1275 status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
1276 new_dentry->d_name.name,
1277 new_dentry->d_name.len,
1278 &insert_entry_bh);
1279 if (status < 0) {
1280 mlog_errno(status);
1281 goto bail;
1282 }
1283 }
1284
1285 handle = ocfs2_start_trans(osb, handle, OCFS2_RENAME_CREDITS);
1286 if (IS_ERR(handle)) {
1287 status = PTR_ERR(handle);
1288 handle = NULL;
1289 mlog_errno(status);
1290 goto bail;
1291 }
1292
1293 if (new_de) {
1294 if (S_ISDIR(new_inode->i_mode)) {
1295 if (!ocfs2_empty_dir(new_inode) ||
1296 new_inode->i_nlink != 2) {
1297 status = -ENOTEMPTY;
1298 goto bail;
1299 }
1300 }
1301 status = ocfs2_journal_access(handle, new_inode, newfe_bh,
1302 OCFS2_JOURNAL_ACCESS_WRITE);
1303 if (status < 0) {
1304 mlog_errno(status);
1305 goto bail;
1306 }
1307
1308 if (S_ISDIR(new_inode->i_mode) ||
1309 (newfe->i_links_count == cpu_to_le16(1))){
1310 status = ocfs2_orphan_add(osb, handle, new_inode,
1311 newfe, orphan_name,
1312 orphan_entry_bh);
1313 if (status < 0) {
1314 mlog_errno(status);
1315 goto bail;
1316 }
1317 }
1318
1319 /* change the dirent to point to the correct inode */
1320 status = ocfs2_journal_access(handle, new_dir, new_de_bh,
1321 OCFS2_JOURNAL_ACCESS_WRITE);
1322 if (status < 0) {
1323 mlog_errno(status);
1324 goto bail;
1325 }
1326 new_de->inode = cpu_to_le64(OCFS2_I(old_inode)->ip_blkno);
1327 new_de->file_type = old_de->file_type;
1328 new_dir->i_version++;
1329 status = ocfs2_journal_dirty(handle, new_de_bh);
1330 if (status < 0) {
1331 mlog_errno(status);
1332 goto bail;
1333 }
1334
1335 if (S_ISDIR(new_inode->i_mode))
1336 newfe->i_links_count = 0;
1337 else
1338 le16_add_cpu(&newfe->i_links_count, -1);
1339
1340 status = ocfs2_journal_dirty(handle, newfe_bh);
1341 if (status < 0) {
1342 mlog_errno(status);
1343 goto bail;
1344 }
1345 } else {
1346 /* if the name was not found in new_dir, add it now */
1347 status = ocfs2_add_entry(handle, new_dentry, old_inode,
1348 OCFS2_I(old_inode)->ip_blkno,
1349 new_dir_bh, insert_entry_bh);
1350 }
1351
1352 old_inode->i_ctime = CURRENT_TIME;
1353 mark_inode_dirty(old_inode);
1354
1355 /* now that the name has been added to new_dir, remove the old name */
1356 status = ocfs2_delete_entry(handle, old_dir, old_de, old_de_bh);
1357 if (status < 0) {
1358 mlog_errno(status);
1359 goto bail;
1360 }
1361
1362 if (new_inode) {
1363 new_inode->i_nlink--;
1364 new_inode->i_ctime = CURRENT_TIME;
1365 }
1366 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1367 if (old_inode_de_bh) {
1368 status = ocfs2_journal_access(handle, old_inode,
1369 old_inode_de_bh,
1370 OCFS2_JOURNAL_ACCESS_WRITE);
1371 PARENT_INO(old_inode_de_bh->b_data) =
1372 cpu_to_le64(OCFS2_I(new_dir)->ip_blkno);
1373 status = ocfs2_journal_dirty(handle, old_inode_de_bh);
1374 old_dir->i_nlink--;
1375 if (new_inode) {
1376 new_inode->i_nlink--;
1377 } else {
d8c76e6f 1378 inc_nlink(new_dir);
ccd979bd
MF
1379 mark_inode_dirty(new_dir);
1380 }
1381 }
1382 mark_inode_dirty(old_dir);
1383 if (new_inode)
1384 mark_inode_dirty(new_inode);
1385
1386 if (old_dir != new_dir)
1387 if (new_dir_nlink != new_dir->i_nlink) {
1388 if (!new_dir_bh) {
1389 mlog(ML_ERROR, "need to change nlink for new "
b0697053
MF
1390 "dir %llu from %d to %d but bh is NULL\n",
1391 (unsigned long long)OCFS2_I(new_dir)->ip_blkno,
ccd979bd
MF
1392 (int)new_dir_nlink, new_dir->i_nlink);
1393 } else {
1394 struct ocfs2_dinode *fe;
1395 status = ocfs2_journal_access(handle,
1396 new_dir,
1397 new_dir_bh,
1398 OCFS2_JOURNAL_ACCESS_WRITE);
1399 fe = (struct ocfs2_dinode *) new_dir_bh->b_data;
1400 fe->i_links_count = cpu_to_le16(new_dir->i_nlink);
1401 status = ocfs2_journal_dirty(handle, new_dir_bh);
1402 }
1403 }
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;
1413 status = ocfs2_journal_access(handle, old_dir,
1414 old_dir_bh,
1415 OCFS2_JOURNAL_ACCESS_WRITE);
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)
1429 ocfs2_commit_trans(handle);
1430
1431 if (new_inode)
1432 sync_mapping_buffers(old_inode->i_mapping);
1433
1434 if (new_inode)
1435 iput(new_inode);
1436 if (newfe_bh)
1437 brelse(newfe_bh);
1438 if (old_dir_bh)
1439 brelse(old_dir_bh);
1440 if (new_dir_bh)
1441 brelse(new_dir_bh);
1442 if (new_de_bh)
1443 brelse(new_de_bh);
1444 if (old_de_bh)
1445 brelse(old_de_bh);
1446 if (old_inode_de_bh)
1447 brelse(old_inode_de_bh);
1448 if (orphan_entry_bh)
1449 brelse(orphan_entry_bh);
1450 if (insert_entry_bh)
1451 brelse(insert_entry_bh);
1452
1453 mlog_exit(status);
1454
1455 return status;
1456}
1457
1458/*
1459 * we expect i_size = strlen(symname). Copy symname into the file
1460 * data, including the null terminator.
1461 */
1462static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
1463 struct ocfs2_journal_handle *handle,
1464 struct inode *inode,
1465 const char *symname)
1466{
1467 struct buffer_head **bhs = NULL;
1468 const char *c;
1469 struct super_block *sb = osb->sb;
1470 u64 p_blkno;
1471 int p_blocks;
1472 int virtual, blocks, status, i, bytes_left;
1473
1474 bytes_left = i_size_read(inode) + 1;
1475 /* we can't trust i_blocks because we're actually going to
1476 * write i_size + 1 bytes. */
1477 blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1478
5515eff8
AM
1479 mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
1480 (unsigned long long)inode->i_blocks,
1481 i_size_read(inode), blocks);
ccd979bd
MF
1482
1483 /* Sanity check -- make sure we're going to fit. */
1484 if (bytes_left >
1485 ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1486 status = -EIO;
1487 mlog_errno(status);
1488 goto bail;
1489 }
1490
1491 bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
1492 if (!bhs) {
1493 status = -ENOMEM;
1494 mlog_errno(status);
1495 goto bail;
1496 }
1497
1498 status = ocfs2_extent_map_get_blocks(inode, 0, 1, &p_blkno,
1499 &p_blocks);
1500 if (status < 0) {
1501 mlog_errno(status);
1502 goto bail;
1503 }
1504
1505 /* links can never be larger than one cluster so we know this
1506 * is all going to be contiguous, but do a sanity check
1507 * anyway. */
1508 if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
1509 status = -EIO;
1510 mlog_errno(status);
1511 goto bail;
1512 }
1513
1514 virtual = 0;
1515 while(bytes_left > 0) {
1516 c = &symname[virtual * sb->s_blocksize];
1517
1518 bhs[virtual] = sb_getblk(sb, p_blkno);
1519 if (!bhs[virtual]) {
1520 status = -ENOMEM;
1521 mlog_errno(status);
1522 goto bail;
1523 }
1524 ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
1525
1526 status = ocfs2_journal_access(handle, inode, bhs[virtual],
1527 OCFS2_JOURNAL_ACCESS_CREATE);
1528 if (status < 0) {
1529 mlog_errno(status);
1530 goto bail;
1531 }
1532
1533 memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
1534
1535 memcpy(bhs[virtual]->b_data, c,
1536 (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
1537 bytes_left);
1538
1539 status = ocfs2_journal_dirty(handle, bhs[virtual]);
1540 if (status < 0) {
1541 mlog_errno(status);
1542 goto bail;
1543 }
1544
1545 virtual++;
1546 p_blkno++;
1547 bytes_left -= sb->s_blocksize;
1548 }
1549
1550 status = 0;
1551bail:
1552
1553 if (bhs) {
1554 for(i = 0; i < blocks; i++)
1555 if (bhs[i])
1556 brelse(bhs[i]);
1557 kfree(bhs);
1558 }
1559
1560 mlog_exit(status);
1561 return status;
1562}
1563
1564static int ocfs2_symlink(struct inode *dir,
1565 struct dentry *dentry,
1566 const char *symname)
1567{
1568 int status, l, credits;
1569 u64 newsize;
1570 struct ocfs2_super *osb = NULL;
1571 struct inode *inode = NULL;
1572 struct super_block *sb;
1573 struct buffer_head *new_fe_bh = NULL;
1574 struct buffer_head *de_bh = NULL;
1575 struct buffer_head *parent_fe_bh = NULL;
1576 struct ocfs2_dinode *fe = NULL;
1577 struct ocfs2_dinode *dirfe;
1578 struct ocfs2_journal_handle *handle = NULL;
1579 struct ocfs2_alloc_context *inode_ac = NULL;
1580 struct ocfs2_alloc_context *data_ac = NULL;
1581
1582 mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
1583 dentry, symname, dentry->d_name.len, dentry->d_name.name);
1584
1585 sb = dir->i_sb;
1586 osb = OCFS2_SB(sb);
1587
1588 l = strlen(symname) + 1;
1589
1590 credits = ocfs2_calc_symlink_credits(sb);
1591
1592 handle = ocfs2_alloc_handle(osb);
1593 if (handle == NULL) {
1594 status = -ENOMEM;
1595 mlog_errno(status);
1596 goto bail;
1597 }
1598
1599 /* lock the parent directory */
1600 status = ocfs2_meta_lock(dir, handle, &parent_fe_bh, 1);
1601 if (status < 0) {
1602 if (status != -ENOENT)
1603 mlog_errno(status);
1604 goto bail;
1605 }
1606
1607 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1608 if (!dirfe->i_links_count) {
1609 /* can't make a file in a deleted directory. */
1610 status = -ENOENT;
1611 goto bail;
1612 }
1613
1614 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
1615 dentry->d_name.len);
1616 if (status)
1617 goto bail;
1618
1619 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
1620 dentry->d_name.name,
1621 dentry->d_name.len, &de_bh);
1622 if (status < 0) {
1623 mlog_errno(status);
1624 goto bail;
1625 }
1626
1627 status = ocfs2_reserve_new_inode(osb, handle, &inode_ac);
1628 if (status < 0) {
1629 if (status != -ENOSPC)
1630 mlog_errno(status);
1631 goto bail;
1632 }
1633
1634 /* don't reserve bitmap space for fast symlinks. */
1635 if (l > ocfs2_fast_symlink_chars(sb)) {
1636 status = ocfs2_reserve_clusters(osb, handle, 1, &data_ac);
1637 if (status < 0) {
1638 if (status != -ENOSPC)
1639 mlog_errno(status);
1640 goto bail;
1641 }
1642 }
1643
1644 handle = ocfs2_start_trans(osb, handle, credits);
1645 if (IS_ERR(handle)) {
1646 status = PTR_ERR(handle);
1647 handle = NULL;
1648 mlog_errno(status);
1649 goto bail;
1650 }
1651
1652 status = ocfs2_mknod_locked(osb, dir, dentry,
1653 S_IFLNK | S_IRWXUGO, 0,
1654 &new_fe_bh, parent_fe_bh, handle,
1655 &inode, inode_ac);
1656 if (status < 0) {
1657 mlog_errno(status);
1658 goto bail;
1659 }
1660
1661 fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
1662 inode->i_rdev = 0;
1663 newsize = l - 1;
1664 if (l > ocfs2_fast_symlink_chars(sb)) {
1665 inode->i_op = &ocfs2_symlink_inode_operations;
1666 status = ocfs2_do_extend_allocation(osb, inode, 1, new_fe_bh,
1667 handle, data_ac, NULL,
1668 NULL);
1669 if (status < 0) {
1670 if (status != -ENOSPC && status != -EINTR) {
b0697053
MF
1671 mlog(ML_ERROR,
1672 "Failed to extend file to %llu\n",
1673 (unsigned long long)newsize);
ccd979bd
MF
1674 mlog_errno(status);
1675 status = -ENOSPC;
1676 }
1677 goto bail;
1678 }
1679 i_size_write(inode, newsize);
1680 inode->i_blocks = ocfs2_align_bytes_to_sectors(newsize);
1681 } else {
1682 inode->i_op = &ocfs2_fast_symlink_inode_operations;
1683 memcpy((char *) fe->id2.i_symlink, symname, l);
1684 i_size_write(inode, newsize);
1685 inode->i_blocks = 0;
1686 }
1687
1688 status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
1689 if (status < 0) {
1690 mlog_errno(status);
1691 goto bail;
1692 }
1693
1694 if (!ocfs2_inode_is_fast_symlink(inode)) {
1695 status = ocfs2_create_symlink_data(osb, handle, inode,
1696 symname);
1697 if (status < 0) {
1698 mlog_errno(status);
1699 goto bail;
1700 }
1701 }
1702
1703 status = ocfs2_add_entry(handle, dentry, inode,
1704 le64_to_cpu(fe->i_blkno), parent_fe_bh,
1705 de_bh);
1706 if (status < 0) {
1707 mlog_errno(status);
1708 goto bail;
1709 }
1710
0027dd5b 1711 status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
379dfe9d
MF
1712 if (status) {
1713 mlog_errno(status);
1714 goto bail;
1715 }
1716
ccd979bd
MF
1717 insert_inode_hash(inode);
1718 dentry->d_op = &ocfs2_dentry_ops;
1719 d_instantiate(dentry, inode);
1720bail:
1721 if (handle)
1722 ocfs2_commit_trans(handle);
1723 if (new_fe_bh)
1724 brelse(new_fe_bh);
1725 if (parent_fe_bh)
1726 brelse(parent_fe_bh);
1727 if (de_bh)
1728 brelse(de_bh);
1729 if (inode_ac)
1730 ocfs2_free_alloc_context(inode_ac);
1731 if (data_ac)
1732 ocfs2_free_alloc_context(data_ac);
1733 if ((status < 0) && inode)
1734 iput(inode);
1735
1736 mlog_exit(status);
1737
1738 return status;
1739}
1740
1741int ocfs2_check_dir_entry(struct inode * dir,
1742 struct ocfs2_dir_entry * de,
1743 struct buffer_head * bh,
1744 unsigned long offset)
1745{
1746 const char *error_msg = NULL;
1747 const int rlen = le16_to_cpu(de->rec_len);
1748
1749 if (rlen < OCFS2_DIR_REC_LEN(1))
1750 error_msg = "rec_len is smaller than minimal";
1751 else if (rlen % 4 != 0)
1752 error_msg = "rec_len % 4 != 0";
1753 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
1754 error_msg = "rec_len is too small for name_len";
1755 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
1756 error_msg = "directory entry across blocks";
1757
1758 if (error_msg != NULL)
b0697053
MF
1759 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
1760 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
1761 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
1762 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
1763 de->name_len);
ccd979bd
MF
1764 return error_msg == NULL ? 1 : 0;
1765}
1766
1767/* we don't always have a dentry for what we want to add, so people
1768 * like orphan dir can call this instead.
1769 *
1770 * If you pass me insert_bh, I'll skip the search of the other dir
1771 * blocks and put the record in there.
1772 */
1773static int __ocfs2_add_entry(struct ocfs2_journal_handle *handle,
1774 struct inode *dir,
1775 const char *name, int namelen,
1776 struct inode *inode, u64 blkno,
1777 struct buffer_head *parent_fe_bh,
1778 struct buffer_head *insert_bh)
1779{
1780 unsigned long offset;
1781 unsigned short rec_len;
1782 struct ocfs2_dir_entry *de, *de1;
1783 struct super_block *sb;
1784 int retval, status;
1785
1786 mlog_entry_void();
1787
1788 sb = dir->i_sb;
1789
1790 if (!namelen)
1791 return -EINVAL;
1792
1793 rec_len = OCFS2_DIR_REC_LEN(namelen);
1794 offset = 0;
1795 de = (struct ocfs2_dir_entry *) insert_bh->b_data;
1796 while (1) {
1797 BUG_ON((char *)de >= sb->s_blocksize + insert_bh->b_data);
1798 /* These checks should've already been passed by the
1799 * prepare function, but I guess we can leave them
1800 * here anyway. */
1801 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
1802 retval = -ENOENT;
1803 goto bail;
1804 }
1805 if (ocfs2_match(namelen, name, de)) {
1806 retval = -EEXIST;
1807 goto bail;
1808 }
1809 if (((le64_to_cpu(de->inode) == 0) &&
1810 (le16_to_cpu(de->rec_len) >= rec_len)) ||
1811 (le16_to_cpu(de->rec_len) >=
1812 (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
1813 status = ocfs2_journal_access(handle, dir, insert_bh,
1814 OCFS2_JOURNAL_ACCESS_WRITE);
1815 /* By now the buffer is marked for journaling */
1816 offset += le16_to_cpu(de->rec_len);
1817 if (le64_to_cpu(de->inode)) {
1818 de1 = (struct ocfs2_dir_entry *)((char *) de +
1819 OCFS2_DIR_REC_LEN(de->name_len));
1820 de1->rec_len =
1821 cpu_to_le16(le16_to_cpu(de->rec_len) -
1822 OCFS2_DIR_REC_LEN(de->name_len));
1823 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1824 de = de1;
1825 }
1826 de->file_type = OCFS2_FT_UNKNOWN;
1827 if (blkno) {
1828 de->inode = cpu_to_le64(blkno);
1829 ocfs2_set_de_type(de, inode->i_mode);
1830 } else
1831 de->inode = 0;
1832 de->name_len = namelen;
1833 memcpy(de->name, name, namelen);
1834
1835 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1836 dir->i_version++;
1837 status = ocfs2_journal_dirty(handle, insert_bh);
1838 retval = 0;
1839 goto bail;
1840 }
1841 offset += le16_to_cpu(de->rec_len);
1842 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
1843 }
1844
1845 /* when you think about it, the assert above should prevent us
1846 * from ever getting here. */
1847 retval = -ENOSPC;
1848bail:
1849
1850 mlog_exit(retval);
1851 return retval;
1852}
1853
1854
1855/*
1856 * ocfs2_delete_entry deletes a directory entry by merging it with the
1857 * previous entry
1858 */
1859static int ocfs2_delete_entry(struct ocfs2_journal_handle *handle,
1860 struct inode *dir,
1861 struct ocfs2_dir_entry *de_del,
1862 struct buffer_head *bh)
1863{
1864 struct ocfs2_dir_entry *de, *pde;
1865 int i, status = -ENOENT;
1866
1867 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
1868
1869 i = 0;
1870 pde = NULL;
1871 de = (struct ocfs2_dir_entry *) bh->b_data;
1872 while (i < bh->b_size) {
1873 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
1874 status = -EIO;
1875 mlog_errno(status);
1876 goto bail;
1877 }
1878 if (de == de_del) {
1879 status = ocfs2_journal_access(handle, dir, bh,
1880 OCFS2_JOURNAL_ACCESS_WRITE);
1881 if (status < 0) {
1882 status = -EIO;
1883 mlog_errno(status);
1884 goto bail;
1885 }
1886 if (pde)
1887 pde->rec_len =
1888 cpu_to_le16(le16_to_cpu(pde->rec_len) +
1889 le16_to_cpu(de->rec_len));
1890 else
1891 de->inode = 0;
1892 dir->i_version++;
1893 status = ocfs2_journal_dirty(handle, bh);
1894 goto bail;
1895 }
1896 i += le16_to_cpu(de->rec_len);
1897 pde = de;
1898 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
1899 }
1900bail:
1901 mlog_exit(status);
1902 return status;
1903}
1904
1905/*
1906 * Returns 0 if not found, -1 on failure, and 1 on success
1907 */
1908static int inline ocfs2_search_dirblock(struct buffer_head *bh,
1909 struct inode *dir,
1910 const char *name, int namelen,
1911 unsigned long offset,
1912 struct ocfs2_dir_entry **res_dir)
1913{
1914 struct ocfs2_dir_entry *de;
1915 char *dlimit, *de_buf;
1916 int de_len;
1917 int ret = 0;
1918
1919 mlog_entry_void();
1920
1921 de_buf = bh->b_data;
1922 dlimit = de_buf + dir->i_sb->s_blocksize;
1923
1924 while (de_buf < dlimit) {
1925 /* this code is executed quadratically often */
1926 /* do minimal checking `by hand' */
1927
1928 de = (struct ocfs2_dir_entry *) de_buf;
1929
1930 if (de_buf + namelen <= dlimit &&
1931 ocfs2_match(namelen, name, de)) {
1932 /* found a match - just to be sure, do a full check */
1933 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1934 ret = -1;
1935 goto bail;
1936 }
1937 *res_dir = de;
1938 ret = 1;
1939 goto bail;
1940 }
1941
1942 /* prevent looping on a bad block */
1943 de_len = le16_to_cpu(de->rec_len);
1944 if (de_len <= 0) {
1945 ret = -1;
1946 goto bail;
1947 }
1948
1949 de_buf += de_len;
1950 offset += de_len;
1951 }
1952
1953bail:
1954 mlog_exit(ret);
1955 return ret;
1956}
1957
1958struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
1959 struct inode *dir,
1960 struct ocfs2_dir_entry **res_dir)
1961{
1962 struct super_block *sb;
1963 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1964 struct buffer_head *bh, *ret = NULL;
1965 unsigned long start, block, b;
1966 int ra_max = 0; /* Number of bh's in the readahead
1967 buffer, bh_use[] */
1968 int ra_ptr = 0; /* Current index into readahead
1969 buffer */
1970 int num = 0;
1971 int nblocks, i, err;
1972
1973 mlog_entry_void();
1974
1975 *res_dir = NULL;
1976 sb = dir->i_sb;
1977
1978 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
1979 start = OCFS2_I(dir)->ip_dir_start_lookup;
1980 if (start >= nblocks)
1981 start = 0;
1982 block = start;
1983
1984restart:
1985 do {
1986 /*
1987 * We deal with the read-ahead logic here.
1988 */
1989 if (ra_ptr >= ra_max) {
1990 /* Refill the readahead buffer */
1991 ra_ptr = 0;
1992 b = block;
1993 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
1994 /*
1995 * Terminate if we reach the end of the
1996 * directory and must wrap, or if our
1997 * search has finished at this block.
1998 */
1999 if (b >= nblocks || (num && block == start)) {
2000 bh_use[ra_max] = NULL;
2001 break;
2002 }
2003 num++;
2004
ccd979bd
MF
2005 bh = ocfs2_bread(dir, b++, &err, 1);
2006 bh_use[ra_max] = bh;
ccd979bd
MF
2007 }
2008 }
2009 if ((bh = bh_use[ra_ptr++]) == NULL)
2010 goto next;
2011 wait_on_buffer(bh);
2012 if (!buffer_uptodate(bh)) {
2013 /* read error, skip block & hope for the best */
aa958874
MF
2014 ocfs2_error(dir->i_sb, "reading directory %llu, "
2015 "offset %lu\n",
2016 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2017 block);
ccd979bd
MF
2018 brelse(bh);
2019 goto next;
2020 }
2021 i = ocfs2_search_dirblock(bh, dir, name, namelen,
2022 block << sb->s_blocksize_bits,
2023 res_dir);
2024 if (i == 1) {
2025 OCFS2_I(dir)->ip_dir_start_lookup = block;
2026 ret = bh;
2027 goto cleanup_and_exit;
2028 } else {
2029 brelse(bh);
2030 if (i < 0)
2031 goto cleanup_and_exit;
2032 }
2033 next:
2034 if (++block >= nblocks)
2035 block = 0;
2036 } while (block != start);
2037
2038 /*
2039 * If the directory has grown while we were searching, then
2040 * search the last part of the directory before giving up.
2041 */
2042 block = nblocks;
2043 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
2044 if (block < nblocks) {
2045 start = 0;
2046 goto restart;
2047 }
2048
2049cleanup_and_exit:
2050 /* Clean up the read-ahead blocks */
2051 for (; ra_ptr < ra_max; ra_ptr++)
2052 brelse(bh_use[ra_ptr]);
2053
2054 mlog_exit_ptr(ret);
2055 return ret;
2056}
2057
2058static int ocfs2_blkno_stringify(u64 blkno, char *name)
2059{
2060 int status, namelen;
2061
2062 mlog_entry_void();
2063
b0697053
MF
2064 namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
2065 (long long)blkno);
ccd979bd
MF
2066 if (namelen <= 0) {
2067 if (namelen)
2068 status = namelen;
2069 else
2070 status = -EINVAL;
2071 mlog_errno(status);
2072 goto bail;
2073 }
2074 if (namelen != OCFS2_ORPHAN_NAMELEN) {
2075 status = -EINVAL;
2076 mlog_errno(status);
2077 goto bail;
2078 }
2079
2080 mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
2081 namelen);
2082
2083 status = 0;
2084bail:
2085 mlog_exit(status);
2086 return status;
2087}
2088
2089static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
2090 struct ocfs2_journal_handle *handle,
2091 struct inode *inode,
2092 char *name,
2093 struct buffer_head **de_bh)
2094{
2095 struct inode *orphan_dir_inode = NULL;
2096 struct buffer_head *orphan_dir_bh = NULL;
2097 int status = 0;
2098
2099 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2100 if (status < 0) {
2101 mlog_errno(status);
2102 goto leave;
2103 }
2104
2105 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
2106 ORPHAN_DIR_SYSTEM_INODE,
2107 osb->slot_num);
2108 if (!orphan_dir_inode) {
2109 status = -ENOENT;
2110 mlog_errno(status);
2111 goto leave;
2112 }
2113
2114 ocfs2_handle_add_inode(handle, orphan_dir_inode);
2115 status = ocfs2_meta_lock(orphan_dir_inode, handle, &orphan_dir_bh, 1);
2116 if (status < 0) {
2117 mlog_errno(status);
2118 goto leave;
2119 }
2120
2121 status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
2122 orphan_dir_bh, name,
2123 OCFS2_ORPHAN_NAMELEN, de_bh);
2124 if (status < 0) {
2125 mlog_errno(status);
2126 goto leave;
2127 }
2128
2129leave:
2130 if (orphan_dir_inode)
2131 iput(orphan_dir_inode);
2132
2133 if (orphan_dir_bh)
2134 brelse(orphan_dir_bh);
2135
2136 mlog_exit(status);
2137 return status;
2138}
2139
2140static int ocfs2_orphan_add(struct ocfs2_super *osb,
2141 struct ocfs2_journal_handle *handle,
2142 struct inode *inode,
2143 struct ocfs2_dinode *fe,
2144 char *name,
2145 struct buffer_head *de_bh)
2146{
2147 struct inode *orphan_dir_inode = NULL;
2148 struct buffer_head *orphan_dir_bh = NULL;
2149 int status = 0;
2150 struct ocfs2_dinode *orphan_fe;
2151
2152 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
2153
2154 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
2155 ORPHAN_DIR_SYSTEM_INODE,
2156 osb->slot_num);
2157 if (!orphan_dir_inode) {
2158 status = -ENOENT;
2159 mlog_errno(status);
2160 goto leave;
2161 }
2162
2163 status = ocfs2_read_block(osb,
2164 OCFS2_I(orphan_dir_inode)->ip_blkno,
2165 &orphan_dir_bh, OCFS2_BH_CACHED,
2166 orphan_dir_inode);
2167 if (status < 0) {
2168 mlog_errno(status);
2169 goto leave;
2170 }
2171
2172 status = ocfs2_journal_access(handle, orphan_dir_inode, orphan_dir_bh,
2173 OCFS2_JOURNAL_ACCESS_WRITE);
2174 if (status < 0) {
2175 mlog_errno(status);
2176 goto leave;
2177 }
2178
2179 /* we're a cluster, and nlink can change on disk from
2180 * underneath us... */
2181 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2182 if (S_ISDIR(inode->i_mode))
2183 le16_add_cpu(&orphan_fe->i_links_count, 1);
2184 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
2185
2186 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
2187 if (status < 0) {
2188 mlog_errno(status);
2189 goto leave;
2190 }
2191
2192 status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
2193 OCFS2_ORPHAN_NAMELEN, inode,
2194 OCFS2_I(inode)->ip_blkno,
2195 orphan_dir_bh, de_bh);
2196 if (status < 0) {
2197 mlog_errno(status);
2198 goto leave;
2199 }
2200
2201 le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
2202
2203 /* Record which orphan dir our inode now resides
2204 * in. delete_inode will use this to determine which orphan
2205 * dir to lock. */
2206 spin_lock(&OCFS2_I(inode)->ip_lock);
2207 OCFS2_I(inode)->ip_orphaned_slot = osb->slot_num;
2208 spin_unlock(&OCFS2_I(inode)->ip_lock);
2209
b0697053
MF
2210 mlog(0, "Inode %llu orphaned in slot %d\n",
2211 (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
ccd979bd
MF
2212
2213leave:
2214 if (orphan_dir_inode)
2215 iput(orphan_dir_inode);
2216
2217 if (orphan_dir_bh)
2218 brelse(orphan_dir_bh);
2219
2220 mlog_exit(status);
2221 return status;
2222}
2223
2224/* unlike orphan_add, we expect the orphan dir to already be locked here. */
2225int ocfs2_orphan_del(struct ocfs2_super *osb,
2226 struct ocfs2_journal_handle *handle,
2227 struct inode *orphan_dir_inode,
2228 struct inode *inode,
2229 struct buffer_head *orphan_dir_bh)
2230{
2231 char name[OCFS2_ORPHAN_NAMELEN + 1];
2232 struct ocfs2_dinode *orphan_fe;
2233 int status = 0;
2234 struct buffer_head *target_de_bh = NULL;
2235 struct ocfs2_dir_entry *target_de = NULL;
2236
2237 mlog_entry_void();
2238
2239 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2240 if (status < 0) {
2241 mlog_errno(status);
2242 goto leave;
2243 }
2244
b0697053
MF
2245 mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
2246 name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
2247 OCFS2_ORPHAN_NAMELEN);
ccd979bd
MF
2248
2249 /* find it's spot in the orphan directory */
2250 target_de_bh = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN,
2251 orphan_dir_inode, &target_de);
2252 if (!target_de_bh) {
2253 status = -ENOENT;
2254 mlog_errno(status);
2255 goto leave;
2256 }
2257
2258 /* remove it from the orphan directory */
2259 status = ocfs2_delete_entry(handle, orphan_dir_inode, target_de,
2260 target_de_bh);
2261 if (status < 0) {
2262 mlog_errno(status);
2263 goto leave;
2264 }
2265
2266 status = ocfs2_journal_access(handle,orphan_dir_inode, orphan_dir_bh,
2267 OCFS2_JOURNAL_ACCESS_WRITE);
2268 if (status < 0) {
2269 mlog_errno(status);
2270 goto leave;
2271 }
2272
2273 /* do the i_nlink dance! :) */
2274 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2275 if (S_ISDIR(inode->i_mode))
2276 le16_add_cpu(&orphan_fe->i_links_count, -1);
2277 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
2278
2279 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
2280 if (status < 0) {
2281 mlog_errno(status);
2282 goto leave;
2283 }
2284
2285leave:
2286 if (target_de_bh)
2287 brelse(target_de_bh);
2288
2289 mlog_exit(status);
2290 return status;
2291}
2292
2293struct inode_operations ocfs2_dir_iops = {
2294 .create = ocfs2_create,
2295 .lookup = ocfs2_lookup,
2296 .link = ocfs2_link,
2297 .unlink = ocfs2_unlink,
2298 .rmdir = ocfs2_unlink,
2299 .symlink = ocfs2_symlink,
2300 .mkdir = ocfs2_mkdir,
2301 .mknod = ocfs2_mknod,
2302 .rename = ocfs2_rename,
2303 .setattr = ocfs2_setattr,
2304 .getattr = ocfs2_getattr,
2305};
This page took 0.209351 seconds and 5 git commands to generate.