blob: d8817f13976357232a21a1ead9577c68b8c4b117 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/sysv/namei.c
3 *
4 * minix/namei.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 *
7 * coh/namei.c
8 * Copyright (C) 1993 Pascal Haible, Bruno Haible
9 *
10 * sysv/namei.c
11 * Copyright (C) 1993 Bruno Haible
12 * Copyright (C) 1997, 1998 Krzysztof G. Baranowski
13 */
14
15#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "sysv.h"
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018static int add_nondir(struct dentry *dentry, struct inode *inode)
19{
20 int err = sysv_add_link(dentry, inode);
21 if (!err) {
22 d_instantiate(dentry, inode);
23 return 0;
24 }
Alexey Dobriyan4e907c32006-03-23 03:00:52 -080025 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 iput(inode);
27 return err;
28}
29
Linus Torvaldsda53be12013-05-21 15:22:44 -070030static int sysv_hash(const struct dentry *dentry, struct qstr *qstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
32 /* Truncate the name in place, avoids having to define a compare
33 function. */
34 if (qstr->len > SYSV_NAMELEN) {
35 qstr->len = SYSV_NAMELEN;
Linus Torvalds8387ff22016-06-10 07:51:30 -070036 qstr->hash = full_name_hash(dentry, qstr->name, qstr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 }
38 return 0;
39}
40
Al Viroe16404e2009-02-20 05:55:13 +000041const struct dentry_operations sysv_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 .d_hash = sysv_hash,
43};
44
Al Viro00cd8dd2012-06-10 17:13:09 -040045static struct dentry *sysv_lookup(struct inode * dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 struct inode * inode = NULL;
48 ino_t ino;
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 if (dentry->d_name.len > SYSV_NAMELEN)
51 return ERR_PTR(-ENAMETOOLONG);
52 ino = sysv_inode_by_name(dentry);
53
54 if (ino) {
David Howellsb8e13432008-02-07 00:15:47 -080055 inode = sysv_iget(dir->i_sb, ino);
56 if (IS_ERR(inode))
57 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
59 d_add(dentry, inode);
60 return NULL;
61}
62
Al Viro1a67aaf2011-07-26 01:52:52 -040063static int sysv_mknod(struct inode * dir, struct dentry * dentry, umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 struct inode * inode;
66 int err;
67
68 if (!old_valid_dev(rdev))
69 return -EINVAL;
70
71 inode = sysv_new_inode(dir, mode);
72 err = PTR_ERR(inode);
73
74 if (!IS_ERR(inode)) {
75 sysv_set_inode(inode, rdev);
76 mark_inode_dirty(inode);
77 err = add_nondir(dentry, inode);
78 }
79 return err;
80}
81
Al Viroebfc3b42012-06-10 18:05:36 -040082static int sysv_create(struct inode * dir, struct dentry * dentry, umode_t mode, bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 return sysv_mknod(dir, dentry, mode, 0);
85}
86
87static int sysv_symlink(struct inode * dir, struct dentry * dentry,
88 const char * symname)
89{
90 int err = -ENAMETOOLONG;
91 int l = strlen(symname)+1;
92 struct inode * inode;
93
94 if (l > dir->i_sb->s_blocksize)
95 goto out;
96
97 inode = sysv_new_inode(dir, S_IFLNK|0777);
98 err = PTR_ERR(inode);
99 if (IS_ERR(inode))
100 goto out;
101
102 sysv_set_inode(inode, 0);
103 err = page_symlink(inode, symname, l);
104 if (err)
105 goto out_fail;
106
107 mark_inode_dirty(inode);
108 err = add_nondir(dentry, inode);
109out:
110 return err;
111
112out_fail:
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800113 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 iput(inode);
115 goto out;
116}
117
118static int sysv_link(struct dentry * old_dentry, struct inode * dir,
119 struct dentry * dentry)
120{
David Howells2b0143b2015-03-17 22:25:59 +0000121 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Deepa Dinamani02027d42016-09-14 07:48:05 -0700123 inode->i_ctime = current_time(inode);
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800124 inode_inc_link_count(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400125 ihold(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 return add_nondir(dentry, inode);
128}
129
Al Viro18bb1db2011-07-26 01:41:39 -0400130static int sysv_mkdir(struct inode * dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct inode * inode;
Al Viro8de52772012-02-06 12:45:27 -0500133 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800135 inode_inc_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 inode = sysv_new_inode(dir, S_IFDIR|mode);
138 err = PTR_ERR(inode);
139 if (IS_ERR(inode))
140 goto out_dir;
141
142 sysv_set_inode(inode, 0);
143
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800144 inode_inc_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 err = sysv_make_empty(inode, dir);
147 if (err)
148 goto out_fail;
149
150 err = sysv_add_link(dentry, inode);
151 if (err)
152 goto out_fail;
153
154 d_instantiate(dentry, inode);
155out:
156 return err;
157
158out_fail:
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800159 inode_dec_link_count(inode);
160 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 iput(inode);
162out_dir:
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800163 inode_dec_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 goto out;
165}
166
167static int sysv_unlink(struct inode * dir, struct dentry * dentry)
168{
David Howells2b0143b2015-03-17 22:25:59 +0000169 struct inode * inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 struct page * page;
171 struct sysv_dir_entry * de;
172 int err = -ENOENT;
173
174 de = sysv_find_entry(dentry, &page);
175 if (!de)
176 goto out;
177
178 err = sysv_delete_entry (de, page);
179 if (err)
180 goto out;
181
182 inode->i_ctime = dir->i_ctime;
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800183 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184out:
185 return err;
186}
187
188static int sysv_rmdir(struct inode * dir, struct dentry * dentry)
189{
David Howells2b0143b2015-03-17 22:25:59 +0000190 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 int err = -ENOTEMPTY;
192
193 if (sysv_empty_dir(inode)) {
194 err = sysv_unlink(dir, dentry);
195 if (!err) {
196 inode->i_size = 0;
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800197 inode_dec_link_count(inode);
198 inode_dec_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200 }
201 return err;
202}
203
204/*
205 * Anybody can rename anything with this: the permission checks are left to the
206 * higher-level routines.
207 */
208static int sysv_rename(struct inode * old_dir, struct dentry * old_dentry,
Miklos Szeredif03b8ad2016-09-27 11:03:57 +0200209 struct inode * new_dir, struct dentry * new_dentry,
210 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
David Howells2b0143b2015-03-17 22:25:59 +0000212 struct inode * old_inode = d_inode(old_dentry);
213 struct inode * new_inode = d_inode(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 struct page * dir_page = NULL;
215 struct sysv_dir_entry * dir_de = NULL;
216 struct page * old_page;
217 struct sysv_dir_entry * old_de;
218 int err = -ENOENT;
219
Miklos Szeredif03b8ad2016-09-27 11:03:57 +0200220 if (flags & ~RENAME_NOREPLACE)
221 return -EINVAL;
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 old_de = sysv_find_entry(old_dentry, &old_page);
224 if (!old_de)
225 goto out;
226
227 if (S_ISDIR(old_inode->i_mode)) {
228 err = -EIO;
229 dir_de = sysv_dotdot(old_inode, &dir_page);
230 if (!dir_de)
231 goto out_old;
232 }
233
234 if (new_inode) {
235 struct page * new_page;
236 struct sysv_dir_entry * new_de;
237
238 err = -ENOTEMPTY;
239 if (dir_de && !sysv_empty_dir(new_inode))
240 goto out_dir;
241
242 err = -ENOENT;
243 new_de = sysv_find_entry(new_dentry, &new_page);
244 if (!new_de)
245 goto out_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 sysv_set_link(new_de, new_page, old_inode);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700247 new_inode->i_ctime = current_time(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (dir_de)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700249 drop_nlink(new_inode);
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800250 inode_dec_link_count(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 err = sysv_add_link(new_dentry, old_inode);
Al Viro4787d452011-03-02 09:38:45 -0500253 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 goto out_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (dir_de)
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800256 inode_inc_link_count(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258
259 sysv_delete_entry(old_de, old_page);
Al Viro4787d452011-03-02 09:38:45 -0500260 mark_inode_dirty(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 if (dir_de) {
263 sysv_set_link(dir_de, dir_page, new_dir);
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800264 inode_dec_link_count(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266 return 0;
267
268out_dir:
269 if (dir_de) {
270 kunmap(dir_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300271 put_page(dir_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273out_old:
274 kunmap(old_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300275 put_page(old_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276out:
277 return err;
278}
279
280/*
281 * directories can handle most operations...
282 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800283const struct inode_operations sysv_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 .create = sysv_create,
285 .lookup = sysv_lookup,
286 .link = sysv_link,
287 .unlink = sysv_unlink,
288 .symlink = sysv_symlink,
289 .mkdir = sysv_mkdir,
290 .rmdir = sysv_rmdir,
291 .mknod = sysv_mknod,
292 .rename = sysv_rename,
293 .getattr = sysv_getattr,
294};