blob: 55f7caadb09333a1d73603f839de3fa5df2f9e42 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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>
Christoph Hellwig907f4552010-03-03 09:05:06 -050035#include <linux/quotaops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "ext2.h"
37#include "xattr.h"
38#include "acl.h"
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
41{
42 int err = ext2_add_link(dentry, inode);
43 if (!err) {
Al Viro41080b52008-12-30 01:52:35 -050044 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +040045 d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 return 0;
47 }
Alexey Dobriyana513b032006-03-23 03:00:53 -080048 inode_dec_link_count(inode);
Al Viro41080b52008-12-30 01:52:35 -050049 unlock_new_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 iput(inode);
51 return err;
52}
53
54/*
55 * Methods themselves.
56 */
57
Al Viro00cd8dd2012-06-10 17:13:09 -040058static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 struct inode * inode;
61 ino_t ino;
62
63 if (dentry->d_name.len > EXT2_NAME_LEN)
64 return ERR_PTR(-ENAMETOOLONG);
65
Al Viroa9885442008-08-24 07:28:39 -040066 ino = ext2_inode_by_name(dir, &dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 inode = NULL;
68 if (ino) {
David Howells52fcf702008-02-07 00:15:35 -080069 inode = ext2_iget(dir->i_sb, ino);
Al Viroa9049372011-07-08 21:20:11 -040070 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);
Bryan Donlan4d6c13f2009-06-30 11:41:24 -070075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
Pekka Enberg082a05c2006-01-14 13:21:07 -080077 return d_splice_alias(inode, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
80struct dentry *ext2_get_parent(struct dentry *child)
81{
Linus Torvalds26fe5752012-05-10 13:14:12 -070082 struct qstr dotdot = QSTR_INIT("..", 2);
David Howells2b0143b2015-03-17 22:25:59 +000083 unsigned long ino = ext2_inode_by_name(d_inode(child), &dotdot);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (!ino)
85 return ERR_PTR(-ENOENT);
Al Virofc640052016-04-10 01:33:30 -040086 return d_obtain_alias(ext2_iget(child->d_sb, ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
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 */
Al Viroebfc3b42012-06-10 18:05:36 -040097static int ext2_create (struct inode * dir, struct dentry * dentry, umode_t mode, bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Christoph Hellwig907f4552010-03-03 09:05:06 -050099 struct inode *inode;
Jan Karac2edb302015-06-29 16:08:45 +0200100 int err;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500101
Jan Karac2edb302015-06-29 16:08:45 +0200102 err = dquot_initialize(dir);
103 if (err)
104 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500105
Eric Paris2a7dba32011-02-01 11:05:39 -0500106 inode = ext2_new_inode(dir, mode, &dentry->d_name);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500107 if (IS_ERR(inode))
108 return PTR_ERR(inode);
109
Dan Williamsfb094c92017-12-21 12:25:11 -0800110 ext2_set_file_ops(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500111 mark_inode_dirty(inode);
112 return ext2_add_nondir(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Al Viro60545d02013-06-07 01:20:27 -0400115static 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
Dan Williamsfb094c92017-12-21 12:25:11 -0800121 ext2_set_file_ops(inode);
Al Viro60545d02013-06-07 01:20:27 -0400122 mark_inode_dirty(inode);
123 d_tmpfile(dentry, inode);
124 unlock_new_inode(inode);
125 return 0;
126}
127
Al Viro1a67aaf2011-07-26 01:52:52 -0400128static int ext2_mknod (struct inode * dir, struct dentry *dentry, umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 struct inode * inode;
131 int err;
132
Jan Karac2edb302015-06-29 16:08:45 +0200133 err = dquot_initialize(dir);
134 if (err)
135 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500136
Eric Paris2a7dba32011-02-01 11:05:39 -0500137 inode = ext2_new_inode (dir, mode, &dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 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
150static 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
Jan Karac2edb302015-06-29 16:08:45 +0200161 err = dquot_initialize(dir);
162 if (err)
163 goto out;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500164
Eric Paris2a7dba32011-02-01 11:05:39 -0500165 inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO, &dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 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;
Al Viro21fc61c2015-11-17 01:07:57 -0500173 inode_nohighmem(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 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;
Al Virocbe0fa32015-05-02 10:02:46 -0400184 inode->i_link = (char*)EXT2_I(inode)->i_data;
185 memcpy(inode->i_link, symname, l);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 inode->i_size = l-1;
187 }
188 mark_inode_dirty(inode);
189
190 err = ext2_add_nondir(dentry, inode);
191out:
192 return err;
193
194out_fail:
Alexey Dobriyana513b032006-03-23 03:00:53 -0800195 inode_dec_link_count(inode);
Al Viro41080b52008-12-30 01:52:35 -0500196 unlock_new_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 iput (inode);
198 goto out;
199}
200
201static int ext2_link (struct dentry * old_dentry, struct inode * dir,
202 struct dentry *dentry)
203{
David Howells2b0143b2015-03-17 22:25:59 +0000204 struct inode *inode = d_inode(old_dentry);
Al Viro41080b52008-12-30 01:52:35 -0500205 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Jan Karac2edb302015-06-29 16:08:45 +0200207 err = dquot_initialize(dir);
208 if (err)
209 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500210
Deepa Dinamani02027d42016-09-14 07:48:05 -0700211 inode->i_ctime = current_time(inode);
Alexey Dobriyana513b032006-03-23 03:00:53 -0800212 inode_inc_link_count(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400213 ihold(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Al Viro41080b52008-12-30 01:52:35 -0500215 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Al Viro18bb1db2011-07-26 01:41:39 -0400225static int ext2_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 struct inode * inode;
Al Viro8de52772012-02-06 12:45:27 -0500228 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Jan Karac2edb302015-06-29 16:08:45 +0200230 err = dquot_initialize(dir);
231 if (err)
232 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500233
Alexey Dobriyana513b032006-03-23 03:00:53 -0800234 inode_inc_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Eric Paris2a7dba32011-02-01 11:05:39 -0500236 inode = ext2_new_inode(dir, S_IFDIR | mode, &dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 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
Alexey Dobriyana513b032006-03-23 03:00:53 -0800248 inode_inc_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
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
Al Viro41080b52008-12-30 01:52:35 -0500258 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +0400259 d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260out:
261 return err;
262
263out_fail:
Alexey Dobriyana513b032006-03-23 03:00:53 -0800264 inode_dec_link_count(inode);
265 inode_dec_link_count(inode);
Al Viro41080b52008-12-30 01:52:35 -0500266 unlock_new_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 iput(inode);
268out_dir:
Alexey Dobriyana513b032006-03-23 03:00:53 -0800269 inode_dec_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 goto out;
271}
272
273static int ext2_unlink(struct inode * dir, struct dentry *dentry)
274{
David Howells2b0143b2015-03-17 22:25:59 +0000275 struct inode * inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 struct ext2_dir_entry_2 * de;
277 struct page * page;
Jan Karac2edb302015-06-29 16:08:45 +0200278 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Jan Karac2edb302015-06-29 16:08:45 +0200280 err = dquot_initialize(dir);
281 if (err)
282 goto out;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500283
Al Viroa9885442008-08-24 07:28:39 -0400284 de = ext2_find_entry (dir, &dentry->d_name, &page);
Jan Karac2edb302015-06-29 16:08:45 +0200285 if (!de) {
286 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 goto out;
Jan Karac2edb302015-06-29 16:08:45 +0200288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 err = ext2_delete_entry (de, page);
291 if (err)
292 goto out;
293
294 inode->i_ctime = dir->i_ctime;
Alexey Dobriyana513b032006-03-23 03:00:53 -0800295 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 err = 0;
297out:
298 return err;
299}
300
301static int ext2_rmdir (struct inode * dir, struct dentry *dentry)
302{
David Howells2b0143b2015-03-17 22:25:59 +0000303 struct inode * inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 int err = -ENOTEMPTY;
305
306 if (ext2_empty_dir(inode)) {
307 err = ext2_unlink(dir, dentry);
308 if (!err) {
309 inode->i_size = 0;
Alexey Dobriyana513b032006-03-23 03:00:53 -0800310 inode_dec_link_count(inode);
311 inode_dec_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313 }
314 return err;
315}
316
317static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
Miklos Szeredif03b8ad2016-09-27 11:03:57 +0200318 struct inode * new_dir, struct dentry * new_dentry,
319 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
David Howells2b0143b2015-03-17 22:25:59 +0000321 struct inode * old_inode = d_inode(old_dentry);
322 struct inode * new_inode = d_inode(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 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;
Jan Karac2edb302015-06-29 16:08:45 +0200327 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Miklos Szeredif03b8ad2016-09-27 11:03:57 +0200329 if (flags & ~RENAME_NOREPLACE)
330 return -EINVAL;
331
Jan Karac2edb302015-06-29 16:08:45 +0200332 err = dquot_initialize(old_dir);
333 if (err)
334 goto out;
335
336 err = dquot_initialize(new_dir);
337 if (err)
338 goto out;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500339
Al Viroa9885442008-08-24 07:28:39 -0400340 old_de = ext2_find_entry (old_dir, &old_dentry->d_name, &old_page);
Jan Karac2edb302015-06-29 16:08:45 +0200341 if (!old_de) {
342 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 goto out;
Jan Karac2edb302015-06-29 16:08:45 +0200344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
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;
Al Viroa9885442008-08-24 07:28:39 -0400362 new_de = ext2_find_entry (new_dir, &new_dentry->d_name, &new_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (!new_de)
364 goto out_dir;
Jan Kara39fe7552009-06-17 16:26:20 -0700365 ext2_set_link(new_dir, new_de, new_page, old_inode, 1);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700366 new_inode->i_ctime = current_time(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (dir_de)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700368 drop_nlink(new_inode);
Alexey Dobriyana513b032006-03-23 03:00:53 -0800369 inode_dec_link_count(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 err = ext2_add_link(new_dentry, old_inode);
Josh Hunte8a80c62011-02-24 11:48:22 +0100372 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 goto out_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (dir_de)
Alexey Dobriyana513b032006-03-23 03:00:53 -0800375 inode_inc_link_count(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
378 /*
379 * Like most other Unix systems, set the ctime for inodes on a
380 * rename.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 */
Deepa Dinamani02027d42016-09-14 07:48:05 -0700382 old_inode->i_ctime = current_time(old_inode);
Josh Hunte8a80c62011-02-24 11:48:22 +0100383 mark_inode_dirty(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 ext2_delete_entry (old_de, old_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 if (dir_de) {
Jan Kara39fe7552009-06-17 16:26:20 -0700388 if (old_dir != new_dir)
389 ext2_set_link(old_inode, dir_de, dir_page, new_dir, 0);
Nicolas Pitre9de68862009-09-05 00:25:37 -0400390 else {
391 kunmap(dir_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300392 put_page(dir_page);
Nicolas Pitre9de68862009-09-05 00:25:37 -0400393 }
Alexey Dobriyana513b032006-03-23 03:00:53 -0800394 inode_dec_link_count(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396 return 0;
397
398
399out_dir:
400 if (dir_de) {
401 kunmap(dir_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300402 put_page(dir_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404out_old:
405 kunmap(old_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300406 put_page(old_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407out:
408 return err;
409}
410
Arjan van de Ven754661f2007-02-12 00:55:38 -0800411const struct inode_operations ext2_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 .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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 .listxattr = ext2_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423#endif
424 .setattr = ext2_setattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200425 .get_acl = ext2_get_acl,
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800426 .set_acl = ext2_set_acl,
Al Viro60545d02013-06-07 01:20:27 -0400427 .tmpfile = ext2_tmpfile,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428};
429
Arjan van de Ven754661f2007-02-12 00:55:38 -0800430const struct inode_operations ext2_special_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431#ifdef CONFIG_EXT2_FS_XATTR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 .listxattr = ext2_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433#endif
434 .setattr = ext2_setattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200435 .get_acl = ext2_get_acl,
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800436 .set_acl = ext2_set_acl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437};