1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/fs/ext2/namei.c 4 * 5 * Rewrite to pagecache. Almost all code had been changed, so blame me 6 * if the things go wrong. Please, send bug reports to 7 * viro@parcelfarce.linux.theplanet.co.uk 8 * 9 * Stuff here is basically a glue between the VFS and generic UNIXish 10 * filesystem that keeps everything in pagecache. All knowledge of the 11 * directory layout is in fs/ext2/dir.c - it turned out to be easily separatable 12 * and it's easier to debug that way. In principle we might want to 13 * generalize that a bit and turn it into a library. Or not. 14 * 15 * The only non-static object here is ext2_dir_inode_operations. 16 * 17 * TODO: get rid of kmap() use, add readahead. 18 * 19 * Copyright (C) 1992, 1993, 1994, 1995 20 * Remy Card (card@masi.ibp.fr) 21 * Laboratoire MASI - Institut Blaise Pascal 22 * Universite Pierre et Marie Curie (Paris VI) 23 * 24 * from 25 * 26 * linux/fs/minix/namei.c 27 * 28 * Copyright (C) 1991, 1992 Linus Torvalds 29 * 30 * Big-endian to little-endian byte-swapping/bitmaps by 31 * David S. Miller (davem@caip.rutgers.edu), 1995 32 */ 33 34 #include <linux/pagemap.h> 35 #include <linux/quotaops.h> 36 #include "ext2.h" 37 #include "xattr.h" 38 #include "acl.h" 39 40 static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode) 41 { 42 int err = ext2_add_link(dentry, inode); 43 if (!err) { 44 unlock_new_inode(inode); 45 d_instantiate(dentry, inode); 46 return 0; 47 } 48 inode_dec_link_count(inode); 49 unlock_new_inode(inode); 50 iput(inode); 51 return err; 52 } 53 54 /* 55 * Methods themselves. 56 */ 57 58 static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, unsigned int flags) 59 { 60 struct inode * inode; 61 ino_t ino; 62 63 if (dentry->d_name.len > EXT2_NAME_LEN) 64 return ERR_PTR(-ENAMETOOLONG); 65 66 ino = ext2_inode_by_name(dir, &dentry->d_name); 67 inode = NULL; 68 if (ino) { 69 inode = ext2_iget(dir->i_sb, ino); 70 if (inode == ERR_PTR(-ESTALE)) { 71 ext2_error(dir->i_sb, __func__, 72 "deleted inode referenced: %lu", 73 (unsigned long) ino); 74 return ERR_PTR(-EIO); 75 } 76 } 77 return d_splice_alias(inode, dentry); 78 } 79 80 struct dentry *ext2_get_parent(struct dentry *child) 81 { 82 struct qstr dotdot = QSTR_INIT("..", 2); 83 unsigned long ino = ext2_inode_by_name(d_inode(child), &dotdot); 84 if (!ino) 85 return ERR_PTR(-ENOENT); 86 return d_obtain_alias(ext2_iget(child->d_sb, ino)); 87 } 88 89 /* 90 * By the time this is called, we already have created 91 * the directory cache entry for the new file, but it 92 * is so far negative - it has no inode. 93 * 94 * If the create succeeds, we fill in the inode information 95 * with d_instantiate(). 96 */ 97 static int ext2_create (struct inode * dir, struct dentry * dentry, umode_t mode, bool excl) 98 { 99 struct inode *inode; 100 int err; 101 102 err = dquot_initialize(dir); 103 if (err) 104 return err; 105 106 inode = ext2_new_inode(dir, mode, &dentry->d_name); 107 if (IS_ERR(inode)) 108 return PTR_ERR(inode); 109 110 inode->i_op = &ext2_file_inode_operations; 111 if (test_opt(inode->i_sb, NOBH)) { 112 inode->i_mapping->a_ops = &ext2_nobh_aops; 113 inode->i_fop = &ext2_file_operations; 114 } else { 115 inode->i_mapping->a_ops = &ext2_aops; 116 inode->i_fop = &ext2_file_operations; 117 } 118 mark_inode_dirty(inode); 119 return ext2_add_nondir(dentry, inode); 120 } 121 122 static int ext2_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) 123 { 124 struct inode *inode = ext2_new_inode(dir, mode, NULL); 125 if (IS_ERR(inode)) 126 return PTR_ERR(inode); 127 128 inode->i_op = &ext2_file_inode_operations; 129 if (test_opt(inode->i_sb, NOBH)) { 130 inode->i_mapping->a_ops = &ext2_nobh_aops; 131 inode->i_fop = &ext2_file_operations; 132 } else { 133 inode->i_mapping->a_ops = &ext2_aops; 134 inode->i_fop = &ext2_file_operations; 135 } 136 mark_inode_dirty(inode); 137 d_tmpfile(dentry, inode); 138 unlock_new_inode(inode); 139 return 0; 140 } 141 142 static int ext2_mknod (struct inode * dir, struct dentry *dentry, umode_t mode, dev_t rdev) 143 { 144 struct inode * inode; 145 int err; 146 147 err = dquot_initialize(dir); 148 if (err) 149 return err; 150 151 inode = ext2_new_inode (dir, mode, &dentry->d_name); 152 err = PTR_ERR(inode); 153 if (!IS_ERR(inode)) { 154 init_special_inode(inode, inode->i_mode, rdev); 155 #ifdef CONFIG_EXT2_FS_XATTR 156 inode->i_op = &ext2_special_inode_operations; 157 #endif 158 mark_inode_dirty(inode); 159 err = ext2_add_nondir(dentry, inode); 160 } 161 return err; 162 } 163 164 static int ext2_symlink (struct inode * dir, struct dentry * dentry, 165 const char * symname) 166 { 167 struct super_block * sb = dir->i_sb; 168 int err = -ENAMETOOLONG; 169 unsigned l = strlen(symname)+1; 170 struct inode * inode; 171 172 if (l > sb->s_blocksize) 173 goto out; 174 175 err = dquot_initialize(dir); 176 if (err) 177 goto out; 178 179 inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO, &dentry->d_name); 180 err = PTR_ERR(inode); 181 if (IS_ERR(inode)) 182 goto out; 183 184 if (l > sizeof (EXT2_I(inode)->i_data)) { 185 /* slow symlink */ 186 inode->i_op = &ext2_symlink_inode_operations; 187 inode_nohighmem(inode); 188 if (test_opt(inode->i_sb, NOBH)) 189 inode->i_mapping->a_ops = &ext2_nobh_aops; 190 else 191 inode->i_mapping->a_ops = &ext2_aops; 192 err = page_symlink(inode, symname, l); 193 if (err) 194 goto out_fail; 195 } else { 196 /* fast symlink */ 197 inode->i_op = &ext2_fast_symlink_inode_operations; 198 inode->i_link = (char*)EXT2_I(inode)->i_data; 199 memcpy(inode->i_link, symname, l); 200 inode->i_size = l-1; 201 } 202 mark_inode_dirty(inode); 203 204 err = ext2_add_nondir(dentry, inode); 205 out: 206 return err; 207 208 out_fail: 209 inode_dec_link_count(inode); 210 unlock_new_inode(inode); 211 iput (inode); 212 goto out; 213 } 214 215 static int ext2_link (struct dentry * old_dentry, struct inode * dir, 216 struct dentry *dentry) 217 { 218 struct inode *inode = d_inode(old_dentry); 219 int err; 220 221 err = dquot_initialize(dir); 222 if (err) 223 return err; 224 225 inode->i_ctime = current_time(inode); 226 inode_inc_link_count(inode); 227 ihold(inode); 228 229 err = ext2_add_link(dentry, inode); 230 if (!err) { 231 d_instantiate(dentry, inode); 232 return 0; 233 } 234 inode_dec_link_count(inode); 235 iput(inode); 236 return err; 237 } 238 239 static int ext2_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode) 240 { 241 struct inode * inode; 242 int err; 243 244 err = dquot_initialize(dir); 245 if (err) 246 return err; 247 248 inode_inc_link_count(dir); 249 250 inode = ext2_new_inode(dir, S_IFDIR | mode, &dentry->d_name); 251 err = PTR_ERR(inode); 252 if (IS_ERR(inode)) 253 goto out_dir; 254 255 inode->i_op = &ext2_dir_inode_operations; 256 inode->i_fop = &ext2_dir_operations; 257 if (test_opt(inode->i_sb, NOBH)) 258 inode->i_mapping->a_ops = &ext2_nobh_aops; 259 else 260 inode->i_mapping->a_ops = &ext2_aops; 261 262 inode_inc_link_count(inode); 263 264 err = ext2_make_empty(inode, dir); 265 if (err) 266 goto out_fail; 267 268 err = ext2_add_link(dentry, inode); 269 if (err) 270 goto out_fail; 271 272 unlock_new_inode(inode); 273 d_instantiate(dentry, inode); 274 out: 275 return err; 276 277 out_fail: 278 inode_dec_link_count(inode); 279 inode_dec_link_count(inode); 280 unlock_new_inode(inode); 281 iput(inode); 282 out_dir: 283 inode_dec_link_count(dir); 284 goto out; 285 } 286 287 static int ext2_unlink(struct inode * dir, struct dentry *dentry) 288 { 289 struct inode * inode = d_inode(dentry); 290 struct ext2_dir_entry_2 * de; 291 struct page * page; 292 int err; 293 294 err = dquot_initialize(dir); 295 if (err) 296 goto out; 297 298 de = ext2_find_entry (dir, &dentry->d_name, &page); 299 if (!de) { 300 err = -ENOENT; 301 goto out; 302 } 303 304 err = ext2_delete_entry (de, page); 305 if (err) 306 goto out; 307 308 inode->i_ctime = dir->i_ctime; 309 inode_dec_link_count(inode); 310 err = 0; 311 out: 312 return err; 313 } 314 315 static int ext2_rmdir (struct inode * dir, struct dentry *dentry) 316 { 317 struct inode * inode = d_inode(dentry); 318 int err = -ENOTEMPTY; 319 320 if (ext2_empty_dir(inode)) { 321 err = ext2_unlink(dir, dentry); 322 if (!err) { 323 inode->i_size = 0; 324 inode_dec_link_count(inode); 325 inode_dec_link_count(dir); 326 } 327 } 328 return err; 329 } 330 331 static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry, 332 struct inode * new_dir, struct dentry * new_dentry, 333 unsigned int flags) 334 { 335 struct inode * old_inode = d_inode(old_dentry); 336 struct inode * new_inode = d_inode(new_dentry); 337 struct page * dir_page = NULL; 338 struct ext2_dir_entry_2 * dir_de = NULL; 339 struct page * old_page; 340 struct ext2_dir_entry_2 * old_de; 341 int err; 342 343 if (flags & ~RENAME_NOREPLACE) 344 return -EINVAL; 345 346 err = dquot_initialize(old_dir); 347 if (err) 348 goto out; 349 350 err = dquot_initialize(new_dir); 351 if (err) 352 goto out; 353 354 old_de = ext2_find_entry (old_dir, &old_dentry->d_name, &old_page); 355 if (!old_de) { 356 err = -ENOENT; 357 goto out; 358 } 359 360 if (S_ISDIR(old_inode->i_mode)) { 361 err = -EIO; 362 dir_de = ext2_dotdot(old_inode, &dir_page); 363 if (!dir_de) 364 goto out_old; 365 } 366 367 if (new_inode) { 368 struct page *new_page; 369 struct ext2_dir_entry_2 *new_de; 370 371 err = -ENOTEMPTY; 372 if (dir_de && !ext2_empty_dir (new_inode)) 373 goto out_dir; 374 375 err = -ENOENT; 376 new_de = ext2_find_entry (new_dir, &new_dentry->d_name, &new_page); 377 if (!new_de) 378 goto out_dir; 379 ext2_set_link(new_dir, new_de, new_page, old_inode, 1); 380 new_inode->i_ctime = current_time(new_inode); 381 if (dir_de) 382 drop_nlink(new_inode); 383 inode_dec_link_count(new_inode); 384 } else { 385 err = ext2_add_link(new_dentry, old_inode); 386 if (err) 387 goto out_dir; 388 if (dir_de) 389 inode_inc_link_count(new_dir); 390 } 391 392 /* 393 * Like most other Unix systems, set the ctime for inodes on a 394 * rename. 395 */ 396 old_inode->i_ctime = current_time(old_inode); 397 mark_inode_dirty(old_inode); 398 399 ext2_delete_entry (old_de, old_page); 400 401 if (dir_de) { 402 if (old_dir != new_dir) 403 ext2_set_link(old_inode, dir_de, dir_page, new_dir, 0); 404 else { 405 kunmap(dir_page); 406 put_page(dir_page); 407 } 408 inode_dec_link_count(old_dir); 409 } 410 return 0; 411 412 413 out_dir: 414 if (dir_de) { 415 kunmap(dir_page); 416 put_page(dir_page); 417 } 418 out_old: 419 kunmap(old_page); 420 put_page(old_page); 421 out: 422 return err; 423 } 424 425 const struct inode_operations ext2_dir_inode_operations = { 426 .create = ext2_create, 427 .lookup = ext2_lookup, 428 .link = ext2_link, 429 .unlink = ext2_unlink, 430 .symlink = ext2_symlink, 431 .mkdir = ext2_mkdir, 432 .rmdir = ext2_rmdir, 433 .mknod = ext2_mknod, 434 .rename = ext2_rename, 435 #ifdef CONFIG_EXT2_FS_XATTR 436 .listxattr = ext2_listxattr, 437 #endif 438 .setattr = ext2_setattr, 439 .get_acl = ext2_get_acl, 440 .set_acl = ext2_set_acl, 441 .tmpfile = ext2_tmpfile, 442 }; 443 444 const struct inode_operations ext2_special_inode_operations = { 445 #ifdef CONFIG_EXT2_FS_XATTR 446 .listxattr = ext2_listxattr, 447 #endif 448 .setattr = ext2_setattr, 449 .get_acl = ext2_get_acl, 450 .set_acl = ext2_set_acl, 451 }; 452