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