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