1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * fs/hmdfs/inode_local.c
4 *
5 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
6 */
7
8 #include <linux/file.h>
9 #include <linux/fs_stack.h>
10 #include <linux/kernel.h>
11 #include <linux/mount.h>
12 #include <linux/namei.h>
13 #include <linux/string.h>
14
15 #include "authority/authentication.h"
16 #include "comm/socket_adapter.h"
17 #include "comm/transport.h"
18 #include "hmdfs_client.h"
19 #include "hmdfs_dentryfile.h"
20 #include "hmdfs_device_view.h"
21 #include "hmdfs_share.h"
22 #include "hmdfs_trace.h"
23
24 extern struct kmem_cache *hmdfs_dentry_cachep;
25
26 struct hmdfs_name_data {
27 struct dir_context ctx;
28 const struct qstr *to_find;
29 char *name;
30 bool found;
31 };
32
init_hmdfs_dentry_info(struct hmdfs_sb_info * sbi,struct dentry * dentry,int dentry_type)33 int init_hmdfs_dentry_info(struct hmdfs_sb_info *sbi, struct dentry *dentry,
34 int dentry_type)
35 {
36 struct hmdfs_dentry_info *info =
37 kmem_cache_zalloc(hmdfs_dentry_cachep, GFP_ATOMIC);
38
39 if (!info)
40 return -ENOMEM;
41 dentry->d_fsdata = info;
42 INIT_LIST_HEAD(&info->cache_list_head);
43 INIT_LIST_HEAD(&info->remote_cache_list_head);
44 spin_lock_init(&info->cache_list_lock);
45 mutex_init(&info->remote_cache_list_lock);
46 mutex_init(&info->cache_pull_lock);
47 spin_lock_init(&info->lock);
48 info->dentry_type = dentry_type;
49 info->device_id = 0;
50 if (dentry_type == HMDFS_LAYER_ZERO ||
51 dentry_type == HMDFS_LAYER_FIRST_DEVICE ||
52 dentry_type == HMDFS_LAYER_SECOND_LOCAL ||
53 dentry_type == HMDFS_LAYER_SECOND_CLOUD ||
54 dentry_type == HMDFS_LAYER_SECOND_REMOTE)
55 d_set_d_op(dentry, &hmdfs_dev_dops);
56 else
57 d_set_d_op(dentry, &hmdfs_dops);
58 return 0;
59 }
60
set_sharefile_flag(struct hmdfs_dentry_info * gdi)61 static inline void set_sharefile_flag(struct hmdfs_dentry_info *gdi)
62 {
63 gdi->file_type = HM_SHARE;
64 }
65
check_and_fixup_share_ops(struct inode * inode,const char * name)66 static void check_and_fixup_share_ops(struct inode *inode,
67 const char *name)
68 {
69 if (is_share_dir(inode, name)) {
70 inode->i_op = &hmdfs_dir_inode_ops_share;
71 inode->i_fop = &hmdfs_dir_ops_share;
72 }
73 }
74
fill_inode_local(struct super_block * sb,struct inode * lower_inode,const char * name)75 struct inode *fill_inode_local(struct super_block *sb,
76 struct inode *lower_inode, const char *name)
77 {
78 int ret = 0;
79 struct inode *inode;
80 struct hmdfs_inode_info *info;
81
82 if (!igrab(lower_inode))
83 return ERR_PTR(-ESTALE);
84
85 inode = hmdfs_iget5_locked_local(sb, lower_inode);
86 if (!inode) {
87 hmdfs_err("iget5_locked get inode NULL");
88 iput(lower_inode);
89 return ERR_PTR(-ENOMEM);
90 }
91 if (!(inode->i_state & I_NEW)) {
92 iput(lower_inode);
93 return inode;
94 }
95
96 info = hmdfs_i(inode);
97 #ifdef CONFIG_HMDFS_FS_PERMISSION
98 info->perm = hmdfs_read_perm(lower_inode);
99 #endif
100 if (S_ISDIR(lower_inode->i_mode))
101 inode->i_mode = (lower_inode->i_mode & S_IFMT) | S_IRWXU |
102 S_IRWXG | S_IXOTH;
103 else if (S_ISREG(lower_inode->i_mode))
104 inode->i_mode = (lower_inode->i_mode & S_IFMT) | S_IRUSR |
105 S_IWUSR | S_IRGRP | S_IWGRP;
106 else if (S_ISLNK(lower_inode->i_mode))
107 inode->i_mode =
108 S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
109
110 #ifdef CONFIG_HMDFS_FS_PERMISSION
111 inode->i_uid = lower_inode->i_uid;
112 inode->i_gid = lower_inode->i_gid;
113 #else
114 inode->i_uid = KUIDT_INIT((uid_t)1000);
115 inode->i_gid = KGIDT_INIT((gid_t)1000);
116 #endif
117 inode->i_atime = lower_inode->i_atime;
118 inode->i_ctime = lower_inode->i_ctime;
119 inode->i_mtime = lower_inode->i_mtime;
120 inode->i_generation = lower_inode->i_generation;
121
122 info->inode_type = HMDFS_LAYER_OTHER_LOCAL;
123 if (S_ISDIR(lower_inode->i_mode)) {
124 inode->i_op = &hmdfs_dir_inode_ops_local;
125 inode->i_fop = &hmdfs_dir_ops_local;
126 inode->i_mode |= S_IXUGO;
127 } else if (S_ISREG(lower_inode->i_mode)) {
128 inode->i_op = &hmdfs_file_iops_local;
129 inode->i_fop = &hmdfs_file_fops_local;
130 } else if (S_ISLNK(lower_inode->i_mode)) {
131 inode->i_op = &hmdfs_symlink_iops_local;
132 inode->i_fop = &hmdfs_file_fops_local;
133 inode->i_size = i_size_read(lower_inode);
134 } else {
135 ret = -EIO;
136 goto bad_inode;
137 }
138
139 fsstack_copy_inode_size(inode, lower_inode);
140 check_and_fixup_share_ops(inode, name);
141 unlock_new_inode(inode);
142 return inode;
143 bad_inode:
144 iget_failed(inode);
145 return ERR_PTR(ret);
146 }
147
148 /* hmdfs_convert_lookup_flags - covert hmdfs lookup flags to vfs lookup flags
149 *
150 * @hmdfs_flags: hmdfs lookup flags
151 * @vfs_flags: pointer to converted flags
152 *
153 * return 0 on success, or err code on failure.
154 */
hmdfs_convert_lookup_flags(unsigned int hmdfs_flags,unsigned int * vfs_flags)155 int hmdfs_convert_lookup_flags(unsigned int hmdfs_flags,
156 unsigned int *vfs_flags)
157 {
158 *vfs_flags = 0;
159
160 /* currently only support HMDFS_LOOKUP_REVAL */
161 if (hmdfs_flags & ~HMDFS_LOOKUP_REVAL)
162 return -EINVAL;
163
164 if (hmdfs_flags & HMDFS_LOOKUP_REVAL)
165 *vfs_flags |= LOOKUP_REVAL;
166
167 return 0;
168 }
169
hmdfs_name_match(struct dir_context * ctx,const char * name,int namelen,loff_t offset,u64 ino,unsigned int d_type)170 static int hmdfs_name_match(struct dir_context *ctx, const char *name,
171 int namelen, loff_t offset, u64 ino,
172 unsigned int d_type)
173 {
174 struct hmdfs_name_data *buf =
175 container_of(ctx, struct hmdfs_name_data, ctx);
176 struct qstr candidate = QSTR_INIT(name, namelen);
177
178 if (qstr_case_eq(buf->to_find, &candidate)) {
179 memcpy(buf->name, name, namelen);
180 buf->name[namelen] = 0;
181 buf->found = true;
182 return 1;
183 }
184 return 0;
185 }
186
__lookup_nosensitive(struct path * lower_parent_path,struct dentry * child_dentry,unsigned int flags,struct path * lower_path)187 static int __lookup_nosensitive(struct path *lower_parent_path,
188 struct dentry *child_dentry, unsigned int flags,
189 struct path *lower_path)
190 {
191 struct file *file;
192 const struct cred *cred = current_cred();
193 const struct qstr *name = &child_dentry->d_name;
194 int err;
195 struct hmdfs_name_data buffer = {
196 .ctx.actor = hmdfs_name_match,
197 .to_find = name,
198 .name = __getname(),
199 .found = false,
200 };
201
202 if (!buffer.name) {
203 err = -ENOMEM;
204 goto out;
205 }
206 file = dentry_open(lower_parent_path, O_RDONLY, cred);
207 if (IS_ERR(file)) {
208 err = PTR_ERR(file);
209 goto put_name;
210 }
211 err = iterate_dir(file, &buffer.ctx);
212 fput(file);
213 if (err)
214 goto put_name;
215 if (buffer.found)
216 err = vfs_path_lookup(lower_parent_path->dentry,
217 lower_parent_path->mnt, buffer.name,
218 flags, lower_path);
219 else
220 err = -ENOENT;
221 put_name:
222 __putname(buffer.name);
223 out:
224 return err;
225 }
226
set_symlink_flag(struct hmdfs_dentry_info * gdi)227 static inline void set_symlink_flag(struct hmdfs_dentry_info *gdi)
228 {
229 gdi->file_type = HM_SYMLINK;
230 }
231
hmdfs_lookup_local(struct inode * parent_inode,struct dentry * child_dentry,unsigned int flags)232 struct dentry *hmdfs_lookup_local(struct inode *parent_inode,
233 struct dentry *child_dentry,
234 unsigned int flags)
235 {
236 const char *d_name = child_dentry->d_name.name;
237 int err = 0;
238 struct path lower_path, lower_parent_path;
239 struct dentry *lower_dentry = NULL, *parent_dentry = NULL, *ret = NULL;
240 struct hmdfs_dentry_info *gdi = NULL;
241 struct inode *child_inode = NULL;
242 struct hmdfs_sb_info *sbi = hmdfs_sb(child_dentry->d_sb);
243
244 trace_hmdfs_lookup_local(parent_inode, child_dentry, flags);
245 if (child_dentry->d_name.len > NAME_MAX) {
246 ret = ERR_PTR(-ENAMETOOLONG);
247 goto out;
248 }
249
250 /* local device */
251 parent_dentry = dget_parent(child_dentry);
252 hmdfs_get_lower_path(parent_dentry, &lower_parent_path);
253 err = init_hmdfs_dentry_info(sbi, child_dentry,
254 HMDFS_LAYER_OTHER_LOCAL);
255 if (err) {
256 ret = ERR_PTR(err);
257 goto out_err;
258 }
259
260 gdi = hmdfs_d(child_dentry);
261
262 flags &= ~LOOKUP_FOLLOW;
263 err = vfs_path_lookup(lower_parent_path.dentry, lower_parent_path.mnt,
264 (child_dentry->d_name.name), 0, &lower_path);
265 if (err == -ENOENT && !sbi->s_case_sensitive)
266 err = __lookup_nosensitive(&lower_parent_path, child_dentry, 0,
267 &lower_path);
268 if (err && err != -ENOENT) {
269 ret = ERR_PTR(err);
270 goto out_err;
271 } else if (!err) {
272 hmdfs_set_lower_path(child_dentry, &lower_path);
273 child_inode = fill_inode_local(parent_inode->i_sb,
274 d_inode(lower_path.dentry),
275 child_dentry->d_name.name);
276
277 if (S_ISLNK(d_inode(lower_path.dentry)->i_mode))
278 set_symlink_flag(gdi);
279 if (IS_ERR(child_inode)) {
280 err = PTR_ERR(child_inode);
281 ret = ERR_PTR(err);
282 hmdfs_put_reset_lower_path(child_dentry);
283 goto out_err;
284 }
285 ret = d_splice_alias(child_inode, child_dentry);
286 if (IS_ERR(ret)) {
287 err = PTR_ERR(ret);
288 hmdfs_put_reset_lower_path(child_dentry);
289 goto out_err;
290 }
291
292 check_and_fixup_ownership(parent_inode, child_inode);
293 goto out_err;
294 }
295 /*
296 * return 0 here, so that vfs can continue the process of making this
297 * negative dentry to a positive one while creating a new file.
298 */
299 err = 0;
300 ret = 0;
301
302 lower_dentry = lookup_one_len_unlocked(d_name, lower_parent_path.dentry,
303 child_dentry->d_name.len);
304 if (IS_ERR(lower_dentry)) {
305 err = PTR_ERR(lower_dentry);
306 ret = lower_dentry;
307 goto out_err;
308 }
309 lower_path.dentry = lower_dentry;
310 lower_path.mnt = mntget(lower_parent_path.mnt);
311 hmdfs_set_lower_path(child_dentry, &lower_path);
312
313 out_err:
314 if (!err)
315 hmdfs_set_time(child_dentry, jiffies);
316 hmdfs_put_lower_path(&lower_parent_path);
317 dput(parent_dentry);
318 out:
319 trace_hmdfs_lookup_local_end(parent_inode, child_dentry, err);
320 return ret;
321 }
322
hmdfs_mkdir_local_dentry(struct inode * dir,struct dentry * dentry,umode_t mode)323 int hmdfs_mkdir_local_dentry(struct inode *dir, struct dentry *dentry,
324 umode_t mode)
325 {
326 struct inode *lower_dir = hmdfs_i(dir)->lower_inode;
327 struct dentry *lower_dir_dentry = NULL;
328 struct super_block *sb = dir->i_sb;
329 struct path lower_path;
330 struct dentry *lower_dentry = NULL;
331 int error = 0;
332 struct inode *lower_inode = NULL;
333 struct inode *child_inode = NULL;
334 bool local_res = false;
335 struct cache_fs_override or;
336 __u16 child_perm;
337 kuid_t tmp_uid;
338
339 error = hmdfs_override_dir_id_fs(&or, dir, dentry, &child_perm);
340 if (error)
341 goto cleanup;
342
343 hmdfs_get_lower_path(dentry, &lower_path);
344 lower_dentry = lower_path.dentry;
345 lower_dir_dentry = lock_parent(lower_dentry);
346
347 tmp_uid = hmdfs_override_inode_uid(lower_dir);
348 mode = (mode & S_IFMT) | 00771;
349
350 error = vfs_mkdir(lower_dir, lower_dentry, mode);
351 hmdfs_revert_inode_uid(lower_dir, tmp_uid);
352 if (error) {
353 hmdfs_err("vfs_mkdir() error:%d", error);
354 goto out;
355 }
356 local_res = true;
357 lower_inode = d_inode(lower_dentry);
358 #ifdef CONFIG_HMDFS_FS_PERMISSION
359 error = hmdfs_persist_perm(lower_dentry, &child_perm);
360 #endif
361 child_inode = fill_inode_local(sb, lower_inode, dentry->d_name.name);
362 if (IS_ERR(child_inode)) {
363 error = PTR_ERR(child_inode);
364 goto out;
365 }
366 d_add(dentry, child_inode);
367 set_nlink(dir, hmdfs_i(dir)->lower_inode->i_nlink);
368 out:
369 unlock_dir(lower_dir_dentry);
370 if (local_res)
371 hmdfs_drop_remote_cache_dents(dentry->d_parent);
372
373 if (error) {
374 hmdfs_clear_drop_flag(dentry->d_parent);
375 d_drop(dentry);
376 }
377 hmdfs_put_lower_path(&lower_path);
378 hmdfs_revert_dir_id_fs(&or);
379 cleanup:
380 return error;
381 }
382
hmdfs_mkdir_local(struct inode * dir,struct dentry * dentry,umode_t mode)383 int hmdfs_mkdir_local(struct inode *dir, struct dentry *dentry, umode_t mode)
384 {
385 int err = 0;
386
387 if (check_filename(dentry->d_name.name, dentry->d_name.len)) {
388 err = -EINVAL;
389 return err;
390 }
391
392 if (hmdfs_file_type(dentry->d_name.name) != HMDFS_TYPE_COMMON) {
393 err = -EACCES;
394 return err;
395 }
396 err = hmdfs_mkdir_local_dentry(dir, dentry, mode);
397 trace_hmdfs_mkdir_local(dir, dentry, err);
398 return err;
399 }
400
hmdfs_create_local_dentry(struct inode * dir,struct dentry * dentry,umode_t mode,bool want_excl)401 int hmdfs_create_local_dentry(struct inode *dir, struct dentry *dentry,
402 umode_t mode, bool want_excl)
403 {
404 struct inode *lower_dir = NULL;
405 struct dentry *lower_dir_dentry = NULL;
406 struct super_block *sb = dir->i_sb;
407 struct path lower_path;
408 struct dentry *lower_dentry = NULL;
409 int error = 0;
410 struct inode *lower_inode = NULL;
411 struct inode *child_inode = NULL;
412 kuid_t tmp_uid;
413 #ifdef CONFIG_HMDFS_FS_PERMISSION
414 const struct cred *saved_cred = NULL;
415 struct fs_struct *saved_fs = NULL, *copied_fs = NULL;
416 __u16 child_perm;
417 #endif
418
419 #ifdef CONFIG_HMDFS_FS_PERMISSION
420 saved_cred = hmdfs_override_file_fsids(dir, &child_perm);
421 if (!saved_cred) {
422 error = -ENOMEM;
423 goto path_err;
424 }
425
426 saved_fs = current->fs;
427 copied_fs = hmdfs_override_fsstruct(saved_fs);
428 if (!copied_fs) {
429 error = -ENOMEM;
430 goto revert_fsids;
431 }
432 #endif
433 hmdfs_get_lower_path(dentry, &lower_path);
434 lower_dentry = lower_path.dentry;
435 mode = (mode & S_IFMT) | 00660;
436 lower_dir_dentry = lock_parent(lower_dentry);
437 lower_dir = d_inode(lower_dir_dentry);
438 tmp_uid = hmdfs_override_inode_uid(lower_dir);
439 error = vfs_create(lower_dir, lower_dentry, mode, want_excl);
440 hmdfs_revert_inode_uid(lower_dir, tmp_uid);
441 unlock_dir(lower_dir_dentry);
442 if (error)
443 goto out;
444
445 lower_inode = d_inode(lower_dentry);
446 #ifdef CONFIG_HMDFS_FS_PERMISSION
447 error = hmdfs_persist_perm(lower_dentry, &child_perm);
448 #endif
449 child_inode = fill_inode_local(sb, lower_inode, dentry->d_name.name);
450 if (IS_ERR(child_inode)) {
451 error = PTR_ERR(child_inode);
452 goto out_created;
453 }
454 d_add(dentry, child_inode);
455
456 out_created:
457 hmdfs_drop_remote_cache_dents(dentry->d_parent);
458 out:
459 if (error) {
460 hmdfs_clear_drop_flag(dentry->d_parent);
461 d_drop(dentry);
462 }
463 hmdfs_put_lower_path(&lower_path);
464
465 #ifdef CONFIG_HMDFS_FS_PERMISSION
466 hmdfs_revert_fsstruct(saved_fs, copied_fs);
467 revert_fsids:
468 hmdfs_revert_fsids(saved_cred);
469 #endif
470 #ifdef CONFIG_HMDFS_FS_PERMISSION
471 path_err:
472 #endif
473 return error;
474 }
475
hmdfs_create_local(struct inode * dir,struct dentry * child_dentry,umode_t mode,bool want_excl)476 int hmdfs_create_local(struct inode *dir, struct dentry *child_dentry,
477 umode_t mode, bool want_excl)
478 {
479 int err = 0;
480
481 if (check_filename(child_dentry->d_name.name,
482 child_dentry->d_name.len)) {
483 err = -EINVAL;
484 return err;
485 }
486
487 if (hmdfs_file_type(child_dentry->d_name.name) != HMDFS_TYPE_COMMON) {
488 err = -EACCES;
489 return err;
490 }
491
492 err = hmdfs_create_local_dentry(dir, child_dentry, mode, want_excl);
493 trace_hmdfs_create_local(dir, child_dentry, err);
494 return err;
495 }
496
hmdfs_rmdir_local_dentry(struct inode * dir,struct dentry * dentry)497 int hmdfs_rmdir_local_dentry(struct inode *dir, struct dentry *dentry)
498 {
499 struct inode *lower_dir = NULL;
500 struct dentry *lower_dir_dentry = NULL;
501 kuid_t tmp_uid;
502 struct path lower_path;
503 struct dentry *lower_dentry = NULL;
504 struct dentry *lookup_dentry = NULL;
505 int error = 0;
506
507 hmdfs_clear_cache_dents(dentry, true);
508 hmdfs_get_lower_path(dentry, &lower_path);
509 lower_dentry = lower_path.dentry;
510 lower_dir_dentry = lock_parent(lower_dentry);
511 lower_dir = d_inode(lower_dir_dentry);
512
513 lookup_dentry = lookup_one_len(lower_dentry->d_name.name, lower_dir_dentry,
514 lower_dentry->d_name.len);
515 if (IS_ERR(lookup_dentry)) {
516 error = PTR_ERR(lookup_dentry);
517 hmdfs_err("lookup_one_len failed, err = %d", error);
518 goto lookup_err;
519 }
520 tmp_uid = hmdfs_override_inode_uid(lower_dir);
521
522 error = vfs_rmdir(lower_dir, lookup_dentry);
523 hmdfs_revert_inode_uid(lower_dir, tmp_uid);
524 dput(lookup_dentry);
525 lookup_err:
526 unlock_dir(lower_dir_dentry);
527 hmdfs_put_lower_path(&lower_path);
528 if (error)
529 goto path_err;
530 hmdfs_drop_remote_cache_dents(dentry->d_parent);
531 path_err:
532 if (error)
533 hmdfs_clear_drop_flag(dentry->d_parent);
534 return error;
535 }
536
hmdfs_rmdir_local(struct inode * dir,struct dentry * dentry)537 int hmdfs_rmdir_local(struct inode *dir, struct dentry *dentry)
538 {
539 int err = 0;
540
541 if (hmdfs_file_type(dentry->d_name.name) != HMDFS_TYPE_COMMON) {
542 err = -EACCES;
543 goto out;
544 }
545
546 err = hmdfs_rmdir_local_dentry(dir, dentry);
547 if (err != 0) {
548 hmdfs_err("rm dir failed:%d", err);
549 goto out;
550 }
551
552 /* drop dentry even remote failed
553 * it maybe cause that one remote devices disconnect
554 * when doing remote rmdir
555 */
556 d_drop(dentry);
557 out:
558 /* return connect device's errcode */
559 trace_hmdfs_rmdir_local(dir, dentry, err);
560 return err;
561 }
562
hmdfs_unlink_local_dentry(struct inode * dir,struct dentry * dentry)563 int hmdfs_unlink_local_dentry(struct inode *dir, struct dentry *dentry)
564 {
565 struct dentry *lower_dir_dentry = NULL;
566 struct path lower_path;
567 struct inode *lower_dir = NULL;
568 struct dentry *lower_dentry = NULL;
569 struct dentry *lookup_dentry = NULL;
570 int error;
571 kuid_t tmp_uid;
572
573 hmdfs_get_lower_path(dentry, &lower_path);
574 lower_dentry = lower_path.dentry;
575 dget(lower_dentry);
576 lower_dir_dentry = lock_parent(lower_dentry);
577 lower_dir = d_inode(lower_dir_dentry);
578 lookup_dentry = lookup_one_len(lower_dentry->d_name.name, lower_dir_dentry,
579 lower_dentry->d_name.len);
580 if (IS_ERR(lookup_dentry)) {
581 error = PTR_ERR(lookup_dentry);
582 hmdfs_err("lookup_one_len failed, err = %d", error);
583 goto lookup_err;
584 }
585
586 tmp_uid = hmdfs_override_inode_uid(lower_dir);
587 error = vfs_unlink(lower_dir, lookup_dentry, NULL);
588 hmdfs_revert_inode_uid(lower_dir, tmp_uid);
589 set_nlink(d_inode(dentry),
590 hmdfs_i(d_inode(dentry))->lower_inode->i_nlink);
591 dput(lookup_dentry);
592 lookup_err:
593 unlock_dir(lower_dir_dentry);
594 dput(lower_dentry);
595 if (error)
596 goto path_err;
597
598 hmdfs_drop_remote_cache_dents(dentry->d_parent);
599 d_drop(dentry);
600 hmdfs_put_lower_path(&lower_path);
601
602 path_err:
603 if (error)
604 hmdfs_clear_drop_flag(dentry->d_parent);
605 return error;
606 }
607
hmdfs_unlink_local(struct inode * dir,struct dentry * dentry)608 int hmdfs_unlink_local(struct inode *dir, struct dentry *dentry)
609 {
610 if (hmdfs_file_type(dentry->d_name.name) != HMDFS_TYPE_COMMON)
611 return -EACCES;
612
613 return hmdfs_unlink_local_dentry(dir, dentry);
614 }
615
hmdfs_rename_local_dentry(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)616 int hmdfs_rename_local_dentry(struct inode *old_dir, struct dentry *old_dentry,
617 struct inode *new_dir, struct dentry *new_dentry,
618 unsigned int flags)
619 {
620 struct path lower_old_path;
621 struct path lower_new_path;
622 struct dentry *lower_old_dentry = NULL;
623 struct dentry *lower_new_dentry = NULL;
624 struct dentry *lower_old_dir_dentry = NULL;
625 struct dentry *lower_new_dir_dentry = NULL;
626 struct dentry *trap = NULL;
627 int rc = 0;
628 kuid_t old_dir_uid, new_dir_uid;
629
630 if (flags)
631 return -EINVAL;
632
633 hmdfs_get_lower_path(old_dentry, &lower_old_path);
634 lower_old_dentry = lower_old_path.dentry;
635 if (!lower_old_dentry) {
636 hmdfs_err("lower_old_dentry as NULL");
637 rc = -EACCES;
638 goto out_put_old_path;
639 }
640
641 hmdfs_get_lower_path(new_dentry, &lower_new_path);
642 lower_new_dentry = lower_new_path.dentry;
643 if (!lower_new_dentry) {
644 hmdfs_err("lower_new_dentry as NULL");
645 rc = -EACCES;
646 goto out_put_new_path;
647 }
648
649 lower_old_dir_dentry = dget_parent(lower_old_dentry);
650 lower_new_dir_dentry = dget_parent(lower_new_dentry);
651 trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
652 new_dir_uid = hmdfs_override_inode_uid(d_inode(lower_new_dir_dentry));
653 old_dir_uid = hmdfs_override_inode_uid(d_inode(lower_old_dir_dentry));
654
655 /* source should not be ancestor of target */
656 if (trap == lower_old_dentry) {
657 rc = -EINVAL;
658 goto out_lock;
659 }
660 /* target should not be ancestor of source */
661 if (trap == lower_new_dentry) {
662 rc = -ENOTEMPTY;
663 goto out_lock;
664 }
665
666 rc = vfs_rename(d_inode(lower_old_dir_dentry), lower_old_dentry,
667 d_inode(lower_new_dir_dentry), lower_new_dentry, NULL,
668 flags);
669 out_lock:
670 dget(old_dentry);
671
672 hmdfs_revert_inode_uid(d_inode(lower_old_dir_dentry), old_dir_uid);
673 hmdfs_revert_inode_uid(d_inode(lower_new_dir_dentry), new_dir_uid);
674
675 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
676 if (rc == 0) {
677 hmdfs_drop_remote_cache_dents(old_dentry->d_parent);
678 if (old_dentry->d_parent != new_dentry->d_parent)
679 hmdfs_drop_remote_cache_dents(new_dentry->d_parent);
680 } else {
681 hmdfs_clear_drop_flag(old_dentry->d_parent);
682 if (old_dentry->d_parent != new_dentry->d_parent)
683 hmdfs_clear_drop_flag(old_dentry->d_parent);
684 d_drop(new_dentry);
685 }
686
687 dput(old_dentry);
688 dput(lower_old_dir_dentry);
689 dput(lower_new_dir_dentry);
690
691 out_put_new_path:
692 hmdfs_put_lower_path(&lower_new_path);
693 out_put_old_path:
694 hmdfs_put_lower_path(&lower_old_path);
695 return rc;
696 }
697
hmdfs_rename_local(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)698 int hmdfs_rename_local(struct inode *old_dir, struct dentry *old_dentry,
699 struct inode *new_dir, struct dentry *new_dentry,
700 unsigned int flags)
701 {
702 int err = 0;
703 int ret = 0;
704
705 trace_hmdfs_rename_local(old_dir, old_dentry, new_dir, new_dentry,
706 flags);
707 if (hmdfs_file_type(old_dentry->d_name.name) != HMDFS_TYPE_COMMON ||
708 hmdfs_file_type(new_dentry->d_name.name) != HMDFS_TYPE_COMMON) {
709 err = -EACCES;
710 goto rename_out;
711 }
712
713 if (hmdfs_i(old_dir)->inode_type != hmdfs_i(new_dir)->inode_type) {
714 hmdfs_err("in different view");
715 err = -EPERM;
716 goto rename_out;
717 }
718
719 if (hmdfs_d(old_dentry)->device_id != hmdfs_d(new_dentry)->device_id) {
720 err = -EXDEV;
721 goto rename_out;
722 }
723
724 if (S_ISREG(old_dentry->d_inode->i_mode)) {
725 err = hmdfs_rename_local_dentry(old_dir, old_dentry, new_dir,
726 new_dentry, flags);
727 } else if (S_ISDIR(old_dentry->d_inode->i_mode)) {
728 ret = hmdfs_rename_local_dentry(old_dir, old_dentry, new_dir,
729 new_dentry, flags);
730 if (ret != 0) {
731 err = ret;
732 goto rename_out;
733 }
734 }
735
736 if (!err)
737 d_invalidate(old_dentry);
738
739 rename_out:
740 return err;
741 }
742
symname_is_allowed(const char * symname)743 static bool symname_is_allowed(const char *symname)
744 {
745 char *p;
746 char *buf = 0;
747 size_t symname_len;
748
749 symname_len = strnlen(symname, PATH_MAX);
750 if (symname_len >= PATH_MAX)
751 return false;
752
753 buf = kzalloc(PATH_MAX + 2, GFP_KERNEL);
754 if (!buf)
755 return false;
756
757 buf[0] = '/';
758 strncpy(buf + 1, symname, symname_len);
759 strcat(buf, "/");
760 p = strstr(symname, "/../");
761 if (p) {
762 kfree(buf);
763 return false;
764 }
765
766 kfree(buf);
767 return true;
768 }
769
hmdfs_symlink_local(struct inode * dir,struct dentry * dentry,const char * symname)770 int hmdfs_symlink_local(struct inode *dir, struct dentry *dentry,
771 const char *symname)
772 {
773 int err;
774 struct dentry *lower_dentry = NULL;
775 struct dentry *lower_parent_dentry = NULL;
776 struct path lower_path;
777 struct inode *child_inode = NULL;
778 struct inode *lower_dir_inode = hmdfs_i(dir)->lower_inode;
779 struct hmdfs_dentry_info *gdi = hmdfs_d(dentry);
780 kuid_t tmp_uid;
781 #ifdef CONFIG_HMDFS_FS_PERMISSION
782 const struct cred *saved_cred = NULL;
783 struct fs_struct *saved_fs = NULL, *copied_fs = NULL;
784 __u16 child_perm;
785 #endif
786
787 if (unlikely(!symname_is_allowed(symname))) {
788 err = -EPERM;
789 goto path_err;
790 }
791
792 #ifdef CONFIG_HMDFS_FS_PERMISSION
793 saved_cred = hmdfs_override_file_fsids(dir, &child_perm);
794 if (!saved_cred) {
795 err = -ENOMEM;
796 goto path_err;
797 }
798
799 saved_fs = current->fs;
800 copied_fs = hmdfs_override_fsstruct(saved_fs);
801 if (!copied_fs) {
802 err = -ENOMEM;
803 goto revert_fsids;
804 }
805 #endif
806 hmdfs_get_lower_path(dentry, &lower_path);
807 lower_dentry = lower_path.dentry;
808 lower_parent_dentry = lock_parent(lower_dentry);
809 tmp_uid = hmdfs_override_inode_uid(lower_dir_inode);
810 err = vfs_symlink(lower_dir_inode, lower_dentry, symname);
811 hmdfs_revert_inode_uid(lower_dir_inode, tmp_uid);
812 unlock_dir(lower_parent_dentry);
813 if (err)
814 goto out_err;
815 set_symlink_flag(gdi);
816 #ifdef CONFIG_HMDFS_FS_PERMISSION
817 err = hmdfs_persist_perm(lower_dentry, &child_perm);
818 #endif
819 child_inode = fill_inode_local(dir->i_sb, d_inode(lower_dentry),
820 dentry->d_name.name);
821 if (IS_ERR(child_inode)) {
822 err = PTR_ERR(child_inode);
823 goto out_err;
824 }
825 d_add(dentry, child_inode);
826 fsstack_copy_attr_times(dir, lower_dir_inode);
827 fsstack_copy_inode_size(dir, lower_dir_inode);
828
829 out_err:
830 hmdfs_put_lower_path(&lower_path);
831 #ifdef CONFIG_HMDFS_FS_PERMISSION
832 hmdfs_revert_fsstruct(saved_fs, copied_fs);
833 revert_fsids:
834 hmdfs_revert_fsids(saved_cred);
835 #endif
836 path_err:
837 return err;
838 }
839
hmdfs_get_link_local(struct dentry * dentry,struct inode * inode,struct delayed_call * done)840 static const char *hmdfs_get_link_local(struct dentry *dentry,
841 struct inode *inode,
842 struct delayed_call *done)
843 {
844 const char *link = NULL;
845 struct dentry *lower_dentry = NULL;
846 struct inode *lower_inode = NULL;
847 struct path lower_path;
848
849 if(!dentry) {
850 hmdfs_err("dentry MULL");
851 link = ERR_PTR(-ECHILD);
852 goto link_out;
853 }
854
855 hmdfs_get_lower_path(dentry, &lower_path);
856 lower_dentry = lower_path.dentry;
857 lower_inode = d_inode(lower_dentry);
858 if(!lower_inode->i_op || !lower_inode->i_op->get_link) {
859 hmdfs_err("The lower inode doesn't support get_link i_op");
860 link = ERR_PTR(-EINVAL);
861 goto out;
862 }
863
864 link = lower_inode->i_op->get_link(lower_dentry, lower_inode, done);
865 if(IS_ERR_OR_NULL(link))
866 goto out;
867 fsstack_copy_attr_atime(inode, lower_inode);
868 out:
869 hmdfs_put_lower_path(&lower_path);
870 link_out:
871 return link;
872 }
873
hmdfs_setattr_local(struct dentry * dentry,struct iattr * ia)874 static int hmdfs_setattr_local(struct dentry *dentry, struct iattr *ia)
875 {
876 struct inode *inode = d_inode(dentry);
877 struct inode *lower_inode = hmdfs_i(inode)->lower_inode;
878 struct path lower_path;
879 struct dentry *lower_dentry = NULL;
880 struct iattr lower_ia;
881 unsigned int ia_valid = ia->ia_valid;
882 int err = 0;
883 kuid_t tmp_uid;
884
885 hmdfs_get_lower_path(dentry, &lower_path);
886 lower_dentry = lower_path.dentry;
887 memcpy(&lower_ia, ia, sizeof(lower_ia));
888 if (ia_valid & ATTR_FILE)
889 lower_ia.ia_file = hmdfs_f(ia->ia_file)->lower_file;
890 lower_ia.ia_valid &= ~(ATTR_UID | ATTR_GID | ATTR_MODE);
891 if (ia_valid & ATTR_SIZE) {
892 err = inode_newsize_ok(inode, ia->ia_size);
893 if (err)
894 goto out;
895 truncate_setsize(inode, ia->ia_size);
896 }
897 inode_lock(lower_inode);
898 tmp_uid = hmdfs_override_inode_uid(lower_inode);
899
900 err = notify_change(lower_dentry, &lower_ia, NULL);
901 i_size_write(inode, i_size_read(lower_inode));
902 inode->i_atime = lower_inode->i_atime;
903 inode->i_mtime = lower_inode->i_mtime;
904 inode->i_ctime = lower_inode->i_ctime;
905 err = update_inode_to_dentry(dentry, inode);
906 hmdfs_revert_inode_uid(lower_inode, tmp_uid);
907
908 inode_unlock(lower_inode);
909 out:
910 hmdfs_put_lower_path(&lower_path);
911 return err;
912 }
913
hmdfs_getattr_local(const struct path * path,struct kstat * stat,u32 request_mask,unsigned int flags)914 static int hmdfs_getattr_local(const struct path *path, struct kstat *stat,
915 u32 request_mask, unsigned int flags)
916 {
917 struct path lower_path;
918 int ret;
919
920 hmdfs_get_lower_path(path->dentry, &lower_path);
921 ret = vfs_getattr(&lower_path, stat, request_mask, flags);
922 stat->ino = d_inode(path->dentry)->i_ino;
923 stat->uid = d_inode(path->dentry)->i_uid;
924 stat->gid = d_inode(path->dentry)->i_gid;
925 hmdfs_put_lower_path(&lower_path);
926
927 return ret;
928 }
929
hmdfs_permission(struct inode * inode,int mask)930 int hmdfs_permission(struct inode *inode, int mask)
931 {
932 #ifdef CONFIG_HMDFS_FS_PERMISSION
933 unsigned int mode = inode->i_mode;
934 kuid_t cur_uid = current_fsuid();
935
936 if (uid_eq(cur_uid, ROOT_UID) || uid_eq(cur_uid, SYSTEM_UID))
937 return 0;
938
939 if (uid_eq(cur_uid, inode->i_uid)) {
940 mode >>= 6;
941 } else if (in_group_p(inode->i_gid)) {
942 mode >>= 3;
943 }
944
945 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
946 return 0;
947
948 trace_hmdfs_permission(inode->i_ino);
949 return -EACCES;
950 #else
951
952 return 0;
953 #endif
954 }
955
hmdfs_local_listxattr(struct dentry * dentry,char * list,size_t size)956 static ssize_t hmdfs_local_listxattr(struct dentry *dentry, char *list,
957 size_t size)
958 {
959 struct path lower_path;
960 ssize_t res = 0;
961 size_t r_size = size;
962
963 if (!hmdfs_support_xattr(dentry))
964 return -EOPNOTSUPP;
965
966 if (size > HMDFS_LISTXATTR_SIZE_MAX)
967 r_size = HMDFS_LISTXATTR_SIZE_MAX;
968
969 hmdfs_get_lower_path(dentry, &lower_path);
970 res = vfs_listxattr(lower_path.dentry, list, r_size);
971 hmdfs_put_lower_path(&lower_path);
972
973 if (res == -ERANGE && r_size != size) {
974 hmdfs_info("no support listxattr size over than %d",
975 HMDFS_LISTXATTR_SIZE_MAX);
976 res = -E2BIG;
977 }
978
979 return res;
980 }
hmdfs_lookup_share(struct inode * parent_inode,struct dentry * child_dentry,unsigned int flags)981 struct dentry *hmdfs_lookup_share(struct inode *parent_inode,
982 struct dentry *child_dentry, unsigned int flags)
983 {
984 const struct qstr *d_name = &child_dentry->d_name;
985 int err = 0;
986 struct dentry *ret = NULL;
987 struct hmdfs_sb_info *sbi = hmdfs_sb(child_dentry->d_sb);
988 struct path src_path;
989 struct inode *child_inode = NULL;
990
991 trace_hmdfs_lookup_share(parent_inode, child_dentry, flags);
992 if (d_name->len > NAME_MAX) {
993 ret = ERR_PTR(-ENAMETOOLONG);
994 goto err_out;
995 }
996
997 err = init_hmdfs_dentry_info(sbi, child_dentry, HMDFS_LAYER_OTHER_LOCAL);
998 if (err) {
999 ret = ERR_PTR(err);
1000 goto err_out;
1001 }
1002
1003 err = get_path_from_share_table(sbi, child_dentry, &src_path);
1004 if (err) {
1005 ret = ERR_PTR(err);
1006 goto err_out;
1007 }
1008
1009 hmdfs_set_lower_path(child_dentry, &src_path);
1010 child_inode = fill_inode_local(parent_inode->i_sb,
1011 d_inode(src_path.dentry), d_name->name);
1012
1013 set_sharefile_flag(hmdfs_d(child_dentry));
1014
1015 if (IS_ERR(child_inode)) {
1016 err = PTR_ERR(child_inode);
1017 ret = ERR_PTR(err);
1018 hmdfs_put_reset_lower_path(child_dentry);
1019 goto err_out;
1020 }
1021 ret = d_splice_alias(child_inode, child_dentry);
1022 if (IS_ERR(ret)) {
1023 err = PTR_ERR(ret);
1024 hmdfs_put_reset_lower_path(child_dentry);
1025 goto err_out;
1026 }
1027
1028 check_and_fixup_ownership(parent_inode, child_inode);
1029
1030 err_out:
1031 trace_hmdfs_lookup_share_end(parent_inode, child_dentry, err);
1032 return ret;
1033 }
1034
1035 const struct inode_operations hmdfs_dir_inode_ops_local = {
1036 .lookup = hmdfs_lookup_local,
1037 .mkdir = hmdfs_mkdir_local,
1038 .create = hmdfs_create_local,
1039 .rmdir = hmdfs_rmdir_local,
1040 .unlink = hmdfs_unlink_local,
1041 .symlink = hmdfs_symlink_local,
1042 .rename = hmdfs_rename_local,
1043 .permission = hmdfs_permission,
1044 .setattr = hmdfs_setattr_local,
1045 .getattr = hmdfs_getattr_local,
1046 };
1047
1048 const struct inode_operations hmdfs_symlink_iops_local = {
1049 .get_link = hmdfs_get_link_local,
1050 .permission = hmdfs_permission,
1051 .setattr = hmdfs_setattr_local,
1052 };
1053
1054 const struct inode_operations hmdfs_dir_inode_ops_share = {
1055 .lookup = hmdfs_lookup_share,
1056 .permission = hmdfs_permission,
1057 };
1058
1059 const struct inode_operations hmdfs_file_iops_local = {
1060 .setattr = hmdfs_setattr_local,
1061 .getattr = hmdfs_getattr_local,
1062 .permission = hmdfs_permission,
1063 .listxattr = hmdfs_local_listxattr,
1064 };
1065