0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/fs.h>
0009 #include <linux/nls.h>
0010
0011 #include "debug.h"
0012 #include "ntfs.h"
0013 #include "ntfs_fs.h"
0014
0015
0016
0017
0018 int fill_name_de(struct ntfs_sb_info *sbi, void *buf, const struct qstr *name,
0019 const struct cpu_str *uni)
0020 {
0021 int err;
0022 struct NTFS_DE *e = buf;
0023 u16 data_size;
0024 struct ATTR_FILE_NAME *fname = (struct ATTR_FILE_NAME *)(e + 1);
0025
0026 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
0027 e->ref.high = fname->home.high = 0;
0028 #endif
0029 if (uni) {
0030 #ifdef __BIG_ENDIAN
0031 int ulen = uni->len;
0032 __le16 *uname = fname->name;
0033 const u16 *name_cpu = uni->name;
0034
0035 while (ulen--)
0036 *uname++ = cpu_to_le16(*name_cpu++);
0037 #else
0038 memcpy(fname->name, uni->name, uni->len * sizeof(u16));
0039 #endif
0040 fname->name_len = uni->len;
0041
0042 } else {
0043
0044 err = ntfs_nls_to_utf16(sbi, name->name, name->len,
0045 (struct cpu_str *)&fname->name_len,
0046 NTFS_NAME_LEN, UTF16_LITTLE_ENDIAN);
0047 if (err < 0)
0048 return err;
0049 }
0050
0051 fname->type = FILE_NAME_POSIX;
0052 data_size = fname_full_size(fname);
0053
0054 e->size = cpu_to_le16(ALIGN(data_size, 8) + sizeof(struct NTFS_DE));
0055 e->key_size = cpu_to_le16(data_size);
0056 e->flags = 0;
0057 e->res = 0;
0058
0059 return 0;
0060 }
0061
0062
0063
0064
0065 static struct dentry *ntfs_lookup(struct inode *dir, struct dentry *dentry,
0066 u32 flags)
0067 {
0068 struct ntfs_inode *ni = ntfs_i(dir);
0069 struct cpu_str *uni = __getname();
0070 struct inode *inode;
0071 int err;
0072
0073 if (!uni)
0074 inode = ERR_PTR(-ENOMEM);
0075 else {
0076 err = ntfs_nls_to_utf16(ni->mi.sbi, dentry->d_name.name,
0077 dentry->d_name.len, uni, NTFS_NAME_LEN,
0078 UTF16_HOST_ENDIAN);
0079 if (err < 0)
0080 inode = ERR_PTR(err);
0081 else {
0082 ni_lock(ni);
0083 inode = dir_search_u(dir, uni, NULL);
0084 ni_unlock(ni);
0085 }
0086 __putname(uni);
0087 }
0088
0089 return d_splice_alias(inode, dentry);
0090 }
0091
0092
0093
0094
0095 static int ntfs_create(struct user_namespace *mnt_userns, struct inode *dir,
0096 struct dentry *dentry, umode_t mode, bool excl)
0097 {
0098 struct inode *inode;
0099
0100 inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFREG | mode,
0101 0, NULL, 0, NULL);
0102
0103 return IS_ERR(inode) ? PTR_ERR(inode) : 0;
0104 }
0105
0106
0107
0108
0109
0110
0111 static int ntfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
0112 struct dentry *dentry, umode_t mode, dev_t rdev)
0113 {
0114 struct inode *inode;
0115
0116 inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, mode, rdev,
0117 NULL, 0, NULL);
0118
0119 return IS_ERR(inode) ? PTR_ERR(inode) : 0;
0120 }
0121
0122
0123
0124
0125 static int ntfs_link(struct dentry *ode, struct inode *dir, struct dentry *de)
0126 {
0127 int err;
0128 struct inode *inode = d_inode(ode);
0129 struct ntfs_inode *ni = ntfs_i(inode);
0130
0131 if (S_ISDIR(inode->i_mode))
0132 return -EPERM;
0133
0134 if (inode->i_nlink >= NTFS_LINK_MAX)
0135 return -EMLINK;
0136
0137 ni_lock_dir(ntfs_i(dir));
0138 if (inode != dir)
0139 ni_lock(ni);
0140
0141 inc_nlink(inode);
0142 ihold(inode);
0143
0144 err = ntfs_link_inode(inode, de);
0145
0146 if (!err) {
0147 dir->i_ctime = dir->i_mtime = inode->i_ctime =
0148 current_time(dir);
0149 mark_inode_dirty(inode);
0150 mark_inode_dirty(dir);
0151 d_instantiate(de, inode);
0152 } else {
0153 drop_nlink(inode);
0154 iput(inode);
0155 }
0156
0157 if (inode != dir)
0158 ni_unlock(ni);
0159 ni_unlock(ntfs_i(dir));
0160
0161 return err;
0162 }
0163
0164
0165
0166
0167 static int ntfs_unlink(struct inode *dir, struct dentry *dentry)
0168 {
0169 struct ntfs_inode *ni = ntfs_i(dir);
0170 int err;
0171
0172 ni_lock_dir(ni);
0173
0174 err = ntfs_unlink_inode(dir, dentry);
0175
0176 ni_unlock(ni);
0177
0178 return err;
0179 }
0180
0181
0182
0183
0184 static int ntfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
0185 struct dentry *dentry, const char *symname)
0186 {
0187 u32 size = strlen(symname);
0188 struct inode *inode;
0189
0190 inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFLNK | 0777,
0191 0, symname, size, NULL);
0192
0193 return IS_ERR(inode) ? PTR_ERR(inode) : 0;
0194 }
0195
0196
0197
0198
0199 static int ntfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
0200 struct dentry *dentry, umode_t mode)
0201 {
0202 struct inode *inode;
0203
0204 inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFDIR | mode,
0205 0, NULL, 0, NULL);
0206
0207 return IS_ERR(inode) ? PTR_ERR(inode) : 0;
0208 }
0209
0210
0211
0212
0213 static int ntfs_rmdir(struct inode *dir, struct dentry *dentry)
0214 {
0215 struct ntfs_inode *ni = ntfs_i(dir);
0216 int err;
0217
0218 ni_lock_dir(ni);
0219
0220 err = ntfs_unlink_inode(dir, dentry);
0221
0222 ni_unlock(ni);
0223
0224 return err;
0225 }
0226
0227
0228
0229
0230 static int ntfs_rename(struct user_namespace *mnt_userns, struct inode *dir,
0231 struct dentry *dentry, struct inode *new_dir,
0232 struct dentry *new_dentry, u32 flags)
0233 {
0234 int err;
0235 struct super_block *sb = dir->i_sb;
0236 struct ntfs_sb_info *sbi = sb->s_fs_info;
0237 struct ntfs_inode *dir_ni = ntfs_i(dir);
0238 struct ntfs_inode *new_dir_ni = ntfs_i(new_dir);
0239 struct inode *inode = d_inode(dentry);
0240 struct ntfs_inode *ni = ntfs_i(inode);
0241 struct inode *new_inode = d_inode(new_dentry);
0242 struct NTFS_DE *de, *new_de;
0243 bool is_same, is_bad;
0244
0245
0246
0247
0248
0249
0250 static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + SIZEOF_RESIDENT < 1024);
0251 static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + sizeof(struct NTFS_DE) <
0252 1024);
0253 static_assert(PATH_MAX >= 4 * 1024);
0254
0255 if (flags & ~RENAME_NOREPLACE)
0256 return -EINVAL;
0257
0258 is_same = dentry->d_name.len == new_dentry->d_name.len &&
0259 !memcmp(dentry->d_name.name, new_dentry->d_name.name,
0260 dentry->d_name.len);
0261
0262 if (is_same && dir == new_dir) {
0263
0264 return 0;
0265 }
0266
0267 if (ntfs_is_meta_file(sbi, inode->i_ino)) {
0268
0269 return -EINVAL;
0270 }
0271
0272 if (new_inode) {
0273
0274 dget(new_dentry);
0275 ni_lock_dir(new_dir_ni);
0276 err = ntfs_unlink_inode(new_dir, new_dentry);
0277 ni_unlock(new_dir_ni);
0278 dput(new_dentry);
0279 if (err)
0280 return err;
0281 }
0282
0283
0284 de = __getname();
0285 if (!de)
0286 return -ENOMEM;
0287
0288
0289 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
0290 if (err < 0)
0291 goto out;
0292
0293 if (is_same) {
0294
0295 new_de = de;
0296 } else {
0297
0298 new_de = Add2Ptr(de, 2048);
0299 err = fill_name_de(sbi, new_de, &new_dentry->d_name, NULL);
0300 if (err < 0)
0301 goto out;
0302 }
0303
0304 ni_lock_dir(dir_ni);
0305 ni_lock(ni);
0306
0307 is_bad = false;
0308 err = ni_rename(dir_ni, new_dir_ni, ni, de, new_de, &is_bad);
0309 if (is_bad) {
0310
0311 _ntfs_bad_inode(inode);
0312 } else if (!err) {
0313 inode->i_ctime = dir->i_ctime = dir->i_mtime =
0314 current_time(dir);
0315 mark_inode_dirty(inode);
0316 mark_inode_dirty(dir);
0317 if (dir != new_dir) {
0318 new_dir->i_mtime = new_dir->i_ctime = dir->i_ctime;
0319 mark_inode_dirty(new_dir);
0320 }
0321
0322 if (IS_DIRSYNC(dir))
0323 ntfs_sync_inode(dir);
0324
0325 if (IS_DIRSYNC(new_dir))
0326 ntfs_sync_inode(inode);
0327 }
0328
0329 ni_unlock(ni);
0330 ni_unlock(dir_ni);
0331 out:
0332 __putname(de);
0333 return err;
0334 }
0335
0336 struct dentry *ntfs3_get_parent(struct dentry *child)
0337 {
0338 struct inode *inode = d_inode(child);
0339 struct ntfs_inode *ni = ntfs_i(inode);
0340
0341 struct ATTR_LIST_ENTRY *le = NULL;
0342 struct ATTRIB *attr = NULL;
0343 struct ATTR_FILE_NAME *fname;
0344
0345 while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
0346 NULL))) {
0347 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
0348 if (!fname)
0349 continue;
0350
0351 return d_obtain_alias(
0352 ntfs_iget5(inode->i_sb, &fname->home, NULL));
0353 }
0354
0355 return ERR_PTR(-ENOENT);
0356 }
0357
0358
0359 const struct inode_operations ntfs_dir_inode_operations = {
0360 .lookup = ntfs_lookup,
0361 .create = ntfs_create,
0362 .link = ntfs_link,
0363 .unlink = ntfs_unlink,
0364 .symlink = ntfs_symlink,
0365 .mkdir = ntfs_mkdir,
0366 .rmdir = ntfs_rmdir,
0367 .mknod = ntfs_mknod,
0368 .rename = ntfs_rename,
0369 .permission = ntfs_permission,
0370 .get_acl = ntfs_get_acl,
0371 .set_acl = ntfs_set_acl,
0372 .setattr = ntfs3_setattr,
0373 .getattr = ntfs_getattr,
0374 .listxattr = ntfs_listxattr,
0375 .fiemap = ntfs_fiemap,
0376 };
0377
0378 const struct inode_operations ntfs_special_inode_operations = {
0379 .setattr = ntfs3_setattr,
0380 .getattr = ntfs_getattr,
0381 .listxattr = ntfs_listxattr,
0382 .get_acl = ntfs_get_acl,
0383 .set_acl = ntfs_set_acl,
0384 };
0385