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