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