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