GNU Linux-libre 6.1.86-gnu
[releases.git] / fs / ntfs3 / namei.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *
4  * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
5  *
6  */
7
8 #include <linux/fs.h>
9 #include <linux/nls.h>
10
11 #include "debug.h"
12 #include "ntfs.h"
13 #include "ntfs_fs.h"
14
15 /*
16  * fill_name_de - Format NTFS_DE in @buf.
17  */
18 int fill_name_de(struct ntfs_sb_info *sbi, void *buf, const struct qstr *name,
19                  const struct cpu_str *uni)
20 {
21         int err;
22         struct NTFS_DE *e = buf;
23         u16 data_size;
24         struct ATTR_FILE_NAME *fname = (struct ATTR_FILE_NAME *)(e + 1);
25
26 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
27         e->ref.high = fname->home.high = 0;
28 #endif
29         if (uni) {
30 #ifdef __BIG_ENDIAN
31                 int ulen = uni->len;
32                 __le16 *uname = fname->name;
33                 const u16 *name_cpu = uni->name;
34
35                 while (ulen--)
36                         *uname++ = cpu_to_le16(*name_cpu++);
37 #else
38                 memcpy(fname->name, uni->name, uni->len * sizeof(u16));
39 #endif
40                 fname->name_len = uni->len;
41
42         } else {
43                 /* Convert input string to unicode. */
44                 err = ntfs_nls_to_utf16(sbi, name->name, name->len,
45                                         (struct cpu_str *)&fname->name_len,
46                                         NTFS_NAME_LEN, UTF16_LITTLE_ENDIAN);
47                 if (err < 0)
48                         return err;
49         }
50
51         fname->type = FILE_NAME_POSIX;
52         data_size = fname_full_size(fname);
53
54         e->size = cpu_to_le16(ALIGN(data_size, 8) + sizeof(struct NTFS_DE));
55         e->key_size = cpu_to_le16(data_size);
56         e->flags = 0;
57         e->res = 0;
58
59         return 0;
60 }
61
62 /*
63  * ntfs_lookup - inode_operations::lookup
64  */
65 static struct dentry *ntfs_lookup(struct inode *dir, struct dentry *dentry,
66                                   u32 flags)
67 {
68         struct ntfs_inode *ni = ntfs_i(dir);
69         struct cpu_str *uni = __getname();
70         struct inode *inode;
71         int err;
72
73         if (!uni)
74                 inode = ERR_PTR(-ENOMEM);
75         else {
76                 err = ntfs_nls_to_utf16(ni->mi.sbi, dentry->d_name.name,
77                                         dentry->d_name.len, uni, NTFS_NAME_LEN,
78                                         UTF16_HOST_ENDIAN);
79                 if (err < 0)
80                         inode = ERR_PTR(err);
81                 else {
82                         ni_lock(ni);
83                         inode = dir_search_u(dir, uni, NULL);
84                         ni_unlock(ni);
85                 }
86                 __putname(uni);
87         }
88
89         /*
90          * Check for a null pointer
91          * If the MFT record of ntfs inode is not a base record, inode->i_op can be NULL.
92          * This causes null pointer dereference in d_splice_alias().
93          */
94         if (!IS_ERR_OR_NULL(inode) && !inode->i_op) {
95                 iput(inode);
96                 inode = ERR_PTR(-EINVAL);
97         }
98
99         return d_splice_alias(inode, dentry);
100 }
101
102 /*
103  * ntfs_create - inode_operations::create
104  */
105 static int ntfs_create(struct user_namespace *mnt_userns, struct inode *dir,
106                        struct dentry *dentry, umode_t mode, bool excl)
107 {
108         struct inode *inode;
109
110         inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFREG | mode,
111                                   0, NULL, 0, NULL);
112
113         return IS_ERR(inode) ? PTR_ERR(inode) : 0;
114 }
115
116 /*
117  * ntfs_mknod
118  *
119  * inode_operations::mknod
120  */
121 static int ntfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
122                       struct dentry *dentry, umode_t mode, dev_t rdev)
123 {
124         struct inode *inode;
125
126         inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, mode, rdev,
127                                   NULL, 0, NULL);
128
129         return IS_ERR(inode) ? PTR_ERR(inode) : 0;
130 }
131
132 /*
133  * ntfs_link - inode_operations::link
134  */
135 static int ntfs_link(struct dentry *ode, struct inode *dir, struct dentry *de)
136 {
137         int err;
138         struct inode *inode = d_inode(ode);
139         struct ntfs_inode *ni = ntfs_i(inode);
140
141         if (S_ISDIR(inode->i_mode))
142                 return -EPERM;
143
144         if (inode->i_nlink >= NTFS_LINK_MAX)
145                 return -EMLINK;
146
147         ni_lock_dir(ntfs_i(dir));
148         if (inode != dir)
149                 ni_lock(ni);
150
151         inc_nlink(inode);
152         ihold(inode);
153
154         err = ntfs_link_inode(inode, de);
155
156         if (!err) {
157                 dir->i_ctime = dir->i_mtime = inode->i_ctime =
158                         current_time(dir);
159                 mark_inode_dirty(inode);
160                 mark_inode_dirty(dir);
161                 d_instantiate(de, inode);
162         } else {
163                 drop_nlink(inode);
164                 iput(inode);
165         }
166
167         if (inode != dir)
168                 ni_unlock(ni);
169         ni_unlock(ntfs_i(dir));
170
171         return err;
172 }
173
174 /*
175  * ntfs_unlink - inode_operations::unlink
176  */
177 static int ntfs_unlink(struct inode *dir, struct dentry *dentry)
178 {
179         struct ntfs_inode *ni = ntfs_i(dir);
180         int err;
181
182         ni_lock_dir(ni);
183
184         err = ntfs_unlink_inode(dir, dentry);
185
186         ni_unlock(ni);
187
188         return err;
189 }
190
191 /*
192  * ntfs_symlink - inode_operations::symlink
193  */
194 static int ntfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
195                         struct dentry *dentry, const char *symname)
196 {
197         u32 size = strlen(symname);
198         struct inode *inode;
199
200         inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFLNK | 0777,
201                                   0, symname, size, NULL);
202
203         return IS_ERR(inode) ? PTR_ERR(inode) : 0;
204 }
205
206 /*
207  * ntfs_mkdir- inode_operations::mkdir
208  */
209 static int ntfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
210                       struct dentry *dentry, umode_t mode)
211 {
212         struct inode *inode;
213
214         inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFDIR | mode,
215                                   0, NULL, 0, NULL);
216
217         return IS_ERR(inode) ? PTR_ERR(inode) : 0;
218 }
219
220 /*
221  * ntfs_rmdir - inode_operations::rmdir
222  */
223 static int ntfs_rmdir(struct inode *dir, struct dentry *dentry)
224 {
225         struct ntfs_inode *ni = ntfs_i(dir);
226         int err;
227
228         ni_lock_dir(ni);
229
230         err = ntfs_unlink_inode(dir, dentry);
231
232         ni_unlock(ni);
233
234         return err;
235 }
236
237 /*
238  * ntfs_rename - inode_operations::rename
239  */
240 static int ntfs_rename(struct user_namespace *mnt_userns, struct inode *dir,
241                        struct dentry *dentry, struct inode *new_dir,
242                        struct dentry *new_dentry, u32 flags)
243 {
244         int err;
245         struct super_block *sb = dir->i_sb;
246         struct ntfs_sb_info *sbi = sb->s_fs_info;
247         struct ntfs_inode *dir_ni = ntfs_i(dir);
248         struct ntfs_inode *new_dir_ni = ntfs_i(new_dir);
249         struct inode *inode = d_inode(dentry);
250         struct ntfs_inode *ni = ntfs_i(inode);
251         struct inode *new_inode = d_inode(new_dentry);
252         struct NTFS_DE *de, *new_de;
253         bool is_same, is_bad;
254         /*
255          * de           - memory of PATH_MAX bytes:
256          * [0-1024)     - original name (dentry->d_name)
257          * [1024-2048)  - paired to original name, usually DOS variant of dentry->d_name
258          * [2048-3072)  - new name (new_dentry->d_name)
259          */
260         static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + SIZEOF_RESIDENT < 1024);
261         static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + sizeof(struct NTFS_DE) <
262                       1024);
263         static_assert(PATH_MAX >= 4 * 1024);
264
265         if (flags & ~RENAME_NOREPLACE)
266                 return -EINVAL;
267
268         is_same = dentry->d_name.len == new_dentry->d_name.len &&
269                   !memcmp(dentry->d_name.name, new_dentry->d_name.name,
270                           dentry->d_name.len);
271
272         if (is_same && dir == new_dir) {
273                 /* Nothing to do. */
274                 return 0;
275         }
276
277         if (ntfs_is_meta_file(sbi, inode->i_ino)) {
278                 /* Should we print an error? */
279                 return -EINVAL;
280         }
281
282         if (new_inode) {
283                 /* Target name exists. Unlink it. */
284                 dget(new_dentry);
285                 ni_lock_dir(new_dir_ni);
286                 err = ntfs_unlink_inode(new_dir, new_dentry);
287                 ni_unlock(new_dir_ni);
288                 dput(new_dentry);
289                 if (err)
290                         return err;
291         }
292
293         /* Allocate PATH_MAX bytes. */
294         de = __getname();
295         if (!de)
296                 return -ENOMEM;
297
298         /* Translate dentry->d_name into unicode form. */
299         err = fill_name_de(sbi, de, &dentry->d_name, NULL);
300         if (err < 0)
301                 goto out;
302
303         if (is_same) {
304                 /* Reuse 'de'. */
305                 new_de = de;
306         } else {
307                 /* Translate new_dentry->d_name into unicode form. */
308                 new_de = Add2Ptr(de, 2048);
309                 err = fill_name_de(sbi, new_de, &new_dentry->d_name, NULL);
310                 if (err < 0)
311                         goto out;
312         }
313
314         ni_lock_dir(dir_ni);
315         ni_lock(ni);
316
317         is_bad = false;
318         err = ni_rename(dir_ni, new_dir_ni, ni, de, new_de, &is_bad);
319         if (is_bad) {
320                 /* Restore after failed rename failed too. */
321                 _ntfs_bad_inode(inode);
322         } else if (!err) {
323                 inode->i_ctime = dir->i_ctime = dir->i_mtime =
324                         current_time(dir);
325                 mark_inode_dirty(inode);
326                 mark_inode_dirty(dir);
327                 if (dir != new_dir) {
328                         new_dir->i_mtime = new_dir->i_ctime = dir->i_ctime;
329                         mark_inode_dirty(new_dir);
330                 }
331
332                 if (IS_DIRSYNC(dir))
333                         ntfs_sync_inode(dir);
334
335                 if (IS_DIRSYNC(new_dir))
336                         ntfs_sync_inode(inode);
337         }
338
339         ni_unlock(ni);
340         ni_unlock(dir_ni);
341 out:
342         __putname(de);
343         return err;
344 }
345
346 struct dentry *ntfs3_get_parent(struct dentry *child)
347 {
348         struct inode *inode = d_inode(child);
349         struct ntfs_inode *ni = ntfs_i(inode);
350
351         struct ATTR_LIST_ENTRY *le = NULL;
352         struct ATTRIB *attr = NULL;
353         struct ATTR_FILE_NAME *fname;
354
355         while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
356                                     NULL))) {
357                 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
358                 if (!fname)
359                         continue;
360
361                 return d_obtain_alias(
362                         ntfs_iget5(inode->i_sb, &fname->home, NULL));
363         }
364
365         return ERR_PTR(-ENOENT);
366 }
367
368 // clang-format off
369 const struct inode_operations ntfs_dir_inode_operations = {
370         .lookup         = ntfs_lookup,
371         .create         = ntfs_create,
372         .link           = ntfs_link,
373         .unlink         = ntfs_unlink,
374         .symlink        = ntfs_symlink,
375         .mkdir          = ntfs_mkdir,
376         .rmdir          = ntfs_rmdir,
377         .mknod          = ntfs_mknod,
378         .rename         = ntfs_rename,
379         .permission     = ntfs_permission,
380         .get_acl        = ntfs_get_acl,
381         .set_acl        = ntfs_set_acl,
382         .setattr        = ntfs3_setattr,
383         .getattr        = ntfs_getattr,
384         .listxattr      = ntfs_listxattr,
385         .fiemap         = ntfs_fiemap,
386 };
387
388 const struct inode_operations ntfs_special_inode_operations = {
389         .setattr        = ntfs3_setattr,
390         .getattr        = ntfs_getattr,
391         .listxattr      = ntfs_listxattr,
392         .get_acl        = ntfs_get_acl,
393         .set_acl        = ntfs_set_acl,
394 };
395 // clang-format on