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