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