blob: 4d5d20491ffd89a0c034a537d87d57b428a416cc [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/sysv/namei.c
4 *
5 * minix/namei.c
6 * Copyright (C) 1991, 1992 Linus Torvalds
7 *
8 * coh/namei.c
9 * Copyright (C) 1993 Pascal Haible, Bruno Haible
10 *
11 * sysv/namei.c
12 * Copyright (C) 1993 Bruno Haible
13 * Copyright (C) 1997, 1998 Krzysztof G. Baranowski
14 */
15
16#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "sysv.h"
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019static int add_nondir(struct dentry *dentry, struct inode *inode)
20{
21 int err = sysv_add_link(dentry, inode);
22 if (!err) {
23 d_instantiate(dentry, inode);
24 return 0;
25 }
Alexey Dobriyan4e907c32006-03-23 03:00:52 -080026 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 iput(inode);
28 return err;
29}
30
Linus Torvaldsda53be12013-05-21 15:22:44 -070031static int sysv_hash(const struct dentry *dentry, struct qstr *qstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
33 /* Truncate the name in place, avoids having to define a compare
34 function. */
35 if (qstr->len > SYSV_NAMELEN) {
36 qstr->len = SYSV_NAMELEN;
Linus Torvalds8387ff22016-06-10 07:51:30 -070037 qstr->hash = full_name_hash(dentry, qstr->name, qstr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 }
39 return 0;
40}
41
Al Viroe16404e2009-02-20 05:55:13 +000042const struct dentry_operations sysv_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 .d_hash = sysv_hash,
44};
45
Al Viro00cd8dd2012-06-10 17:13:09 -040046static struct dentry *sysv_lookup(struct inode * dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 struct inode * inode = NULL;
49 ino_t ino;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 if (dentry->d_name.len > SYSV_NAMELEN)
52 return ERR_PTR(-ENAMETOOLONG);
53 ino = sysv_inode_by_name(dentry);
Al Viro5bf35442018-04-30 20:09:10 -040054 if (ino)
David Howellsb8e13432008-02-07 00:15:47 -080055 inode = sysv_iget(dir->i_sb, ino);
Al Viro5bf35442018-04-30 20:09:10 -040056 return d_splice_alias(inode, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
Al Viro1a67aaf2011-07-26 01:52:52 -040059static int sysv_mknod(struct inode * dir, struct dentry * dentry, umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 struct inode * inode;
62 int err;
63
64 if (!old_valid_dev(rdev))
65 return -EINVAL;
66
67 inode = sysv_new_inode(dir, mode);
68 err = PTR_ERR(inode);
69
70 if (!IS_ERR(inode)) {
71 sysv_set_inode(inode, rdev);
72 mark_inode_dirty(inode);
73 err = add_nondir(dentry, inode);
74 }
75 return err;
76}
77
Al Viroebfc3b42012-06-10 18:05:36 -040078static int sysv_create(struct inode * dir, struct dentry * dentry, umode_t mode, bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 return sysv_mknod(dir, dentry, mode, 0);
81}
82
83static int sysv_symlink(struct inode * dir, struct dentry * dentry,
84 const char * symname)
85{
86 int err = -ENAMETOOLONG;
87 int l = strlen(symname)+1;
88 struct inode * inode;
89
90 if (l > dir->i_sb->s_blocksize)
91 goto out;
92
93 inode = sysv_new_inode(dir, S_IFLNK|0777);
94 err = PTR_ERR(inode);
95 if (IS_ERR(inode))
96 goto out;
97
98 sysv_set_inode(inode, 0);
99 err = page_symlink(inode, symname, l);
100 if (err)
101 goto out_fail;
102
103 mark_inode_dirty(inode);
104 err = add_nondir(dentry, inode);
105out:
106 return err;
107
108out_fail:
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800109 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 iput(inode);
111 goto out;
112}
113
114static int sysv_link(struct dentry * old_dentry, struct inode * dir,
115 struct dentry * dentry)
116{
David Howells2b0143b2015-03-17 22:25:59 +0000117 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Deepa Dinamani02027d42016-09-14 07:48:05 -0700119 inode->i_ctime = current_time(inode);
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800120 inode_inc_link_count(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400121 ihold(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 return add_nondir(dentry, inode);
124}
125
Al Viro18bb1db2011-07-26 01:41:39 -0400126static int sysv_mkdir(struct inode * dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 struct inode * inode;
Al Viro8de52772012-02-06 12:45:27 -0500129 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800131 inode_inc_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 inode = sysv_new_inode(dir, S_IFDIR|mode);
134 err = PTR_ERR(inode);
135 if (IS_ERR(inode))
136 goto out_dir;
137
138 sysv_set_inode(inode, 0);
139
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800140 inode_inc_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 err = sysv_make_empty(inode, dir);
143 if (err)
144 goto out_fail;
145
146 err = sysv_add_link(dentry, inode);
147 if (err)
148 goto out_fail;
149
150 d_instantiate(dentry, inode);
151out:
152 return err;
153
154out_fail:
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800155 inode_dec_link_count(inode);
156 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 iput(inode);
158out_dir:
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800159 inode_dec_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 goto out;
161}
162
163static int sysv_unlink(struct inode * dir, struct dentry * dentry)
164{
David Howells2b0143b2015-03-17 22:25:59 +0000165 struct inode * inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 struct page * page;
167 struct sysv_dir_entry * de;
168 int err = -ENOENT;
169
170 de = sysv_find_entry(dentry, &page);
171 if (!de)
172 goto out;
173
174 err = sysv_delete_entry (de, page);
175 if (err)
176 goto out;
177
178 inode->i_ctime = dir->i_ctime;
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800179 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180out:
181 return err;
182}
183
184static int sysv_rmdir(struct inode * dir, struct dentry * dentry)
185{
David Howells2b0143b2015-03-17 22:25:59 +0000186 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 int err = -ENOTEMPTY;
188
189 if (sysv_empty_dir(inode)) {
190 err = sysv_unlink(dir, dentry);
191 if (!err) {
192 inode->i_size = 0;
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800193 inode_dec_link_count(inode);
194 inode_dec_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196 }
197 return err;
198}
199
200/*
201 * Anybody can rename anything with this: the permission checks are left to the
202 * higher-level routines.
203 */
204static int sysv_rename(struct inode * old_dir, struct dentry * old_dentry,
Miklos Szeredif03b8ad2016-09-27 11:03:57 +0200205 struct inode * new_dir, struct dentry * new_dentry,
206 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
David Howells2b0143b2015-03-17 22:25:59 +0000208 struct inode * old_inode = d_inode(old_dentry);
209 struct inode * new_inode = d_inode(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 struct page * dir_page = NULL;
211 struct sysv_dir_entry * dir_de = NULL;
212 struct page * old_page;
213 struct sysv_dir_entry * old_de;
214 int err = -ENOENT;
215
Miklos Szeredif03b8ad2016-09-27 11:03:57 +0200216 if (flags & ~RENAME_NOREPLACE)
217 return -EINVAL;
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 old_de = sysv_find_entry(old_dentry, &old_page);
220 if (!old_de)
221 goto out;
222
223 if (S_ISDIR(old_inode->i_mode)) {
224 err = -EIO;
225 dir_de = sysv_dotdot(old_inode, &dir_page);
226 if (!dir_de)
227 goto out_old;
228 }
229
230 if (new_inode) {
231 struct page * new_page;
232 struct sysv_dir_entry * new_de;
233
234 err = -ENOTEMPTY;
235 if (dir_de && !sysv_empty_dir(new_inode))
236 goto out_dir;
237
238 err = -ENOENT;
239 new_de = sysv_find_entry(new_dentry, &new_page);
240 if (!new_de)
241 goto out_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 sysv_set_link(new_de, new_page, old_inode);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700243 new_inode->i_ctime = current_time(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (dir_de)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700245 drop_nlink(new_inode);
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800246 inode_dec_link_count(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 err = sysv_add_link(new_dentry, old_inode);
Al Viro4787d452011-03-02 09:38:45 -0500249 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 goto out_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (dir_de)
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800252 inode_inc_link_count(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254
255 sysv_delete_entry(old_de, old_page);
Al Viro4787d452011-03-02 09:38:45 -0500256 mark_inode_dirty(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 if (dir_de) {
259 sysv_set_link(dir_de, dir_page, new_dir);
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800260 inode_dec_link_count(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262 return 0;
263
264out_dir:
265 if (dir_de) {
266 kunmap(dir_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300267 put_page(dir_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269out_old:
270 kunmap(old_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300271 put_page(old_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272out:
273 return err;
274}
275
276/*
277 * directories can handle most operations...
278 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800279const struct inode_operations sysv_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 .create = sysv_create,
281 .lookup = sysv_lookup,
282 .link = sysv_link,
283 .unlink = sysv_unlink,
284 .symlink = sysv_symlink,
285 .mkdir = sysv_mkdir,
286 .rmdir = sysv_rmdir,
287 .mknod = sysv_mknod,
288 .rename = sysv_rename,
289 .getattr = sysv_getattr,
290};