Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) International Business Machines Corp., 2000-2004 |
| 3 | * Portions Copyright (C) Christoph Hellwig, 2001-2002 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 7 | * the Free Software Foundation; either version 2 of the License, or |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * (at your option) any later version. |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 9 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 16 | * along with this program; if not, write to the Free Software |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | #include <linux/fs.h> |
Nick Piggin | 2bc334d | 2011-01-07 17:49:25 +1100 | [diff] [blame] | 21 | #include <linux/namei.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/ctype.h> |
| 23 | #include <linux/quotaops.h> |
Christoph Hellwig | d425de7 | 2007-10-21 16:42:09 -0700 | [diff] [blame] | 24 | #include <linux/exportfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "jfs_incore.h" |
| 26 | #include "jfs_superblock.h" |
| 27 | #include "jfs_inode.h" |
| 28 | #include "jfs_dinode.h" |
| 29 | #include "jfs_dmap.h" |
| 30 | #include "jfs_unicode.h" |
| 31 | #include "jfs_metapage.h" |
| 32 | #include "jfs_xattr.h" |
| 33 | #include "jfs_acl.h" |
| 34 | #include "jfs_debug.h" |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | /* |
| 37 | * forward references |
| 38 | */ |
Al Viro | ad28b4e | 2009-02-20 06:00:49 +0000 | [diff] [blame] | 39 | const struct dentry_operations jfs_ci_dentry_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | static s64 commitZeroLink(tid_t, struct inode *); |
| 42 | |
| 43 | /* |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 44 | * NAME: free_ea_wmap(inode) |
| 45 | * |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 46 | * FUNCTION: free uncommitted extended attributes from working map |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 47 | * |
| 48 | */ |
| 49 | static inline void free_ea_wmap(struct inode *inode) |
| 50 | { |
| 51 | dxd_t *ea = &JFS_IP(inode)->ea; |
| 52 | |
| 53 | if (ea->flag & DXD_EXTENT) { |
| 54 | /* free EA pages from cache */ |
| 55 | invalidate_dxd_metapages(inode, *ea); |
| 56 | dbFree(inode, addressDXD(ea), lengthDXD(ea)); |
| 57 | } |
| 58 | ea->flag = 0; |
| 59 | } |
| 60 | |
| 61 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | * NAME: jfs_create(dip, dentry, mode) |
| 63 | * |
| 64 | * FUNCTION: create a regular file in the parent directory <dip> |
| 65 | * with name = <from dentry> and mode = <mode> |
| 66 | * |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 67 | * PARAMETER: dip - parent directory vnode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | * dentry - dentry of new file |
| 69 | * mode - create mode (rwxrwxrwx). |
| 70 | * nd- nd struct |
| 71 | * |
| 72 | * RETURN: Errors from subroutines |
| 73 | * |
| 74 | */ |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 75 | static int jfs_create(struct inode *dip, struct dentry *dentry, umode_t mode, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 76 | bool excl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
| 78 | int rc = 0; |
| 79 | tid_t tid; /* transaction id */ |
| 80 | struct inode *ip = NULL; /* child directory inode */ |
| 81 | ino_t ino; |
| 82 | struct component_name dname; /* child directory name */ |
| 83 | struct btstack btstack; |
| 84 | struct inode *iplist[2]; |
| 85 | struct tblock *tblk; |
| 86 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 87 | jfs_info("jfs_create: dip:0x%p name:%pd", dip, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Dave Kleikamp | acc84b0 | 2015-07-15 13:53:19 -0500 | [diff] [blame] | 89 | rc = dquot_initialize(dip); |
| 90 | if (rc) |
| 91 | goto out1; |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | /* |
| 94 | * search parent directory for entry/freespace |
| 95 | * (dtSearch() returns parent directory page pinned) |
| 96 | */ |
| 97 | if ((rc = get_UCSname(&dname, dentry))) |
| 98 | goto out1; |
| 99 | |
| 100 | /* |
| 101 | * Either iAlloc() or txBegin() may block. Deadlock can occur if we |
| 102 | * block there while holding dtree page, so we allocate the inode & |
| 103 | * begin the transaction before we search the directory. |
| 104 | */ |
| 105 | ip = ialloc(dip, mode); |
Akinobu Mita | 087387f | 2006-09-14 09:22:38 -0500 | [diff] [blame] | 106 | if (IS_ERR(ip)) { |
| 107 | rc = PTR_ERR(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | goto out2; |
| 109 | } |
| 110 | |
| 111 | tid = txBegin(dip->i_sb, 0); |
| 112 | |
Dave Kleikamp | 82d5b9a | 2007-01-09 14:14:48 -0600 | [diff] [blame] | 113 | mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT); |
| 114 | mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 116 | rc = jfs_init_acl(tid, ip, dip); |
| 117 | if (rc) |
| 118 | goto out3; |
| 119 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 120 | rc = jfs_init_security(tid, ip, dip, &dentry->d_name); |
Dave Kleikamp | 1d15b10f | 2005-09-01 09:05:39 -0500 | [diff] [blame] | 121 | if (rc) { |
| 122 | txAbort(tid, 0); |
| 123 | goto out3; |
| 124 | } |
| 125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) { |
| 127 | jfs_err("jfs_create: dtSearch returned %d", rc); |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 128 | txAbort(tid, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | goto out3; |
| 130 | } |
| 131 | |
| 132 | tblk = tid_to_tblock(tid); |
| 133 | tblk->xflag |= COMMIT_CREATE; |
| 134 | tblk->ino = ip->i_ino; |
| 135 | tblk->u.ixpxd = JFS_IP(ip)->ixpxd; |
| 136 | |
| 137 | iplist[0] = dip; |
| 138 | iplist[1] = ip; |
| 139 | |
| 140 | /* |
| 141 | * initialize the child XAD tree root in-line in inode |
| 142 | */ |
| 143 | xtInitRoot(tid, ip); |
| 144 | |
| 145 | /* |
| 146 | * create entry in parent directory for child directory |
| 147 | * (dtInsert() releases parent directory page) |
| 148 | */ |
| 149 | ino = ip->i_ino; |
| 150 | if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) { |
| 151 | if (rc == -EIO) { |
| 152 | jfs_err("jfs_create: dtInsert returned -EIO"); |
| 153 | txAbort(tid, 1); /* Marks Filesystem dirty */ |
| 154 | } else |
| 155 | txAbort(tid, 0); /* Filesystem full */ |
| 156 | goto out3; |
| 157 | } |
| 158 | |
| 159 | ip->i_op = &jfs_file_inode_operations; |
| 160 | ip->i_fop = &jfs_file_operations; |
| 161 | ip->i_mapping->a_ops = &jfs_aops; |
| 162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | mark_inode_dirty(ip); |
| 164 | |
| 165 | dip->i_ctime = dip->i_mtime = CURRENT_TIME; |
| 166 | |
| 167 | mark_inode_dirty(dip); |
| 168 | |
| 169 | rc = txCommit(tid, 2, &iplist[0], 0); |
| 170 | |
| 171 | out3: |
| 172 | txEnd(tid); |
Ingo Molnar | 1de8744 | 2006-01-24 15:22:50 -0600 | [diff] [blame] | 173 | mutex_unlock(&JFS_IP(ip)->commit_mutex); |
Evgeniy Dushistov | 48ce8b0 | 2006-06-05 08:21:03 -0500 | [diff] [blame] | 174 | mutex_unlock(&JFS_IP(dip)->commit_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | if (rc) { |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 176 | free_ea_wmap(ip); |
Miklos Szeredi | 6d6b77f | 2011-10-28 14:13:28 +0200 | [diff] [blame] | 177 | clear_nlink(ip); |
Dave Kleikamp | 1f3403f | 2008-12-30 22:08:37 -0600 | [diff] [blame] | 178 | unlock_new_inode(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | iput(ip); |
Dave Kleikamp | 1f3403f | 2008-12-30 22:08:37 -0600 | [diff] [blame] | 180 | } else { |
Dave Kleikamp | 1f3403f | 2008-12-30 22:08:37 -0600 | [diff] [blame] | 181 | unlock_new_inode(ip); |
Al Viro | 8fc37ec | 2012-07-19 09:18:15 +0400 | [diff] [blame] | 182 | d_instantiate(dentry, ip); |
Dave Kleikamp | 1f3403f | 2008-12-30 22:08:37 -0600 | [diff] [blame] | 183 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
| 185 | out2: |
| 186 | free_UCSname(&dname); |
| 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | out1: |
| 189 | |
| 190 | jfs_info("jfs_create: rc:%d", rc); |
| 191 | return rc; |
| 192 | } |
| 193 | |
| 194 | |
| 195 | /* |
| 196 | * NAME: jfs_mkdir(dip, dentry, mode) |
| 197 | * |
| 198 | * FUNCTION: create a child directory in the parent directory <dip> |
| 199 | * with name = <from dentry> and mode = <mode> |
| 200 | * |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 201 | * PARAMETER: dip - parent directory vnode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | * dentry - dentry of child directory |
| 203 | * mode - create mode (rwxrwxrwx). |
| 204 | * |
| 205 | * RETURN: Errors from subroutines |
| 206 | * |
| 207 | * note: |
| 208 | * EACCESS: user needs search+write permission on the parent directory |
| 209 | */ |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 210 | static int jfs_mkdir(struct inode *dip, struct dentry *dentry, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | { |
| 212 | int rc = 0; |
| 213 | tid_t tid; /* transaction id */ |
| 214 | struct inode *ip = NULL; /* child directory inode */ |
| 215 | ino_t ino; |
| 216 | struct component_name dname; /* child directory name */ |
| 217 | struct btstack btstack; |
| 218 | struct inode *iplist[2]; |
| 219 | struct tblock *tblk; |
| 220 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 221 | jfs_info("jfs_mkdir: dip:0x%p name:%pd", dip, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Dave Kleikamp | acc84b0 | 2015-07-15 13:53:19 -0500 | [diff] [blame] | 223 | rc = dquot_initialize(dip); |
| 224 | if (rc) |
| 225 | goto out1; |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | /* |
| 228 | * search parent directory for entry/freespace |
| 229 | * (dtSearch() returns parent directory page pinned) |
| 230 | */ |
| 231 | if ((rc = get_UCSname(&dname, dentry))) |
| 232 | goto out1; |
| 233 | |
| 234 | /* |
| 235 | * Either iAlloc() or txBegin() may block. Deadlock can occur if we |
| 236 | * block there while holding dtree page, so we allocate the inode & |
| 237 | * begin the transaction before we search the directory. |
| 238 | */ |
| 239 | ip = ialloc(dip, S_IFDIR | mode); |
Akinobu Mita | 087387f | 2006-09-14 09:22:38 -0500 | [diff] [blame] | 240 | if (IS_ERR(ip)) { |
| 241 | rc = PTR_ERR(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | goto out2; |
| 243 | } |
| 244 | |
| 245 | tid = txBegin(dip->i_sb, 0); |
| 246 | |
Dave Kleikamp | 82d5b9a | 2007-01-09 14:14:48 -0600 | [diff] [blame] | 247 | mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT); |
| 248 | mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 250 | rc = jfs_init_acl(tid, ip, dip); |
| 251 | if (rc) |
| 252 | goto out3; |
| 253 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 254 | rc = jfs_init_security(tid, ip, dip, &dentry->d_name); |
Dave Kleikamp | 1d15b10f | 2005-09-01 09:05:39 -0500 | [diff] [blame] | 255 | if (rc) { |
| 256 | txAbort(tid, 0); |
| 257 | goto out3; |
| 258 | } |
| 259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) { |
| 261 | jfs_err("jfs_mkdir: dtSearch returned %d", rc); |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 262 | txAbort(tid, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | goto out3; |
| 264 | } |
| 265 | |
| 266 | tblk = tid_to_tblock(tid); |
| 267 | tblk->xflag |= COMMIT_CREATE; |
| 268 | tblk->ino = ip->i_ino; |
| 269 | tblk->u.ixpxd = JFS_IP(ip)->ixpxd; |
| 270 | |
| 271 | iplist[0] = dip; |
| 272 | iplist[1] = ip; |
| 273 | |
| 274 | /* |
| 275 | * initialize the child directory in-line in inode |
| 276 | */ |
| 277 | dtInitRoot(tid, ip, dip->i_ino); |
| 278 | |
| 279 | /* |
| 280 | * create entry in parent directory for child directory |
| 281 | * (dtInsert() releases parent directory page) |
| 282 | */ |
| 283 | ino = ip->i_ino; |
| 284 | if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) { |
| 285 | if (rc == -EIO) { |
| 286 | jfs_err("jfs_mkdir: dtInsert returned -EIO"); |
| 287 | txAbort(tid, 1); /* Marks Filesystem dirty */ |
| 288 | } else |
| 289 | txAbort(tid, 0); /* Filesystem full */ |
| 290 | goto out3; |
| 291 | } |
| 292 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 293 | set_nlink(ip, 2); /* for '.' */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | ip->i_op = &jfs_dir_inode_operations; |
| 295 | ip->i_fop = &jfs_dir_operations; |
| 296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | mark_inode_dirty(ip); |
| 298 | |
| 299 | /* update parent directory inode */ |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 300 | inc_nlink(dip); /* for '..' from child directory */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | dip->i_ctime = dip->i_mtime = CURRENT_TIME; |
| 302 | mark_inode_dirty(dip); |
| 303 | |
| 304 | rc = txCommit(tid, 2, &iplist[0], 0); |
| 305 | |
| 306 | out3: |
| 307 | txEnd(tid); |
Ingo Molnar | 1de8744 | 2006-01-24 15:22:50 -0600 | [diff] [blame] | 308 | mutex_unlock(&JFS_IP(ip)->commit_mutex); |
Evgeniy Dushistov | 48ce8b0 | 2006-06-05 08:21:03 -0500 | [diff] [blame] | 309 | mutex_unlock(&JFS_IP(dip)->commit_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | if (rc) { |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 311 | free_ea_wmap(ip); |
Miklos Szeredi | 6d6b77f | 2011-10-28 14:13:28 +0200 | [diff] [blame] | 312 | clear_nlink(ip); |
Dave Kleikamp | 1f3403f | 2008-12-30 22:08:37 -0600 | [diff] [blame] | 313 | unlock_new_inode(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | iput(ip); |
Dave Kleikamp | 1f3403f | 2008-12-30 22:08:37 -0600 | [diff] [blame] | 315 | } else { |
Dave Kleikamp | 1f3403f | 2008-12-30 22:08:37 -0600 | [diff] [blame] | 316 | unlock_new_inode(ip); |
Al Viro | 8fc37ec | 2012-07-19 09:18:15 +0400 | [diff] [blame] | 317 | d_instantiate(dentry, ip); |
Dave Kleikamp | 1f3403f | 2008-12-30 22:08:37 -0600 | [diff] [blame] | 318 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
| 320 | out2: |
| 321 | free_UCSname(&dname); |
| 322 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
| 324 | out1: |
| 325 | |
| 326 | jfs_info("jfs_mkdir: rc:%d", rc); |
| 327 | return rc; |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * NAME: jfs_rmdir(dip, dentry) |
| 332 | * |
| 333 | * FUNCTION: remove a link to child directory |
| 334 | * |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 335 | * PARAMETER: dip - parent inode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | * dentry - child directory dentry |
| 337 | * |
| 338 | * RETURN: -EINVAL - if name is . or .. |
Dave Kleikamp | f720e3b | 2007-06-06 15:28:35 -0500 | [diff] [blame] | 339 | * -EINVAL - if . or .. exist but are invalid. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | * errors from subroutines |
| 341 | * |
| 342 | * note: |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 343 | * if other threads have the directory open when the last link |
| 344 | * is removed, the "." and ".." entries, if present, are removed before |
| 345 | * rmdir() returns and no new entries may be created in the directory, |
| 346 | * but the directory is not removed until the last reference to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | * the directory is released (cf.unlink() of regular file). |
| 348 | */ |
| 349 | static int jfs_rmdir(struct inode *dip, struct dentry *dentry) |
| 350 | { |
| 351 | int rc; |
| 352 | tid_t tid; /* transaction id */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 353 | struct inode *ip = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | ino_t ino; |
| 355 | struct component_name dname; |
| 356 | struct inode *iplist[2]; |
| 357 | struct tblock *tblk; |
| 358 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 359 | jfs_info("jfs_rmdir: dip:0x%p name:%pd", dip, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
| 361 | /* Init inode for quota operations. */ |
Dave Kleikamp | acc84b0 | 2015-07-15 13:53:19 -0500 | [diff] [blame] | 362 | rc = dquot_initialize(dip); |
| 363 | if (rc) |
| 364 | goto out; |
| 365 | rc = dquot_initialize(ip); |
| 366 | if (rc) |
| 367 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
| 369 | /* directory must be empty to be removed */ |
| 370 | if (!dtEmpty(ip)) { |
| 371 | rc = -ENOTEMPTY; |
| 372 | goto out; |
| 373 | } |
| 374 | |
| 375 | if ((rc = get_UCSname(&dname, dentry))) { |
| 376 | goto out; |
| 377 | } |
| 378 | |
| 379 | tid = txBegin(dip->i_sb, 0); |
| 380 | |
Dave Kleikamp | 82d5b9a | 2007-01-09 14:14:48 -0600 | [diff] [blame] | 381 | mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT); |
| 382 | mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
| 384 | iplist[0] = dip; |
| 385 | iplist[1] = ip; |
| 386 | |
| 387 | tblk = tid_to_tblock(tid); |
| 388 | tblk->xflag |= COMMIT_DELETE; |
| 389 | tblk->u.ip = ip; |
| 390 | |
| 391 | /* |
| 392 | * delete the entry of target directory from parent directory |
| 393 | */ |
| 394 | ino = ip->i_ino; |
| 395 | if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) { |
| 396 | jfs_err("jfs_rmdir: dtDelete returned %d", rc); |
| 397 | if (rc == -EIO) |
| 398 | txAbort(tid, 1); |
| 399 | txEnd(tid); |
Ingo Molnar | 1de8744 | 2006-01-24 15:22:50 -0600 | [diff] [blame] | 400 | mutex_unlock(&JFS_IP(ip)->commit_mutex); |
Evgeniy Dushistov | 48ce8b0 | 2006-06-05 08:21:03 -0500 | [diff] [blame] | 401 | mutex_unlock(&JFS_IP(dip)->commit_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
| 403 | goto out2; |
| 404 | } |
| 405 | |
| 406 | /* update parent directory's link count corresponding |
| 407 | * to ".." entry of the target directory deleted |
| 408 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | dip->i_ctime = dip->i_mtime = CURRENT_TIME; |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 410 | inode_dec_link_count(dip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
| 412 | /* |
| 413 | * OS/2 could have created EA and/or ACL |
| 414 | */ |
| 415 | /* free EA from both persistent and working map */ |
| 416 | if (JFS_IP(ip)->ea.flag & DXD_EXTENT) { |
| 417 | /* free EA pages */ |
| 418 | txEA(tid, ip, &JFS_IP(ip)->ea, NULL); |
| 419 | } |
| 420 | JFS_IP(ip)->ea.flag = 0; |
| 421 | |
| 422 | /* free ACL from both persistent and working map */ |
| 423 | if (JFS_IP(ip)->acl.flag & DXD_EXTENT) { |
| 424 | /* free ACL pages */ |
| 425 | txEA(tid, ip, &JFS_IP(ip)->acl, NULL); |
| 426 | } |
| 427 | JFS_IP(ip)->acl.flag = 0; |
| 428 | |
| 429 | /* mark the target directory as deleted */ |
Dave Hansen | ce71ec3 | 2006-09-30 23:29:06 -0700 | [diff] [blame] | 430 | clear_nlink(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | mark_inode_dirty(ip); |
| 432 | |
| 433 | rc = txCommit(tid, 2, &iplist[0], 0); |
| 434 | |
| 435 | txEnd(tid); |
| 436 | |
Ingo Molnar | 1de8744 | 2006-01-24 15:22:50 -0600 | [diff] [blame] | 437 | mutex_unlock(&JFS_IP(ip)->commit_mutex); |
Evgeniy Dushistov | 48ce8b0 | 2006-06-05 08:21:03 -0500 | [diff] [blame] | 438 | mutex_unlock(&JFS_IP(dip)->commit_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
| 440 | /* |
| 441 | * Truncating the directory index table is not guaranteed. It |
| 442 | * may need to be done iteratively |
| 443 | */ |
| 444 | if (test_cflag(COMMIT_Stale, dip)) { |
| 445 | if (dip->i_size > 1) |
| 446 | jfs_truncate_nolock(dip, 0); |
| 447 | |
| 448 | clear_cflag(COMMIT_Stale, dip); |
| 449 | } |
| 450 | |
| 451 | out2: |
| 452 | free_UCSname(&dname); |
| 453 | |
| 454 | out: |
| 455 | jfs_info("jfs_rmdir: rc:%d", rc); |
| 456 | return rc; |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | * NAME: jfs_unlink(dip, dentry) |
| 461 | * |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 462 | * FUNCTION: remove a link to object <vp> named by <name> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | * from parent directory <dvp> |
| 464 | * |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 465 | * PARAMETER: dip - inode of parent directory |
| 466 | * dentry - dentry of object to be removed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | * |
| 468 | * RETURN: errors from subroutines |
| 469 | * |
| 470 | * note: |
| 471 | * temporary file: if one or more processes have the file open |
| 472 | * when the last link is removed, the link will be removed before |
| 473 | * unlink() returns, but the removal of the file contents will be |
| 474 | * postponed until all references to the files are closed. |
| 475 | * |
| 476 | * JFS does NOT support unlink() on directories. |
| 477 | * |
| 478 | */ |
| 479 | static int jfs_unlink(struct inode *dip, struct dentry *dentry) |
| 480 | { |
| 481 | int rc; |
| 482 | tid_t tid; /* transaction id */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 483 | struct inode *ip = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | ino_t ino; |
| 485 | struct component_name dname; /* object name */ |
| 486 | struct inode *iplist[2]; |
| 487 | struct tblock *tblk; |
| 488 | s64 new_size = 0; |
| 489 | int commit_flag; |
| 490 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 491 | jfs_info("jfs_unlink: dip:0x%p name:%pd", dip, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | |
| 493 | /* Init inode for quota operations. */ |
Dave Kleikamp | acc84b0 | 2015-07-15 13:53:19 -0500 | [diff] [blame] | 494 | rc = dquot_initialize(dip); |
| 495 | if (rc) |
| 496 | goto out; |
| 497 | rc = dquot_initialize(ip); |
| 498 | if (rc) |
| 499 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
| 501 | if ((rc = get_UCSname(&dname, dentry))) |
| 502 | goto out; |
| 503 | |
Dave Kleikamp | 82d5b9a | 2007-01-09 14:14:48 -0600 | [diff] [blame] | 504 | IWRITE_LOCK(ip, RDWRLOCK_NORMAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
| 506 | tid = txBegin(dip->i_sb, 0); |
| 507 | |
Dave Kleikamp | 82d5b9a | 2007-01-09 14:14:48 -0600 | [diff] [blame] | 508 | mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT); |
| 509 | mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
| 511 | iplist[0] = dip; |
| 512 | iplist[1] = ip; |
| 513 | |
| 514 | /* |
| 515 | * delete the entry of target file from parent directory |
| 516 | */ |
| 517 | ino = ip->i_ino; |
| 518 | if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) { |
| 519 | jfs_err("jfs_unlink: dtDelete returned %d", rc); |
| 520 | if (rc == -EIO) |
| 521 | txAbort(tid, 1); /* Marks FS Dirty */ |
| 522 | txEnd(tid); |
Ingo Molnar | 1de8744 | 2006-01-24 15:22:50 -0600 | [diff] [blame] | 523 | mutex_unlock(&JFS_IP(ip)->commit_mutex); |
Evgeniy Dushistov | 48ce8b0 | 2006-06-05 08:21:03 -0500 | [diff] [blame] | 524 | mutex_unlock(&JFS_IP(dip)->commit_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | IWRITE_UNLOCK(ip); |
| 526 | goto out1; |
| 527 | } |
| 528 | |
| 529 | ASSERT(ip->i_nlink); |
| 530 | |
| 531 | ip->i_ctime = dip->i_ctime = dip->i_mtime = CURRENT_TIME; |
| 532 | mark_inode_dirty(dip); |
| 533 | |
| 534 | /* update target's inode */ |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 535 | inode_dec_link_count(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
| 537 | /* |
Dave Kleikamp | f720e3b | 2007-06-06 15:28:35 -0500 | [diff] [blame] | 538 | * commit zero link count object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | */ |
| 540 | if (ip->i_nlink == 0) { |
| 541 | assert(!test_cflag(COMMIT_Nolink, ip)); |
| 542 | /* free block resources */ |
| 543 | if ((new_size = commitZeroLink(tid, ip)) < 0) { |
| 544 | txAbort(tid, 1); /* Marks FS Dirty */ |
| 545 | txEnd(tid); |
Ingo Molnar | 1de8744 | 2006-01-24 15:22:50 -0600 | [diff] [blame] | 546 | mutex_unlock(&JFS_IP(ip)->commit_mutex); |
Evgeniy Dushistov | 48ce8b0 | 2006-06-05 08:21:03 -0500 | [diff] [blame] | 547 | mutex_unlock(&JFS_IP(dip)->commit_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | IWRITE_UNLOCK(ip); |
| 549 | rc = new_size; |
| 550 | goto out1; |
| 551 | } |
| 552 | tblk = tid_to_tblock(tid); |
| 553 | tblk->xflag |= COMMIT_DELETE; |
| 554 | tblk->u.ip = ip; |
| 555 | } |
| 556 | |
| 557 | /* |
| 558 | * Incomplete truncate of file data can |
| 559 | * result in timing problems unless we synchronously commit the |
| 560 | * transaction. |
| 561 | */ |
| 562 | if (new_size) |
| 563 | commit_flag = COMMIT_SYNC; |
| 564 | else |
| 565 | commit_flag = 0; |
| 566 | |
| 567 | /* |
| 568 | * If xtTruncate was incomplete, commit synchronously to avoid |
| 569 | * timing complications |
| 570 | */ |
| 571 | rc = txCommit(tid, 2, &iplist[0], commit_flag); |
| 572 | |
| 573 | txEnd(tid); |
| 574 | |
Ingo Molnar | 1de8744 | 2006-01-24 15:22:50 -0600 | [diff] [blame] | 575 | mutex_unlock(&JFS_IP(ip)->commit_mutex); |
Evgeniy Dushistov | 48ce8b0 | 2006-06-05 08:21:03 -0500 | [diff] [blame] | 576 | mutex_unlock(&JFS_IP(dip)->commit_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | |
| 578 | while (new_size && (rc == 0)) { |
| 579 | tid = txBegin(dip->i_sb, 0); |
Ingo Molnar | 1de8744 | 2006-01-24 15:22:50 -0600 | [diff] [blame] | 580 | mutex_lock(&JFS_IP(ip)->commit_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | new_size = xtTruncate_pmap(tid, ip, new_size); |
| 582 | if (new_size < 0) { |
| 583 | txAbort(tid, 1); /* Marks FS Dirty */ |
| 584 | rc = new_size; |
| 585 | } else |
| 586 | rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC); |
| 587 | txEnd(tid); |
Ingo Molnar | 1de8744 | 2006-01-24 15:22:50 -0600 | [diff] [blame] | 588 | mutex_unlock(&JFS_IP(ip)->commit_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | if (ip->i_nlink == 0) |
| 592 | set_cflag(COMMIT_Nolink, ip); |
| 593 | |
| 594 | IWRITE_UNLOCK(ip); |
| 595 | |
| 596 | /* |
| 597 | * Truncating the directory index table is not guaranteed. It |
| 598 | * may need to be done iteratively |
| 599 | */ |
| 600 | if (test_cflag(COMMIT_Stale, dip)) { |
| 601 | if (dip->i_size > 1) |
| 602 | jfs_truncate_nolock(dip, 0); |
| 603 | |
| 604 | clear_cflag(COMMIT_Stale, dip); |
| 605 | } |
| 606 | |
| 607 | out1: |
| 608 | free_UCSname(&dname); |
| 609 | out: |
| 610 | jfs_info("jfs_unlink: rc:%d", rc); |
| 611 | return rc; |
| 612 | } |
| 613 | |
| 614 | /* |
| 615 | * NAME: commitZeroLink() |
| 616 | * |
Dave Kleikamp | f720e3b | 2007-06-06 15:28:35 -0500 | [diff] [blame] | 617 | * FUNCTION: for non-directory, called by jfs_remove(), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | * truncate a regular file, directory or symbolic |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 619 | * link to zero length. return 0 if type is not |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | * one of these. |
| 621 | * |
| 622 | * if the file is currently associated with a VM segment |
| 623 | * only permanent disk and inode map resources are freed, |
| 624 | * and neither the inode nor indirect blocks are modified |
| 625 | * so that the resources can be later freed in the work |
| 626 | * map by ctrunc1. |
| 627 | * if there is no VM segment on entry, the resources are |
| 628 | * freed in both work and permanent map. |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 629 | * (? for temporary file - memory object is cached even |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | * after no reference: |
| 631 | * reference count > 0 - ) |
| 632 | * |
| 633 | * PARAMETERS: cd - pointer to commit data structure. |
| 634 | * current inode is the one to truncate. |
| 635 | * |
| 636 | * RETURN: Errors from subroutines |
| 637 | */ |
| 638 | static s64 commitZeroLink(tid_t tid, struct inode *ip) |
| 639 | { |
| 640 | int filetype; |
| 641 | struct tblock *tblk; |
| 642 | |
| 643 | jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip); |
| 644 | |
| 645 | filetype = ip->i_mode & S_IFMT; |
| 646 | switch (filetype) { |
| 647 | case S_IFREG: |
| 648 | break; |
| 649 | case S_IFLNK: |
| 650 | /* fast symbolic link */ |
| 651 | if (ip->i_size < IDATASIZE) { |
| 652 | ip->i_size = 0; |
| 653 | return 0; |
| 654 | } |
| 655 | break; |
| 656 | default: |
| 657 | assert(filetype != S_IFDIR); |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | set_cflag(COMMIT_Freewmap, ip); |
| 662 | |
| 663 | /* mark transaction of block map update type */ |
| 664 | tblk = tid_to_tblock(tid); |
| 665 | tblk->xflag |= COMMIT_PMAP; |
| 666 | |
| 667 | /* |
| 668 | * free EA |
| 669 | */ |
| 670 | if (JFS_IP(ip)->ea.flag & DXD_EXTENT) |
| 671 | /* acquire maplock on EA to be freed from block map */ |
| 672 | txEA(tid, ip, &JFS_IP(ip)->ea, NULL); |
| 673 | |
| 674 | /* |
| 675 | * free ACL |
| 676 | */ |
| 677 | if (JFS_IP(ip)->acl.flag & DXD_EXTENT) |
| 678 | /* acquire maplock on EA to be freed from block map */ |
| 679 | txEA(tid, ip, &JFS_IP(ip)->acl, NULL); |
| 680 | |
| 681 | /* |
| 682 | * free xtree/data (truncate to zero length): |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 683 | * free xtree/data pages from cache if COMMIT_PWMAP, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | * free xtree/data blocks from persistent block map, and |
| 685 | * free xtree/data blocks from working block map if COMMIT_PWMAP; |
| 686 | */ |
| 687 | if (ip->i_size) |
| 688 | return xtTruncate_pmap(tid, ip, 0); |
| 689 | |
| 690 | return 0; |
| 691 | } |
| 692 | |
| 693 | |
| 694 | /* |
Dave Kleikamp | 1868f4a | 2005-05-04 15:29:35 -0500 | [diff] [blame] | 695 | * NAME: jfs_free_zero_link() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | * |
Dave Kleikamp | f720e3b | 2007-06-06 15:28:35 -0500 | [diff] [blame] | 697 | * FUNCTION: for non-directory, called by iClose(), |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 698 | * free resources of a file from cache and WORKING map |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | * for a file previously committed with zero link count |
| 700 | * while associated with a pager object, |
| 701 | * |
| 702 | * PARAMETER: ip - pointer to inode of file. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | */ |
Dave Kleikamp | 1868f4a | 2005-05-04 15:29:35 -0500 | [diff] [blame] | 704 | void jfs_free_zero_link(struct inode *ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | int type; |
| 707 | |
Dave Kleikamp | 1868f4a | 2005-05-04 15:29:35 -0500 | [diff] [blame] | 708 | jfs_info("jfs_free_zero_link: ip = 0x%p", ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | |
| 710 | /* return if not reg or symbolic link or if size is |
| 711 | * already ok. |
| 712 | */ |
| 713 | type = ip->i_mode & S_IFMT; |
| 714 | |
| 715 | switch (type) { |
| 716 | case S_IFREG: |
| 717 | break; |
| 718 | case S_IFLNK: |
| 719 | /* if its contained in inode nothing to do */ |
| 720 | if (ip->i_size < IDATASIZE) |
Dave Kleikamp | 1868f4a | 2005-05-04 15:29:35 -0500 | [diff] [blame] | 721 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | break; |
| 723 | default: |
Dave Kleikamp | 1868f4a | 2005-05-04 15:29:35 -0500 | [diff] [blame] | 724 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | /* |
| 728 | * free EA |
| 729 | */ |
| 730 | if (JFS_IP(ip)->ea.flag & DXD_EXTENT) { |
| 731 | s64 xaddr = addressDXD(&JFS_IP(ip)->ea); |
| 732 | int xlen = lengthDXD(&JFS_IP(ip)->ea); |
| 733 | struct maplock maplock; /* maplock for COMMIT_WMAP */ |
| 734 | struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */ |
| 735 | |
| 736 | /* free EA pages from cache */ |
| 737 | invalidate_dxd_metapages(ip, JFS_IP(ip)->ea); |
| 738 | |
| 739 | /* free EA extent from working block map */ |
| 740 | maplock.index = 1; |
| 741 | pxdlock = (struct pxd_lock *) & maplock; |
| 742 | pxdlock->flag = mlckFREEPXD; |
| 743 | PXDaddress(&pxdlock->pxd, xaddr); |
| 744 | PXDlength(&pxdlock->pxd, xlen); |
| 745 | txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP); |
| 746 | } |
| 747 | |
| 748 | /* |
| 749 | * free ACL |
| 750 | */ |
| 751 | if (JFS_IP(ip)->acl.flag & DXD_EXTENT) { |
| 752 | s64 xaddr = addressDXD(&JFS_IP(ip)->acl); |
| 753 | int xlen = lengthDXD(&JFS_IP(ip)->acl); |
| 754 | struct maplock maplock; /* maplock for COMMIT_WMAP */ |
| 755 | struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */ |
| 756 | |
| 757 | invalidate_dxd_metapages(ip, JFS_IP(ip)->acl); |
| 758 | |
| 759 | /* free ACL extent from working block map */ |
| 760 | maplock.index = 1; |
| 761 | pxdlock = (struct pxd_lock *) & maplock; |
| 762 | pxdlock->flag = mlckFREEPXD; |
| 763 | PXDaddress(&pxdlock->pxd, xaddr); |
| 764 | PXDlength(&pxdlock->pxd, xlen); |
| 765 | txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP); |
| 766 | } |
| 767 | |
| 768 | /* |
| 769 | * free xtree/data (truncate to zero length): |
| 770 | * free xtree/data pages from cache, and |
| 771 | * free xtree/data blocks from working block map; |
| 772 | */ |
| 773 | if (ip->i_size) |
Dave Kleikamp | 1868f4a | 2005-05-04 15:29:35 -0500 | [diff] [blame] | 774 | xtTruncate(0, ip, 0, COMMIT_WMAP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | /* |
| 778 | * NAME: jfs_link(vp, dvp, name, crp) |
| 779 | * |
| 780 | * FUNCTION: create a link to <vp> by the name = <name> |
| 781 | * in the parent directory <dvp> |
| 782 | * |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 783 | * PARAMETER: vp - target object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | * dvp - parent directory of new link |
| 785 | * name - name of new link to target object |
| 786 | * crp - credential |
| 787 | * |
| 788 | * RETURN: Errors from subroutines |
| 789 | * |
| 790 | * note: |
| 791 | * JFS does NOT support link() on directories (to prevent circular |
| 792 | * path in the directory hierarchy); |
| 793 | * EPERM: the target object is a directory, and either the caller |
| 794 | * does not have appropriate privileges or the implementation prohibits |
| 795 | * using link() on directories [XPG4.2]. |
| 796 | * |
| 797 | * JFS does NOT support links between file systems: |
| 798 | * EXDEV: target object and new link are on different file systems and |
| 799 | * implementation does not support links between file systems [XPG4.2]. |
| 800 | */ |
| 801 | static int jfs_link(struct dentry *old_dentry, |
| 802 | struct inode *dir, struct dentry *dentry) |
| 803 | { |
| 804 | int rc; |
| 805 | tid_t tid; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 806 | struct inode *ip = d_inode(old_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | ino_t ino; |
| 808 | struct component_name dname; |
| 809 | struct btstack btstack; |
| 810 | struct inode *iplist[2]; |
| 811 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 812 | jfs_info("jfs_link: %pd %pd", old_dentry, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | |
Dave Kleikamp | acc84b0 | 2015-07-15 13:53:19 -0500 | [diff] [blame] | 814 | rc = dquot_initialize(dir); |
| 815 | if (rc) |
| 816 | goto out; |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | tid = txBegin(ip->i_sb, 0); |
| 819 | |
Dave Kleikamp | 82d5b9a | 2007-01-09 14:14:48 -0600 | [diff] [blame] | 820 | mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT); |
| 821 | mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | |
| 823 | /* |
| 824 | * scan parent directory for entry/freespace |
| 825 | */ |
| 826 | if ((rc = get_UCSname(&dname, dentry))) |
Dave Kleikamp | acc84b0 | 2015-07-15 13:53:19 -0500 | [diff] [blame] | 827 | goto out_tx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | |
| 829 | if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) |
| 830 | goto free_dname; |
| 831 | |
| 832 | /* |
| 833 | * create entry for new link in parent directory |
| 834 | */ |
| 835 | ino = ip->i_ino; |
| 836 | if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) |
| 837 | goto free_dname; |
| 838 | |
| 839 | /* update object inode */ |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 840 | inc_nlink(ip); /* for new link */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | ip->i_ctime = CURRENT_TIME; |
Dave Kleikamp | 988a649 | 2005-10-31 16:53:04 -0600 | [diff] [blame] | 842 | dir->i_ctime = dir->i_mtime = CURRENT_TIME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | mark_inode_dirty(dir); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 844 | ihold(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | |
| 846 | iplist[0] = ip; |
| 847 | iplist[1] = dir; |
| 848 | rc = txCommit(tid, 2, &iplist[0], 0); |
| 849 | |
| 850 | if (rc) { |
Miklos Szeredi | 6d6b77f | 2011-10-28 14:13:28 +0200 | [diff] [blame] | 851 | drop_nlink(ip); /* never instantiated */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | iput(ip); |
| 853 | } else |
| 854 | d_instantiate(dentry, ip); |
| 855 | |
| 856 | free_dname: |
| 857 | free_UCSname(&dname); |
| 858 | |
Dave Kleikamp | acc84b0 | 2015-07-15 13:53:19 -0500 | [diff] [blame] | 859 | out_tx: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | txEnd(tid); |
| 861 | |
Ingo Molnar | 1de8744 | 2006-01-24 15:22:50 -0600 | [diff] [blame] | 862 | mutex_unlock(&JFS_IP(ip)->commit_mutex); |
Evgeniy Dushistov | 48ce8b0 | 2006-06-05 08:21:03 -0500 | [diff] [blame] | 863 | mutex_unlock(&JFS_IP(dir)->commit_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | |
Dave Kleikamp | acc84b0 | 2015-07-15 13:53:19 -0500 | [diff] [blame] | 865 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | jfs_info("jfs_link: rc:%d", rc); |
| 867 | return rc; |
| 868 | } |
| 869 | |
| 870 | /* |
| 871 | * NAME: jfs_symlink(dip, dentry, name) |
| 872 | * |
| 873 | * FUNCTION: creates a symbolic link to <symlink> by name <name> |
Dave Kleikamp | f720e3b | 2007-06-06 15:28:35 -0500 | [diff] [blame] | 874 | * in directory <dip> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | * |
Dave Kleikamp | f720e3b | 2007-06-06 15:28:35 -0500 | [diff] [blame] | 876 | * PARAMETER: dip - parent directory vnode |
| 877 | * dentry - dentry of symbolic link |
| 878 | * name - the path name of the existing object |
| 879 | * that will be the source of the link |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | * |
| 881 | * RETURN: errors from subroutines |
| 882 | * |
| 883 | * note: |
| 884 | * ENAMETOOLONG: pathname resolution of a symbolic link produced |
| 885 | * an intermediate result whose length exceeds PATH_MAX [XPG4.2] |
| 886 | */ |
| 887 | |
| 888 | static int jfs_symlink(struct inode *dip, struct dentry *dentry, |
| 889 | const char *name) |
| 890 | { |
| 891 | int rc; |
| 892 | tid_t tid; |
| 893 | ino_t ino = 0; |
| 894 | struct component_name dname; |
| 895 | int ssize; /* source pathname size */ |
| 896 | struct btstack btstack; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 897 | struct inode *ip = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | s64 xlen = 0; |
| 899 | int bmask = 0, xsize; |
Dave Kleikamp | 3c2c226 | 2011-06-20 13:00:27 -0500 | [diff] [blame] | 900 | s64 xaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | struct metapage *mp; |
| 902 | struct super_block *sb; |
| 903 | struct tblock *tblk; |
| 904 | |
| 905 | struct inode *iplist[2]; |
| 906 | |
| 907 | jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name); |
| 908 | |
Dave Kleikamp | acc84b0 | 2015-07-15 13:53:19 -0500 | [diff] [blame] | 909 | rc = dquot_initialize(dip); |
| 910 | if (rc) |
| 911 | goto out1; |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 912 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | ssize = strlen(name) + 1; |
| 914 | |
| 915 | /* |
| 916 | * search parent directory for entry/freespace |
| 917 | * (dtSearch() returns parent directory page pinned) |
| 918 | */ |
| 919 | |
| 920 | if ((rc = get_UCSname(&dname, dentry))) |
| 921 | goto out1; |
| 922 | |
| 923 | /* |
| 924 | * allocate on-disk/in-memory inode for symbolic link: |
| 925 | * (iAlloc() returns new, locked inode) |
| 926 | */ |
| 927 | ip = ialloc(dip, S_IFLNK | 0777); |
Akinobu Mita | 087387f | 2006-09-14 09:22:38 -0500 | [diff] [blame] | 928 | if (IS_ERR(ip)) { |
| 929 | rc = PTR_ERR(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | goto out2; |
| 931 | } |
| 932 | |
| 933 | tid = txBegin(dip->i_sb, 0); |
| 934 | |
Dave Kleikamp | 82d5b9a | 2007-01-09 14:14:48 -0600 | [diff] [blame] | 935 | mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT); |
| 936 | mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 938 | rc = jfs_init_security(tid, ip, dip, &dentry->d_name); |
Dave Kleikamp | 1d15b10f | 2005-09-01 09:05:39 -0500 | [diff] [blame] | 939 | if (rc) |
| 940 | goto out3; |
| 941 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | tblk = tid_to_tblock(tid); |
| 943 | tblk->xflag |= COMMIT_CREATE; |
| 944 | tblk->ino = ip->i_ino; |
| 945 | tblk->u.ixpxd = JFS_IP(ip)->ixpxd; |
| 946 | |
| 947 | /* fix symlink access permission |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 948 | * (dir_create() ANDs in the u.u_cmask, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | * but symlinks really need to be 777 access) |
| 950 | */ |
| 951 | ip->i_mode |= 0777; |
| 952 | |
| 953 | /* |
| 954 | * write symbolic link target path name |
| 955 | */ |
| 956 | xtInitRoot(tid, ip); |
| 957 | |
| 958 | /* |
| 959 | * write source path name inline in on-disk inode (fast symbolic link) |
| 960 | */ |
| 961 | |
| 962 | if (ssize <= IDATASIZE) { |
Dmitry Monakhov | c7f2e1f | 2010-04-16 08:05:50 -0500 | [diff] [blame] | 963 | ip->i_op = &jfs_fast_symlink_inode_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | |
Al Viro | ad476fe | 2015-05-02 10:41:20 -0400 | [diff] [blame] | 965 | ip->i_link = JFS_IP(ip)->i_inline; |
| 966 | memcpy(ip->i_link, name, ssize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | ip->i_size = ssize - 1; |
| 968 | |
| 969 | /* |
| 970 | * if symlink is > 128 bytes, we don't have the space to |
| 971 | * store inline extended attributes |
| 972 | */ |
| 973 | if (ssize > sizeof (JFS_IP(ip)->i_inline)) |
| 974 | JFS_IP(ip)->mode2 &= ~INLINEEA; |
| 975 | |
| 976 | jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ", |
| 977 | ssize, name); |
| 978 | } |
| 979 | /* |
| 980 | * write source path name in a single extent |
| 981 | */ |
| 982 | else { |
| 983 | jfs_info("jfs_symlink: allocate extent ip:0x%p", ip); |
| 984 | |
Dmitry Monakhov | c7f2e1f | 2010-04-16 08:05:50 -0500 | [diff] [blame] | 985 | ip->i_op = &jfs_symlink_inode_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | ip->i_mapping->a_ops = &jfs_aops; |
| 987 | |
| 988 | /* |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 989 | * even though the data of symlink object (source |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | * path name) is treated as non-journaled user data, |
| 991 | * it is read/written thru buffer cache for performance. |
| 992 | */ |
| 993 | sb = ip->i_sb; |
| 994 | bmask = JFS_SBI(sb)->bsize - 1; |
| 995 | xsize = (ssize + bmask) & ~bmask; |
| 996 | xaddr = 0; |
| 997 | xlen = xsize >> JFS_SBI(sb)->l2bsize; |
| 998 | if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) { |
| 999 | txAbort(tid, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | goto out3; |
| 1001 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | ip->i_size = ssize - 1; |
| 1003 | while (ssize) { |
| 1004 | /* This is kind of silly since PATH_MAX == 4K */ |
| 1005 | int copy_size = min(ssize, PSIZE); |
| 1006 | |
| 1007 | mp = get_metapage(ip, xaddr, PSIZE, 1); |
| 1008 | |
| 1009 | if (mp == NULL) { |
| 1010 | xtTruncate(tid, ip, 0, COMMIT_PWMAP); |
| 1011 | rc = -EIO; |
| 1012 | txAbort(tid, 0); |
| 1013 | goto out3; |
| 1014 | } |
| 1015 | memcpy(mp->data, name, copy_size); |
| 1016 | flush_metapage(mp); |
| 1017 | ssize -= copy_size; |
| 1018 | name += copy_size; |
| 1019 | xaddr += JFS_SBI(sb)->nbperpage; |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | /* |
| 1024 | * create entry for symbolic link in parent directory |
| 1025 | */ |
| 1026 | rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE); |
| 1027 | if (rc == 0) { |
| 1028 | ino = ip->i_ino; |
| 1029 | rc = dtInsert(tid, dip, &dname, &ino, &btstack); |
| 1030 | } |
| 1031 | if (rc) { |
| 1032 | if (xlen) |
| 1033 | xtTruncate(tid, ip, 0, COMMIT_PWMAP); |
| 1034 | txAbort(tid, 0); |
| 1035 | /* discard new inode */ |
| 1036 | goto out3; |
| 1037 | } |
| 1038 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | mark_inode_dirty(ip); |
| 1040 | |
Dave Kleikamp | 988a649 | 2005-10-31 16:53:04 -0600 | [diff] [blame] | 1041 | dip->i_ctime = dip->i_mtime = CURRENT_TIME; |
| 1042 | mark_inode_dirty(dip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | /* |
| 1044 | * commit update of parent directory and link object |
| 1045 | */ |
| 1046 | |
| 1047 | iplist[0] = dip; |
| 1048 | iplist[1] = ip; |
| 1049 | rc = txCommit(tid, 2, &iplist[0], 0); |
| 1050 | |
| 1051 | out3: |
| 1052 | txEnd(tid); |
Ingo Molnar | 1de8744 | 2006-01-24 15:22:50 -0600 | [diff] [blame] | 1053 | mutex_unlock(&JFS_IP(ip)->commit_mutex); |
Evgeniy Dushistov | 48ce8b0 | 2006-06-05 08:21:03 -0500 | [diff] [blame] | 1054 | mutex_unlock(&JFS_IP(dip)->commit_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | if (rc) { |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 1056 | free_ea_wmap(ip); |
Miklos Szeredi | 6d6b77f | 2011-10-28 14:13:28 +0200 | [diff] [blame] | 1057 | clear_nlink(ip); |
Dave Kleikamp | 1f3403f | 2008-12-30 22:08:37 -0600 | [diff] [blame] | 1058 | unlock_new_inode(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | iput(ip); |
Dave Kleikamp | 1f3403f | 2008-12-30 22:08:37 -0600 | [diff] [blame] | 1060 | } else { |
Dave Kleikamp | 1f3403f | 2008-12-30 22:08:37 -0600 | [diff] [blame] | 1061 | unlock_new_inode(ip); |
Al Viro | 8fc37ec | 2012-07-19 09:18:15 +0400 | [diff] [blame] | 1062 | d_instantiate(dentry, ip); |
Dave Kleikamp | 1f3403f | 2008-12-30 22:08:37 -0600 | [diff] [blame] | 1063 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | |
| 1065 | out2: |
| 1066 | free_UCSname(&dname); |
| 1067 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | out1: |
| 1069 | jfs_info("jfs_symlink: rc:%d", rc); |
| 1070 | return rc; |
| 1071 | } |
| 1072 | |
| 1073 | |
| 1074 | /* |
Dave Kleikamp | f720e3b | 2007-06-06 15:28:35 -0500 | [diff] [blame] | 1075 | * NAME: jfs_rename |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | * |
Dave Kleikamp | f720e3b | 2007-06-06 15:28:35 -0500 | [diff] [blame] | 1077 | * FUNCTION: rename a file or directory |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | */ |
| 1079 | static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 1080 | struct inode *new_dir, struct dentry *new_dentry) |
| 1081 | { |
| 1082 | struct btstack btstack; |
| 1083 | ino_t ino; |
| 1084 | struct component_name new_dname; |
| 1085 | struct inode *new_ip; |
| 1086 | struct component_name old_dname; |
| 1087 | struct inode *old_ip; |
| 1088 | int rc; |
| 1089 | tid_t tid; |
| 1090 | struct tlock *tlck; |
| 1091 | struct dt_lock *dtlck; |
| 1092 | struct lv *lv; |
| 1093 | int ipcount; |
| 1094 | struct inode *iplist[4]; |
| 1095 | struct tblock *tblk; |
| 1096 | s64 new_size = 0; |
| 1097 | int commit_flag; |
| 1098 | |
| 1099 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1100 | jfs_info("jfs_rename: %pd %pd", old_dentry, new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | |
Dave Kleikamp | acc84b0 | 2015-07-15 13:53:19 -0500 | [diff] [blame] | 1102 | rc = dquot_initialize(old_dir); |
| 1103 | if (rc) |
| 1104 | goto out1; |
| 1105 | rc = dquot_initialize(new_dir); |
| 1106 | if (rc) |
| 1107 | goto out1; |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 1108 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1109 | old_ip = d_inode(old_dentry); |
| 1110 | new_ip = d_inode(new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | |
| 1112 | if ((rc = get_UCSname(&old_dname, old_dentry))) |
| 1113 | goto out1; |
| 1114 | |
| 1115 | if ((rc = get_UCSname(&new_dname, new_dentry))) |
| 1116 | goto out2; |
| 1117 | |
| 1118 | /* |
| 1119 | * Make sure source inode number is what we think it is |
| 1120 | */ |
| 1121 | rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP); |
| 1122 | if (rc || (ino != old_ip->i_ino)) { |
| 1123 | rc = -ENOENT; |
| 1124 | goto out3; |
| 1125 | } |
| 1126 | |
| 1127 | /* |
| 1128 | * Make sure dest inode number (if any) is what we think it is |
| 1129 | */ |
| 1130 | rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP); |
Joe Perches | 09aaa74 | 2007-11-13 22:16:08 -0600 | [diff] [blame] | 1131 | if (!rc) { |
Dave Kleikamp | da8a41d | 2007-11-13 22:25:41 -0600 | [diff] [blame] | 1132 | if ((!new_ip) || (ino != new_ip->i_ino)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | rc = -ESTALE; |
| 1134 | goto out3; |
| 1135 | } |
| 1136 | } else if (rc != -ENOENT) |
| 1137 | goto out3; |
| 1138 | else if (new_ip) { |
| 1139 | /* no entry exists, but one was expected */ |
| 1140 | rc = -ESTALE; |
| 1141 | goto out3; |
| 1142 | } |
| 1143 | |
| 1144 | if (S_ISDIR(old_ip->i_mode)) { |
| 1145 | if (new_ip) { |
| 1146 | if (!dtEmpty(new_ip)) { |
| 1147 | rc = -ENOTEMPTY; |
| 1148 | goto out3; |
| 1149 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | } |
| 1151 | } else if (new_ip) { |
Dave Kleikamp | 82d5b9a | 2007-01-09 14:14:48 -0600 | [diff] [blame] | 1152 | IWRITE_LOCK(new_ip, RDWRLOCK_NORMAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | /* Init inode for quota operations. */ |
Dave Kleikamp | acc84b0 | 2015-07-15 13:53:19 -0500 | [diff] [blame] | 1154 | rc = dquot_initialize(new_ip); |
| 1155 | if (rc) |
| 1156 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | /* |
| 1160 | * The real work starts here |
| 1161 | */ |
| 1162 | tid = txBegin(new_dir->i_sb, 0); |
| 1163 | |
Dave Kleikamp | 82d5b9a | 2007-01-09 14:14:48 -0600 | [diff] [blame] | 1164 | /* |
| 1165 | * How do we know the locking is safe from deadlocks? |
| 1166 | * The vfs does the hard part for us. Any time we are taking nested |
| 1167 | * commit_mutexes, the vfs already has i_mutex held on the parent. |
| 1168 | * Here, the vfs has already taken i_mutex on both old_dir and new_dir. |
| 1169 | */ |
| 1170 | mutex_lock_nested(&JFS_IP(new_dir)->commit_mutex, COMMIT_MUTEX_PARENT); |
| 1171 | mutex_lock_nested(&JFS_IP(old_ip)->commit_mutex, COMMIT_MUTEX_CHILD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | if (old_dir != new_dir) |
Dave Kleikamp | 82d5b9a | 2007-01-09 14:14:48 -0600 | [diff] [blame] | 1173 | mutex_lock_nested(&JFS_IP(old_dir)->commit_mutex, |
| 1174 | COMMIT_MUTEX_SECOND_PARENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | |
| 1176 | if (new_ip) { |
Dave Kleikamp | 82d5b9a | 2007-01-09 14:14:48 -0600 | [diff] [blame] | 1177 | mutex_lock_nested(&JFS_IP(new_ip)->commit_mutex, |
| 1178 | COMMIT_MUTEX_VICTIM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | /* |
| 1180 | * Change existing directory entry to new inode number |
| 1181 | */ |
| 1182 | ino = new_ip->i_ino; |
| 1183 | rc = dtModify(tid, new_dir, &new_dname, &ino, |
| 1184 | old_ip->i_ino, JFS_RENAME); |
| 1185 | if (rc) |
Dave Kleikamp | 2645695 | 2015-07-15 12:52:47 -0500 | [diff] [blame] | 1186 | goto out_tx; |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 1187 | drop_nlink(new_ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | if (S_ISDIR(new_ip->i_mode)) { |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 1189 | drop_nlink(new_ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | if (new_ip->i_nlink) { |
Evgeniy Dushistov | 48ce8b0 | 2006-06-05 08:21:03 -0500 | [diff] [blame] | 1191 | mutex_unlock(&JFS_IP(new_ip)->commit_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | if (old_dir != new_dir) |
Ingo Molnar | 1de8744 | 2006-01-24 15:22:50 -0600 | [diff] [blame] | 1193 | mutex_unlock(&JFS_IP(old_dir)->commit_mutex); |
Evgeniy Dushistov | 48ce8b0 | 2006-06-05 08:21:03 -0500 | [diff] [blame] | 1194 | mutex_unlock(&JFS_IP(old_ip)->commit_mutex); |
| 1195 | mutex_unlock(&JFS_IP(new_dir)->commit_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | if (!S_ISDIR(old_ip->i_mode) && new_ip) |
| 1197 | IWRITE_UNLOCK(new_ip); |
| 1198 | jfs_error(new_ip->i_sb, |
Joe Perches | eb8630d | 2013-06-04 16:39:15 -0700 | [diff] [blame] | 1199 | "new_ip->i_nlink != 0\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | return -EIO; |
| 1201 | } |
| 1202 | tblk = tid_to_tblock(tid); |
| 1203 | tblk->xflag |= COMMIT_DELETE; |
| 1204 | tblk->u.ip = new_ip; |
| 1205 | } else if (new_ip->i_nlink == 0) { |
| 1206 | assert(!test_cflag(COMMIT_Nolink, new_ip)); |
| 1207 | /* free block resources */ |
| 1208 | if ((new_size = commitZeroLink(tid, new_ip)) < 0) { |
| 1209 | txAbort(tid, 1); /* Marks FS Dirty */ |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 1210 | rc = new_size; |
Dave Kleikamp | 2645695 | 2015-07-15 12:52:47 -0500 | [diff] [blame] | 1211 | goto out_tx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | } |
| 1213 | tblk = tid_to_tblock(tid); |
| 1214 | tblk->xflag |= COMMIT_DELETE; |
| 1215 | tblk->u.ip = new_ip; |
| 1216 | } else { |
| 1217 | new_ip->i_ctime = CURRENT_TIME; |
| 1218 | mark_inode_dirty(new_ip); |
| 1219 | } |
| 1220 | } else { |
| 1221 | /* |
| 1222 | * Add new directory entry |
| 1223 | */ |
| 1224 | rc = dtSearch(new_dir, &new_dname, &ino, &btstack, |
| 1225 | JFS_CREATE); |
| 1226 | if (rc) { |
| 1227 | jfs_err("jfs_rename didn't expect dtSearch to fail " |
| 1228 | "w/rc = %d", rc); |
Dave Kleikamp | 2645695 | 2015-07-15 12:52:47 -0500 | [diff] [blame] | 1229 | goto out_tx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | ino = old_ip->i_ino; |
| 1233 | rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack); |
| 1234 | if (rc) { |
| 1235 | if (rc == -EIO) |
| 1236 | jfs_err("jfs_rename: dtInsert returned -EIO"); |
Dave Kleikamp | 2645695 | 2015-07-15 12:52:47 -0500 | [diff] [blame] | 1237 | goto out_tx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | } |
| 1239 | if (S_ISDIR(old_ip->i_mode)) |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 1240 | inc_nlink(new_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | } |
| 1242 | /* |
| 1243 | * Remove old directory entry |
| 1244 | */ |
| 1245 | |
| 1246 | ino = old_ip->i_ino; |
| 1247 | rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE); |
| 1248 | if (rc) { |
| 1249 | jfs_err("jfs_rename did not expect dtDelete to return rc = %d", |
| 1250 | rc); |
| 1251 | txAbort(tid, 1); /* Marks Filesystem dirty */ |
Dave Kleikamp | 2645695 | 2015-07-15 12:52:47 -0500 | [diff] [blame] | 1252 | goto out_tx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | } |
| 1254 | if (S_ISDIR(old_ip->i_mode)) { |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 1255 | drop_nlink(old_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | if (old_dir != new_dir) { |
| 1257 | /* |
| 1258 | * Change inode number of parent for moved directory |
| 1259 | */ |
| 1260 | |
| 1261 | JFS_IP(old_ip)->i_dtroot.header.idotdot = |
| 1262 | cpu_to_le32(new_dir->i_ino); |
| 1263 | |
| 1264 | /* Linelock header of dtree */ |
| 1265 | tlck = txLock(tid, old_ip, |
| 1266 | (struct metapage *) &JFS_IP(old_ip)->bxflag, |
| 1267 | tlckDTREE | tlckBTROOT | tlckRELINK); |
| 1268 | dtlck = (struct dt_lock *) & tlck->lock; |
| 1269 | ASSERT(dtlck->index == 0); |
| 1270 | lv = & dtlck->lv[0]; |
| 1271 | lv->offset = 0; |
| 1272 | lv->length = 1; |
| 1273 | dtlck->index++; |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | /* |
| 1278 | * Update ctime on changed/moved inodes & mark dirty |
| 1279 | */ |
| 1280 | old_ip->i_ctime = CURRENT_TIME; |
| 1281 | mark_inode_dirty(old_ip); |
| 1282 | |
| 1283 | new_dir->i_ctime = new_dir->i_mtime = current_fs_time(new_dir->i_sb); |
| 1284 | mark_inode_dirty(new_dir); |
| 1285 | |
| 1286 | /* Build list of inodes modified by this transaction */ |
| 1287 | ipcount = 0; |
| 1288 | iplist[ipcount++] = old_ip; |
| 1289 | if (new_ip) |
| 1290 | iplist[ipcount++] = new_ip; |
| 1291 | iplist[ipcount++] = old_dir; |
| 1292 | |
| 1293 | if (old_dir != new_dir) { |
| 1294 | iplist[ipcount++] = new_dir; |
| 1295 | old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME; |
| 1296 | mark_inode_dirty(old_dir); |
| 1297 | } |
| 1298 | |
| 1299 | /* |
| 1300 | * Incomplete truncate of file data can |
| 1301 | * result in timing problems unless we synchronously commit the |
| 1302 | * transaction. |
| 1303 | */ |
| 1304 | if (new_size) |
| 1305 | commit_flag = COMMIT_SYNC; |
| 1306 | else |
| 1307 | commit_flag = 0; |
| 1308 | |
| 1309 | rc = txCommit(tid, ipcount, iplist, commit_flag); |
| 1310 | |
Dave Kleikamp | 2645695 | 2015-07-15 12:52:47 -0500 | [diff] [blame] | 1311 | out_tx: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | txEnd(tid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | if (new_ip) |
Ingo Molnar | 1de8744 | 2006-01-24 15:22:50 -0600 | [diff] [blame] | 1314 | mutex_unlock(&JFS_IP(new_ip)->commit_mutex); |
Evgeniy Dushistov | 48ce8b0 | 2006-06-05 08:21:03 -0500 | [diff] [blame] | 1315 | if (old_dir != new_dir) |
| 1316 | mutex_unlock(&JFS_IP(old_dir)->commit_mutex); |
| 1317 | mutex_unlock(&JFS_IP(old_ip)->commit_mutex); |
| 1318 | mutex_unlock(&JFS_IP(new_dir)->commit_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | |
| 1320 | while (new_size && (rc == 0)) { |
| 1321 | tid = txBegin(new_ip->i_sb, 0); |
Ingo Molnar | 1de8744 | 2006-01-24 15:22:50 -0600 | [diff] [blame] | 1322 | mutex_lock(&JFS_IP(new_ip)->commit_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | new_size = xtTruncate_pmap(tid, new_ip, new_size); |
| 1324 | if (new_size < 0) { |
| 1325 | txAbort(tid, 1); |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 1326 | rc = new_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | } else |
| 1328 | rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC); |
| 1329 | txEnd(tid); |
Ingo Molnar | 1de8744 | 2006-01-24 15:22:50 -0600 | [diff] [blame] | 1330 | mutex_unlock(&JFS_IP(new_ip)->commit_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | } |
| 1332 | if (new_ip && (new_ip->i_nlink == 0)) |
| 1333 | set_cflag(COMMIT_Nolink, new_ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | /* |
| 1335 | * Truncating the directory index table is not guaranteed. It |
| 1336 | * may need to be done iteratively |
| 1337 | */ |
| 1338 | if (test_cflag(COMMIT_Stale, old_dir)) { |
| 1339 | if (old_dir->i_size > 1) |
| 1340 | jfs_truncate_nolock(old_dir, 0); |
| 1341 | |
| 1342 | clear_cflag(COMMIT_Stale, old_dir); |
| 1343 | } |
Dave Kleikamp | acc84b0 | 2015-07-15 13:53:19 -0500 | [diff] [blame] | 1344 | out_unlock: |
Dave Kleikamp | 2645695 | 2015-07-15 12:52:47 -0500 | [diff] [blame] | 1345 | if (new_ip && !S_ISDIR(new_ip->i_mode)) |
| 1346 | IWRITE_UNLOCK(new_ip); |
| 1347 | out3: |
| 1348 | free_UCSname(&new_dname); |
| 1349 | out2: |
| 1350 | free_UCSname(&old_dname); |
| 1351 | out1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | jfs_info("jfs_rename: returning %d", rc); |
| 1353 | return rc; |
| 1354 | } |
| 1355 | |
| 1356 | |
| 1357 | /* |
Dave Kleikamp | f720e3b | 2007-06-06 15:28:35 -0500 | [diff] [blame] | 1358 | * NAME: jfs_mknod |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | * |
Dave Kleikamp | f720e3b | 2007-06-06 15:28:35 -0500 | [diff] [blame] | 1360 | * FUNCTION: Create a special file (device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | */ |
| 1362 | static int jfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 1363 | umode_t mode, dev_t rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | { |
| 1365 | struct jfs_inode_info *jfs_ip; |
| 1366 | struct btstack btstack; |
| 1367 | struct component_name dname; |
| 1368 | ino_t ino; |
| 1369 | struct inode *ip; |
| 1370 | struct inode *iplist[2]; |
| 1371 | int rc; |
| 1372 | tid_t tid; |
| 1373 | struct tblock *tblk; |
| 1374 | |
| 1375 | if (!new_valid_dev(rdev)) |
| 1376 | return -EINVAL; |
| 1377 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1378 | jfs_info("jfs_mknod: %pd", dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | |
Dave Kleikamp | acc84b0 | 2015-07-15 13:53:19 -0500 | [diff] [blame] | 1380 | rc = dquot_initialize(dir); |
| 1381 | if (rc) |
| 1382 | goto out; |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 1383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | if ((rc = get_UCSname(&dname, dentry))) |
| 1385 | goto out; |
| 1386 | |
| 1387 | ip = ialloc(dir, mode); |
Akinobu Mita | 087387f | 2006-09-14 09:22:38 -0500 | [diff] [blame] | 1388 | if (IS_ERR(ip)) { |
| 1389 | rc = PTR_ERR(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | goto out1; |
| 1391 | } |
| 1392 | jfs_ip = JFS_IP(ip); |
| 1393 | |
| 1394 | tid = txBegin(dir->i_sb, 0); |
| 1395 | |
Dave Kleikamp | 82d5b9a | 2007-01-09 14:14:48 -0600 | [diff] [blame] | 1396 | mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT); |
| 1397 | mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 1399 | rc = jfs_init_acl(tid, ip, dir); |
| 1400 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | goto out3; |
| 1402 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 1403 | rc = jfs_init_security(tid, ip, dir, &dentry->d_name); |
Dave Kleikamp | 1d15b10f | 2005-09-01 09:05:39 -0500 | [diff] [blame] | 1404 | if (rc) { |
| 1405 | txAbort(tid, 0); |
| 1406 | goto out3; |
| 1407 | } |
| 1408 | |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 1409 | if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) { |
| 1410 | txAbort(tid, 0); |
| 1411 | goto out3; |
| 1412 | } |
| 1413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | tblk = tid_to_tblock(tid); |
| 1415 | tblk->xflag |= COMMIT_CREATE; |
| 1416 | tblk->ino = ip->i_ino; |
| 1417 | tblk->u.ixpxd = JFS_IP(ip)->ixpxd; |
| 1418 | |
| 1419 | ino = ip->i_ino; |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 1420 | if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) { |
| 1421 | txAbort(tid, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | goto out3; |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 1423 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | |
| 1425 | ip->i_op = &jfs_file_inode_operations; |
| 1426 | jfs_ip->dev = new_encode_dev(rdev); |
| 1427 | init_special_inode(ip, ip->i_mode, rdev); |
| 1428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | mark_inode_dirty(ip); |
| 1430 | |
| 1431 | dir->i_ctime = dir->i_mtime = CURRENT_TIME; |
| 1432 | |
| 1433 | mark_inode_dirty(dir); |
| 1434 | |
| 1435 | iplist[0] = dir; |
| 1436 | iplist[1] = ip; |
| 1437 | rc = txCommit(tid, 2, iplist, 0); |
| 1438 | |
| 1439 | out3: |
| 1440 | txEnd(tid); |
Ingo Molnar | 1de8744 | 2006-01-24 15:22:50 -0600 | [diff] [blame] | 1441 | mutex_unlock(&JFS_IP(ip)->commit_mutex); |
| 1442 | mutex_unlock(&JFS_IP(dir)->commit_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | if (rc) { |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 1444 | free_ea_wmap(ip); |
Miklos Szeredi | 6d6b77f | 2011-10-28 14:13:28 +0200 | [diff] [blame] | 1445 | clear_nlink(ip); |
Dave Kleikamp | 1f3403f | 2008-12-30 22:08:37 -0600 | [diff] [blame] | 1446 | unlock_new_inode(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | iput(ip); |
Dave Kleikamp | 1f3403f | 2008-12-30 22:08:37 -0600 | [diff] [blame] | 1448 | } else { |
Dave Kleikamp | 1f3403f | 2008-12-30 22:08:37 -0600 | [diff] [blame] | 1449 | unlock_new_inode(ip); |
Al Viro | 8fc37ec | 2012-07-19 09:18:15 +0400 | [diff] [blame] | 1450 | d_instantiate(dentry, ip); |
Dave Kleikamp | 1f3403f | 2008-12-30 22:08:37 -0600 | [diff] [blame] | 1451 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | |
| 1453 | out1: |
| 1454 | free_UCSname(&dname); |
| 1455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | out: |
| 1457 | jfs_info("jfs_mknod: returning %d", rc); |
| 1458 | return rc; |
| 1459 | } |
| 1460 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 1461 | static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | { |
| 1463 | struct btstack btstack; |
| 1464 | ino_t inum; |
| 1465 | struct inode *ip; |
| 1466 | struct component_name key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | int rc; |
| 1468 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1469 | jfs_info("jfs_lookup: name = %pd", dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | |
Al Viro | 79ac5a4 | 2011-07-17 10:17:46 -0400 | [diff] [blame] | 1471 | if ((rc = get_UCSname(&key, dentry))) |
| 1472 | return ERR_PTR(rc); |
| 1473 | rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP); |
| 1474 | free_UCSname(&key); |
| 1475 | if (rc == -ENOENT) { |
| 1476 | ip = NULL; |
| 1477 | } else if (rc) { |
| 1478 | jfs_err("jfs_lookup: dtSearch returned %d", rc); |
| 1479 | ip = ERR_PTR(rc); |
| 1480 | } else { |
| 1481 | ip = jfs_iget(dip->i_sb, inum); |
| 1482 | if (IS_ERR(ip)) |
| 1483 | jfs_err("jfs_lookup: iget failed on inum %d", (uint)inum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | } |
| 1485 | |
Al Viro | 94b77bd | 2010-12-18 10:59:31 -0500 | [diff] [blame] | 1486 | return d_splice_alias(ip, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | } |
| 1488 | |
Christoph Hellwig | d425de7 | 2007-10-21 16:42:09 -0700 | [diff] [blame] | 1489 | static struct inode *jfs_nfs_get_inode(struct super_block *sb, |
| 1490 | u64 ino, u32 generation) |
Christoph Hellwig | 5ca2960 | 2007-07-17 04:04:29 -0700 | [diff] [blame] | 1491 | { |
Christoph Hellwig | 5ca2960 | 2007-07-17 04:04:29 -0700 | [diff] [blame] | 1492 | struct inode *inode; |
Christoph Hellwig | 5ca2960 | 2007-07-17 04:04:29 -0700 | [diff] [blame] | 1493 | |
| 1494 | if (ino == 0) |
| 1495 | return ERR_PTR(-ESTALE); |
David Howells | eab1df7 | 2008-02-07 00:15:43 -0800 | [diff] [blame] | 1496 | inode = jfs_iget(sb, ino); |
| 1497 | if (IS_ERR(inode)) |
| 1498 | return ERR_CAST(inode); |
Christoph Hellwig | 5ca2960 | 2007-07-17 04:04:29 -0700 | [diff] [blame] | 1499 | |
David Howells | eab1df7 | 2008-02-07 00:15:43 -0800 | [diff] [blame] | 1500 | if (generation && inode->i_generation != generation) { |
Christoph Hellwig | d425de7 | 2007-10-21 16:42:09 -0700 | [diff] [blame] | 1501 | iput(inode); |
| 1502 | return ERR_PTR(-ESTALE); |
Christoph Hellwig | 5ca2960 | 2007-07-17 04:04:29 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
Christoph Hellwig | d425de7 | 2007-10-21 16:42:09 -0700 | [diff] [blame] | 1505 | return inode; |
| 1506 | } |
Christoph Hellwig | 5ca2960 | 2007-07-17 04:04:29 -0700 | [diff] [blame] | 1507 | |
Christoph Hellwig | d425de7 | 2007-10-21 16:42:09 -0700 | [diff] [blame] | 1508 | struct dentry *jfs_fh_to_dentry(struct super_block *sb, struct fid *fid, |
| 1509 | int fh_len, int fh_type) |
| 1510 | { |
| 1511 | return generic_fh_to_dentry(sb, fid, fh_len, fh_type, |
| 1512 | jfs_nfs_get_inode); |
| 1513 | } |
| 1514 | |
| 1515 | struct dentry *jfs_fh_to_parent(struct super_block *sb, struct fid *fid, |
| 1516 | int fh_len, int fh_type) |
| 1517 | { |
| 1518 | return generic_fh_to_parent(sb, fid, fh_len, fh_type, |
| 1519 | jfs_nfs_get_inode); |
Christoph Hellwig | 5ca2960 | 2007-07-17 04:04:29 -0700 | [diff] [blame] | 1520 | } |
| 1521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | struct dentry *jfs_get_parent(struct dentry *dentry) |
| 1523 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | unsigned long parent_ino; |
| 1525 | |
| 1526 | parent_ino = |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1527 | le32_to_cpu(JFS_IP(d_inode(dentry))->i_dtroot.header.idotdot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1529 | return d_obtain_alias(jfs_iget(d_inode(dentry)->i_sb, parent_ino)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | } |
| 1531 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 1532 | const struct inode_operations jfs_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | .create = jfs_create, |
| 1534 | .lookup = jfs_lookup, |
| 1535 | .link = jfs_link, |
| 1536 | .unlink = jfs_unlink, |
| 1537 | .symlink = jfs_symlink, |
| 1538 | .mkdir = jfs_mkdir, |
| 1539 | .rmdir = jfs_rmdir, |
| 1540 | .mknod = jfs_mknod, |
| 1541 | .rename = jfs_rename, |
| 1542 | .setxattr = jfs_setxattr, |
| 1543 | .getxattr = jfs_getxattr, |
| 1544 | .listxattr = jfs_listxattr, |
| 1545 | .removexattr = jfs_removexattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | .setattr = jfs_setattr, |
Christoph Hellwig | 759bfee | 2010-03-03 09:05:02 -0500 | [diff] [blame] | 1547 | #ifdef CONFIG_JFS_POSIX_ACL |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 1548 | .get_acl = jfs_get_acl, |
Christoph Hellwig | 2cc6a5a | 2013-12-20 05:16:51 -0800 | [diff] [blame] | 1549 | .set_acl = jfs_set_acl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | #endif |
| 1551 | }; |
| 1552 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1553 | const struct file_operations jfs_dir_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | .read = generic_read_dir, |
Al Viro | 070a0eb | 2013-05-17 17:00:34 -0400 | [diff] [blame] | 1555 | .iterate = jfs_readdir, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | .fsync = jfs_fsync, |
Andi Kleen | baab81f | 2008-01-27 16:58:51 -0600 | [diff] [blame] | 1557 | .unlocked_ioctl = jfs_ioctl, |
Andi Kleen | ef1fc2f | 2008-01-27 17:02:02 -0600 | [diff] [blame] | 1558 | #ifdef CONFIG_COMPAT |
| 1559 | .compat_ioctl = jfs_compat_ioctl, |
| 1560 | #endif |
Christoph Hellwig | 3222a3e | 2008-09-03 21:53:01 +0200 | [diff] [blame] | 1561 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | }; |
| 1563 | |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 1564 | static int jfs_ci_hash(const struct dentry *dir, struct qstr *this) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | { |
| 1566 | unsigned long hash; |
| 1567 | int i; |
| 1568 | |
| 1569 | hash = init_name_hash(); |
| 1570 | for (i=0; i < this->len; i++) |
| 1571 | hash = partial_name_hash(tolower(this->name[i]), hash); |
| 1572 | this->hash = end_name_hash(hash); |
| 1573 | |
| 1574 | return 0; |
| 1575 | } |
| 1576 | |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 1577 | static int jfs_ci_compare(const struct dentry *parent, const struct dentry *dentry, |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 1578 | unsigned int len, const char *str, const struct qstr *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | { |
| 1580 | int i, result = 1; |
| 1581 | |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 1582 | if (len != name->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | goto out; |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 1584 | for (i=0; i < len; i++) { |
| 1585 | if (tolower(str[i]) != tolower(name->name[i])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | goto out; |
| 1587 | } |
| 1588 | result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | out: |
| 1590 | return result; |
| 1591 | } |
| 1592 | |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 1593 | static int jfs_ci_revalidate(struct dentry *dentry, unsigned int flags) |
Nick Piggin | 2bc334d | 2011-01-07 17:49:25 +1100 | [diff] [blame] | 1594 | { |
| 1595 | /* |
| 1596 | * This is not negative dentry. Always valid. |
| 1597 | * |
| 1598 | * Note, rename() to existing directory entry will have ->d_inode, |
| 1599 | * and will use existing name which isn't specified name by user. |
| 1600 | * |
| 1601 | * We may be able to drop this positive dentry here. But dropping |
| 1602 | * positive dentry isn't good idea. So it's unsupported like |
| 1603 | * rename("filename", "FILENAME") for now. |
| 1604 | */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1605 | if (d_really_is_positive(dentry)) |
Nick Piggin | 2bc334d | 2011-01-07 17:49:25 +1100 | [diff] [blame] | 1606 | return 1; |
| 1607 | |
| 1608 | /* |
| 1609 | * This may be nfsd (or something), anyway, we can't see the |
| 1610 | * intent of this. So, since this can be for creation, drop it. |
| 1611 | */ |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 1612 | if (!flags) |
Nick Piggin | 2bc334d | 2011-01-07 17:49:25 +1100 | [diff] [blame] | 1613 | return 0; |
| 1614 | |
| 1615 | /* |
| 1616 | * Drop the negative dentry, in order to make sure to use the |
| 1617 | * case sensitive name which is specified by user if this is |
| 1618 | * for creation. |
| 1619 | */ |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 1620 | if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) |
Al Viro | 407938e | 2011-06-25 21:37:18 -0400 | [diff] [blame] | 1621 | return 0; |
Nick Piggin | 2bc334d | 2011-01-07 17:49:25 +1100 | [diff] [blame] | 1622 | return 1; |
| 1623 | } |
| 1624 | |
Al Viro | ad28b4e | 2009-02-20 06:00:49 +0000 | [diff] [blame] | 1625 | const struct dentry_operations jfs_ci_dentry_operations = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | { |
| 1627 | .d_hash = jfs_ci_hash, |
| 1628 | .d_compare = jfs_ci_compare, |
Nick Piggin | 2bc334d | 2011-01-07 17:49:25 +1100 | [diff] [blame] | 1629 | .d_revalidate = jfs_ci_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | }; |