xen: import arch generic part of xencomm
[safe/jmp/linux-2.6] / fs / xfs / xfs_rename.c
index 23b48ac..ee37189 100644 (file)
@@ -1,61 +1,44 @@
 /*
- * Copyright (c) 2000-2003 Silicon Graphics, Inc.  All Rights Reserved.
+ * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
+ * All Rights Reserved.
  *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
  * published by the Free Software Foundation.
  *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
  *
- * Further, this software is distributed without any warranty that it is
- * free of the rightful claim of any third person regarding infringement
- * or the like.  Any license provided herein, whether implied or
- * otherwise, applies only to this software file.  Patent licenses, if
- * any, provided herein do not apply to combinations of this program with
- * other software, or any other product whatsoever.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc., 59
- * Temple Place - Suite 330, Boston MA 02111-1307, USA.
- *
- * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
- * Mountain View, CA  94043, or:
- *
- * http://www.sgi.com
- *
- * For further information regarding this notice, see:
- *
- * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write the Free Software Foundation,
+ * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
-
 #include "xfs.h"
-#include "xfs_macros.h"
+#include "xfs_fs.h"
 #include "xfs_types.h"
-#include "xfs_inum.h"
 #include "xfs_log.h"
+#include "xfs_inum.h"
 #include "xfs_trans.h"
 #include "xfs_sb.h"
-#include "xfs_dir.h"
+#include "xfs_ag.h"
 #include "xfs_dir2.h"
 #include "xfs_dmapi.h"
 #include "xfs_mount.h"
+#include "xfs_da_btree.h"
 #include "xfs_bmap_btree.h"
-#include "xfs_attr_sf.h"
-#include "xfs_dir_sf.h"
 #include "xfs_dir2_sf.h"
+#include "xfs_attr_sf.h"
 #include "xfs_dinode.h"
-#include "xfs_inode_item.h"
 #include "xfs_inode.h"
+#include "xfs_inode_item.h"
 #include "xfs_bmap.h"
 #include "xfs_error.h"
 #include "xfs_quota.h"
-#include "xfs_refcache.h"
 #include "xfs_utils.h"
 #include "xfs_trans_space.h"
-#include "xfs_da_btree.h"
-#include "xfs_dir_leaf.h"
+#include "xfs_vnodeops.h"
 
 
 /*
@@ -100,25 +83,23 @@ int xfs_rename_skip, xfs_rename_nskip;
  */
 STATIC int
 xfs_lock_for_rename(
-       xfs_inode_t     *dp1,   /* old (source) directory inode */
-       xfs_inode_t     *dp2,   /* new (target) directory inode */
-       vname_t         *vname1,/* old entry name */
-       vname_t         *vname2,/* new entry name */
-       xfs_inode_t     **ipp1, /* inode of old entry */
-       xfs_inode_t     **ipp2, /* inode of new entry, if it
+       xfs_inode_t     *dp1,   /* in: old (source) directory inode */
+       xfs_inode_t     *dp2,   /* in: new (target) directory inode */
+       xfs_inode_t     *ip1,   /* in: inode of old entry */
+       struct xfs_name *name2, /* in: new entry name */
+       xfs_inode_t     **ipp2, /* out: inode of new entry, if it
                                   already exists, NULL otherwise. */
-       xfs_inode_t     **i_tab,/* array of inode returned, sorted */
-       int             *num_inodes)  /* number of inodes in array */
+       xfs_inode_t     **i_tab,/* out: array of inode returned, sorted */
+       int             *num_inodes)  /* out: number of inodes in array */
 {
-       xfs_inode_t             *ip1, *ip2, *temp;
+       xfs_inode_t             *ip2 = NULL;
+       xfs_inode_t             *temp;
        xfs_ino_t               inum1, inum2;
        int                     error;
        int                     i, j;
        uint                    lock_mode;
        int                     diff_dirs = (dp1 != dp2);
 
-       ip2 = NULL;
-
        /*
         * First, find out the current inums of the entries so that we
         * can determine the initial locking order.  We'll have to
@@ -126,28 +107,20 @@ xfs_lock_for_rename(
         * to see if we still have the right inodes, directories, etc.
         */
        lock_mode = xfs_ilock_map_shared(dp1);
-       error = xfs_get_dir_entry(vname1, &ip1);
-       if (error) {
-               xfs_iunlock_map_shared(dp1, lock_mode);
-               return error;
-       }
+       IHOLD(ip1);
+       xfs_itrace_ref(ip1);
 
        inum1 = ip1->i_ino;
 
-       ASSERT(ip1);
-       ITRACE(ip1);
-
        /*
         * Unlock dp1 and lock dp2 if they are different.
         */
-
        if (diff_dirs) {
                xfs_iunlock_map_shared(dp1, lock_mode);
                lock_mode = xfs_ilock_map_shared(dp2);
        }
 
-       error = xfs_dir_lookup_int(XFS_ITOBHV(dp2), lock_mode,
-                                  vname2, &inum2, &ip2);
+       error = xfs_dir_lookup_int(dp2, lock_mode, name2, &inum2, &ip2);
        if (error == ENOENT) {          /* target does not need to exist. */
                inum2 = 0;
        } else if (error) {
@@ -159,7 +132,7 @@ xfs_lock_for_rename(
                IRELE (ip1);
                return error;
        } else {
-               ITRACE(ip2);
+               xfs_itrace_ref(ip2);
        }
 
        /*
@@ -179,6 +152,7 @@ xfs_lock_for_rename(
                *num_inodes = 4;
                i_tab[3] = ip2;
        }
+       *ipp2 = i_tab[3];
 
        /*
         * Sort the elements via bubble sort.  (Remember, there are at
@@ -216,21 +190,6 @@ xfs_lock_for_rename(
                xfs_lock_inodes(i_tab, *num_inodes, 0, XFS_ILOCK_SHARED);
        }
 
-       /*
-        * Set the return value. Null out any unused entries in i_tab.
-        */
-       *ipp1 = *ipp2 = NULL;
-       for (i=0; i < *num_inodes; i++) {
-               if (i_tab[i]->i_ino == inum1) {
-                       *ipp1 = i_tab[i];
-               }
-               if (i_tab[i]->i_ino == inum2) {
-                       *ipp2 = i_tab[i];
-               }
-       }
-       for (;i < 4; i++) {
-               i_tab[i] = NULL;
-       }
        return 0;
 }
 
@@ -239,15 +198,15 @@ xfs_lock_for_rename(
  */
 int
 xfs_rename(
-       bhv_desc_t      *src_dir_bdp,
-       vname_t         *src_vname,
-       vnode_t         *target_dir_vp,
-       vname_t         *target_vname,
-       cred_t          *credp)
+       xfs_inode_t     *src_dp,
+       struct xfs_name *src_name,
+       xfs_inode_t     *src_ip,
+       xfs_inode_t     *target_dp,
+       struct xfs_name *target_name)
 {
        xfs_trans_t     *tp;
-       xfs_inode_t     *src_dp, *target_dp, *src_ip, *target_ip;
-       xfs_mount_t     *mp;
+       xfs_inode_t     *target_ip;
+       xfs_mount_t     *mp = src_dp->i_mount;
        int             new_parent;             /* moving to a new dir */
        int             src_is_directory;       /* src_name is a directory */
        int             error;
@@ -257,41 +216,19 @@ xfs_rename(
        int             committed;
        xfs_inode_t     *inodes[4];
        int             target_ip_dropped = 0;  /* dropped target_ip link? */
-       vnode_t         *src_dir_vp;
-       bhv_desc_t      *target_dir_bdp;
        int             spaceres;
        int             target_link_zero = 0;
        int             num_inodes;
-       char            *src_name = VNAME(src_vname);
-       char            *target_name = VNAME(target_vname);
-       int             src_namelen = VNAMELEN(src_vname);
-       int             target_namelen = VNAMELEN(target_vname);
-
-       src_dir_vp = BHV_TO_VNODE(src_dir_bdp);
-       vn_trace_entry(src_dir_vp, "xfs_rename", (inst_t *)__return_address);
-       vn_trace_entry(target_dir_vp, "xfs_rename", (inst_t *)__return_address);
-
-       /*
-        * Find the XFS behavior descriptor for the target directory
-        * vnode since it was not handed to us.
-        */
-       target_dir_bdp = vn_bhv_lookup_unlocked(VN_BHV_HEAD(target_dir_vp),
-                                               &xfs_vnodeops);
-       if (target_dir_bdp == NULL) {
-               return XFS_ERROR(EXDEV);
-       }
 
-       src_dp = XFS_BHVTOI(src_dir_bdp);
-       target_dp = XFS_BHVTOI(target_dir_bdp);
-       mp = src_dp->i_mount;
+       xfs_itrace_entry(src_dp);
+       xfs_itrace_entry(target_dp);
 
-       if (DM_EVENT_ENABLED(src_dir_vp->v_vfsp, src_dp, DM_EVENT_RENAME) ||
-           DM_EVENT_ENABLED(target_dir_vp->v_vfsp,
-                               target_dp, DM_EVENT_RENAME)) {
+       if (DM_EVENT_ENABLED(src_dp, DM_EVENT_RENAME) ||
+           DM_EVENT_ENABLED(target_dp, DM_EVENT_RENAME)) {
                error = XFS_SEND_NAMESP(mp, DM_EVENT_RENAME,
-                                       src_dir_vp, DM_RIGHT_NULL,
-                                       target_dir_vp, DM_RIGHT_NULL,
-                                       src_name, target_name,
+                                       src_dp, DM_RIGHT_NULL,
+                                       target_dp, DM_RIGHT_NULL,
+                                       src_name->name, target_name->name,
                                        0, 0, 0);
                if (error) {
                        return error;
@@ -308,10 +245,8 @@ xfs_rename(
         * does not exist in the source directory.
         */
        tp = NULL;
-       error = xfs_lock_for_rename(src_dp, target_dp, src_vname,
-                       target_vname, &src_ip, &target_ip, inodes,
-                       &num_inodes);
-
+       error = xfs_lock_for_rename(src_dp, target_dp, src_ip, target_name,
+                                       &target_ip, inodes, &num_inodes);
        if (error) {
                /*
                 * We have nothing locked, no inode references, and
@@ -334,6 +269,18 @@ xfs_rename(
                }
        }
 
+       /*
+        * If we are using project inheritance, we only allow renames
+        * into our tree when the project IDs are the same; else the
+        * tree quota mechanism would be circumvented.
+        */
+       if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
+                    (target_dp->i_d.di_projid != src_ip->i_d.di_projid))) {
+               error = XFS_ERROR(EXDEV);
+               xfs_rename_unlock4(inodes, XFS_ILOCK_SHARED);
+               goto rele_return;
+       }
+
        new_parent = (src_dp != target_dp);
        src_is_directory = ((src_ip->i_d.di_mode & S_IFMT) == S_IFDIR);
 
@@ -345,7 +292,7 @@ xfs_rename(
        XFS_BMAP_INIT(&free_list, &first_block);
        tp = xfs_trans_alloc(mp, XFS_TRANS_RENAME);
        cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
-       spaceres = XFS_RENAME_SPACE_RES(mp, target_namelen);
+       spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
        error = xfs_trans_reserve(tp, spaceres, XFS_RENAME_LOG_RES(mp), 0,
                        XFS_TRANS_PERM_LOG_RES, XFS_RENAME_LOG_COUNT);
        if (error == ENOSPC) {
@@ -379,10 +326,10 @@ xfs_rename(
         * them when they unlock the inodes.  Also, we need to be careful
         * not to add an inode to the transaction more than once.
         */
-       VN_HOLD(src_dir_vp);
+       IHOLD(src_dp);
        xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
        if (new_parent) {
-               VN_HOLD(target_dir_vp);
+               IHOLD(target_dp);
                xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL);
        }
        if ((src_ip != src_dp) && (src_ip != target_dp)) {
@@ -403,35 +350,29 @@ xfs_rename(
                 * If there's no space reservation, check the entry will
                 * fit before actually inserting it.
                 */
-               if (spaceres == 0 &&
-                   (error = XFS_DIR_CANENTER(mp, tp, target_dp, target_name,
-                               target_namelen))) {
+               error = xfs_dir_canenter(tp, target_dp, target_name, spaceres);
+               if (error)
                        goto error_return;
-               }
                /*
                 * If target does not exist and the rename crosses
                 * directories, adjust the target directory link count
                 * to account for the ".." reference from the new entry.
                 */
-               error = XFS_DIR_CREATENAME(mp, tp, target_dp, target_name,
-                                          target_namelen, src_ip->i_ino,
-                                          &first_block, &free_list, spaceres);
-               if (error == ENOSPC) {
+               error = xfs_dir_createname(tp, target_dp, target_name,
+                                               src_ip->i_ino, &first_block,
+                                               &free_list, spaceres);
+               if (error == ENOSPC)
                        goto error_return;
-               }
-               if (error) {
+               if (error)
                        goto abort_return;
-               }
                xfs_ichgtime(target_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
 
                if (new_parent && src_is_directory) {
                        error = xfs_bumplink(tp, target_dp);
-                       if (error) {
+                       if (error)
                                goto abort_return;
-                       }
                }
        } else { /* target_ip != NULL */
-
                /*
                 * If target exists and it's a directory, check that both
                 * target and source are directories and that target can be
@@ -441,7 +382,7 @@ xfs_rename(
                        /*
                         * Make sure target dir is empty.
                         */
-                       if (!(XFS_DIR_ISEMPTY(target_ip->i_mount, target_ip)) ||
+                       if (!(xfs_dir_isempty(target_ip)) ||
                            (target_ip->i_d.di_nlink > 2)) {
                                error = XFS_ERROR(EEXIST);
                                goto error_return;
@@ -457,12 +398,11 @@ xfs_rename(
                 * In case there is already an entry with the same
                 * name at the destination directory, remove it first.
                 */
-               error = XFS_DIR_REPLACE(mp, tp, target_dp, target_name,
-                       target_namelen, src_ip->i_ino, &first_block,
-                       &free_list, spaceres);
-               if (error) {
+               error = xfs_dir_replace(tp, target_dp, target_name,
+                                       src_ip->i_ino,
+                                       &first_block, &free_list, spaceres);
+               if (error)
                        goto abort_return;
-               }
                xfs_ichgtime(target_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
 
                /*
@@ -470,9 +410,8 @@ xfs_rename(
                 * dir no longer points to it.
                 */
                error = xfs_droplink(tp, target_ip);
-               if (error) {
+               if (error)
                        goto abort_return;
-               }
                target_ip_dropped = 1;
 
                if (src_is_directory) {
@@ -480,9 +419,8 @@ xfs_rename(
                         * Drop the link from the old "." entry.
                         */
                        error = xfs_droplink(tp, target_ip);
-                       if (error) {
+                       if (error)
                                goto abort_return;
-                       }
                }
 
                /* Do this test while we still hold the locks */
@@ -494,18 +432,16 @@ xfs_rename(
         * Remove the source.
         */
        if (new_parent && src_is_directory) {
-
                /*
                 * Rewrite the ".." entry to point to the new
                 * directory.
                 */
-               error = XFS_DIR_REPLACE(mp, tp, src_ip, "..", 2,
-                                       target_dp->i_ino, &first_block,
-                                       &free_list, spaceres);
+               error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
+                                       target_dp->i_ino,
+                                       &first_block, &free_list, spaceres);
                ASSERT(error != EEXIST);
-               if (error) {
+               if (error)
                        goto abort_return;
-               }
                xfs_ichgtime(src_ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
 
        } else {
@@ -533,16 +469,14 @@ xfs_rename(
                 * entry that's moved no longer points to it.
                 */
                error = xfs_droplink(tp, src_dp);
-               if (error) {
+               if (error)
                        goto abort_return;
-               }
        }
 
-       error = XFS_DIR_REMOVENAME(mp, tp, src_dp, src_name, src_namelen,
-                       src_ip->i_ino, &first_block, &free_list, spaceres);
-       if (error) {
+       error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
+                                       &first_block, &free_list, spaceres);
+       if (error)
                goto abort_return;
-       }
        xfs_ichgtime(src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
 
        /*
@@ -587,7 +521,7 @@ xfs_rename(
                IHOLD(target_ip);
        IHOLD(src_ip);
 
-       error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
+       error = xfs_bmap_finish(&tp, &free_list, &committed);
        if (error) {
                xfs_bmap_cancel(&free_list);
                xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
@@ -606,34 +540,26 @@ xfs_rename(
         * trans_commit will unlock src_ip, target_ip & decrement
         * the vnode references.
         */
-       error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
-       if (target_ip != NULL) {
-               xfs_refcache_purge_ip(target_ip);
+       error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
+       if (target_ip != NULL)
                IRELE(target_ip);
-       }
        /*
         * Let interposed file systems know about removed links.
         */
-       if (target_ip_dropped) {
-               VOP_LINK_REMOVED(XFS_ITOV(target_ip), target_dir_vp,
-                                       target_link_zero);
+       if (target_ip_dropped)
                IRELE(target_ip);
-       }
-
-       FSC_NOTIFY_NAME_CHANGED(XFS_ITOV(src_ip));
 
        IRELE(src_ip);
 
        /* Fall through to std_return with error = 0 or errno from
         * xfs_trans_commit      */
 std_return:
-       if (DM_EVENT_ENABLED(src_dir_vp->v_vfsp, src_dp, DM_EVENT_POSTRENAME) ||
-           DM_EVENT_ENABLED(target_dir_vp->v_vfsp,
-                               target_dp, DM_EVENT_POSTRENAME)) {
+       if (DM_EVENT_ENABLED(src_dp, DM_EVENT_POSTRENAME) ||
+           DM_EVENT_ENABLED(target_dp, DM_EVENT_POSTRENAME)) {
                (void) XFS_SEND_NAMESP (mp, DM_EVENT_POSTRENAME,
-                                       src_dir_vp, DM_RIGHT_NULL,
-                                       target_dir_vp, DM_RIGHT_NULL,
-                                       src_name, target_name,
+                                       src_dp, DM_RIGHT_NULL,
+                                       target_dp, DM_RIGHT_NULL,
+                                       src_name->name, target_name->name,
                                        0, error, 0);
        }
        return error;