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 ext2_set_file_ops(inode); 111 mark_inode_dirty(inode); 112 return ext2_add_nondir(dentry, inode); 113 } 114 115 static int ext2_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) 116 { 117 struct inode *inode = ext2_new_inode(dir, mode, NULL); 118 if (IS_ERR(inode)) 119 return PTR_ERR(inode); 120 121 ext2_set_file_ops(inode); 122 mark_inode_dirty(inode); 123 d_tmpfile(dentry, inode); 124 unlock_new_inode(inode); 125 return 0; 126 } 127 128 static int ext2_mknod (struct inode * dir, struct dentry *dentry, umode_t mode, dev_t rdev) 129 { 130 struct inode * inode; 131 int err; 132 133 err = dquot_initialize(dir); 134 if (err) 135 return err; 136 137 inode = ext2_new_inode (dir, mode, &dentry->d_name); 138 err = PTR_ERR(inode); 139 if (!IS_ERR(inode)) { 140 init_special_inode(inode, inode->i_mode, rdev); 141 #ifdef CONFIG_EXT2_FS_XATTR 142 inode->i_op = &ext2_special_inode_operations; 143 #endif 144 mark_inode_dirty(inode); 145 err = ext2_add_nondir(dentry, inode); 146 } 147 return err; 148 } 149 150 static int ext2_symlink (struct inode * dir, struct dentry * dentry, 151 const char * symname) 152 { 153 struct super_block * sb = dir->i_sb; 154 int err = -ENAMETOOLONG; 155 unsigned l = strlen(symname)+1; 156 struct inode * inode; 157 158 if (l > sb->s_blocksize) 159 goto out; 160 161 err = dquot_initialize(dir); 162 if (err) 163 goto out; 164 165 inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO, &dentry->d_name); 166 err = PTR_ERR(inode); 167 if (IS_ERR(inode)) 168 goto out; 169 170 if (l > sizeof (EXT2_I(inode)->i_data)) { 171 /* slow symlink */ 172 inode->i_op = &ext2_symlink_inode_operations; 173 inode_nohighmem(inode); 174 if (test_opt(inode->i_sb, NOBH)) 175 inode->i_mapping->a_ops = &ext2_nobh_aops; 176 else 177 inode->i_mapping->a_ops = &ext2_aops; 178 err = page_symlink(inode, symname, l); 179 if (err) 180 goto out_fail; 181 } else { 182 /* fast symlink */ 183 inode->i_op = &ext2_fast_symlink_inode_operations; 184 inode->i_link = (char*)EXT2_I(inode)->i_data; 185 memcpy(inode->i_link, symname, l); 186 inode->i_size = l-1; 187 } 188 mark_inode_dirty(inode); 189 190 err = ext2_add_nondir(dentry, inode); 191 out: 192 return err; 193 194 out_fail: 195 inode_dec_link_count(inode); 196 unlock_new_inode(inode); 197 iput (inode); 198 goto out; 199 } 200 201 static int ext2_link (struct dentry * old_dentry, struct inode * dir, 202 struct dentry *dentry) 203 { 204 struct inode *inode = d_inode(old_dentry); 205 int err; 206 207 err = dquot_initialize(dir); 208 if (err) 209 return err; 210 211 inode->i_ctime = current_time(inode); 212 inode_inc_link_count(inode); 213 ihold(inode); 214 215 err = ext2_add_link(dentry, inode); 216 if (!err) { 217 d_instantiate(dentry, inode); 218 return 0; 219 } 220 inode_dec_link_count(inode); 221 iput(inode); 222 return err; 223 } 224 225 static int ext2_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode) 226 { 227 struct inode * inode; 228 int err; 229 230 err = dquot_initialize(dir); 231 if (err) 232 return err; 233 234 inode_inc_link_count(dir); 235 236 inode = ext2_new_inode(dir, S_IFDIR | mode, &dentry->d_name); 237 err = PTR_ERR(inode); 238 if (IS_ERR(inode)) 239 goto out_dir; 240 241 inode->i_op = &ext2_dir_inode_operations; 242 inode->i_fop = &ext2_dir_operations; 243 if (test_opt(inode->i_sb, NOBH)) 244 inode->i_mapping->a_ops = &ext2_nobh_aops; 245 else 246 inode->i_mapping->a_ops = &ext2_aops; 247 248 inode_inc_link_count(inode); 249 250 err = ext2_make_empty(inode, dir); 251 if (err) 252 goto out_fail; 253 254 err = ext2_add_link(dentry, inode); 255 if (err) 256 goto out_fail; 257 258 unlock_new_inode(inode); 259 d_instantiate(dentry, inode); 260 out: 261 return err; 262 263 out_fail: 264 inode_dec_link_count(inode); 265 inode_dec_link_count(inode); 266 unlock_new_inode(inode); 267 iput(inode); 268 out_dir: 269 inode_dec_link_count(dir); 270 goto out; 271 } 272 273 static int ext2_unlink(struct inode * dir, struct dentry *dentry) 274 { 275 struct inode * inode = d_inode(dentry); 276 struct ext2_dir_entry_2 * de; 277 struct page * page; 278 int err; 279 280 err = dquot_initialize(dir); 281 if (err) 282 goto out; 283 284 de = ext2_find_entry (dir, &dentry->d_name, &page); 285 if (!de) { 286 err = -ENOENT; 287 goto out; 288 } 289 290 err = ext2_delete_entry (de, page); 291 if (err) 292 goto out; 293 294 inode->i_ctime = dir->i_ctime; 295 inode_dec_link_count(inode); 296 err = 0; 297 out: 298 return err; 299 } 300 301 static int ext2_rmdir (struct inode * dir, struct dentry *dentry) 302 { 303 struct inode * inode = d_inode(dentry); 304 int err = -ENOTEMPTY; 305 306 if (ext2_empty_dir(inode)) { 307 err = ext2_unlink(dir, dentry); 308 if (!err) { 309 inode->i_size = 0; 310 inode_dec_link_count(inode); 311 inode_dec_link_count(dir); 312 } 313 } 314 return err; 315 } 316 317 static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry, 318 struct inode * new_dir, struct dentry * new_dentry, 319 unsigned int flags) 320 { 321 struct inode * old_inode = d_inode(old_dentry); 322 struct inode * new_inode = d_inode(new_dentry); 323 struct page * dir_page = NULL; 324 struct ext2_dir_entry_2 * dir_de = NULL; 325 struct page * old_page; 326 struct ext2_dir_entry_2 * old_de; 327 int err; 328 329 if (flags & ~RENAME_NOREPLACE) 330 return -EINVAL; 331 332 err = dquot_initialize(old_dir); 333 if (err) 334 goto out; 335 336 err = dquot_initialize(new_dir); 337 if (err) 338 goto out; 339 340 old_de = ext2_find_entry (old_dir, &old_dentry->d_name, &old_page); 341 if (!old_de) { 342 err = -ENOENT; 343 goto out; 344 } 345 346 if (S_ISDIR(old_inode->i_mode)) { 347 err = -EIO; 348 dir_de = ext2_dotdot(old_inode, &dir_page); 349 if (!dir_de) 350 goto out_old; 351 } 352 353 if (new_inode) { 354 struct page *new_page; 355 struct ext2_dir_entry_2 *new_de; 356 357 err = -ENOTEMPTY; 358 if (dir_de && !ext2_empty_dir (new_inode)) 359 goto out_dir; 360 361 err = -ENOENT; 362 new_de = ext2_find_entry (new_dir, &new_dentry->d_name, &new_page); 363 if (!new_de) 364 goto out_dir; 365 ext2_set_link(new_dir, new_de, new_page, old_inode, 1); 366 new_inode->i_ctime = current_time(new_inode); 367 if (dir_de) 368 drop_nlink(new_inode); 369 inode_dec_link_count(new_inode); 370 } else { 371 err = ext2_add_link(new_dentry, old_inode); 372 if (err) 373 goto out_dir; 374 if (dir_de) 375 inode_inc_link_count(new_dir); 376 } 377 378 /* 379 * Like most other Unix systems, set the ctime for inodes on a 380 * rename. 381 */ 382 old_inode->i_ctime = current_time(old_inode); 383 mark_inode_dirty(old_inode); 384 385 ext2_delete_entry (old_de, old_page); 386 387 if (dir_de) { 388 if (old_dir != new_dir) 389 ext2_set_link(old_inode, dir_de, dir_page, new_dir, 0); 390 else { 391 kunmap(dir_page); 392 put_page(dir_page); 393 } 394 inode_dec_link_count(old_dir); 395 } 396 return 0; 397 398 399 out_dir: 400 if (dir_de) { 401 kunmap(dir_page); 402 put_page(dir_page); 403 } 404 out_old: 405 kunmap(old_page); 406 put_page(old_page); 407 out: 408 return err; 409 } 410 411 const struct inode_operations ext2_dir_inode_operations = { 412 .create = ext2_create, 413 .lookup = ext2_lookup, 414 .link = ext2_link, 415 .unlink = ext2_unlink, 416 .symlink = ext2_symlink, 417 .mkdir = ext2_mkdir, 418 .rmdir = ext2_rmdir, 419 .mknod = ext2_mknod, 420 .rename = ext2_rename, 421 #ifdef CONFIG_EXT2_FS_XATTR 422 .listxattr = ext2_listxattr, 423 #endif 424 .setattr = ext2_setattr, 425 .get_acl = ext2_get_acl, 426 .set_acl = ext2_set_acl, 427 .tmpfile = ext2_tmpfile, 428 }; 429 430 const struct inode_operations ext2_special_inode_operations = { 431 #ifdef CONFIG_EXT2_FS_XATTR 432 .listxattr = ext2_listxattr, 433 #endif 434 .setattr = ext2_setattr, 435 .get_acl = ext2_get_acl, 436 .set_acl = ext2_set_acl, 437 }; 438