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